Improve version_string.sh: add <refspec> argument

This commit is contained in:
Andrew Bettison 2014-02-20 17:48:11 +10:30
parent fd1b36a9da
commit 2854821678

View File

@ -18,27 +18,31 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
usage() { usage() {
echo "Usage: ${0##*/} [options]"' echo "Usage: $ME [options] [--] [<refspec>]"'
Produces a version string for the HEAD of the Git repository in the current Produces a version string for the commit <refspec> (current working copy by
working directory. The version string has the following form: default) of the given Git repository (current working directory by default).
The version string has the following form:
D.D[extra] [ -N-gXXXXXXX ] [ +USER-YYYYMMDDHHMMSS ] D.D[extra] [ -N-gXXXXXXX ] [ +USER-YYYYMMDDHHMMSS ]
Where: Where:
D.D[extra] is the most recent version tag prior to HEAD, eg, D.D[extra] is the most recent version tag prior to HEAD, eg,
0.91.RC2; if there is no tag, then a default tag 0.91.RC2; if there is no tag, then a default tag of
of "START" is used to represent the first commit in "START" is used to represent the first commit in the
the repository repository
-N-gXXXXXXX is appended if there are any commits since the -N-gXXXXXXX is appended if there are any commits since the version
version tag; N is the number of commits, XXXXXXX is tag; N is the number of commits, XXXXXXX is the
the abbreviated Git commit Id abbreviated Git commit Id
+USER-YYYYMMDDHHMMSS is appended if there are any local modifications; +USER-YYYYMMDDHHMMSS is appended if <refspec> is omitted or empty and the
USER is the email address of the user who owns the current working copy has local modifications; USER is
repository, YYYYMMDDHHMMSS is the current local time the email address of the user who owns the repository,
YYYYMMDDHHMMSS is the current local time
Options: Options:
--help Show this message --help Show this message
--repository=PATH Use repository at PATH instead of the current working
directory
--ignore-untracked Do not count any untracked local changes to determine --ignore-untracked Do not count any untracked local changes to determine
whether the version is locally modified whether the version is locally modified
--unmodified Fail with an error if there are any local modifications --unmodified Fail with an error if there are any local modifications
@ -48,14 +52,14 @@ Options:
"START" tag "START" tag
--default-tag=TAG Use "TAG" instead of "START" for the default tag if no --default-tag=TAG Use "TAG" instead of "START" for the default tag if no
version tag is found version tag is found
--repository=PATH Produce a version string for the repository in the
directory at PATH instead of the current working
directory
' '
} }
ME="${0##*/}"
set -e set -e
refspec=
allow_modified=true allow_modified=true
untracked_files=normal untracked_files=normal
ignore_submodules=none ignore_submodules=none
@ -66,19 +70,32 @@ version_tag_glob='[0-9].[0-9]*'
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case "$1" in case "$1" in
--help) usage; exit 0;; --help) usage; exit 0;;
--ignore-untracked) untracked_files=no; ignore_submodules=untracked; shift;; --repository=*) repo_path="${1#*=}";;
--unmodified) allow_modified=false; shift;; --ignore-untracked) untracked_files=no; ignore_submodules=untracked;;
--no-default-tag) default_tag=; shift;; --unmodified) allow_modified=false;;
--default-tag=*) default_tag="${1#*=}"; shift;; --no-default-tag) default_tag=;;
--repository=*) repo_path="${1#*=}"; shift;; --default-tag=*) default_tag="${1#*=}";;
*) --) shift; break;;
echo "$0: unrecognised option: $1" >&2 -*)
echo "Try \`${0##*/} --help' for more information." >&2 echo "$ME: unrecognised option: $1" >&2
echo "Try \`$ME --help' for more information." >&2
exit 1 exit 1
;; ;;
*) break;;
esac esac
shift
done done
case $# in
0);;
1) refspec="$1";;
*)
echo "$ME: too many arguments" >&2
echo "Try \`$ME --help' for more information." >&2
exit 1
;;
esac
cd "$repo_path" >/dev/null cd "$repo_path" >/dev/null
if [ ! -d .git ]; then if [ ! -d .git ]; then
@ -100,26 +117,28 @@ get_author_label() {
esac esac
} }
# The --dirty option to "git describe" always counts untracked changes as dirt.
# In order to implement the --ignore-untracked and --unmodified options, use the
# "git status" command to detect local modifications.
dirty= dirty=
if [ $(git status --porcelain --untracked-files=$untracked_files --ignore-submodules=$ignore_submodules 2>/dev/null | wc -l) -ne 0 ]; then if [ -z "$refspec" ]; then
get_author_label # The --dirty option to "git describe" always counts untracked changes as dirt.
dirty="+$author_label-$(date '+%Y%m%d%H%M%S')" # In order to implement the --ignore-untracked and --unmodified options, use the
# "git status" command to detect local modifications.
if [ $(git status --porcelain --untracked-files=$untracked_files --ignore-submodules=$ignore_submodules 2>/dev/null | wc -l) -ne 0 ]; then
get_author_label
dirty="+$author_label-$(date '+%Y%m%d%H%M%S')"
fi
fi fi
if [ -n "$dirty" ] && ! $allow_modified; then if [ -n "$dirty" ] && ! $allow_modified; then
echo "$0: cannot form version string for repository: $(pwd -P)" >&2 echo "$ME: cannot form version string for repository: $(pwd -P)" >&2
echo "$0: repository has local modifications" >&2 echo "$ME: repository has local modifications" >&2
exit 3 exit 3
fi fi
# Use the "git describe" command to form the version string and append $dirty. # Use the "git describe" command to form the version string and append $dirty.
# This ugly construction is required for use on machines with bash version < 4. # This ugly construction is required for use on machines with bash version < 4.
error="$(git describe --match="$version_tag_glob" 2>&1 1>/dev/null)" || true error="$(git describe --match="$version_tag_glob $refspec" 2>&1 1>/dev/null)" || true
if [ -z "$error" ]; then if [ -z "$error" ]; then
echo "$(git describe --match="$version_tag_glob")$dirty" echo "$(git describe --match="$version_tag_glob" $refspec)$dirty"
exit 0 exit 0
fi fi
@ -131,14 +150,14 @@ case "$error" in
*[Nn]'o tags can describe'* | \ *[Nn]'o tags can describe'* | \
*[Cc]'annot describe'* ) *[Cc]'annot describe'* )
if [ -n "$default_tag" ]; then if [ -n "$default_tag" ]; then
commit=$(git rev-list --abbrev-commit --max-count 1 HEAD) commit=$(git rev-list --abbrev-commit --max-count 1 ${refspec:-HEAD})
count=$(( $(git rev-list HEAD | wc -l) - 1 )) count=$(( $(git rev-list ${refspec:-HEAD} | wc -l) - 1 ))
echo "$default_tag-$count-g$commit$dirty" echo "$default_tag-$count-g$commit$dirty"
exit 0 exit 0
fi fi
;; ;;
esac esac
echo "$0: cannot form version string for repository: $(pwd -P)" >&2 echo "$ME: cannot form version string for repository: $(pwd -P)" >&2
echo "$0: git returned: ${error#fatal: }" >&2 echo "$ME: git returned: ${error#fatal: }" >&2
exit 2 exit 2