From: Toon Claes <toon@iotcl.com>
To: Patrick Steinhardt <ps@pks.im>, git@vger.kernel.org
Cc: Evan Martin <evan.martin@gmail.com>,
Eli Schwartz <eschwartz@gentoo.org>,
Junio C Hamano <gitster@pobox.com>,
M Hickford <mirth.hickford@gmail.com>
Subject: Re: [PATCH v2 02/11] GIT-VERSION-GEN: allow running without input and output files
Date: Tue, 21 Jan 2025 14:16:11 +0100 [thread overview]
Message-ID: <87jzaoi9yc.fsf@iotcl.com> (raw)
In-Reply-To: <20250114-b4-pks-meson-additions-v2-2-8d7ec676cfd9@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
> 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 <ps@pks.im>
> ---
> GIT-VERSION-GEN | 44 +++++++++++++++++++++++++++++---------------
> 1 file changed, 29 insertions(+), 15 deletions(-)
>
> diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
> index b8b683b9337e5771e14a2cfb84022a11489bb432..9d201a98fd2766911544225c62159cbfe8dff5fe 100755
> --- a/GIT-VERSION-GEN
> +++ b/GIT-VERSION-GEN
> @@ -5,21 +5,29 @@ DEF_VER=v2.48.0
> LF='
> '
>
> -if test "$#" -ne 3
> +if test "$#" -lt 2 || test "$#" -gt 3
> then
> - echo >&2 "USAGE: $0 <SOURCE_DIR> <INPUT> <OUTPUT>"
> + echo >&2 "USAGE: $0 <SOURCE_DIR> (--format=<STRING>|<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
> +case "$2" in
> +--format=*)
> + INPUT="${2#--format=}"
> + ;;
> +*)
> + if ! test -f "$2"
> + then
> + echo >&2 "Input is not a file: $2"
> + exit 1
> + fi
> + INPUT=$(cat "$2")
> + ;;
> +esac
> +
> +OUTPUT="$3"
>
> # 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
> @@ -74,19 +82,25 @@ read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION GIT_PATCH_LEVEL trail
> $(echo "$GIT_VERSION" 0 0 0 0 | tr '.a-zA-Z-' ' ')
> EOF
>
> -sed -e "s|@GIT_VERSION@|$GIT_VERSION|" \
> +REPLACED=$(printf "%s" "$INPUT" | 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_BUILT_FROM_COMMIT@|$GIT_BUILT_FROM_COMMIT|" \
> -e "s|@GIT_USER_AGENT@|$GIT_USER_AGENT|" \
> - -e "s|@GIT_DATE@|$GIT_DATE|" \
> - "$INPUT" >"$OUTPUT".$$+
> + -e "s|@GIT_DATE@|$GIT_DATE|"
> +)
>
> -if ! test -f "$OUTPUT" || ! cmp "$OUTPUT".$$+ "$OUTPUT" >/dev/null
> +if test -z "$OUTPUT"
> then
> - mv "$OUTPUT".$$+ "$OUTPUT"
> + printf "%s\n" "$REPLACED"
> else
> - rm "$OUTPUT".$$+
> + printf "%s\n" "$REPLACED" >"$OUTPUT".$$+
> + if ! test -f "$OUTPUT" || ! cmp "$OUTPUT".$$+ "$OUTPUT" >/dev/null
> + then
> + mv "$OUTPUT".$$+ "$OUTPUT"
> + else
> + rm "$OUTPUT".$$+
> + fi
> fi
>
> --
> 2.48.0.257.gd3603152ad.dirty
I was considering to suggest to have GIT-VERSION-GEN accept --stdin for
$INPUT and have it read from stdin instead of passing the format as an
argument, but looking at the following commits I agree with your
approach.
--
Toon
next prev parent reply other threads:[~2025-01-21 13:16 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-13 8:33 [PATCH 0/9] meson: a couple of additions Patrick Steinhardt
2025-01-13 8:33 ` [PATCH 1/9] GIT-VERSION-GEN: simplify computing the dirty marker Patrick Steinhardt
2025-01-13 8:33 ` [PATCH 2/9] GIT-VERSION-GEN: move default version into a separate file Patrick Steinhardt
2025-01-13 17:42 ` Junio C Hamano
2025-01-13 17:51 ` Eli Schwartz
2025-01-14 9:13 ` Patrick Steinhardt
2025-01-13 8:33 ` [PATCH 3/9] meson: fix dependencies for generated headers Patrick Steinhardt
2025-01-13 8:33 ` [PATCH 4/9] meson: wire up development environments Patrick Steinhardt
2025-01-13 8:33 ` [PATCH 5/9] meson: wire up generation of distribution archive Patrick Steinhardt
2025-01-13 17:55 ` Junio C Hamano
2025-01-14 9:14 ` Patrick Steinhardt
2025-01-13 8:33 ` [PATCH 6/9] meson: wire up fuzzers Patrick Steinhardt
2025-01-13 17:48 ` Junio C Hamano
2025-01-14 10:31 ` Patrick Steinhardt
2025-01-13 8:33 ` [PATCH 7/9] meson: make the CSPRNG backend configurable Patrick Steinhardt
2025-01-13 9:25 ` Patrick Steinhardt
2025-01-13 17:59 ` Junio C Hamano
2025-01-14 9:13 ` Patrick Steinhardt
2025-01-14 19:13 ` Junio C Hamano
2025-01-13 8:33 ` [PATCH 8/9] meson: fix compilation with Visual Studio Patrick Steinhardt
2025-01-13 21:12 ` M Hickford
2025-01-13 8:33 ` [PATCH 9/9] ci: wire up Visual Studio build with Meson Patrick Steinhardt
2025-01-14 11:56 ` [PATCH v2 00/11] meson: a couple of additions Patrick Steinhardt
2025-01-14 11:56 ` [PATCH v2 01/11] GIT-VERSION-GEN: simplify computing the dirty marker Patrick Steinhardt
2025-01-14 11:56 ` [PATCH v2 02/11] GIT-VERSION-GEN: allow running without input and output files Patrick Steinhardt
2025-01-21 13:16 ` Toon Claes [this message]
2025-01-14 11:56 ` [PATCH v2 03/11] meson: populate project version via GIT-VERSION-GEN Patrick Steinhardt
2025-01-21 13:13 ` Toon Claes
2025-01-14 11:56 ` [PATCH v2 04/11] meson: fix dependencies for generated headers Patrick Steinhardt
2025-01-14 11:56 ` [PATCH v2 05/11] meson: wire up development environments Patrick Steinhardt
2025-01-14 11:56 ` [PATCH v2 06/11] meson: wire up generation of distribution archive Patrick Steinhardt
2025-01-21 12:37 ` Toon Claes
2025-01-22 12:05 ` Patrick Steinhardt
2025-01-14 11:56 ` [PATCH v2 07/11] meson: wire up fuzzers Patrick Steinhardt
2025-01-14 11:56 ` [PATCH v2 08/11] meson: make the CSPRNG backend configurable Patrick Steinhardt
2025-01-14 11:56 ` [PATCH v2 09/11] meson: fix compilation with Visual Studio Patrick Steinhardt
2025-01-14 11:56 ` [PATCH v2 10/11] ci: raise error when Meson generates warnings Patrick Steinhardt
2025-01-21 12:59 ` Toon Claes
2025-01-14 11:56 ` [PATCH v2 11/11] ci: wire up Visual Studio build with Meson Patrick Steinhardt
2025-01-14 17:46 ` [PATCH v2 00/11] meson: a couple of additions Junio C Hamano
2025-01-22 12:05 ` [PATCH v3 " Patrick Steinhardt
2025-01-22 12:05 ` [PATCH v3 01/11] GIT-VERSION-GEN: simplify computing the dirty marker Patrick Steinhardt
2025-01-22 12:05 ` [PATCH v3 02/11] GIT-VERSION-GEN: allow running without input and output files Patrick Steinhardt
2025-01-22 12:05 ` [PATCH v3 03/11] meson: populate project version via GIT-VERSION-GEN Patrick Steinhardt
2025-01-22 12:05 ` [PATCH v3 04/11] meson: fix dependencies for generated headers Patrick Steinhardt
2025-01-22 12:05 ` [PATCH v3 05/11] meson: wire up development environments Patrick Steinhardt
2025-01-22 12:05 ` [PATCH v3 06/11] meson: wire up generation of distribution archive Patrick Steinhardt
2025-01-22 12:05 ` [PATCH v3 07/11] meson: wire up fuzzers Patrick Steinhardt
2025-01-22 12:05 ` [PATCH v3 08/11] meson: make the CSPRNG backend configurable Patrick Steinhardt
2025-01-22 12:05 ` [PATCH v3 09/11] meson: fix compilation with Visual Studio Patrick Steinhardt
2025-01-22 12:05 ` [PATCH v3 10/11] ci: raise error when Meson generates warnings Patrick Steinhardt
2025-01-22 12:05 ` [PATCH v3 11/11] ci: wire up Visual Studio build with Meson Patrick Steinhardt
2025-01-22 21:42 ` [PATCH v3 00/11] meson: a couple of additions Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87jzaoi9yc.fsf@iotcl.com \
--to=toon@iotcl.com \
--cc=eschwartz@gentoo.org \
--cc=evan.martin@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=mirth.hickford@gmail.com \
--cc=ps@pks.im \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).