Makefile: refactor GIT-VERSION-GEN to be reusable

Our "GIT-VERSION-GEN" script always writes the "GIT-VERSION-FILE" into the current directory, where the expectation is that it should exist in the source directory. But other build systems that support out-of-tree builds may not want to do that to keep the source directory pristine, even though CMake currently doesn't care. Refactor the script such that it won't write the "GIT-VERSION-FILE" directly anymore, but instead knows to replace @PLACEHOLDERS@ in an arbitrary input file. This allows us to simplify the logic in CMake to determine the project version, but can also be reused later on in order to generate other files that need to contain version information like our "git.rc" file. While at it, change the format of the version file by removing the spaces around the equals sign. Like this we can continue to include the file in our Makefiles, but can also start to source it in shell scripts in subsequent steps. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Dec 6, 2024 at 14:24 UTC 4838deab65e71686353ad2e40cad679e26bfc0a4
6 files changed +59 -34
GIT-VERSION-FILE.in new
+1
@@ -0,0 +1 @@
1 +GIT_VERSION=@GIT_VERSION@
GIT-VERSION-GEN
+46 -15
@@ -1,23 +1,48 @@
1 #!/bin/sh
2
3 -GVF=GIT-VERSION-FILE
3 DEF_VER=v2.47.GIT
4
5 LF='
6 '
7
8 +if test "$#" -ne 3
9 +then
10 + echo >&2 "USAGE: $0 <SOURCE_DIR> <INPUT> <OUTPUT>"
11 + exit 1
12 +fi
13 +
14 +SOURCE_DIR="$1"
15 +INPUT="$2"
16 +OUTPUT="$3"
17 +
18 +if ! test -f "$INPUT"
19 +then
20 + echo >&2 "Input is not a file: $INPUT"
21 + exit 1
22 +fi
23 +
24 +# Protect us from reading Git version information outside of the Git directory
25 +# in case it is not a repository itself, but embedded in an unrelated
26 +# repository.
27 +GIT_CEILING_DIRECTORIES="$SOURCE_DIR/.."
28 +export GIT_CEILING_DIRECTORIES
29 +
30 # First see if there is a version file (included in release tarballs),
31 # then try git-describe, then default.
11 -if test -f version
32 +if test -f "$SOURCE_DIR"/version
33 then
13 - VN=$(cat version) || VN="$DEF_VER"
14 -elif { test -d "${GIT_DIR:-.git}" || test -f .git; } &&
15 - VN=$(git describe --match "v[0-9]*" HEAD 2>/dev/null) &&
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]*)
19 - git update-index -q --refresh
20 - test -z "$(git diff-index --name-only HEAD --)" ||
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
@@ -26,15 +51,21 @@ else
51 VN="$DEF_VER"
52 fi
53
29 -VN=$(expr "$VN" : v*'\(.*\)')
54 +GIT_VERSION=$(expr "$VN" : v*'\(.*\)')
55 +
56 +read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION trailing <<EOF
57 +$(echo "$GIT_VERSION" 0 0 0 | tr '.a-zA-Z-' ' ')
58 +EOF
59 +
60 +sed -e "s|@GIT_VERSION@|$GIT_VERSION|" \
61 + -e "s|@GIT_MAJOR_VERSION@|$GIT_MAJOR_VERSION|" \
62 + -e "s|@GIT_MINOR_VERSION@|$GIT_MINOR_VERSION|" \
63 + -e "s|@GIT_MICRO_VERSION@|$GIT_MICRO_VERSION|" \
64 + "$INPUT" >"$OUTPUT"+
65
31 -if test -r $GVF
66 +if ! test -f "$OUTPUT" || ! cmp "$OUTPUT"+ "$OUTPUT" >/dev/null
67 then
33 - VC=$(sed -e 's/^GIT_VERSION = //' <$GVF)
68 + mv "$OUTPUT"+ "$OUTPUT"
69 else
35 - VC=unset
70 + rm "$OUTPUT"+
71 fi
37 -test "$VN" = "$VC" || {
38 - echo >&2 "GIT_VERSION = $VN"
39 - echo "GIT_VERSION = $VN" >$GVF
40 -}
Makefile
+4 -1
@@ -592,7 +592,10 @@ include shared.mak
592 # Disable -pedantic compilation.
593
594 GIT-VERSION-FILE: FORCE
595 - @$(SHELL_PATH) ./GIT-VERSION-GEN
595 + @OLD=$$(cat $@ 2>/dev/null || :) && \
596 + $(SHELL_PATH) ./GIT-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
601 # Set our default configuration.
ci/test-documentation.sh
+1 -1
@@ -6,7 +6,7 @@
6 . ${0%/*}/lib.sh
7
8 filter_log () {
9 - sed -e '/^GIT_VERSION = /d' \
9 + sed -e '/^GIT_VERSION=/d' \
10 -e "/constant Gem::ConfigMap is deprecated/d" \
11 -e '/^ \* new asciidoc flags$/d' \
12 -e '/stripped namespace before processing/d' \
contrib/buildsystems/CMakeLists.txt
+6 -17
@@ -83,23 +83,12 @@ if(NOT SH_EXE)
83 "On Windows, you can get it as part of 'Git for Windows' install at https://gitforwindows.org/")
84 endif()
85
86 -#Create GIT-VERSION-FILE using GIT-VERSION-GEN
87 -if(NOT EXISTS ${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE)
88 - message("Generating GIT-VERSION-FILE")
89 - execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN
90 - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
91 -endif()
92 -
93 -#Parse GIT-VERSION-FILE to get the version
94 -file(STRINGS ${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE git_version REGEX "GIT_VERSION = (.*)")
95 -string(REPLACE "GIT_VERSION = " "" git_version ${git_version})
96 -string(FIND ${git_version} "GIT" location)
97 -if(location EQUAL -1)
98 - string(REGEX MATCH "[0-9]*\\.[0-9]*\\.[0-9]*" git_version ${git_version})
99 -else()
100 - string(REGEX MATCH "[0-9]*\\.[0-9]*" git_version ${git_version})
101 - string(APPEND git_version ".0") #for building from a snapshot
102 -endif()
86 +message("Generating Git version")
87 +execute_process(COMMAND ${SH_EXE} "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
88 + "${CMAKE_SOURCE_DIR}"
89 + "${CMAKE_SOURCE_DIR}/contrib/buildsystems/git-version.in"
90 + "${CMAKE_BINARY_DIR}/git-version")
91 +file(STRINGS "${CMAKE_BINARY_DIR}/git-version" git_version)
92
93 project(git
94 VERSION ${git_version}
contrib/buildsystems/git-version.in new
+1
@@ -0,0 +1 @@
1 +@GIT_MAJOR_VERSION@.@GIT_MINOR_VERSION@.@GIT_MICRO_VERSION@