Skip to content

Instantly share code, notes, and snippets.

@liweitianux
Last active July 29, 2024 13:37
Show Gist options
  • 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.
#
# License: Public Domain
#
# URL: https://gist.github.com/liweitianux/9fce0fc404c41b8f28512e87fbca1562
#
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:

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

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

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

% ./git-getversion.sh count
27

% ./git-getversion.sh commit
8babfb7

For a repo without any tag:

% ./git-getversion.sh full
785-3191600

% ./git-getversion.sh full v0.1   
785-3191600

% ./git-getversion.sh tag v0.1
v0.1

% ./git-getversion.sh count v0.1
785

% ./git-getversion.sh commit v0.1
3191600

@liweitianux
Copy link
Author

License: Public Domain

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