From 28f31366113964140f99bf794ed0443bf4c92276 Mon Sep 17 00:00:00 2001 From: Avery Pennarun Date: Tue, 10 Nov 2020 23:33:23 -0500 Subject: [PATCH] version.sh: remove use of `git describe --exclude` This option isn't available on slightly older versions of git. We were no longer using the real describe functionality anyway, so let's just do something simpler to detect a dirty worktree. While we're here, fix up a little bit of sh style. Signed-off-by: Avery Pennarun --- version/version.sh | 62 ++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/version/version.sh b/version/version.sh index cd403e211..01a10c7e3 100755 --- a/version/version.sh +++ b/version/version.sh @@ -1,22 +1,43 @@ #!/bin/sh - set -eu +# Return the commitid of the given ref in the given repo dir. If the worktree +# or index is dirty, also appends -dirty. +# +# $ git_hash_dirty ../.. HEAD +# 1be01ddc6e430ca3aa9beea3587d16750efb3241-dirty +git_hash_dirty() { + ( + cd "$1" && + x=$(git rev-parse HEAD) && + if ! git diff-index --quiet HEAD; then + x="$x-dirty" + fi + echo "$x" + ) +} + case $# in 0|1) - # extra_hash describes a git repository other than the current - # one. It gets embedded as an additional commit hash in built + # extra_hash_or_dir is either: + # - a git commitid + # or + # - the path to a git repo from which to calculate the real hash. + # + # It gets embedded as an additional commit hash in built # binaries, to help us locate the exact set of tools and code # that were used. - extra_hash="${1:-}" - if [ -z "$extra_hash" ]; then + extra_hash_or_dir="${1:-}" + if [ -z "$extra_hash_or_dir" ]; then # Nothing, empty extra hash is fine. extra_hash="" - elif [ -d "$extra_hash/.git" ]; then - extra_hash=$(cd "$extra_hash" && git describe --always --dirty --exclude '*' --abbrev=200) + elif [ -d "$extra_hash_or_dir/.git" ]; then + extra_hash=$(git_hash_dirty "$extra_hash_or_dir" HEAD) elif ! expr "$extra_hash" : "^[0-9a-f]*$"; then echo "Invalid extra hash '$extra_hash', must be a git commit hash or path to a git repo" >&2 exit 1 + else + extra_hash="$extra_hash_or_dir" fi # Load the base version and optional corresponding git hash @@ -25,15 +46,12 @@ case $# in version_file="$(dirname $0)/../VERSION.txt" IFS=".$IFS" read -r major minor patch base_git_hash <"$version_file" if [ -z "$base_git_hash" ]; then - base_git_hash=$(git rev-list --max-count=1 HEAD -- $version_file) + base_git_hash=$(git rev-list --max-count=1 HEAD -- "$version_file") fi - # The full git has we're currently building at. --abbrev=200 is an - # arbitrary large number larger than all currently-known hashes, so - # that git displays the full commit hash. - git_hash=$(git describe --always --dirty --exclude '*' --abbrev=200) + git_hash=$(git_hash_dirty . HEAD) # The number of extra commits between the release base to git_hash. - change_count=$(git rev-list ${base_git_hash}..HEAD | wc -l | sed 's/ *//') + change_count=$(git rev-list --count HEAD "^$base_git_hash") ;; 6) # Test mode: rather than run git commands and whatnot, take in @@ -52,8 +70,8 @@ esac # Shortened versions of git hashes, so that they fit neatly into an # "elongated" but still human-readable version number. -short_git_hash=$(echo $git_hash | cut -c-9) -short_extra_hash=$(echo $extra_hash | cut -c-9) +short_git_hash=$(echo "$git_hash" | cut -c1-9) +short_extra_hash=$(echo "$extra_hash" | cut -c1-9) # Convert major/minor/patch/change_count into an adjusted # major/minor/patch. This block is where all our policies on @@ -62,7 +80,7 @@ if expr "$minor" : "[0-9]*[13579]$" >/dev/null; then # Odd minor numbers are unstable builds. if [ "$patch" != "0" ]; then # This is a fatal error, because a non-zero patch number - # indicates that we created an unstable git tag in violation + # indicates that we created an unstable VERSION.txt in violation # of our versioning policy, and we want to blow up loudly to # get that fixed. echo "Unstable release $major.$minor.$patch has a non-zero patch number, which is not allowed" >&2 @@ -100,10 +118,10 @@ else long_version_suffix="-t${short_git_hash}-g${short_extra_hash}" fi cat <