Skip to content

Commit f6a2efd

Browse files
pks-tgitster
authored andcommitted
GIT-VERSION-GEN: allow running without input and output files
The GIT-VERSION-GEN script requires an input file containing formatting directives to be replaced as well as an output file that will get overwritten in case the file contents have changed. When computing the project version for Meson we don't want to have either though: - We only want to compute the version without anything else, but don't have an input file that would match that exact format. While we could of course introduce a new file just for that usecase, it feels suboptimal to add another file every time we want to have a slightly different format for versioned data. - The computed version needs to be read from stdout so that Meson can wire it up for the project. Extend the script to handle both usecases by recognizing `--format=` as alternative to providing an input path and by writing to stdout in case no output file was given. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e40622a commit f6a2efd

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

GIT-VERSION-GEN

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,29 @@ DEF_VER=v2.48.0
55
LF='
66
'
77

8-
if test "$#" -ne 3
8+
if test "$#" -lt 2 || test "$#" -gt 3
99
then
10-
echo >&2 "USAGE: $0 <SOURCE_DIR> <INPUT> <OUTPUT>"
10+
echo >&2 "USAGE: $0 <SOURCE_DIR> (--format=<STRING>|<INPUT>) [<OUTPUT>]"
1111
exit 1
1212
fi
1313

1414
SOURCE_DIR="$1"
15-
INPUT="$2"
16-
OUTPUT="$3"
1715

18-
if ! test -f "$INPUT"
19-
then
20-
echo >&2 "Input is not a file: $INPUT"
21-
exit 1
22-
fi
16+
case "$2" in
17+
--format=*)
18+
INPUT="${2#--format=}"
19+
;;
20+
*)
21+
if ! test -f "$2"
22+
then
23+
echo >&2 "Input is not a file: $2"
24+
exit 1
25+
fi
26+
INPUT=$(cat "$2")
27+
;;
28+
esac
29+
30+
OUTPUT="$3"
2331

2432
# Protect us from reading Git version information outside of the Git directory
2533
# in case it is not a repository itself, but embedded in an unrelated
@@ -74,19 +82,25 @@ read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION GIT_PATCH_LEVEL trail
7482
$(echo "$GIT_VERSION" 0 0 0 0 | tr '.a-zA-Z-' ' ')
7583
EOF
7684

77-
sed -e "s|@GIT_VERSION@|$GIT_VERSION|" \
85+
REPLACED=$(printf "%s" "$INPUT" | sed -e "s|@GIT_VERSION@|$GIT_VERSION|" \
7886
-e "s|@GIT_MAJOR_VERSION@|$GIT_MAJOR_VERSION|" \
7987
-e "s|@GIT_MINOR_VERSION@|$GIT_MINOR_VERSION|" \
8088
-e "s|@GIT_MICRO_VERSION@|$GIT_MICRO_VERSION|" \
8189
-e "s|@GIT_PATCH_LEVEL@|$GIT_PATCH_LEVEL|" \
8290
-e "s|@GIT_BUILT_FROM_COMMIT@|$GIT_BUILT_FROM_COMMIT|" \
8391
-e "s|@GIT_USER_AGENT@|$GIT_USER_AGENT|" \
84-
-e "s|@GIT_DATE@|$GIT_DATE|" \
85-
"$INPUT" >"$OUTPUT".$$+
92+
-e "s|@GIT_DATE@|$GIT_DATE|"
93+
)
8694

87-
if ! test -f "$OUTPUT" || ! cmp "$OUTPUT".$$+ "$OUTPUT" >/dev/null
95+
if test -z "$OUTPUT"
8896
then
89-
mv "$OUTPUT".$$+ "$OUTPUT"
97+
printf "%s\n" "$REPLACED"
9098
else
91-
rm "$OUTPUT".$$+
99+
printf "%s\n" "$REPLACED" >"$OUTPUT".$$+
100+
if ! test -f "$OUTPUT" || ! cmp "$OUTPUT".$$+ "$OUTPUT" >/dev/null
101+
then
102+
mv "$OUTPUT".$$+ "$OUTPUT"
103+
else
104+
rm "$OUTPUT".$$+
105+
fi
92106
fi

0 commit comments

Comments
 (0)