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