Skip to content

Commit 4838dea

Browse files
pks-tgitster
authored andcommittedDec 6, 2024
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dbe46c0 commit 4838dea

File tree

6 files changed

+59
-34
lines changed

6 files changed

+59
-34
lines changed
 

‎GIT-VERSION-FILE.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GIT_VERSION=@GIT_VERSION@

‎GIT-VERSION-GEN

+46-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,48 @@
11
#!/bin/sh
22

3-
GVF=GIT-VERSION-FILE
43
DEF_VER=v2.47.GIT
54

65
LF='
76
'
87

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+
930
# First see if there is a version file (included in release tarballs),
1031
# then try git-describe, then default.
11-
if test -f version
32+
if test -f "$SOURCE_DIR"/version
1233
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) &&
1641
case "$VN" in
1742
*$LF*) (exit 1) ;;
1843
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 --)" ||
2146
VN="$VN-dirty" ;;
2247
esac
2348
then
@@ -26,15 +51,21 @@ else
2651
VN="$DEF_VER"
2752
fi
2853

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"+
3065

31-
if test -r $GVF
66+
if ! test -f "$OUTPUT" || ! cmp "$OUTPUT"+ "$OUTPUT" >/dev/null
3267
then
33-
VC=$(sed -e 's/^GIT_VERSION = //' <$GVF)
68+
mv "$OUTPUT"+ "$OUTPUT"
3469
else
35-
VC=unset
70+
rm "$OUTPUT"+
3671
fi
37-
test "$VN" = "$VC" || {
38-
echo >&2 "GIT_VERSION = $VN"
39-
echo "GIT_VERSION = $VN" >$GVF
40-
}

‎Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,10 @@ include shared.mak
592592
# Disable -pedantic compilation.
593593

594594
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
596599
-include GIT-VERSION-FILE
597600

598601
# Set our default configuration.

‎ci/test-documentation.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
. ${0%/*}/lib.sh
77

88
filter_log () {
9-
sed -e '/^GIT_VERSION = /d' \
9+
sed -e '/^GIT_VERSION=/d' \
1010
-e "/constant Gem::ConfigMap is deprecated/d" \
1111
-e '/^ \* new asciidoc flags$/d' \
1212
-e '/stripped namespace before processing/d' \

‎contrib/buildsystems/CMakeLists.txt

+6-17
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,12 @@ if(NOT SH_EXE)
8383
"On Windows, you can get it as part of 'Git for Windows' install at https://gitforwindows.org/")
8484
endif()
8585

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)
10392

10493
project(git
10594
VERSION ${git_version}

‎contrib/buildsystems/git-version.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@GIT_MAJOR_VERSION@.@GIT_MINOR_VERSION@.@GIT_MICRO_VERSION@

0 commit comments

Comments
 (0)