setlocalversion 5.3 KB

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