Andy Wingo wrote: > On Sat 07 Jul 2012 22:42, Jim Meyering <j...@meyering.net> writes: > >>> - --prefix prefix of git tags (default 'v') >>> + --prefix prefix of git tags to strip from version (default 'v') >>> + --match pattern for git tags to match >> >> Please mention that it's a "glob" pattern, >> so people don't think it's a regexp. > > Done. > >>> +function massage_git_version() { >>> + local v=$1 >> >> Using the "function" and "local" keywords would make this script >> fail for some older shells. I'd like to remain portable to those. > > Bummer. OK, fixed. These functions no longer take or print values, > they just set the $v variable. > >> Oh, and your patch added at least one trailing blank. > > Fixed. > > Thanks for the review, new patch attached. ... > Subject: [PATCH] git-version-gen: add --match argument. > > * build-aux/git-version-gen (Options): Add --match argument. > (match): > (tag_sed_script): > (tarball_version_file): Protect against ambient values of these > variables. > (git_describe, massage_git_version): New helper functions. Require a > match in the --match case, and otherwise have our previous behavior. > > Using --match can prevent a merge from e.g. a version 2 branch to a 3 > branch from causing the 3 branch to show a 2 version, if the 2 version > tagged more recently than the 3 version.
I've had to make changes. First, upon failure, the new function must return 1, not exit 1, or else, e.g., in a repo with no tags, the script will simply exit 1 rather than printing UNKNOWN and exiting 0. Here's the incremental: diff --git a/build-aux/git-version-gen b/build-aux/git-version-gen index f80a9fb..3caf84a 100755 --- a/build-aux/git-version-gen +++ b/build-aux/git-version-gen @@ -1,6 +1,6 @@ #!/bin/sh # Print a version string. -scriptversion=2012-07-10.09; # UTC +scriptversion=2012-07-11.20; # UTC # Copyright (C) 2007-2012 Free Software Foundation, Inc. # @@ -181,28 +181,29 @@ massage_git_version() { # If successful, sets 'v'. git_describe() { # Make sure this working directory has commits. - test "`git log -1 --pretty=format:x . 2>&1`" = x || exit 1 + test "`git log -1 --pretty=format:x . 2>&1`" = x || return 1 # If the user specified --match, look for a matching tag. # Otherwise, look for a tag starting with --prefix, or the first tag # we find. if test -n "$match" then - v=`git describe --abbrev=4 --match="$match" HEAD 2>/dev/null` || exit 1 + v=`git describe --abbrev=4 --match="$match" HEAD 2>/dev/null` || return 1 else v=`git describe --abbrev=4 --match="$prefix*" HEAD 2>/dev/null \ - || git describe --abbrev=4 HEAD 2>/dev/null` || exit 1 + || git describe --abbrev=4 HEAD 2>/dev/null` || return 1 fi - v=`printf '%s\n' "$v" | sed "$tag_sed_script"` || exit 1 + v=`printf '%s\n' "$v" | sed "$tag_sed_script"` || return 1 case $v in $prefix[0-9]*) massage_git_version ;; *) - exit 1 + return 1 ;; esac + return 0 } if test -n "$v" Other than that, I tweaked the log formatting and put a copy of it in the ChangeLog file: Here's the complete patch. I'll wait to hear from you before pushing this: >From 54982db5324078539f6fe2743ac7223672bfb85a Mon Sep 17 00:00:00 2001 From: Andy Wingo <wi...@pobox.com> Date: Fri, 6 Jul 2012 17:27:37 +0200 Subject: [PATCH] git-version-gen: add --match argument. * build-aux/git-version-gen (Options): Add --match argument. (match, tag_sed_script, (tarball_version_file): Protect against ambient values of these variables. (git_describe, massage_git_version): New helper functions. Require a match in the --match case, and otherwise have our previous behavior. Using --match can prevent a merge from e.g. a version 2 branch to a 3 branch from causing the 3 branch to show a 2 version, if the 2 version tagged more recently than the 3 version. --- ChangeLog | 13 +++++++++ build-aux/git-version-gen | 70 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index d94f7b1..a13a021 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2012-07-11 Andy Wingo <wi...@pobox.com> + + git-version-gen: add --match argument. + * build-aux/git-version-gen (Options): Add --match argument. + (match, tag_sed_script, (tarball_version_file): Protect against + ambient values of these variables. + (git_describe, massage_git_version): New helper functions. Require a + match in the --match case, and otherwise have our previous behavior. + + Using --match can prevent a merge from e.g. a version 2 branch to a 3 + branch from causing the 3 branch to show a 2 version, if the 2 version + tagged more recently than the 3 version. + 2012-07-10 Akim Demaille <a...@lrde.epita.fr> bootstrap: let warn be like tests/init.sh's warn_ diff --git a/build-aux/git-version-gen b/build-aux/git-version-gen index 0fa9063..3caf84a 100755 --- a/build-aux/git-version-gen +++ b/build-aux/git-version-gen @@ -1,6 +1,6 @@ #!/bin/sh # Print a version string. -scriptversion=2012-03-18.17; # UTC +scriptversion=2012-07-11.20; # UTC # Copyright (C) 2007-2012 Free Software Foundation, Inc. # @@ -85,20 +85,27 @@ Print a version string. Options: - --prefix prefix of git tags (default 'v') + --prefix prefix of git tags to strip from version (default 'v') + --match glob pattern for git tags to match - --help display this help and exit - --version output version information and exit + --help display this help and exit + --version output version information and exit -Running without arguments will suffice in most cases." +Running without arguments will suffice in most cases. If no --match +argument is given, try to match tags that begin with the --prefix, +falling back to the first tag that git-describe finds." prefix=v +match= +tag_sed_script= +tarball_version_file= while test $# -gt 0; do case $1 in --help) echo "$usage"; exit 0;; --version) echo "$version"; exit 0;; --prefix) shift; prefix="$1";; + --match) shift; match="$1";; -*) echo "$0: Unknown option '$1'." >&2 echo "$0: Try '--help' for more information." >&2 @@ -144,21 +151,7 @@ then && echo "$0: WARNING: $tarball_version_file is missing or damaged" 1>&2 fi -if test -n "$v" -then - : # use $v -# Otherwise, if there is at least one git commit involving the working -# directory, and "git describe" output looks sensible, use that to -# derive a version string. -elif test "`git log -1 --pretty=format:x . 2>&1`" = x \ - && v=`git describe --abbrev=4 --match="$prefix*" HEAD 2>/dev/null \ - || git describe --abbrev=4 HEAD 2>/dev/null` \ - && v=`printf '%s\n' "$v" | sed "$tag_sed_script"` \ - && case $v in - $prefix[0-9]*) ;; - *) (exit 1) ;; - esac -then +massage_git_version() { # Is this a new git that lists number of commits since the last # tag or the previous older version that did not? # Newer: v6.10-77-g0f8faeb @@ -182,7 +175,42 @@ then # Change the first '-' to a '.', so version-comparing tools work properly. # Remove the "g" in git describe's output string, to save a byte. - v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`; + v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'` +} + +# If successful, sets 'v'. +git_describe() { + # Make sure this working directory has commits. + test "`git log -1 --pretty=format:x . 2>&1`" = x || return 1 + + # If the user specified --match, look for a matching tag. + # Otherwise, look for a tag starting with --prefix, or the first tag + # we find. + if test -n "$match" + then + v=`git describe --abbrev=4 --match="$match" HEAD 2>/dev/null` || return 1 + else + v=`git describe --abbrev=4 --match="$prefix*" HEAD 2>/dev/null \ + || git describe --abbrev=4 HEAD 2>/dev/null` || return 1 + fi + + v=`printf '%s\n' "$v" | sed "$tag_sed_script"` || return 1 + case $v in + $prefix[0-9]*) + massage_git_version + ;; + *) + return 1 + ;; + esac + return 0 +} + +if test -n "$v" +then + : # use $v +elif git_describe +then v_from_git=1 else v=UNKNOWN -- 1.7.11.1.165.g299666c