getver.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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=375587f4b29039d86880030c6ab9a135e0342567
  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 | wc -l | awk '{print $1}')"
  19. 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 | 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 | 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 --format="%h" -1)}"
  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"