Skip to content

Commit

Permalink
Makefile: refactor GIT-VERSION-GEN to be reusable
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
pks-t authored and gitster committed Dec 6, 2024
1 parent dbe46c0 commit 4838dea
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 34 deletions.
1 change: 1 addition & 0 deletions GIT-VERSION-FILE.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GIT_VERSION=@GIT_VERSION@
61 changes: 46 additions & 15 deletions GIT-VERSION-GEN
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
#!/bin/sh

GVF=GIT-VERSION-FILE
DEF_VER=v2.47.GIT

LF='
'

if test "$#" -ne 3
then
echo >&2 "USAGE: $0 <SOURCE_DIR> <INPUT> <OUTPUT>"
exit 1
fi

SOURCE_DIR="$1"
INPUT="$2"
OUTPUT="$3"

if ! test -f "$INPUT"
then
echo >&2 "Input is not a file: $INPUT"
exit 1
fi

# Protect us from reading Git version information outside of the Git directory
# in case it is not a repository itself, but embedded in an unrelated
# repository.
GIT_CEILING_DIRECTORIES="$SOURCE_DIR/.."
export GIT_CEILING_DIRECTORIES

# First see if there is a version file (included in release tarballs),
# then try git-describe, then default.
if test -f version
if test -f "$SOURCE_DIR"/version
then
VN=$(cat version) || VN="$DEF_VER"
elif { test -d "${GIT_DIR:-.git}" || test -f .git; } &&
VN=$(git describe --match "v[0-9]*" HEAD 2>/dev/null) &&
VN=$(cat "$SOURCE_DIR"/version) || VN="$DEF_VER"
elif {
test -d "$SOURCE_DIR/.git" ||
test -d "${GIT_DIR:-.git}" ||
test -f "$SOURCE_DIR"/.git;
} &&
VN=$(git -C "$SOURCE_DIR" describe --match "v[0-9]*" HEAD 2>/dev/null) &&
case "$VN" in
*$LF*) (exit 1) ;;
v[0-9]*)
git update-index -q --refresh
test -z "$(git diff-index --name-only HEAD --)" ||
git -C "$SOURCE_DIR" update-index -q --refresh
test -z "$(git -C "$SOURCE_DIR" diff-index --name-only HEAD --)" ||
VN="$VN-dirty" ;;
esac
then
Expand All @@ -26,15 +51,21 @@ else
VN="$DEF_VER"
fi

VN=$(expr "$VN" : v*'\(.*\)')
GIT_VERSION=$(expr "$VN" : v*'\(.*\)')

read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION trailing <<EOF
$(echo "$GIT_VERSION" 0 0 0 | tr '.a-zA-Z-' ' ')
EOF

sed -e "s|@GIT_VERSION@|$GIT_VERSION|" \
-e "s|@GIT_MAJOR_VERSION@|$GIT_MAJOR_VERSION|" \
-e "s|@GIT_MINOR_VERSION@|$GIT_MINOR_VERSION|" \
-e "s|@GIT_MICRO_VERSION@|$GIT_MICRO_VERSION|" \
"$INPUT" >"$OUTPUT"+

if test -r $GVF
if ! test -f "$OUTPUT" || ! cmp "$OUTPUT"+ "$OUTPUT" >/dev/null
then
VC=$(sed -e 's/^GIT_VERSION = //' <$GVF)
mv "$OUTPUT"+ "$OUTPUT"
else
VC=unset
rm "$OUTPUT"+
fi
test "$VN" = "$VC" || {
echo >&2 "GIT_VERSION = $VN"
echo "GIT_VERSION = $VN" >$GVF
}
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,10 @@ include shared.mak
# Disable -pedantic compilation.

GIT-VERSION-FILE: FORCE
@$(SHELL_PATH) ./GIT-VERSION-GEN
@OLD=$$(cat $@ 2>/dev/null || :) && \
$(SHELL_PATH) ./GIT-VERSION-GEN "$(shell pwd)" GIT-VERSION-FILE.in $@ && \
NEW=$$(cat $@ 2>/dev/null || :) && \
if test "$$OLD" != "$$NEW"; then echo "$$NEW" >&2; fi
-include GIT-VERSION-FILE

# Set our default configuration.
Expand Down
2 changes: 1 addition & 1 deletion ci/test-documentation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
. ${0%/*}/lib.sh

filter_log () {
sed -e '/^GIT_VERSION = /d' \
sed -e '/^GIT_VERSION=/d' \
-e "/constant Gem::ConfigMap is deprecated/d" \
-e '/^ \* new asciidoc flags$/d' \
-e '/stripped namespace before processing/d' \
Expand Down
23 changes: 6 additions & 17 deletions contrib/buildsystems/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,12 @@ if(NOT SH_EXE)
"On Windows, you can get it as part of 'Git for Windows' install at https://gitforwindows.org/")
endif()

#Create GIT-VERSION-FILE using GIT-VERSION-GEN
if(NOT EXISTS ${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE)
message("Generating GIT-VERSION-FILE")
execute_process(COMMAND ${SH_EXE} ${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
endif()

#Parse GIT-VERSION-FILE to get the version
file(STRINGS ${CMAKE_SOURCE_DIR}/GIT-VERSION-FILE git_version REGEX "GIT_VERSION = (.*)")
string(REPLACE "GIT_VERSION = " "" git_version ${git_version})
string(FIND ${git_version} "GIT" location)
if(location EQUAL -1)
string(REGEX MATCH "[0-9]*\\.[0-9]*\\.[0-9]*" git_version ${git_version})
else()
string(REGEX MATCH "[0-9]*\\.[0-9]*" git_version ${git_version})
string(APPEND git_version ".0") #for building from a snapshot
endif()
message("Generating Git version")
execute_process(COMMAND ${SH_EXE} "${CMAKE_SOURCE_DIR}/GIT-VERSION-GEN"
"${CMAKE_SOURCE_DIR}"
"${CMAKE_SOURCE_DIR}/contrib/buildsystems/git-version.in"
"${CMAKE_BINARY_DIR}/git-version")
file(STRINGS "${CMAKE_BINARY_DIR}/git-version" git_version)

project(git
VERSION ${git_version}
Expand Down
1 change: 1 addition & 0 deletions contrib/buildsystems/git-version.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@GIT_MAJOR_VERSION@.@GIT_MINOR_VERSION@.@GIT_MICRO_VERSION@

0 comments on commit 4838dea

Please sign in to comment.