]> git.ipfire.org Git - thirdparty/xz.git/commitdiff
Add build-aux/git_commit_info.sh
authorLasse Collin <lasse.collin@tukaani.org>
Mon, 7 Apr 2025 14:38:32 +0000 (17:38 +0300)
committerLasse Collin <lasse.collin@tukaani.org>
Mon, 7 Apr 2025 14:38:32 +0000 (17:38 +0300)
It will help with adding Git commit information to xz --version
and such places when the code is built from the Git repository.

build-aux/git_commit_info.sh [new file with mode: 0644]

diff --git a/build-aux/git_commit_info.sh b/build-aux/git_commit_info.sh
new file mode 100644 (file)
index 0000000..2c09006
--- /dev/null
@@ -0,0 +1,55 @@
+#!/bin/sh
+# SPDX-License-Identifier: 0BSD
+
+###############################################################################
+#
+# Create or update git_commit_info.h if it is out of date. The file contains
+# Git repository commit information to be included in --version messages
+# and such places. "make clean" or the equivalent should remove the file.
+#
+# $1 = path to source tree (default is current directory)
+# $2 = path/to/git_commit_info.h to create or update;
+#      if not provided, print to standard output
+#
+###############################################################################
+#
+# Author: Lasse Collin
+#
+###############################################################################
+
+set -e
+
+SRCDIR=${1:-.}
+FILE=$2
+
+COMMIT=
+if test -d "$SRCDIR/.git" && type git > /dev/null 2>&1
+then
+       # Abbreviated commit ID could look prettier, but web search engines
+       # won't find anything with those.
+       COMMIT=`git -C "$SRCDIR" log -n1 --pretty='%cs %H'`
+       COMMIT=" ($COMMIT)"
+fi
+
+NEW="#define GIT_COMMIT_INFO \"$COMMIT\""
+
+# If no target file was provided, print the result to standard output.
+if test -z "$FILE"
+then
+       printf '%s\n' "$NEW"
+       exit 0
+fi
+
+# Check if the file needs to be created or updated. If the contents haven't
+# changed, preserve the file timestamp to avoid useless rebuilds.
+OLD=
+if test -f "$FILE"
+then
+       OLD=`cat "$FILE"`
+fi
+
+if test "x$NEW" != "x$OLD"
+then
+       rm -f "$FILE"
+       printf '%s\n' "$NEW" > "$FILE"
+fi