setlocalversion 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. fi
  62. # Is this git on svn?
  63. if git config --get svn-remote.svn.url >/dev/null; then
  64. printf -- '-svn%s' "`git svn find-rev $head`"
  65. fi
  66. # Check for uncommitted changes
  67. if git diff-index --name-only HEAD | grep -qv "^scripts/package"; then
  68. printf '%s' -dirty
  69. fi
  70. # All done with git
  71. return
  72. fi
  73. # Check for mercurial and a mercurial repo.
  74. if test -d .hg && hgid=`hg id 2>/dev/null`; then
  75. # Do we have an tagged version? If so, latesttagdistance == 1
  76. if [ "`hg log -r . --template '{latesttagdistance}'`" == "1" ]; then
  77. id=`hg log -r . --template '{latesttag}'`
  78. printf '%s%s' -hg "$id"
  79. else
  80. tag=`printf '%s' "$hgid" | cut -d' ' -f2`
  81. if [ -z "$tag" -o "$tag" = tip ]; then
  82. id=`printf '%s' "$hgid" | sed 's/[+ ].*//'`
  83. printf '%s%s' -hg "$id"
  84. fi
  85. fi
  86. # Are there uncommitted changes?
  87. # These are represented by + after the changeset id.
  88. case "$hgid" in
  89. *+|*+\ *) printf '%s' -dirty ;;
  90. esac
  91. # All done with mercurial
  92. return
  93. fi
  94. # Check for svn and a svn repo.
  95. if rev=`LANG= LC_ALL= LC_MESSAGES=C svn info 2>/dev/null | grep '^Last Changed Rev'`; then
  96. rev=`echo $rev | awk '{print $NF}'`
  97. printf -- '-svn%s' "$rev"
  98. # All done with svn
  99. return
  100. fi
  101. }
  102. collect_files()
  103. {
  104. local file res
  105. for file; do
  106. case "$file" in
  107. *\~*)
  108. continue
  109. ;;
  110. esac
  111. if test -e "$file"; then
  112. res="$res$(cat "$file")"
  113. fi
  114. done
  115. echo "$res"
  116. }
  117. if $scm_only; then
  118. if test ! -e .scmversion; then
  119. res=$(scm_version)
  120. echo "$res" >.scmversion
  121. fi
  122. exit
  123. fi
  124. if test -e include/config/auto.conf; then
  125. . include/config/auto.conf
  126. else
  127. echo "Error: kernelrelease not valid - run 'make prepare' to update it" >&2
  128. exit 1
  129. fi
  130. # localversion* files in the build and source directory
  131. res="$(collect_files localversion*)"
  132. if test ! "$srctree" -ef .; then
  133. res="$res$(collect_files "$srctree"/localversion*)"
  134. fi
  135. # CONFIG_LOCALVERSION and LOCALVERSION (if set)
  136. res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
  137. # scm version string if not at a tagged commit
  138. if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
  139. # full scm version string
  140. res="$res$(scm_version)"
  141. else
  142. # append a plus sign if the repository is not in a clean
  143. # annotated or signed tagged state (as git describe only
  144. # looks at signed or annotated tags - git tag -a/-s) and
  145. # LOCALVERSION= is not specified
  146. if test "${LOCALVERSION+set}" != "set"; then
  147. scm=$(scm_version --short)
  148. res="$res${scm:++}"
  149. fi
  150. fi
  151. echo "$res"