setlocalversion 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # This scripts adds local version information from the version
  5. # control systems git, mercurial (hg) and subversion (svn).
  6. #
  7. # If something goes wrong, send a mail the kernel build mailinglist
  8. # (see MAINTAINERS) and CC Nico Schottelius
  9. # <nico-linuxsetlocalversion -at- schottelius.org>.
  10. #
  11. #
  12. usage() {
  13. echo "Usage: $0 [--save-scmversion] [srctree] [branch] [kmi-generation]" >&2
  14. exit 1
  15. }
  16. scm_only=false
  17. srctree=.
  18. android_release=
  19. kmi_generation=
  20. if test "$1" = "--save-scmversion"; then
  21. scm_only=true
  22. shift
  23. fi
  24. if test $# -gt 0; then
  25. srctree=$1
  26. shift
  27. fi
  28. if test $# -gt 0; then
  29. # Extract the Android release version. If there is no match, then return 255
  30. # and clear the var $android_release
  31. android_release=`echo "$1" | sed -e '/android[0-9]\{2,\}/!{q255}; \
  32. s/^\(android[0-9]\{2,\}\)-.*/\1/'`
  33. if test $? -ne 0; then
  34. android_release=
  35. fi
  36. shift
  37. if test $# -gt 0; then
  38. kmi_generation=$1
  39. [ $(expr $kmi_generation : '^[0-9]\+$') -eq 0 ] && usage
  40. shift
  41. fi
  42. fi
  43. if test $# -gt 0 -o ! -d "$srctree"; then
  44. usage
  45. fi
  46. scm_version()
  47. {
  48. local short
  49. short=false
  50. cd "$srctree"
  51. if test -e .scmversion; then
  52. cat .scmversion
  53. return
  54. fi
  55. if test "$1" = "--short"; then
  56. short=true
  57. fi
  58. # Check for git and a git repo.
  59. if head=$(git rev-parse --verify HEAD 2>/dev/null); then
  60. if [ -n "$android_release" ] && [ -n "$kmi_generation" ]; then
  61. printf '%s' "-$android_release-$kmi_generation"
  62. elif [ -n "$android_release" ]; then
  63. printf '%s' "-$android_release"
  64. fi
  65. # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
  66. # it, because this version is defined in the top level Makefile.
  67. if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then
  68. # If only the short version is requested, don't bother
  69. # running further git commands
  70. if $short; then
  71. echo "+"
  72. return
  73. fi
  74. # If we are past a tagged commit (like
  75. # "v2.6.30-rc5-302-g72357d5"), we pretty print it.
  76. #
  77. # Ensure the abbreviated sha1 has exactly 12
  78. # hex characters, to make the output
  79. # independent of git version, local
  80. # core.abbrev settings and/or total number of
  81. # objects in the current repository - passing
  82. # --abbrev=12 ensures a minimum of 12, and the
  83. # awk substr() then picks the 'g' and first 12
  84. # hex chars.
  85. if atag="$(git describe --abbrev=12 2>/dev/null)"; then
  86. echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),substr($(NF),0,13))}'
  87. # If we don't have a tag at all we print -g{commitish},
  88. # again using exactly 12 hex chars.
  89. else
  90. head="$(echo $head | cut -c1-12)"
  91. printf '%s%s' -g $head
  92. fi
  93. fi
  94. # Is this git on svn?
  95. if git config --get svn-remote.svn.url >/dev/null; then
  96. printf -- '-svn%s' "$(git svn find-rev $head)"
  97. fi
  98. # Check for uncommitted changes.
  99. # First, with git-status, but --no-optional-locks is only
  100. # supported in git >= 2.14, so fall back to git-diff-index if
  101. # it fails. Note that git-diff-index does not refresh the
  102. # index, so it may give misleading results. See
  103. # git-update-index(1), git-diff-index(1), and git-status(1).
  104. if {
  105. git --no-optional-locks status -uno --porcelain 2>/dev/null ||
  106. git diff-index --name-only HEAD
  107. } | grep -qvE '^(.. )?scripts/package'; then
  108. printf '%s' -dirty
  109. fi
  110. # All done with git
  111. return
  112. fi
  113. # Check for mercurial and a mercurial repo.
  114. if hgid=$(hg id 2>/dev/null); then
  115. # Do we have an tagged version? If so, latesttagdistance == 1
  116. if [ "$(hg log -r . --template '{latesttagdistance}')" = "1" ]; then
  117. id=$(hg log -r . --template '{latesttag}')
  118. printf '%s%s' -hg "$id"
  119. else
  120. tag=$(printf '%s' "$hgid" | cut -d' ' -f2)
  121. if [ -z "$tag" -o "$tag" = tip ]; then
  122. id=$(printf '%s' "$hgid" | sed 's/[+ ].*//')
  123. printf '%s%s' -hg "$id"
  124. fi
  125. fi
  126. # Are there uncommitted changes?
  127. # These are represented by + after the changeset id.
  128. case "$hgid" in
  129. *+|*+\ *) printf '%s' -dirty ;;
  130. esac
  131. # All done with mercurial
  132. return
  133. fi
  134. # Check for svn and a svn repo.
  135. if rev=$(LANG= LC_ALL= LC_MESSAGES=C svn info 2>/dev/null | grep '^Last Changed Rev'); then
  136. rev=$(echo $rev | awk '{print $NF}')
  137. printf -- '-svn%s' "$rev"
  138. # All done with svn
  139. return
  140. fi
  141. }
  142. collect_files()
  143. {
  144. local file res=
  145. for file; do
  146. case "$file" in
  147. *\~*)
  148. continue
  149. ;;
  150. esac
  151. if test -e "$file"; then
  152. res="$res$(cat "$file")"
  153. fi
  154. done
  155. echo "$res"
  156. }
  157. if $scm_only; then
  158. if test ! -e .scmversion; then
  159. res=$(scm_version)
  160. echo "$res" >.scmversion
  161. fi
  162. exit
  163. fi
  164. if test -e include/config/auto.conf; then
  165. . include/config/auto.conf
  166. else
  167. echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
  168. exit 1
  169. fi
  170. # localversion* files in the build and source directory
  171. res="$(collect_files localversion*)"
  172. if test ! "$srctree" -ef .; then
  173. res="$res$(collect_files "$srctree"/localversion*)"
  174. fi
  175. # CONFIG_LOCALVERSION and LOCALVERSION (if set)
  176. res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
  177. # scm version string if not at a tagged commit
  178. if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
  179. # full scm version string
  180. res="$res$(scm_version)"
  181. else
  182. # append a plus sign if the repository is not in a clean
  183. # annotated or signed tagged state (as git describe only
  184. # looks at signed or annotated tags - git tag -a/-s) and
  185. # LOCALVERSION= is not specified
  186. if test "${LOCALVERSION+set}" != "set"; then
  187. scm=$(scm_version --short)
  188. res="$res${scm:++}"
  189. fi
  190. fi
  191. echo "$res"