From b9a62d690745eadf5e54aa47e5ff07cbb9d39214 Mon Sep 17 00:00:00 2001 From: Alain BKR Date: Sun, 30 Apr 2023 20:46:23 +0200 Subject: [PATCH 15/15] Improve gitrev handling Patch modified by Marko Lindqvist See osdn #47967 --- bootstrap/fc_gitrev_gen.h.tmpl | 33 ++++++++++++++++++++-------- bootstrap/generate_gitrev.sh | 40 +++++++++++++++++++++++++--------- common/version.c | 7 +++--- translations/Strings.txt | 6 ++--- 4 files changed, 61 insertions(+), 25 deletions(-) diff --git a/bootstrap/fc_gitrev_gen.h.tmpl b/bootstrap/fc_gitrev_gen.h.tmpl index f52d207d60..9825fc77e3 100644 --- a/bootstrap/fc_gitrev_gen.h.tmpl +++ b/bootstrap/fc_gitrev_gen.h.tmpl @@ -11,23 +11,38 @@ GNU General Public License for more details. ***********************************************************************/ -/* common/fc_gitrev_gen.h is automatically generated from - * bootstrap/fc_gitrev_gen.h.tmpl. Do not edit fc_gitrev_gen.h, edit - * fc_gitrev_gen.h.tmpl */ +/* DO NOT edit fc_gitrev_gen.h, edit fc_gitrev_gen.h.tmpl + * + * fc_gitrev_gen.h is automatically generated + * by bootstrap/generate_gitrev.sh + * from bootstrap/fc_gitrev_gen.h.tmpl + */ #ifndef FC__FC_GITREV_H #define FC__FC_GITREV_H /* One of the macros FC_GITREV_OFF, FC_GITREV_ON or FC_GITREV_MOD defined. - * If FC_GITREV_ON is defined, FC_GITREV2 contains valid git commit id - * string. - * If FC_GITREV_MOD is defined, this seems to be modified git commit. - * In that case FC_GITREV1 contains word "modified " and FC_GITREV2 - * commit id string. + * + * If FC_GITREV_ON is defined, + * FC_GITREV1 contains "origin/S3_1 9c68c0843" + * - a string "origin/current_branch" + * - git commit id corresponding to latest commit from origin. + * + * FC_GITREV2 contains "HEAD a96adce7b (+6)" + * - a string "HEAD" + * - git commit id corresponding to HEAD + * - followed by (+number) of local commit since FC_GITREV1. (+6) in our example + * - optionally followed by the "modified" word if there are local uncommited changes + * (FC_GITREV_MOD is defined). + * * If FC_GITREV_OFF is defined, you should not use FC_GITREV1 and - * FC_GITREV2 even though they contain fallback string (currently "dist") */ + * FC_GITREV2 even though they contain fallback string (currently "dist") + * + * (Read bootstrap/generate_gitrev.sh for more details) + */ #define FC_GITREV_ #define FC_GITREV1 "" #define FC_GITREV2 "" +#define FC_GITREV3 "" #endif /* FC__FC_GITREV_H */ diff --git a/bootstrap/generate_gitrev.sh b/bootstrap/generate_gitrev.sh index 64878d47b1..7b042ef367 100755 --- a/bootstrap/generate_gitrev.sh +++ b/bootstrap/generate_gitrev.sh @@ -9,7 +9,7 @@ # $2 - output file # -# Absolete paths +# Absolute paths SRCROOT="$(cd "$1" ; pwd)" INPUTDIR="$(cd "$1/bootstrap" ; pwd)" OUTPUTFILE="$2" @@ -17,6 +17,9 @@ OUTPUTFILE="$2" REVSTATE="OFF" REV1="" REV2="dist" +REV3="" +EXT1="" +EXT2="" (cd "$INPUTDIR" # Check that all commands required by this script are available @@ -24,22 +27,39 @@ REV2="dist" # (REVSTATE will be OFF) if command -v git >/dev/null && command -v tail >/dev/null && - command -v wc >/dev/null ; then - REVTMP="$(git rev-parse --short HEAD 2>/dev/null)" - if test "x$REVTMP" != "x" ; then + command -v head >/dev/null && + command -v sed >/dev/null && + command -v awk >/dev/null && + command -v grep >/dev/null ; then + + BRANCH="origin/$(cd "$SRCROOT"; git branch|grep '^*'|sed -e 's:[/ (),*]: :g' |awk '{ print $NR }' 2>/dev/null)" + ORIGIN="$(cd "$SRCROOT"; git rev-parse --short $BRANCH 2>/dev/null)" + HEAD="$(cd "$SRCROOT"; git rev-parse --short HEAD 2>/dev/null)" + + if test "$HEAD" != "" ; then # This is git repository. Check for local modifications - if (cd "$SRCROOT" ; git diff --quiet); then - REVSTATE=ON - REV2="$REVTMP" + if (cd "$SRCROOT" ; git diff $BRANCH --quiet); then + REVSTATE="ON $BRANCH $ORIGIN " + REV2="HEAD $HEAD" else REVSTATE=MOD - REV1="modified " - REV2="$REVTMP" + COUNT=$(git rev-list --count HEAD ^$ORIGIN) + if [ "$COUNT" != "0" ]; then + EXT1="(+$COUNT)" + fi + if [ "$(cd "$SRCROOT" ; git diff $HEAD --quiet; echo $?)" != "0" ] ; then + # This must match string in translations/Strings.txt + REV3=" (modified)" + fi + REV1="$BRANCH $ORIGIN " + REV2="HEAD $HEAD $EXT1 $EXT2" fi fi fi - sed -e "s,,$REV1," -e "s,,$REV2," -e "s,,$REVSTATE," fc_gitrev_gen.h.tmpl) > "${OUTPUTFILE}.tmp" + sed -e "s,,$REV1," -e "s,,$REV2," -e "s,,$REV3," \ + -e "s,,$REVSTATE," fc_gitrev_gen.h.tmpl +) > "${OUTPUTFILE}.tmp" if ! test -f "${OUTPUTFILE}" || ! cmp "${OUTPUTFILE}" "${OUTPUTFILE}.tmp" >/dev/null diff --git a/common/version.c b/common/version.c index 1e1ffbd1f2..0c509b8f65 100644 --- a/common/version.c +++ b/common/version.c @@ -76,10 +76,11 @@ const char *fc_git_revision(void) { #if defined(GITREV) && !defined(FC_GITREV_OFF) static char buf[100]; - bool translate = FC_GITREV1[0] != '\0'; + bool translate = FC_GITREV3[0] != '\0'; - fc_snprintf(buf, sizeof(buf), "%s%s", - translate ? _(FC_GITREV1) : FC_GITREV1, FC_GITREV2); + fc_snprintf(buf, sizeof(buf), "%s%s%s", + FC_GITREV1, FC_GITREV2, + translate ? _(FC_GITREV3) : FC_GITREV3); return buf; /* Either revision, or modified revision */ #else /* FC_GITREV_OFF */ diff --git a/translations/Strings.txt b/translations/Strings.txt index d9fb4ef902..f108cfa68b 100644 --- a/translations/Strings.txt +++ b/translations/Strings.txt @@ -29,9 +29,9 @@ _("?vertag:macos-S3_3") Generated version header /* TRANS: part of revision based version number string - "(modified 855bfb8e2f)" (the second 'word' is a git hash) - (Preserve the trailing space.) */ -_("modified ") + "(origin/S3_1 9c68c0843 (modified))" + (Preserve the space) */ +_(" (modified)") Typical Modpack subtypes -- 2.39.2