scripts/getver.sh: try to get branch/upstream automatically
[openwrt/staging/mkresin.git] / scripts / getver.sh
index a60b91ede1390a5fdceccbf9d649460ccea93801..38d526cb532cc00be0e91c8575674de733fb15d9 100755 (executable)
@@ -19,12 +19,26 @@ try_git() {
        case "$GET_REV" in
        r*)
                GET_REV="$(echo $GET_REV | tr -d 'r')"
-               BASE_REV="$(git rev-list reboot..HEAD --count)"
+               BASE_REV="$(git rev-list reboot..HEAD | wc -l)"
                REV="$(git rev-parse HEAD~$((BASE_REV - GET_REV)))"
                ;;
        *)
+               BRANCH="$(git rev-parse --abbrev-ref HEAD)"
+               ORIGIN="$(git rev-parse --symbolic-full-name ${BRANCH}@{u} 2>/dev/null)"
+               [ -n "$ORIGIN" ] || ORIGIN="$(git rev-parse --symbolic-full-name master@{u} 2>/dev/null)"
+               REV="$(git rev-list reboot..$GET_REV | wc -l | awk '{print $1}')"
+
+               if [ -n "$ORIGIN" ]; then
+                       UPSTREAM_BASE="$(git merge-base $GET_REV $ORIGIN)"
+                       UPSTREAM_REV="$(git rev-list reboot..$UPSTREAM_BASE | wc -l | awk '{print $1}')"
+               else
+                       UPSTREAM_REV=$REV
+               fi
+
+               if [ "$REV" -gt "$UPSTREAM_REV" ]; then
+                       REV="${UPSTREAM_REV}+$((REV - UPSTREAM_REV))"
+               fi
 
-               REV="$(git rev-list reboot..$GET_REV --count)"
                REV="${REV:+r$REV}"
                ;;
        esac