git-gui: prepare GIT-VERSION-GEN for out-of-tree builds

The GIT-VERSION-GEN unconditionally writes version information into the source directory in the form of the "GIT-VERSION-FILE". We are about to introduce the Meson build system though, which enforces out-of-tree builds by default, and in that context we cannot continue to write version information into the source tree. Prepare the script for out-of-tree builds by treating the source directory different from the output file. Signed-off-by: Patrick Steinhardt <ps@pks.im>

Patrick Steinhardt committed Mar 11, 2025 at 11:06 UTC 3ef470fa5127b9858d203990211f4375d38d5dd6
2 files changed +29 -15
GIT-VERSION-GEN
+28 -14
@@ -1,19 +1,33 @@
1 #!/bin/sh
2
3 -GVF=GIT-VERSION-FILE
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
13 - for p in $(git rev-list --parents --max-count=1 $head 2>/dev/null)
27 + for p in $(git -C "$SOURCE_DIR" rev-list --parents --max-count=1 $head 2>/dev/null)
28 do
15 - test $tree = $(git rev-parse $p^{tree} 2>/dev/null) &&
16 - vn=$(git describe --abbrev=4 $p 2>/dev/null) &&
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,14 +48,14 @@ tree_search ()
48 # If we are at the toplevel or the merge assumption fails
49 # try looking for a gitgui-* tag.
50
37 -if test -f version &&
38 - VN=$(cat version)
51 +if test -f "$SOURCE_DIR"/version &&
52 + VN=$(cat "$SOURCE_DIR"/version)
53 then
54 : happy
41 -elif prefix="$(git rev-parse --show-prefix 2>/dev/null)"
55 +elif prefix="$(git -C "$SOURCE_DIR" rev-parse --show-prefix 2>/dev/null)"
56 test -n "$prefix" &&
43 - head=$(git rev-list --max-count=1 HEAD -- . 2>/dev/null) &&
44 - tree=$(git rev-parse --verify "HEAD:$prefix" 2>/dev/null) &&
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 ;;
@@ -49,7 +63,7 @@ elif prefix="$(git rev-parse --show-prefix 2>/dev/null)"
63 esac
64 then
65 VN=$(echo "$VN" | sed -e 's/^gitgui-//;s/-/./g');
52 -elif VN=$(git describe --abbrev=4 HEAD 2>/dev/null) &&
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) ;;
@@ -60,7 +74,7 @@ else
74 VN="$DEF_VER"
75 fi
76
63 -dirty=$(sh -c 'git diff-index --name-only HEAD' 2>/dev/null) || dirty=
77 +dirty=$(git -C "$SOURCE_DIR" diff-index --name-only HEAD 2>/dev/null) || dirty=
78 case "$dirty" in
79 '')
80 ;;
@@ -68,13 +82,13 @@ case "$dirty" in
82 VN="$VN-dirty" ;;
83 esac
84
71 -if test -r $GVF
85 +if test -r "$OUTPUT"
86 then
73 - VC=$(sed -e 's/^GITGUI_VERSION = //' <$GVF)
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"
79 - echo "GITGUI_VERSION = $VN" >$GVF
93 + echo "GITGUI_VERSION = $VN" >"$OUTPUT"
94 }
Makefile
+1 -1
@@ -8,7 +8,7 @@ all::
8 #
9
10 GIT-VERSION-FILE: FORCE
11 - @$(SHELL_PATH) ./GIT-VERSION-GEN
11 + @$(SHELL_PATH) ./GIT-VERSION-GEN . $@
12 ifneq ($(MAKECMDGOALS),clean)
13 -include GIT-VERSION-FILE
14 endif