diff options
Diffstat (limited to 'GIT-VERSION-GEN')
-rwxr-xr-x | GIT-VERSION-GEN | 142 |
1 files changed, 77 insertions, 65 deletions
diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN index c2767b4136..63463c8773 100755 --- a/GIT-VERSION-GEN +++ b/GIT-VERSION-GEN @@ -1,18 +1,33 @@ #!/bin/sh -DEF_VER=0.21.GITGUI +DEF_VER=v2.50.GIT LF=' ' -if test "$#" -ne 2 +if test "$#" -lt 2 || test "$#" -gt 3 then - echo >&2 "usage: $0 <SOURCE_DIR> <OUTPUT>" - exit 1 + echo >&2 "USAGE: $0 <SOURCE_DIR> (--format=<STRING>|<INPUT>) [<OUTPUT>]" + exit 1 fi SOURCE_DIR="$1" -OUTPUT="$2" + +case "$2" in +--format=*) + INPUT="${2#--format=}" + ;; +*) + if ! test -f "$2" + then + echo >&2 "Input is not a file: $2" + exit 1 + fi + INPUT=$(cat "$2") + ;; +esac + +OUTPUT="$3" # Protect us from reading Git version information outside of the Git directory # in case it is not a repository itself, but embedded in an unrelated @@ -20,75 +35,72 @@ OUTPUT="$2" GIT_CEILING_DIRECTORIES="$SOURCE_DIR/.." export GIT_CEILING_DIRECTORIES -tree_search () -{ - head=$1 - tree=$2 - for p in $(git -C "$SOURCE_DIR" rev-list --parents --max-count=1 $head 2>/dev/null) - do - test $tree = $(git -C "$SOURCE_DIR" rev-parse $p^{tree} 2>/dev/null) && - vn=$(git -C "$SOURCE_DIR" describe --abbrev=4 $p 2>/dev/null) && - case "$vn" in - gitgui-[0-9]*) echo $vn; break;; +if test -z "$GIT_VERSION" +then + # First see if there is a version file (included in release tarballs), + # then try git-describe, then default. + if test -f "$SOURCE_DIR"/version + then + VN=$(cat "$SOURCE_DIR"/version) || VN="$DEF_VER" + elif { + test -d "$SOURCE_DIR/.git" || + test -d "${GIT_DIR:-.git}" || + test -f "$SOURCE_DIR"/.git; + } && + VN=$(git -C "$SOURCE_DIR" describe --dirty --match="v[0-9]*" 2>/dev/null) && + case "$VN" in + *$LF*) (exit 1) ;; esac - done -} + then + VN=$(echo "$VN" | sed -e 's/-/./g'); + else + VN="$DEF_VER" + fi -# Always use the tarball version file if found, just -# in case we are somehow contained in a larger git -# repository that doesn't actually track our state. -# (At least one package manager is doing this.) -# -# We may be a subproject, so try looking for the merge -# commit that supplied this directory content if we are -# not at the toplevel. We probably will always be the -# second parent in the commit, but we shouldn't rely on -# that fact. -# -# If we are at the toplevel or the merge assumption fails -# try looking for a gitgui-* tag. + GIT_VERSION=$(expr "$VN" : v*'\(.*\)') +fi -if test -f "$SOURCE_DIR"/version && - VN=$(cat "$SOURCE_DIR"/version) +if test -z "$GIT_BUILT_FROM_COMMIT" then - : happy -elif prefix="$(git -C "$SOURCE_DIR" rev-parse --show-prefix 2>/dev/null)" - test -n "$prefix" && - head=$(git -C "$SOURCE_DIR" rev-list --max-count=1 HEAD -- . 2>/dev/null) && - tree=$(git -C "$SOURCE_DIR" rev-parse --verify "HEAD:$prefix" 2>/dev/null) && - VN=$(tree_search $head $tree) - case "$VN" in - gitgui-[0-9]*) : happy ;; - *) (exit 1) ;; - esac + GIT_BUILT_FROM_COMMIT=$(git -C "$SOURCE_DIR" rev-parse -q --verify HEAD 2>/dev/null) +fi + +if test -z "$GIT_DATE" then - VN=$(echo "$VN" | sed -e 's/^gitgui-//;s/-/./g'); -elif VN=$(git -C "$SOURCE_DIR" describe --abbrev=4 HEAD 2>/dev/null) && - case "$VN" in - gitgui-[0-9]*) : happy ;; - *) (exit 1) ;; - esac + GIT_DATE=$(git -C "$SOURCE_DIR" show --quiet --format='%as' 2>/dev/null) +fi + +if test -z "$GIT_USER_AGENT" then - VN=$(echo "$VN" | sed -e 's/^gitgui-//;s/-/./g'); -else - VN="$DEF_VER" + GIT_USER_AGENT="git/$GIT_VERSION" fi -dirty=$(git -C "$SOURCE_DIR" diff-index --name-only HEAD 2>/dev/null) || dirty= -case "$dirty" in -'') - ;; -*) - VN="$VN-dirty" ;; -esac +# While released Git versions only have three numbers, development builds also +# have a fourth number that corresponds to the number of patches since the last +# release. +read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION GIT_PATCH_LEVEL trailing <<EOF +$(echo "$GIT_VERSION" 0 0 0 0 | tr '.a-zA-Z-' ' ') +EOF + +REPLACED=$(printf "%s\n" "$INPUT" | sed -e "s|@GIT_VERSION@|$GIT_VERSION|" \ + -e "s|@GIT_MAJOR_VERSION@|$GIT_MAJOR_VERSION|" \ + -e "s|@GIT_MINOR_VERSION@|$GIT_MINOR_VERSION|" \ + -e "s|@GIT_MICRO_VERSION@|$GIT_MICRO_VERSION|" \ + -e "s|@GIT_PATCH_LEVEL@|$GIT_PATCH_LEVEL|" \ + -e "s|@GIT_BUILT_FROM_COMMIT@|$GIT_BUILT_FROM_COMMIT|" \ + -e "s|@GIT_USER_AGENT@|$GIT_USER_AGENT|" \ + -e "s|@GIT_DATE@|$GIT_DATE|" +) -if test -r "$OUTPUT" +if test -z "$OUTPUT" then - VC=$(sed -e 's/^GITGUI_VERSION=//' <"$OUTPUT") + printf "%s\n" "$REPLACED" else - VC=unset + printf "%s\n" "$REPLACED" >"$OUTPUT".$$+ + if ! test -f "$OUTPUT" || ! cmp "$OUTPUT".$$+ "$OUTPUT" >/dev/null + then + mv "$OUTPUT".$$+ "$OUTPUT" + else + rm "$OUTPUT".$$+ + fi fi -test "$VN" = "$VC" || { - echo >&2 "GITGUI_VERSION=$VN" - echo "GITGUI_VERSION=$VN" >"$OUTPUT" -} |