| 1 | #!/bin/sh |
| 2 | |
| 3 | DEF_VER=0.21.GITGUI |
| 4 | |
| 5 | LF=' |
| 6 | ' |
| 7 | |
| 8 | if test "$#" -lt 2 |
| 9 | then |
| 10 | echo >&2 "usage: $0 <SOURCE_DIR> <OUTPUT> [<PARENT_PROJECT_DIR>]" |
| 11 | exit 1 |
| 12 | fi |
| 13 | |
| 14 | SOURCE_DIR="$1" |
| 15 | OUTPUT="$2" |
| 16 | PARENT_PROJECT_DIR="$3" |
| 17 | |
| 18 | # Protect us from reading Git version information outside of the Git directory |
| 19 | # in case it is not a repository itself, but embedded in an unrelated |
| 20 | # repository. The PARENT_PROJECT_DIR variable can be used to override this, for |
| 21 | # example when git-gui is included as a subproject. |
| 22 | if test -n "$PARENT_PROJECT_DIR" |
| 23 | then |
| 24 | GIT_CEILING_DIRECTORIES="$PARENT_PROJECT_DIR/.." |
| 25 | else |
| 26 | GIT_CEILING_DIRECTORIES="$SOURCE_DIR/.." |
| 27 | fi |
| 28 | |
| 29 | export GIT_CEILING_DIRECTORIES |
| 30 | |
| 31 | tree_search () |
| 32 | { |
| 33 | head=$1 |
| 34 | tree=$2 |
| 35 | for p in $(git -C "$SOURCE_DIR" rev-list --parents --max-count=1 $head 2>/dev/null) |
| 36 | do |
| 37 | test $tree = $(git -C "$SOURCE_DIR" rev-parse $p^{tree} 2>/dev/null) && |
| 38 | vn=$(git -C "$SOURCE_DIR" describe --abbrev=4 $p 2>/dev/null) && |
| 39 | case "$vn" in |
| 40 | gitgui-[0-9]*) echo $vn; break;; |
| 41 | esac |
| 42 | done |
| 43 | } |
| 44 | |
| 45 | # Always use the tarball version file if found, just |
| 46 | # in case we are somehow contained in a larger git |
| 47 | # repository that doesn't actually track our state. |
| 48 | # (At least one package manager is doing this.) |
| 49 | # |
| 50 | # We may be a subproject, so try looking for the merge |
| 51 | # commit that supplied this directory content if we are |
| 52 | # not at the toplevel. We probably will always be the |
| 53 | # second parent in the commit, but we shouldn't rely on |
| 54 | # that fact. |
| 55 | # |
| 56 | # If we are at the toplevel or the merge assumption fails |
| 57 | # try looking for a gitgui-* tag. |
| 58 | |
| 59 | if test -f "$SOURCE_DIR"/version && |
| 60 | VN=$(cat "$SOURCE_DIR"/version) |
| 61 | then |
| 62 | : happy |
| 63 | elif prefix="$(git -C "$SOURCE_DIR" rev-parse --show-prefix 2>/dev/null)" |
| 64 | test -n "$prefix" && |
| 65 | head=$(git -C "$SOURCE_DIR" rev-list --max-count=1 HEAD -- . 2>/dev/null) && |
| 66 | tree=$(git -C "$SOURCE_DIR" rev-parse --verify "HEAD:$prefix" 2>/dev/null) && |
| 67 | VN=$(tree_search $head $tree) |
| 68 | case "$VN" in |
| 69 | gitgui-[0-9]*) : happy ;; |
| 70 | *) (exit 1) ;; |
| 71 | esac |
| 72 | then |
| 73 | VN=$(echo "$VN" | sed -e 's/^gitgui-//;s/-/./g'); |
| 74 | elif VN=$(git -C "$SOURCE_DIR" describe --abbrev=4 HEAD 2>/dev/null) && |
| 75 | case "$VN" in |
| 76 | gitgui-[0-9]*) : happy ;; |
| 77 | *) (exit 1) ;; |
| 78 | esac |
| 79 | then |
| 80 | VN=$(echo "$VN" | sed -e 's/^gitgui-//;s/-/./g'); |
| 81 | else |
| 82 | VN="$DEF_VER" |
| 83 | fi |
| 84 | |
| 85 | dirty=$(git -C "$SOURCE_DIR" diff-index --name-only HEAD 2>/dev/null) || dirty= |
| 86 | case "$dirty" in |
| 87 | '') |
| 88 | ;; |
| 89 | *) |
| 90 | VN="$VN-dirty" ;; |
| 91 | esac |
| 92 | |
| 93 | if test -r "$OUTPUT" |
| 94 | then |
| 95 | VC=$(sed -e 's/^GITGUI_VERSION=//' <"$OUTPUT") |
| 96 | else |
| 97 | VC=unset |
| 98 | fi |
| 99 | test "$VN" = "$VC" || { |
| 100 | echo >&2 "GITGUI_VERSION=$VN" |
| 101 | echo "GITGUI_VERSION=$VN" >"$OUTPUT" |
| 102 | } |