setlocalversion 3.5 KB

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