Skip to content

Commit 992bc56

Browse files
pks-tgitster
authored andcommitted
GIT-VERSION-GEN: fix overriding GIT_VERSION
GIT-VERSION-GEN tries to derive the version that Git is being built from via multiple different sources in the following order: 1. A file called "version" in the source tree's root directory, if it exists. 2. The current commit in case Git is built from a Git repository. 3. Otherwise, we use a fallback version stored in a variable which is bumped whenever a new Git version is getting tagged. It used to be possible to override the version by overriding the `GIT_VERSION` Makefile variable (e.g. `make GIT_VERSION=foo`). This worked somewhat by chance, only: `GIT-VERSION-GEN` would write the actual Git version into `GIT-VERSION-FILE`, not the overridden value, but when including the file into our Makefile we would not override the `GIT_VERSION` variable because it has already been set by the user. And because our Makefile used the variable to propagate the version to our build tools instead of using `GIT-VERSION-FILE` the resulting build artifacts used the overridden version. But that subtle mechanism broke with 4838dea (Makefile: refactor GIT-VERSION-GEN to be reusable, 2024-12-06) and subsequent commits because the version information is not propagated via the Makefile variable anymore, but instead via the files that `GIT-VERSION-GEN` started to write. And as the script never knew about the `GIT_VERSION` environment variable in the first place it uses one of the values listed above instead of the overridden value. Fix this issue by making `GIT-VERSION-GEN` handle the case where `GIT_VERSION` has been set via the environment. Note that this requires us to introduce a new GIT_VERSION_OVERRIDE variable that stores a potential user-provided value, either via the environment or via "config.mak". Ideally we wouldn't need it and could just continue to use GIT_VERSION for this. But unfortunately, Makefiles will first include all sub-Makefiles before figuring out whether it needs to re-make any of them [1]. Consequently, if there already is a GIT-VERSION-FILE, we would have slurped in its value of GIT_VERSION before we call GIT-VERSION-GEN, and because GIT-VERSION-GEN now uses that value as an override it would mean that the first generated value for GIT_VERSION will remain unchanged. Furthermore we have to move the include for "GIT-VERSION-FILE" after the includes for "config.mak" and related so that GIT_VERSION_OVERRIDE can be set to the value provided by "config.mak". [1]: https://www.gnu.org/software/make/manual/html_node/Remaking-Makefiles.html Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 114494a commit 992bc56

File tree

4 files changed

+43
-29
lines changed

4 files changed

+43
-29
lines changed

Documentation/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ endif
181181
-include ../config.mak.autogen
182182
-include ../config.mak
183183

184+
# Set GIT_VERSION_OVERRIDE such that version_gen knows to substitute
185+
# GIT_VERSION in case it was set by the user.
186+
GIT_VERSION_OVERRIDE := $(GIT_VERSION)
187+
184188
ifndef NO_MAN_BOLD_LITERAL
185189
XMLTO_EXTRA += -m manpage-bold-literal.xsl
186190
endif

GIT-VERSION-GEN

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,35 @@ fi
2727
GIT_CEILING_DIRECTORIES="$SOURCE_DIR/.."
2828
export GIT_CEILING_DIRECTORIES
2929

