git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH RFC v7 03/22] Makefile: refactor GIT-VERSION-GEN to be reusable

From
Patrick Steinhardt <ps@pks.im>
Date
Nov 15, 2024, 07:21 UTC
Message-ID
<20241115-pks-meson-v7-3-47ec19b780b2@pks.im>
In-Reply-To
<20241115-pks-meson-v7-0-47ec19b780b2@pks.im>

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. Furthermore, we already start to replace some placeholders that are not yet of relevance in this commit. These are going to become relevant in subsequent steps, as well.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 GIT-VERSION-FILE.in                 |  1 +
 GIT-VERSION-GEN                     | 62 ++++++++++++++++++++++++++++---------
 Makefile                            |  5 ++-
 ci/test-documentation.sh            |  2 +-
 contrib/buildsystems/CMakeLists.txt | 23 ++++----------
 contrib/buildsystems/git-version.in |  1 +
 6 files changed, 60 insertions(+), 34 deletions(-)
diff --git a/GIT-VERSION-FILE.in b/GIT-VERSION-FILE.in
new file mode 100644
index 0000000000000000000000000000000000000000..3789a48a34a3f9e37531fc24b577ffe3c657a3e9
--- /dev/null
+++ b/GIT-VERSION-FILE.in
@@ -0,0 +1 @@
+GIT_VERSION=@GIT_VERSION@
diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 78e8631f677bbebe8f18d83191f0fd014465563e..3a2dfd477b9b9c18ef34b33c9bf6a0d81b2740b5 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -1,23 +1,41 @@
 #!/bin/sh
 
-GVF=GIT-VERSION-FILE
 DEF_VER=v2.47.GIT
 
 LF='
 '
 
+if test "$#" -ne 3
+then
+    echo "USAGE: $0 <SOURCE_DIR> <INPUT> <OUTPUT>" >&2
+    exit 1
+fi
+
+SOURCE_DIR="$1"
+INPUT="$2"
+OUTPUT="$3"
+
+if ! test -f "$INPUT"
+then
+	echo "Input is not a file: $INPUT" >&2
+	exit 1
+fi
+
+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
@@ -26,15 +44,29 @@ else
 	VN="$DEF_VER"
 fi
 
