Skip to content

Instantly share code, notes, and snippets.

@liweitianux
Created April 27, 2021 04:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liweitianux/9fce0fc404c41b8f28512e87fbca1562 to your computer and use it in GitHub Desktop.
Save liweitianux/9fce0fc404c41b8f28512e87fbca1562 to your computer and use it in GitHub Desktop.
Generate version information based on Git tags/commits
#!/bin/sh
#
# Generate version information based on Git tags/commits.
#
dirty_mark='+'
usage() {
cat >/dev/stderr <<EOF
usage: ${0##*/} <full|tag|count|commit> [--dirty] [default]
full : generate full version with tag, count and commit info
tag : get the last tag
count : get the commit count since last tag
commit : get the current commit
--dirty : append a mark (${dirty_mark}) if repo is dirty
default : default tag value if no tag exists
EOF
exit 1
}
get_last_tag() {
git describe --tags --abbrev=0 2>/dev/null
}
get_count() {
local tag=$(get_last_tag)
if [ -n "${tag}" ]; then
git rev-list ${tag}..HEAD --count
else
git rev-list HEAD --count
fi
}
get_commit() {
git rev-list --max-count=1 --abbrev-commit HEAD
}
get_full() {
local tag=$(get_last_tag)
if [ -n "${tag}" ]; then
git describe --tags --always
else
echo "$(get_count)-$(get_commit)"
fi
}
# Credit: https://github.com/sindresorhus/pure/issues/115
is_dirty() {
! git diff --no-ext-diff --quiet
}
case $1 in
'' | help | -h | --help)
usage
;;
esac
action="$1"
shift
if [ "$1" = "--dirty" ]; then
shift
is_dirty && dirty=${dirty_mark} || dirty=''
fi
default="$1"
case ${action} in
full)
echo "$(get_full)${dirty}"
;;
tag)
tag=$(get_last_tag)
[ -n "${tag}" ] || tag=${default}
echo "${tag}${dirty}"
;;
count)
echo "$(get_count)${dirty}"
;;
commit)
echo "$(get_commit)${dirty}"
;;
*)
usage
;;
esac
@liweitianux
Copy link
Author

liweitianux commented Apr 27, 2021

Example output:

% ./getversion.sh full
v1.8.4-7-27-g8babfb7

% ./getversion.sh full --dirty
v1.8.4-7-27-g8babfb7+

% ./getversion.sh tag         
v1.8.4-7

% ./getversion.sh count
27

% ./getversion.sh commit
8babfb7

For a repo without any tag:

% getversion.sh full
785-3191600

% getversion.sh full v0.1   
785-3191600

% getversion.sh tag v0.1
v0.1

% getversion.sh count v0.1
785

% getversion.sh commit v0.1
3191600

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment