scripts/getver.sh: allow conversion between git hash and revision
[openwrt/openwrt.git] / scripts / getver.sh
1 #!/usr/bin/env bash
2 export LANG=C
3 export LC_ALL=C
4 [ -n "$TOPDIR" ] && cd $TOPDIR
5
6 GET_REV=$1
7
8 try_version() {
9 [ -f version ] || return 1
10 REV="$(cat version)"
11 [ -n "$REV" ]
12 }
13
14 try_git() {
15 git rev-parse --git-dir >/dev/null 2>&1 || return 1
16
17 [ -n "$GET_REV" ] || GET_REV="HEAD"
18
19 case "$GET_REV" in
20 r*)
21 GET_REV="$(echo $GET_REV | tr -d 'r')"
22 BASE_REV="$(git rev-list reboot..HEAD --count)"
23 REV="$(git rev-parse HEAD~$((BASE_REV - GET_REV)))"
24 ;;
25 *)
26
27 REV="$(git rev-list reboot..$GET_REV --count)"
28 REV="${REV:+r$REV}"
29 ;;
30 esac
31
32 [ -n "$REV" ]
33 }
34
35 try_hg() {
36 [ -d .hg ] || return 1
37 REV="$(hg log -r-1 --template '{desc}' | awk '{print $2}' | sed 's/\].*//')"
38 REV="${REV:+r$REV}"
39 [ -n "$REV" ]
40 }
41
42 try_version || try_git || try_hg || REV="unknown"
43 echo "$REV"