Alex Thiessen has uploaded this change for review. ( https://review.coreboot.org/24979
Change subject: util/gitconfig: support pre-v2.5.0 git ......................................................................
util/gitconfig: support pre-v2.5.0 git
In commit fda071ca "Make gitconfig.sh support gitfile", the `gitconfig.sh` was changed to use `git rev-parse --git-path hooks` to determine the path to copy the hooks to. The `--git-path` option was added only recently on 2014-11-30 in git v2.5.0-rc0 commit 557bd833 "git_path(): be aware of file relocation in $GIT_DIR". Hence, for users of older distributions (e.g. Ubuntu 14.04) the `make gitconfig` stopped working.
This commit adds a script that provides functions for comparing git versions and modifies the `gitconfig.sh` script to use a simpler method for `hooks` directory lookup when an older, pre-v2.5.0-rc0 git is found installed.
Change-Id: If48f22b382a43246bbb54e41d6afbb18cbfde23d Signed-off-by: Alex Thiessen alex.thiessen.de+coreboot@gmail.com --- M util/gitconfig/gitconfig.sh A util/scripts/git-version-compare.sh 2 files changed, 233 insertions(+), 1 deletion(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/79/24979/1
diff --git a/util/gitconfig/gitconfig.sh b/util/gitconfig/gitconfig.sh index 5d6fe36..4934dbd 100755 --- a/util/gitconfig/gitconfig.sh +++ b/util/gitconfig/gitconfig.sh @@ -24,7 +24,15 @@ echo "Error: Not in root of a git repository" exit 1 fi -coreboot_hooks=$(git rev-parse --git-path hooks) + +source util/scripts/git-version-compare.sh +# `rev-parse --git-path` was added in commit 557bd833 for v2.5.0-rc0 +if git_version_ge 2 5 0 0; then + coreboot_hooks=$(git rev-parse --git-path hooks) +else + coreboot_hooks=$(git rev-parse --git-dir)/hooks +fi + mkdir -p "${coreboot_hooks}" for hook in commit-msg pre-commit ; do if [ util/gitconfig/${hook} -nt "${coreboot_hooks}/${hook}" ] \ diff --git a/util/scripts/git-version-compare.sh b/util/scripts/git-version-compare.sh new file mode 100644 index 0000000..54f3de3 --- /dev/null +++ b/util/scripts/git-version-compare.sh @@ -0,0 +1,224 @@ +#!/bin/sh + +## +## This file is part of the coreboot project. +## +## Copyright (C) 2003-2018 Alex Thiessen alex.thiessen.de+coreboot@gmail.com +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; version 3 or later of the License. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## SPDX-License-Identifier: GPL-3.0-or-later +## https://spdx.org/licenses/GPL-3.0-or-later.html +## + +# dependency check +for dependency in cut false git grep true; do + if ! command -v "${dependency}" 1>/dev/null; then + echo "missing ${dependency}, please install first" >&2 + fi +done + +get_git_version_part() { + if ! [ ${#} -eq 1 ]; then + echo >&2 'usage: get_git_version_part <version part number>' + echo >&2 'i.e.: 2.5.0-rc1' + echo >&2 ' ^ ^ ^ ^' + echo >&2 ' 1 2 3 4 <version part number>' + false; return + fi + part_nr=${1} + + if ! { [ 1 -le "${part_nr}" ] && [ "${part_nr}" -le 4 ]; }; then + echo 'version part number must be a number between 1 and 4' >&2 + false; return + fi + + git_version_output=$(git --version) + if [ ${?} -ne 0 ]; then + echo 'cannot determine `git" version' >&2 + false; return + fi + + git_version_dotted=$(echo "${git_version_output}" | cut -d' ' -f3) + if [ ${?} -ne 0 ]; then + echo 'cannot extract `git" version' >&2 + false; return + fi + + if [ "${part_nr}" -lt 3 ]; then + git_version_part=$(echo "${git_version_dotted}" \ + | cut -d'.' -f"${part_nr}") + else # treat the release candidate part specially + git_version_part=$(echo "${git_version_dotted}" \ + | cut -d'.' -f3) + if [ ${?} -ne 0 ]; then + echo 'cannot extract `git" version part' >&2 + false; return + fi + + if [ "${part_nr}" -eq 3 ]; then + git_version_part="$(echo "${git_version_part}" \ + | cut -d'-' -f1)" + elif { echo "${git_version_part}" \ + | grep -Eqx "[0-9]+-rc[0-9]+"; }; then + git_version_part="$(echo "${git_version_part}" \ + | sed -E 's/^[0-9+]-rc([0-9]+)$/\1/')" + else # not a release candidate + git_version_part="" + fi + fi + if [ ${?} -ne 0 ]; then + echo 'cannot extract `git" version part' >&2 + false; return + fi + + echo "${git_version_part}" + true; return +} + +git_version_lt() { + if ! { [ 3 -le ${#} ] && [ ${#} -le 4 ]; }; then + echo >&2 'compare installed git version to a specific one' + echo >&2 'usage: git_version_lt <version>' + echo >&2 'e.g.: git_version_lt 2 5 0 1' + echo >&2 ' ^ ^ ^ ^' + echo >&2 ' 2.5.0-rc1' + return 127 + fi + + for i in $(seq 1 4); do + git_version_part_lhs=$(get_git_version_part "${i}") + if ! [ ${?} -eq 0 ]; then + return 127; + fi + + git_version_part_rhs=$(eval echo ${"${i}"}) + if ! [ ${?} -eq 0 ]; then + return 127; + fi + + if [ "${i}" -eq 4 ]; then + # empty release candidate is greater than any other one + if [ -z "${git_version_part_lhs}" ]; then + false; return + elif [ -z "${git_version_part_rhs}" ]; then + true; return + else + [ "${git_version_part_lhs}" \ + -lt "${git_version_part_rhs}" ] + return + fi + elif [ "${git_version_part_lhs}" \ + -lt "${git_version_part_rhs}" ]; then + true; return + elif [ "${git_version_part_lhs}" \ + -gt "${git_version_part_rhs}" ]; then + false; return + fi + done + + # should not get here + return 127 +} + +git_version_ge() { + ! git_version_lt "${@}"; return +} + +git_version_eq() { + if [ ${#} -lt 3 ]; then + echo >&2 'compare installed git version to a specific one' + echo >&2 'usage: git_version_eq <version>' + echo >&2 'e.g.: git_version_eq 2 5 0 1' + echo >&2 ' ^ ^ ^ ^' + echo >&2 ' 2.5.0-rc1' + return 127 + fi + + for i in $(seq 1 4); do + git_version_part_lhs=$(get_git_version_part "${i}") + if ! [ ${?} -eq 0 ]; then + return 127 + fi + + git_version_part_rhs=$(eval echo ${"${i}"}) + if ! [ ${?} -eq 0 ]; then + return 127 + fi + + if [ "${i}" -eq 4 ]; then + # special treatment of the release candidates + if [ -n "${git_version_part_lhs}" ]; then + if [ -z "${git_version_part_lhs}" ]; then + false; return + fi + + [ "${git_version_part_lhs}" \ + -eq "${git_version_part_rhs}" ]; return + else + [ -z "${git_version_part_rhs}" ]; return + fi + fi + done + + # should not get here + return 127 +} + +git_version_le() { + git_version_lt "${@}" || git_version_eq "${@}"; return +} + +git_version_gt() { + ! git_version_le "${@}"; return +} + +test_git_version() { + git_real=$(command -v git) + + git() { + if [ ${#} -eq 1 ] && [ "${1}" = "--version" ]; then + echo "${git_fake_version}" + else + "${git_real}" "${@}" + fi + + return 0 + } + + git_fake_version="git version 1.23.4" + + if ! get_git_version_part ; then echo ok; else echo nok; fi + if ! get_git_version_part 0; then echo ok; else echo nok; fi + if get_git_version_part 1; then echo ok; else echo nok; fi + if get_git_version_part 2; then echo ok; else echo nok; fi + if get_git_version_part 3; then echo ok; else echo nok; fi + if get_git_version_part 4; then echo ok; else echo nok; fi + if ! get_git_version_part 5; then echo ok; else echo nok; fi + if ! get_git_version_part x; then echo ok; else echo nok; fi + + if ! git_version_lt 1 11 0 ; then echo ok; else echo nok; fi + if ! git_version_lt 1 23 4 1; then echo ok; else echo nok; fi + if ! git_version_lt 1 23 4 ; then echo ok; else echo nok; fi + if git_version_lt 1 23 5 ; then echo ok; else echo nok; fi + + git_fake_version="git version 2.11.1-rc2" + + if git_version_lt 2 12 0 ; then echo ok; else echo nok; fi + if git_version_lt 2 11 1 ; then echo ok; else echo nok; fi + if git_version_lt 2 11 1 3; then echo ok; else echo nok; fi + if ! git_version_lt 2 11 1 2; then echo ok; else echo nok; fi + if ! git_version_lt 2 11 1 1; then echo ok; else echo nok; fi + if ! git_version_lt 2 10 0 ; then echo ok; else echo nok; fi +} + +if [ ${#} -eq 1 ] && [ "${1}" = "--test" ]; then + test_git_version +fi