Merge branch 't/opus'
[paraslash.git] / GIT-VERSION-GEN
1 #!/usr/bin/env bash
2
3 (($# > 1)) && { echo >&2 "usage: $0 [<version file>]"; exit 1; }
4 GVF="$1"
5
6 DEF_VER="unnamed_version"
7 LF='
8 '
9
10 # First see if there is a version file (included in release tarballs),
11 # then try git-describe, then default.
12 if [[ -f VERSION ]]; then
13         VN=$(cat VERSION) || VN="$DEF_VER"
14 elif [[ -d .git || -f .git ]] &&
15         VN=$(git describe --abbrev=4 HEAD 2>/dev/null) &&
16         case "$VN" in
17         *$LF*) (exit 1) ;;
18         v[0-9]*)
19                 git update-index -q --refresh
20                 test -z "$(git diff-index --name-only HEAD --)" ||
21                 VN="$VN-dirty" ;;
22         esac
23 then
24         VN=$(echo "$VN" | sed -e 's/-/./g');
25 else
26         VN="$DEF_VER"
27 fi
28
29 VN=$(expr "$VN" : v*'\(.*\)')
30 echo "$VN"
31
32 [[ -z "$GVF" ]] && exit 0
33 if [[ -r "$GVF" ]]; then
34         pattern='^#define GIT_VERSION "'
35         VC=$(grep "$pattern" < "$GVF" | sed -e "s/$pattern//; s/\"$//")
36         [[ "$VN" == "$VC" ]] && exit 0
37 fi
38 echo >&2 "new git version: $VN"
39 echo "/** \\file ${GVF##*/} Auto-generated version file. Do not edit. */" >$GVF
40 echo "#define GIT_VERSION \"$VN\"" >> $GVF