getver.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env bash
  2. export LANG=C
  3. export LC_ALL=C
  4. [ -n "$TOPDIR" ] && cd $TOPDIR
  5. GET_REV=$1
  6. try_version() {
  7. [ -f version ] || return 1
  8. REV="$(cat version)"
  9. [ -n "$REV" ]
  10. }
  11. try_git() {
  12. REBOOT=ee53a240ac902dc83209008a2671e7fdcf55957a
  13. git rev-parse --git-dir >/dev/null 2>&1 || return 1
  14. [ -n "$GET_REV" ] || GET_REV="HEAD"
  15. case "$GET_REV" in
  16. r*)
  17. GET_REV="$(echo $GET_REV | tr -d 'r')"
  18. BASE_REV="$(git rev-list ${REBOOT}..HEAD 2>/dev/null | wc -l | awk '{print $1}')"
  19. [ $((BASE_REV - GET_REV)) -ge 0 ] && REV="$(git rev-parse HEAD~$((BASE_REV - GET_REV)))"
  20. ;;
  21. *)
  22. BRANCH="$(git rev-parse --abbrev-ref HEAD)"
  23. ORIGIN="$(git rev-parse --verify --symbolic-full-name ${BRANCH}@{u} 2>/dev/null)"
  24. [ -n "$ORIGIN" ] || ORIGIN="$(git rev-parse --verify --symbolic-full-name master@{u} 2>/dev/null)"
  25. REV="$(git rev-list ${REBOOT}..$GET_REV 2>/dev/null | wc -l | awk '{print $1}')"
  26. if [ -n "$ORIGIN" ]; then
  27. UPSTREAM_BASE="$(git merge-base $GET_REV $ORIGIN)"
  28. UPSTREAM_REV="$(git rev-list ${REBOOT}..$UPSTREAM_BASE 2>/dev/null | wc -l | awk '{print $1}')"
  29. else
  30. UPSTREAM_REV=0
  31. fi
  32. if [ "$REV" -gt "$UPSTREAM_REV" ]; then
  33. REV="${UPSTREAM_REV}+$((REV - UPSTREAM_REV))"
  34. fi
  35. REV="${REV:+r$REV-$(git log -n 1 --format="%h" $UPSTREAM_BASE)}"
  36. ;;
  37. esac
  38. [ -n "$REV" ]
  39. }
  40. try_hg() {
  41. [ -d .hg ] || return 1
  42. REV="$(hg log -r-1 --template '{desc}' | awk '{print $2}' | sed 's/\].*//')"
  43. REV="${REV:+r$REV}"
  44. [ -n "$REV" ]
  45. }
  46. try_version || try_git || try_hg || REV="unknown"
  47. echo "$REV"