30-
# First see if there is a version file (included in release tarballs),
31-
# then try git-describe, then default.
32-
if test -f "$SOURCE_DIR"/version
30+
if test -z "$GIT_VERSION"
3331
then
34-
VN=$(cat "$SOURCE_DIR"/version) || VN="$DEF_VER"
35-
elif {
36-
test -d "$SOURCE_DIR/.git" ||
37-
test -d "${GIT_DIR:-.git}" ||
38-
test -f "$SOURCE_DIR"/.git;
39-
} &&
40-
VN=$(git -C "$SOURCE_DIR" describe --match "v[0-9]*" HEAD 2>/dev/null) &&
41-
case "$VN" in
42-
*$LF*) (exit 1) ;;
43-
v[0-9]*)
44-
git -C "$SOURCE_DIR" update-index -q --refresh
45-
test -z "$(git -C "$SOURCE_DIR" diff-index --name-only HEAD --)" ||
46-
VN="$VN-dirty" ;;
47-
esac
48-
then
49-
VN=$(echo "$VN" | sed -e 's/-/./g');
50-
else
51-
VN="$DEF_VER"
32+
# First see if there is a version file (included in release tarballs),
33+
# then try git-describe, then default.
34+
if test -f "$SOURCE_DIR"/version
35+
then
36+
VN=$(cat "$SOURCE_DIR"/version) || VN="$DEF_VER"
37+
elif {
38+
test -d "$SOURCE_DIR/.git" ||
39+
test -d "${GIT_DIR:-.git}" ||
40+
test -f "$SOURCE_DIR"/.git;
41+
} &&
42+
VN=$(git -C "$SOURCE_DIR" describe --match "v[0-9]*" HEAD 2>/dev/null) &&
43+
case "$VN" in
44+
*$LF*) (exit 1) ;;
45+
v[0-9]*)
46+
git -C "$SOURCE_DIR" update-index -q --refresh
47+
test -z "$(git -C "$SOURCE_DIR" diff-index --name-only HEAD --)" ||
48+
VN="$VN-dirty" ;;
49+
esac
50+
then
51+
VN=$(echo "$VN" | sed -e 's/-/./g');
52+
else
53+
VN="$DEF_VER"
54+
fi
55+
56+
GIT_VERSION=$(expr "$VN" : v*'\(.*\)')
5257
fi
5358

54-
GIT_VERSION=$(expr "$VN" : v*'\(.*\)')
5559
GIT_BUILT_FROM_COMMIT=$(git -C "$SOURCE_DIR" rev-parse -q --verify HEAD 2>/dev/null)
5660
GIT_DATE=$(git -C "$SOURCE_DIR" show --quiet --format='%as' 2>/dev/null)
5761
if test -z "$GIT_USER_AGENT"

Makefile

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -591,13 +591,6 @@ include shared.mak
591591
#
592592
# Disable -pedantic compilation.
593593

594-
GIT-VERSION-FILE: FORCE
595-
@OLD=$$(cat $@ 2>/dev/null || :) && \
596-
$(call version_gen,"$(shell pwd)",GIT-VERSION-FILE.in,$@) && \
597-
NEW=$$(cat $@ 2>/dev/null || :) && \
598-
if test "$$OLD" != "$$NEW"; then echo "$$NEW" >&2; fi
599-
-include GIT-VERSION-FILE
600-
601594
# Set our default configuration.
602595
#
603596
# Among the variables below, these:
@@ -1465,6 +1458,18 @@ ifdef DEVELOPER
14651458
include config.mak.dev
14661459
endif
14671460

1461+
GIT-VERSION-FILE: FORCE
1462+
@OLD=$$(cat $@ 2>/dev/null || :) && \
1463+
$(call version_gen,"$(shell pwd)",GIT-VERSION-FILE.in,$@) && \
1464+
NEW=$$(cat $@ 2>/dev/null || :) && \
1465+
if test "$$OLD" != "$$NEW"; then echo "$$NEW" >&2; fi
1466+
1467+
# We need to set GIT_VERSION_OVERRIDE before including the version file as
1468+
# otherwise any user-provided value for GIT_VERSION would have been overridden
1469+
# already.
1470+
GIT_VERSION_OVERRIDE := $(GIT_VERSION)
1471+
-include GIT-VERSION-FILE
1472+
14681473
# what 'all' will build and 'install' will install in gitexecdir,
14691474
# excluding programs for built-in commands
14701475
ALL_PROGRAMS = $(PROGRAMS) $(SCRIPTS)

shared.mak

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,6 @@ endef
122122
# as arguments, in that order.
123123
define version_gen
124124
GIT_USER_AGENT="$(GIT_USER_AGENT)" \
125+
GIT_VERSION="$(GIT_VERSION_OVERRIDE)" \
125126
$(SHELL_PATH) "$(1)/GIT-VERSION-GEN" "$(1)" "$(2)" "$(3)"
126127
endef

0 commit comments

Comments
 (0)