-VN=$(expr "$VN" : v*'\(.*\)')
+GIT_BUILT_FROM_COMMIT=$(git -C "$SOURCE_DIR" rev-parse -q --verify HEAD 2>/dev/null)
+GIT_VERSION=$(expr "$VN" : v*'\(.*\)')
+if test -z "$GIT_USER_AGENT"
+then
+	GIT_USER_AGENT=git/$GIT_VERSION
+fi
+
+read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION GIT_PATCH_LEVEL trailing <<EOF
+$(echo "$GIT_VERSION" 0 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|" \
+	-e "s|@GIT_PATCH_LEVEL@|$GIT_PATCH_LEVEL|" \
+	-e "s|@GIT_USER_AGENT@|$GIT_USER_AGENT|" \
+	-e "s|@GIT_BUILT_FROM_COMMIT@|$GIT_BUILT_FROM_COMMIT|" \
+	"$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
-}
diff --git a/Makefile b/Makefile
index d59f6c16868e24a1e13b0c16cc05adb79f81bbc8..6c55c47e961047a8e9a80ab948e21c7466cf54de 100644
--- a/Makefile
+++ b/Makefile
@@ -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.
diff --git a/ci/test-documentation.sh b/ci/test-documentation.sh
index 02b3af394117f840e078479ca60030141e47f998..6c018b673e0563fa5589195a77804c91deb93515 100755
--- a/ci/test-documentation.sh
+++ b/ci/test-documentation.sh
@@ -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' \
diff --git a/contrib/buildsystems/CMakeLists.txt b/contrib/buildsystems/CMakeLists.txt
index 8b0020edeba289e5eaa15a5452013fa2e4c3ed33..752479cac59d3833e7fff9239ebea75179692bf4 100644
--- a/contrib/buildsystems/CMakeLists.txt
+++ b/contrib/buildsystems/CMakeLists.txt
@@ -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}
diff --git a/contrib/buildsystems/git-version.in b/contrib/buildsystems/git-version.in
new file mode 100644
index 0000000000000000000000000000000000000000..9750505ae77685ebb31a38468caaf13501b6739d
--- /dev/null
+++ b/contrib/buildsystems/git-version.in
@@ -0,0 +1 @@
+@GIT_MAJOR_VERSION@.@GIT_MINOR_VERSION@.@GIT_MICRO_VERSION@
-- 
2.47.0.251.gb31fb630c0.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 4 of 31 in “Modernize the build system”
  1. 00/22 Modernize the build systemPatrick Steinhardt, Nov 15, 2024
  2. 01/22 Makefile: use common template for GIT-BUILD-OPTIONSPatrick Steinhardt, Nov 15, 2024
  3. 02/22 Makefile: consistently use @PLACEHOLDER@ to substitutePatrick Steinhardt, Nov 15, 2024
  4. 03/22 Makefile: refactor GIT-VERSION-GEN to be reusablePatrick Steinhardt, Nov 15, 2024
  5. 04/22 Makefile: propagate Git version via generated headerPatrick Steinhardt, Nov 15, 2024
  6. 05/22 Makefile: generate "git.rc" via GIT-VERSION-GENPatrick Steinhardt, Nov 15, 2024
  7. 06/22 Makefile: consistently use PERL_PATHPatrick Steinhardt, Nov 15, 2024
  8. 07/22 Makefile: extract script to massage Perl scriptsPatrick Steinhardt, Nov 15, 2024
  9. 08/22 Makefile: use "generate-perl.sh" to massage Perl libraryPatrick Steinhardt, Nov 15, 2024
  10. 10/22 Makefile: extract script to generate gitweb.cgiPatrick Steinhardt, Nov 15, 2024
  11. 09/22 Makefile: extract script to massage Shell scriptsPatrick Steinhardt, Nov 15, 2024
  12. 11/22 Makefile: extract script to generate gitweb.jsPatrick Steinhardt, Nov 15, 2024
  13. 12/22 Makefile: refactor generators to be PWD-independentPatrick Steinhardt, Nov 15, 2024
  14. 13/22 Makefile: allow "bin-wrappers/" directory to existPatrick Steinhardt, Nov 15, 2024
  15. 14/22 Makefile: simplify building of templatesPatrick Steinhardt, Nov 15, 2024
  16. 16/22 Documentation: teach "cmd-list.perl" about out-of-tree buildsPatrick Steinhardt, Nov 15, 2024
  17. 15/22 Documentation: allow sourcing generated includes from separate dirPatrick Steinhardt, Nov 15, 2024
  18. 17/22 Documentation: extract script to generate a list of mergetoolsPatrick Steinhardt, Nov 15, 2024
  19. 19/22 t: allow overriding build dirPatrick Steinhardt, Nov 15, 2024
  20. 18/22 t: better support for out-of-tree buildsPatrick Steinhardt, Nov 15, 2024
  21. 20/22 Documentation: add comparison of build systemsPatrick Steinhardt, Nov 15, 2024
  22. 21/22 Introduce support for the Meson build systemPatrick Steinhardt, Nov 15, 2024
  23. Christian CouderNov 15, 2024
  24. Patrick SteinhardtNov 18, 2024
  25. 22/22 meson: fix conflicts with in-flight topicsPatrick Steinhardt, Nov 15, 2024
  26. Patrick SteinhardtNov 15, 2024
  27. Ramsay JonesNov 16, 2024
  28. Patrick SteinhardtNov 18, 2024
  29. Junio C HamanoNov 18, 2024
  30. Patrick SteinhardtNov 18, 2024
  31. Junio C HamanoNov 18, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.