setlocalversion 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. cd "$srctree"
  33. if test -e .scmversion; then
  34. cat .scmversion
  35. return
  36. fi
  37. if test "$1" = "--short"; then
  38. short=true
  39. fi
  40. # Check for git and a git repo.
  41. if test -d .git && head=`git rev-parse --verify --short HEAD 2>/dev/null`; then
  42. # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
  43. # it, because this version is defined in the top level Makefile.
  44. if atag="`git describe --exact-match --abbrev=0 2>/dev/null`"; then
  45. # Make sure we're at the tag that matches the Makefile.
  46. # If not place the hash of the tag as well for
  47. # v2.6.30-rc5-g314aef
  48. if [ "x$atag" != "x$VERSION" ]; then
  49. # If only the short version is requested,
  50. # don't bother running further git commands
  51. if $short; then
  52. echo "+"
  53. return
  54. fi
  55. printf '%s%s' -g "`git show-ref -s --abbrev --tags $atag 2>/dev/null`"
  56. fi
  57. else
  58. # If only the short version is requested, don't bother
  59. # running further git commands
  60. if $short; then
  61. echo "+"
  62. return
  63. fi
  64. # If we are past a tagged commit (like
  65. # "v2.6.30-rc5-302-g72357d5"), we pretty print it and
  66. # include the hash of any new tag on top.
  67. if atag="`git describe 2>/dev/null`"; then
  68. tag="`git describe --abbrev=0 2>/dev/null`"
  69. commit="`echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}'`"
  70. printf '%s%s%s' -g "`git show-ref -s --abbrev --tags $tag 2>/dev/null`" $commit
  71. # If we don't have a tag at all we print -g{commitish}.
  72. else
  73. printf '%s%s' -g $head
  74. fi
  75. fi
  76. # Is this git on svn?
  77. if git config --get svn-remote.svn.url >/dev/null; then
  78. printf -- '-svn%s' "`git svn find-rev $head`"
  79. fi
  80. # Update index only on r/w media
  81. [ -w . ] && git update-index --refresh --unmerged > /dev/null
  82. # Check for uncommitted changes
  83. if git diff-index --name-only HEAD | grep -qv "^scripts/package"; then
  84. printf '%s' -dirty
  85. fi
  86. # All done with git
  87. return
  88. fi
  89. # Check for mercurial and a mercurial repo.
  90. if test -d .hg && hgid=`hg id 2>/dev/null`; then
  91. # Do we have an tagged version? If so, latesttagdistance == 1
  92. if [ "`hg log -r . --template '{latesttagdistance}'`" == "1" ]; then
  93. id=`hg log -r . --template '{latesttag}'`
  94. printf '%s%s' -hg "$id"
  95. else
  96. tag=`printf '%s' "$hgid" | cut -d' ' -f2`
  97. if [ -z "$tag" -o "$tag" = tip ]; then
  98. id=`printf '%s' "$hgid" | sed 's/[+ ].*//'`
  99. printf '%s%s' -hg "$id"
  100. fi
  101. fi
  102. # Are there uncommitted changes?
  103. # These are represented by + after the changeset id.
  104. case "$hgid" in
  105. *+|*+\ *) printf '%s' -dirty ;;
  106. esac
  107. # All done with mercurial
  108. return
  109. fi
  110. # Check for svn and a svn repo.
  111. if rev=`svn info 2>/dev/null | grep '^Last Changed Rev'`; then
  112. rev=`echo $rev | awk '{print $NF}'`
  113. printf -- '-svn%s' "$rev"
  114. # All done with svn
  115. return
  116. fi
  117. }
  118. collect_files()
  119. {
  120. local file res
  121. for file; do
  122. case "$file" in
  123. *\~*)
  124. continue
  125. ;;
  126. esac
  127. if test -e "$file"; then
  128. res="$res$(cat "$file")"
  129. fi
  130. done
  131. echo "$res"
  132. }
  133. if $scm_only; then
  134. if test ! -e .scmversion; then
  135. res=$(scm_version)
  136. echo "$res" >.scmversion
  137. fi
  138. exit
  139. fi
  140. if test -e include/config/auto.conf; then
  141. . include/config/auto.conf
  142. else
  143. echo "Error: kernelrelease not valid - run 'make prepare' to update it"
  144. exit 1
  145. fi
  146. # localversion* files in the build and source directory
  147. res="$(collect_files localversion*)"
  148. if test ! "$srctree" -ef .; then
  149. res="$res$(collect_files "$srctree"/localversion*)"
  150. fi
  151. # CONFIG_LOCALVERSION and LOCALVERSION (if set)
  152. res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
  153. # scm version string if not at a tagged commit
  154. if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
  155. # full scm version string
  156. res="$res$(scm_version)"
  157. else
  158. # append a plus sign if the repository is not in a clean
  159. # annotated or signed tagged state (as git describe only
  160. # looks at signed or annotated tags - git tag -a/-s) and
  161. # LOCALVERSION= is not specified
  162. if test "${LOCALVERSION+set}" != "set"; then
  163. scm=$(scm_version --short)
  164. res="$res${scm:++}"
  165. fi
  166. fi
  167. echo "$res"