setlocalversion 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. # It was originally copied from the Linux kernel v3.2.0-rc4 and modified
  7. # to support the U-Boot build-system.
  8. #
  9. usage() {
  10. echo "Usage: $0 [--save-scmversion] [srctree]" >&2
  11. exit 1
  12. }
  13. scm_only=false
  14. srctree=.
  15. if test "$1" = "--save-scmversion"; then
  16. scm_only=true
  17. shift
  18. fi
  19. if test $# -gt 0; then
  20. srctree=$1
  21. shift
  22. fi
  23. if test $# -gt 0 -o ! -d "$srctree"; then
  24. usage
  25. fi
  26. scm_version()
  27. {
  28. local short
  29. short=false
  30. cd "$srctree"
  31. if test -e .scmversion; then
  32. cat .scmversion
  33. return
  34. fi
  35. if test "$1" = "--short"; then
  36. short=true
  37. fi
  38. # Check for git and a git repo.
  39. if test -e .git && head=`git rev-parse --verify --short HEAD 2>/dev/null`; then
  40. # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
  41. # it, because this version is defined in the top level Makefile.
  42. if [ -z "`git describe --exact-match 2>/dev/null`" ]; then
  43. # If only the short version is requested, don't bother
  44. # running further git commands
  45. if $short; then
  46. echo "+"
  47. return
  48. fi
  49. # If we are past a tagged commit (like
  50. # "v2.6.30-rc5-302-g72357d5"), we pretty print it.
  51. if atag="`git describe 2>/dev/null`"; then
  52. echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}'
  53. # If we don't have a tag at all we print -g{commitish}.
  54. else
  55. printf '%s%s' -g $head
  56. fi
  57. fi
  58. # Is this git on svn?
  59. if git config --get svn-remote.svn.url >/dev/null; then
  60. printf -- '-svn%s' "`git svn find-rev $head`"
  61. fi
  62. # Update index only on r/w media
  63. [ -w . ] && git update-index --refresh --unmerged > /dev/null
  64. # Check for uncommitted changes
  65. if git diff-index --name-only HEAD | grep -v "^scripts/package" \
  66. | read dummy; then
  67. printf '%s' -dirty
  68. fi
  69. # All done with git
  70. return
  71. fi
  72. # Check for mercurial and a mercurial repo.
  73. if test -d .hg && hgid=`hg id 2>/dev/null`; then
  74. # Do we have an tagged version? If so, latesttagdistance == 1
  75. if [ "`hg log -r . --template '{latesttagdistance}'`" == "1" ]; then
  76. id=`hg log -r . --template '{latesttag}'`
  77. printf '%s%s' -hg "$id"
  78. else
  79. tag=`printf '%s' "$hgid" | cut -d' ' -f2`
  80. if [ -z "$tag" -o "$tag" = tip ]; then
  81. id=`printf '%s' "$hgid" | sed 's/[+ ].*//'`
  82. printf '%s%s' -hg "$id"
  83. fi
  84. fi
  85. # Are there uncommitted changes?
  86. # These are represented by + after the changeset id.
  87. case "$hgid" in
  88. *+|*+\ *) printf '%s' -dirty ;;
  89. esac
  90. # All done with mercurial
  91. return
  92. fi
  93. # Check for svn and a svn repo.
  94. if rev=`svn info 2>/dev/null | grep '^Last Changed Rev'`; then
  95. rev=`echo $rev | awk '{print $NF}'`
  96. printf -- '-svn%s' "$rev"
  97. # All done with svn
  98. return
  99. fi
  100. }
  101. collect_files()
  102. {
  103. local file res
  104. for file; do
  105. case "$file" in
  106. *\~*)
  107. continue
  108. ;;
  109. esac
  110. if test -e "$file"; then
  111. res="$res$(cat "$file")"
  112. fi
  113. done
  114. echo "$res"
  115. }
  116. if $scm_only; then
  117. if test ! -e .scmversion; then
  118. res=$(scm_version)
  119. echo "$res" >.scmversion
  120. fi
  121. exit
  122. fi
  123. #if test -e include/config/auto.conf; then
  124. # . include/config/auto.conf
  125. #else
  126. # echo "Error: kernelrelease not valid - run 'make prepare' to update it"
  127. # exit 1
  128. #fi
  129. CONFIG_LOCALVERSION=
  130. CONFIG_LOCALVERSION_AUTO=y
  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"