Git fork

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>

authored by

Patrick Steinhardt and committed by
Junio C Hamano
4838deab dbe46c0f

+59 -34
+1
GIT-VERSION-FILE.in
··· 1 + GIT_VERSION=@GIT_VERSION@
+46 -15
GIT-VERSION-GEN
··· 1 1 #!/bin/sh 2 2 3 - GVF=GIT-VERSION-FILE 4 3 DEF_VER=v2.47.GIT 5 4 6 5 LF=' 7 6 ' 8 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 + 9 30 # First see if there is a version file (included in release tarballs), 10 31 # then try git-describe, then default. 11 - if test -f version 32 + if test -f "$SOURCE_DIR"/version 12 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) && 16 41 case "$VN" in 17 42 *$LF*) (exit 1) ;; 18 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 --)" || 21 46 VN="$VN-dirty" ;; 22 47 esac 23 48 then ··· 26 51 VN="$DEF_VER" 27 52 fi 28 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 30 59 31 - if test -r $GVF 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 + 66 + if ! test -f "$OUTPUT" || ! cmp "$OUTPUT"+ "$OUTPUT" >/dev/null 32 67 then 33 - VC=$(sed -e 's/^GIT_VERSION = //' <$GVF) 68 + mv "$OUTPUT"+ "$OUTPUT" 34 69 else 35 - VC=unset 70 + rm "$OUTPUT"+ 36 71 fi 37 - test "$VN" = "$VC" || { 38 - echo >&2 "GIT_VERSION = $VN" 39 - echo "GIT_VERSION = $VN" >$GVF 40 - }
+4 -1
Makefile
··· 592 592 # Disable -pedantic compilation. 593 593 594 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 596 599 -include GIT-VERSION-FILE 597 600 598 601 # Set our default configuration.
+1 -1
ci/test-documentation.sh
··· 6 6 . ${0%/*}/lib.sh 7 7 8 8 filter_log () { 9 - sed -e '/^GIT_VERSION = /d' \ 9 + sed -e '/^GIT_VERSION=/d' \ 10 10 -e "/constant Gem::ConfigMap is deprecated/d" \ 11 11 -e '/^ \* new asciidoc flags$/d' \ 12 12 -e '/stripped namespace before processing/d' \
+6 -17
contrib/buildsystems/CMakeLists.txt
··· 83 83 "On Windows, you can get it as part of 'Git for Windows' install at https://gitforwindows.org/") 84 84 endif() 85 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) 103 92 104 93 project(git 105 94 VERSION ${git_version}
+1
contrib/buildsystems/git-version.in
··· 1 + @GIT_MAJOR_VERSION@.@GIT_MINOR_VERSION@.@GIT_MICRO_VERSION@