setlocalversion 3.9 KB

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