scripts/getver.sh: fix revision number on BSD/MacOS
[openwrt/staging/blogic.git] / scripts / getver.sh
index d0916713ad92a7a6f915fa5a281f3155bbfd5456..5d8788f20200906fa8c0326d2e7e3242b3b1c691 100755 (executable)
@@ -3,6 +3,8 @@ export LANG=C
 export LC_ALL=C
 [ -n "$TOPDIR" ] && cd $TOPDIR
 
+GET_REV=$1
+
 try_version() {
        [ -f version ] || return 1
        REV="$(cat version)"
@@ -11,8 +13,27 @@ try_version() {
 
 try_git() {
        git rev-parse --git-dir >/dev/null 2>&1 || return 1
-       REV="$(git describe --match reboot | sed "s/reboot-\([0-9]*\)-.*/\1/g")"
-       REV="${REV:+r$REV}"
+
+       [ -n "$GET_REV" ] || GET_REV="HEAD"
+
+       case "$GET_REV" in
+       r*)
+               GET_REV="$(echo $GET_REV | tr -d 'r')"
+               BASE_REV="$(git rev-list reboot..HEAD | wc -l)"
+               REV="$(git rev-parse HEAD~$((BASE_REV - GET_REV)))"
+               ;;
+       *)
+               UPSTREAM_BASE="$(git merge-base $GET_REV origin/master)"
+               UPSTREAM_REV="$(git rev-list reboot..$UPSTREAM_BASE | wc -l | awk '{print $1}')"
+               REV="$(git rev-list reboot..$GET_REV | wc -l | awk '{print $1}')"
+               if [ -n "$REV" -a -n "$UPSTREAM_REV" -a "$REV" -gt "$UPSTREAM_REV" ]; then
+                       REV="r${UPSTREAM_REV}+$((REV - UPSTREAM_REV))"
+               else
+                       REV="${REV:+r$REV}"
+               fi
+               ;;
+       esac
+
        [ -n "$REV" ]
 }