version.sh: Include leading 'v' from git describe
[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 export GIT_DIR=$(dirname $0)/.git/
9
10 if head=$(git rev-parse --short=8 --verify HEAD 2>/dev/null); then
11
12         suffix=''
13         # Add a '-dirty' suffix for uncommitted changes.
14         if git diff-index HEAD | read dummy; then
15                 suffix=-dirty
16         fi
17
18         tag=$(git describe --tags 2>/dev/null)
19         version=$(printf "%s%s" ${tag} ${suffix})
20 else
21         # Check if a specific version is set, eg: by buildroot
22         if [ ! -z "$PETITBOOT_VERSION" ];
23         then
24                 # Full git hash
25                 len=$(echo -n "${PETITBOOT_VERSION}" | wc -c)
26                 if [[ ${len} == 40 ]]; then
27                         version=`echo -n ${PETITBOOT_VERSION} | \
28                                 sed "s/^\([0-9a-f]\{7\}\).*/\1/;"`
29                 else
30                         version="$PETITBOOT_VERSION"
31                 fi
32         else
33                 # Default to current date and time.
34                 version="$(date +dev.$datefmt)"
35         fi
36 fi
37
38 echo $version