setlocalversion 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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]" >&2
  14. exit 1
  15. }
  16. scm_only=false
  17. srctree=.
  18. if test "$1" = "--save-scmversion"; then
  19. scm_only=true
  20. shift
  21. fi
  22. if test $# -gt 0; then
  23. srctree=$1
  24. shift
  25. fi
  26. if test $# -gt 0 -o ! -d "$srctree"; then
  27. usage
  28. fi
  29. scm_version()
  30. {
  31. local short
  32. short=false
  33. cd "$srctree"
  34. if test -e .scmversion; then
  35. cat .scmversion
  36. return
  37. fi
  38. if test "$1" = "--short"; then
  39. short=true
  40. fi
  41. # Check for git and a git repo.
  42. if test -z "$(git rev-parse --show-cdup 2>/dev/null)" &&
  43. head=$(git rev-parse --verify --short HEAD 2>/dev/null); then
  44. # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
  45. # it, because this version is defined in the top level Makefile.
  46. if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then
  47. # If only the short version is requested, don't bother
  48. # running further git commands
  49. if $short; then
  50. echo "+"
  51. return
  52. fi
  53. # If we are past a tagged commit (like
  54. # "v2.6.30-rc5-302-g72357d5"), we pretty print it.
  55. if atag="$(git describe 2>/dev/null)"; then
  56. echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}'
  57. # If we don't have a tag at all we print -g{commitish}.
  58. else
  59. printf '%s%s' -g $head
  60. fi
  61. else
  62. atag="`git describe 2>/dev/null`"
  63. printf '%s%s' - ${atag}
  64. #printf '%s%s' -g $head
  65. fi
  66. # Is this git on svn?
  67. if git config --get svn-remote.svn.url >/dev/null; then
  68. printf -- '-svn%s' "$(git svn find-rev $head)"
  69. fi
  70. # Check for uncommitted changes.
  71. # First, with git-status, but --no-optional-locks is only
  72. # supported in git >= 2.14, so fall back to git-diff-index if
  73. # it fails. Note that git-diff-index does not refresh the
  74. # index, so it may give misleading results. See
  75. # git-update-index(1), git-diff-index(1), and git-status(1).
  76. if {
  77. git --no-optional-locks status -uno --porcelain 2>/dev/null ||
  78. git diff-index --name-only HEAD
  79. } | grep -qvE '^(.. )?scripts/package'; then
  80. printf '%s' -dirty
  81. fi
  82. # All done with git
  83. return
  84. fi
  85. # Check for mercurial and a mercurial repo.
  86. if test -d .hg && hgid=$(hg id 2>/dev/null); then
  87. # Do we have an tagged version? If so, latesttagdistance == 1
  88. if [ "$(hg log -r . --template '{latesttagdistance}')" = "1" ]; then
  89. id=$(hg log -r . --template '{latesttag}')
  90. printf '%s%s' -hg "$id"
  91. else
  92. tag=$(printf '%s' "$hgid" | cut -d' ' -f2)
  93. if [ -z "$tag" -o "$tag" = tip ]; then
  94. id=$(printf '%s' "$hgid" | sed 's/[+ ].*//')
  95. printf '%s%s' -hg "$id"
  96. fi
  97. fi
  98. # Are there uncommitted changes?
  99. # These are represented by + after the changeset id.
  100. case "$hgid" in
  101. *+|*+\ *) printf '%s' -dirty ;;
  102. esac
  103. # All done with mercurial
  104. return
  105. fi
  106. # Check for svn and a svn repo.
  107. if rev=$(LANG= LC_ALL= LC_MESSAGES=C svn info 2>/dev/null | grep '^Last Changed Rev'); then
  108. rev=$(echo $rev | awk '{print $NF}')
  109. printf -- '-svn%s' "$rev"
  110. # All done with svn
  111. return
  112. fi
  113. }
  114. collect_files()
  115. {
  116. local file res=
  117. for file; do
  118. case "$file" in
  119. *\~*)
  120. continue
  121. ;;
  122. esac
  123. if test -e "$file"; then
  124. res="$res$(cat "$file")"
  125. fi
  126. done
  127. echo "$res"
  128. }
  129. if $scm_only; then
  130. if test ! -e .scmversion; then
  131. res=$(scm_version)
  132. echo "$res" >.scmversion
  133. fi
  134. exit
  135. fi
  136. if test -e include/config/auto.conf; then
  137. # We are interested only in CONFIG_LOCALVERSION and
  138. # CONFIG_LOCALVERSION_AUTO, so extract these in a safe
  139. # way (i.e. w/o sourcing auto.conf)
  140. # xargs echo removes quotes
  141. CONFIG_LOCALVERSION=`cat include/config/auto.conf | awk -F '=' '/^CONFIG_LOCALVERSION=/ {print $2}' | xargs echo`
  142. CONFIG_LOCALVERSION_AUTO=`cat include/config/auto.conf | awk -F '=' '/^CONFIG_LOCALVERSION_AUTO=/ {print $2}' | xargs echo`
  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"