setlocalversion 4.7 KB

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