b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 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 | REBOOT=ee53a240ac902dc83209008a2671e7fdcf55957a |
| 16 | git rev-parse --git-dir >/dev/null 2>&1 || return 1 |
| 17 | |
| 18 | [ -n "$GET_REV" ] || GET_REV="HEAD" |
| 19 | |
| 20 | case "$GET_REV" in |
| 21 | r*) |
| 22 | GET_REV="$(echo $GET_REV | tr -d 'r')" |
| 23 | BASE_REV="$(git rev-list ${REBOOT}..HEAD 2>/dev/null | wc -l | awk '{print $1}')" |
| 24 | [ $((BASE_REV - GET_REV)) -ge 0 ] && REV="$(git rev-parse HEAD~$((BASE_REV - GET_REV)))" |
| 25 | ;; |
| 26 | *) |
| 27 | BRANCH="$(git rev-parse --abbrev-ref HEAD)" |
| 28 | ORIGIN="$(git rev-parse --verify --symbolic-full-name ${BRANCH}@{u} 2>/dev/null)" |
| 29 | [ -n "$ORIGIN" ] || ORIGIN="$(git rev-parse --verify --symbolic-full-name main@{u} 2>/dev/null)" |
| 30 | REV="$(git rev-list ${REBOOT}..$GET_REV 2>/dev/null | wc -l | awk '{print $1}')" |
| 31 | |
| 32 | if [ -n "$ORIGIN" ]; then |
| 33 | UPSTREAM_BASE="$(git merge-base $GET_REV $ORIGIN)" |
| 34 | UPSTREAM_REV="$(git rev-list ${REBOOT}..$UPSTREAM_BASE 2>/dev/null | wc -l | awk '{print $1}')" |
| 35 | else |
| 36 | UPSTREAM_REV=0 |
| 37 | fi |
| 38 | |
| 39 | if [ "$REV" -gt "$UPSTREAM_REV" ]; then |
| 40 | REV="${UPSTREAM_REV}+$((REV - UPSTREAM_REV))" |
| 41 | fi |
| 42 | |
| 43 | REV="${REV:+r$REV-$(git log -n 1 --format="%h" $UPSTREAM_BASE)}" |
| 44 | |
| 45 | ;; |
| 46 | esac |
| 47 | |
| 48 | [ -n "$REV" ] |
| 49 | } |
| 50 | |
| 51 | try_hg() { |
| 52 | [ -d .hg ] || return 1 |
| 53 | REV="$(hg log -r-1 --template '{desc}' | awk '{print $2}' | sed 's/\].*//')" |
| 54 | REV="${REV:+r$REV}" |
| 55 | [ -n "$REV" ] |
| 56 | } |
| 57 | |
| 58 | try_version || try_git || try_hg || REV="unknown" |
| 59 | echo "$REV" |