version.sh: Use git tags in versions
[petitboot] / version.sh
1 #! /bin/bash
2 #
3 # version.sh: create a version string for use by configure.ac
4
5 version=
6 datefmt='%Y%m%d'
7
8 if head=$(git rev-parse --short=8 --verify HEAD 2>/dev/null); then
9
10         suffix=''
11         # Add a '-dirty' suffix for uncommitted changes.
12         if git diff-index HEAD | read dummy; then
13                 suffix=-dirty
14         fi
15
16         if tag=$(git describe --tags --exact-match 2>/dev/null); then
17                 # use a tag; remove any 'v' prefix from v<VERSION> tags
18                 tag=${tag#v}
19                 version=$(printf "%s%s" ${tag} ${suffix})
20         else
21                 # Use the git commit revision for the package version, and add
22                 # a date prefix for easy comparisons.
23                 date=$(git log --pretty=format:"%ct" -1 HEAD)
24                 version=$(printf "%($datefmt)T-g%s%s" ${date} ${head} ${suffix})
25         fi
26 else
27         # Default to current date and time.
28         version="$(date +dev-$datefmt)"
29 fi
30
31 echo $version