setlocalversion 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 [--save-scmversion] [srctree]" >&2
  13. exit 1
  14. }
  15. scm_only=false
  16. srctree=.
  17. if test "$1" = "--save-scmversion"; then
  18. scm_only=true
  19. shift
  20. fi
  21. if test $# -gt 0; then
  22. srctree=$1
  23. shift
  24. fi
  25. if test $# -gt 0 -o ! -d "$srctree"; then
  26. usage
  27. fi
  28. scm_version()
  29. {
  30. local short
  31. short=false
  32. cd "$srctree"
  33. if test -e .scmversion; then
  34. cat .scmversion
  35. return
  36. fi
  37. if test "$1" = "--short"; then
  38. short=true
  39. fi
  40. # Check for git and a git repo.
  41. if test -z "$(git rev-parse --show-cdup 2>/dev/null)" &&
  42. head=`git rev-parse --verify --short HEAD 2>/dev/null`; then
  43. # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
  44. # it, because this version is defined in the top level Makefile.
  45. if atag="`git describe --exact-match --abbrev=0 2>/dev/null`"; then
  46. # Make sure we're at the tag that matches the Makefile.
  47. # If not place the hash of the tag as well for
  48. # v2.6.30-rc5-g314aef
  49. if [ "x$atag" != "x$VERSION" ]; then
  50. # If only the short version is requested,
  51. # don't bother running further git commands
  52. if $short; then
  53. echo "+"
  54. return
  55. fi
  56. printf '%s%s' -g "`git show-ref -s --abbrev --tags $atag 2>/dev/null`"
  57. fi
  58. else
  59. # If only the short version is requested, don't bother
  60. # running further git commands
  61. if $short; then
  62. echo "+"
  63. return
  64. fi
  65. # If we are past a tagged commit (like
  66. # "v2.6.30-rc5-302-g72357d5"), we pretty print it and
  67. # include the hash of any new tag on top.
  68. if atag="`git describe 2>/dev/null`"; then
  69. tag="`git describe --abbrev=0 2>/dev/null`"
  70. commit="`echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}'`"
  71. printf '%s%s%s' -g "`git show-ref -s --abbrev --tags $tag 2>/dev/null`" $commit
  72. # If we don't have a tag at all we print -g{commitish}.
  73. else
  74. printf '%s%s' -g $head
  75. fi
  76. fi
  77. # Is this git on svn?
  78. if git config --get svn-remote.svn.url >/dev/null; then
  79. printf -- '-svn%s' "`git svn find-rev $head`"
  80. fi
  81. # Update index only on r/w media
  82. [ -w . ] && git update-index --refresh --unmerged > /dev/null
  83. # Check for uncommitted changes
  84. if git diff-index --name-only HEAD | grep -qv "^scripts/package"; then
  85. printf '%s' -dirty
  86. fi
  87. # All done with git
  88. return
  89. fi
  90. # Check for mercurial and a mercurial repo.
  91. if test -d .hg && hgid=`hg id 2>/dev/null`; then
  92. # Do we have an tagged version? If so, latesttagdistance == 1
  93. if [ "`hg log -r . --template '{latesttagdistance}'`" == "1" ]; then
  94. id=`hg log -r . --template '{latesttag}'`
  95. printf '%s%s' -hg "$id"
  96. else
  97. tag=`printf '%s' "$hgid" | cut -d' ' -f2`
  98. if [ -z "$tag" -o "$tag" = tip ]; then
  99. id=`printf '%s' "$hgid" | sed 's/[+ ].*//'`
  100. printf '%s%s' -hg "$id"
  101. fi
  102. fi
  103. # Are there uncommitted changes?
  104. # These are represented by + after the changeset id.
  105. case "$hgid" in
  106. *+|*+\ *) printf '%s' -dirty ;;
  107. esac
  108. # All done with mercurial
  109. return
  110. fi
  111. # Check for svn and a svn repo.
  112. if rev=`svn info 2>/dev/null | grep '^Last Changed Rev'`; then
  113. rev=`echo $rev | awk '{print $NF}'`
  114. printf -- '-svn%s' "$rev"
  115. # All done with svn
  116. return
  117. fi
  118. }
  119. collect_files()
  120. {
  121. local file res
  122. for file; do
  123. case "$file" in
  124. *\~*)
  125. continue
  126. ;;
  127. esac
  128. if test -e "$file"; then
  129. res="$res$(cat "$file")"
  130. fi
  131. done
  132. echo "$res"
  133. }
  134. if $scm_only; then
  135. if test ! -e .scmversion; then
  136. res=$(scm_version)
  137. echo "$res" >.scmversion
  138. fi
  139. exit
  140. fi
  141. if test -e include/config/auto.conf; then
  142. . include/config/auto.conf
  143. else
  144. echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
  145. exit 1
  146. fi
  147. # localversion* files in the build and source directory
  148. res="$(collect_files localversion*)"
  149. if test ! "$srctree" -ef .; then
  150. res="$res$(collect_files "$srctree"/localversion*)"
  151. fi
  152. # CONFIG_LOCALVERSION and LOCALVERSION (if set)
  153. res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
  154. # scm version string if not at a tagged commit
  155. if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
  156. # full scm version string
  157. res="$res$(scm_version)"
  158. else
  159. # append a plus sign if the repository is not in a clean
  160. # annotated or signed tagged state (as git describe only
  161. # looks at signed or annotated tags - git tag -a/-s) and
  162. # LOCALVERSION= is not specified
  163. if test "${LOCALVERSION+set}" != "set"; then
  164. scm=$(scm_version --short)
  165. res="$res${scm:++}"
  166. fi
  167. fi
  168. echo "$res"