setlocalversion 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # This scripts adds local version information from the version
  5. # control system git.
  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. set -e
  13. usage() {
  14. echo "Usage: $0 [--no-local] [srctree]" >&2
  15. exit 1
  16. }
  17. no_local=false
  18. if test "$1" = "--no-local"; then
  19. no_local=true
  20. shift
  21. fi
  22. srctree=.
  23. if test $# -gt 0; then
  24. srctree=$1
  25. shift
  26. fi
  27. if test $# -gt 0 -o ! -d "$srctree"; then
  28. usage
  29. fi
  30. try_tag() {
  31. tag="$1"
  32. # Is $tag an annotated tag?
  33. if [ "$(git cat-file -t "$tag" 2> /dev/null)" != tag ]; then
  34. return
  35. fi
  36. # Is it an ancestor of HEAD, and if so, how many commits are in $tag..HEAD?
  37. # shellcheck disable=SC2046 # word splitting is the point here
  38. set -- $(git rev-list --count --left-right "$tag"...HEAD 2> /dev/null)
  39. # $1 is 0 if and only if $tag is an ancestor of HEAD. Use
  40. # string comparison, because $1 is empty if the 'git rev-list'
  41. # command somehow failed.
  42. if [ "$1" != 0 ]; then
  43. return
  44. fi
  45. # $2 is the number of commits in the range $tag..HEAD, possibly 0.
  46. count="$2"
  47. }
  48. scm_version()
  49. {
  50. local short=false
  51. local no_dirty=false
  52. local tag
  53. while [ $# -gt 0 ];
  54. do
  55. case "$1" in
  56. --short)
  57. short=true;;
  58. --no-dirty)
  59. no_dirty=true;;
  60. esac
  61. shift
  62. done
  63. cd "$srctree"
  64. if test -n "$(git rev-parse --show-cdup 2>/dev/null)"; then
  65. return
  66. fi
  67. if ! head=$(git rev-parse --verify HEAD 2>/dev/null); then
  68. return
  69. fi
  70. # mainline kernel: 6.2.0-rc5 -> v6.2-rc5
  71. # stable kernel: 6.1.7 -> v6.1.7
  72. version_tag=v$(echo "${KERNELVERSION}" | sed -E 's/^([0-9]+\.[0-9]+)\.0(.*)$/\1\2/')
  73. # try_tag initializes count if the tag is usable.
  74. count=
  75. # If a localversion* file exists, and the corresponding
  76. # annotated tag exists and is an ancestor of HEAD, use
  77. # it. This is the case in linux-next.
  78. if [ -n "${file_localversion#-}" ] ; then
  79. try_tag "${file_localversion#-}"
  80. fi
  81. # Otherwise, if a localversion* file exists, and the tag
  82. # obtained by appending it to the tag derived from
  83. # KERNELVERSION exists and is an ancestor of HEAD, use
  84. # it. This is e.g. the case in linux-rt.
  85. if [ -z "${count}" ] && [ -n "${file_localversion}" ]; then
  86. try_tag "${version_tag}${file_localversion}"
  87. fi
  88. # Otherwise, default to the annotated tag derived from KERNELVERSION.
  89. if [ -z "${count}" ]; then
  90. try_tag "${version_tag}"
  91. fi
  92. # If we are at the tagged commit, we ignore it because the
  93. # version is well-defined. If none of the attempted tags exist
  94. # or were usable, $count is still empty.
  95. if [ -z "${count}" ] || [ "${count}" -gt 0 ]; then
  96. # If only the short version is requested, don't bother
  97. # running further git commands
  98. if $short; then
  99. echo "+"
  100. return
  101. fi
  102. # If we are past the tagged commit, we pretty print it.
  103. # (like 6.1.0-14595-g292a089d78d3)
  104. if [ -n "${count}" ]; then
  105. printf "%s%05d" "-" "${count}"
  106. fi
  107. # Add -g and exactly 12 hex chars.
  108. printf '%s%.12s' -g "$head"
  109. fi
  110. if ${no_dirty}; then
  111. return
  112. fi
  113. # Check for uncommitted changes.
  114. # This script must avoid any write attempt to the source tree, which
  115. # might be read-only.
  116. # You cannot use 'git describe --dirty' because it tries to create
  117. # .git/index.lock .
  118. # First, with git-status, but --no-optional-locks is only supported in
  119. # git >= 2.14, so fall back to git-diff-index if it fails. Note that
  120. # git-diff-index does not refresh the index, so it may give misleading
  121. # results.
  122. # See git-update-index(1), git-diff-index(1), and git-status(1).
  123. if {
  124. git --no-optional-locks status -uno --porcelain 2>/dev/null ||
  125. git diff-index --name-only HEAD
  126. } | read dummy; then
  127. printf '%s' -dirty
  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 [ -z "${KERNELVERSION}" ]; then
  146. echo "KERNELVERSION is not set" >&2
  147. exit 1
  148. fi
  149. # localversion* files in the build and source directory
  150. file_localversion="$(collect_files localversion*)"
  151. if test ! "$srctree" -ef .; then
  152. file_localversion="${file_localversion}$(collect_files "$srctree"/localversion*)"
  153. fi
  154. if ${no_local}; then
  155. echo "${KERNELVERSION}$(scm_version --no-dirty)"
  156. exit 0
  157. fi
  158. if ! test -e include/config/auto.conf; then
  159. echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
  160. exit 1
  161. fi
  162. # version string from CONFIG_LOCALVERSION
  163. config_localversion=$(sed -n 's/^CONFIG_LOCALVERSION=\(.*\)$/\1/p' include/config/auto.conf)
  164. # scm version string if not at the kernel version tag or at the file_localversion
  165. if grep -q "^CONFIG_LOCALVERSION_AUTO=y$" include/config/auto.conf; then
  166. # full scm version string
  167. scm_version="$(scm_version)"
  168. elif [ "${LOCALVERSION+set}" != "set" ]; then
  169. # If the variable LOCALVERSION is not set, append a plus
  170. # sign if the repository is not in a clean annotated or
  171. # signed tagged state (as git describe only looks at signed
  172. # or annotated tags - git tag -a/-s).
  173. #
  174. # If the variable LOCALVERSION is set (including being set
  175. # to an empty string), we don't want to append a plus sign.
  176. scm_version="$(scm_version --short)"
  177. fi
  178. echo "${KERNELVERSION}${file_localversion}${config_localversion}${LOCALVERSION}${scm_version}"