setlocalversion 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/sh
  2. #
  3. # This scripts adds local version information from the version
  4. # control systems git, mercurial (hg) and subversion (svn).
  5. #
  6. # If something goes wrong, send a mail the kernel build mailinglist
  7. # (see MAINTAINERS) and CC Nico Schottelius
  8. # <nico-linuxsetlocalversion -at- schottelius.org>.
  9. #
  10. #
  11. usage() {
  12. echo "Usage: $0 [srctree]" >&2
  13. exit 1
  14. }
  15. cd "${1:-.}" || usage
  16. # Check for git and a git repo.
  17. if head=`git rev-parse --verify --short HEAD 2>/dev/null`; then
  18. atag="`git describe 2>/dev/null`"
  19. # Show -g<commit> if we have no tag, or just the tag
  20. # otherwise.
  21. if [ -z "${atag}" ] ; then
  22. printf "%s%s" -g ${head}
  23. else
  24. printf ${atag}
  25. fi
  26. # Is this git on svn?
  27. if git config --get svn-remote.svn.url >/dev/null; then
  28. printf -- '-svn%s' "`git svn find-rev $head`"
  29. fi
  30. # Update index only on r/w media
  31. [ -w . ] && git update-index --refresh --unmerged > /dev/null
  32. # Check for uncommitted changes
  33. if git diff-index --name-only HEAD | grep -v "^scripts/package" \
  34. | read dummy; then
  35. printf '%s' -dirty
  36. fi
  37. # All done with git
  38. exit
  39. fi
  40. # Check for mercurial and a mercurial repo.
  41. # In the git case, 'git describe' will show the latest tag, and unless we are
  42. # exactly on that tag, the number of commits since then, and last commit id.
  43. # Mimic something similar in the Mercurial case.
  44. if hgid=`HGRCPATH= hg id --id --tags 2>/dev/null`; then
  45. tag=`printf '%s' "$hgid" | cut -d' ' -f2 --only-delimited`
  46. # Do we have an untagged version?
  47. if [ -z "$tag" -o "$tag" = tip ]; then
  48. # current revision is not tagged, determine latest tag
  49. latesttag=`HGRCPATH= hg log -r. -T '{latesttag}' 2>/dev/null`
  50. # In case there is more than one tag on the latest tagged commit,
  51. # 'latesttag' will separate them by colon (:). We'll retain this.
  52. # In case there is no tag at all, 'null' will be returned.
  53. if [ "$latesttag" = "null" ]; then
  54. latesttag=''
  55. fi
  56. # add the commit id
  57. id=`printf '%s' "$hgid" | sed 's/[+ ].*//'`
  58. printf '%s%s%s' "${latesttag}" -hg "$id"
  59. else
  60. # current revision is tagged, just print the tag
  61. printf ${tag}
  62. fi
  63. # Are there uncommitted changes?
  64. # These are represented by + after the changeset id.
  65. case "$hgid" in
  66. *+|*+\ *) printf '%s' -dirty ;;
  67. esac
  68. # All done with mercurial
  69. exit
  70. fi
  71. # Check for svn and a svn repo.
  72. if rev=`LC_ALL=C svn info 2>/dev/null | grep '^Last Changed Rev'`; then
  73. rev=`echo $rev | awk '{print $NF}'`
  74. printf -- '-svn%s' "$rev"
  75. # All done with svn
  76. exit
  77. fi