autorevision.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/bin/sh
  2. # Copyright 2008 Paul Wise
  3. # Copyright 2009 Konstantin Dmitriev
  4. #
  5. # This package is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License as
  7. # published by the Free Software Foundation; either version 2 of
  8. # the License, or (at your option) any later version.
  9. #
  10. # This package is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. get_git_id(){
  15. export SCM=git
  16. export REVISION_ID=`cd "$1"; git log --no-color -1 | head -n 1 | cut -f 2 -d ' ' | cut -c -6`
  17. export BRANCH="`cd "$1"; git branch -a --no-color --contains HEAD | sed -e s/\*\ // | sed -e s/\(no\ branch\)// | tr '\n' ' ' | tr -s ' ' | sed s/^' '//`"
  18. if ( echo $BRANCH | egrep origin/master > /dev/null ); then
  19. #give a priority to master branch
  20. BRANCH='master'
  21. else
  22. BRANCH=`echo $BRANCH | cut -d ' ' -f 1`
  23. BRANCH=${BRANCH##*/}
  24. fi
  25. export REVISION=`git show --pretty=format:%ci HEAD | head -c 10 | tr -d '-'`
  26. export COMPARE=`git rev-parse --git-dir`
  27. # The extra * at the end is for Modified
  28. #REVISION="$REVISION"`cd "$1"; [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"`
  29. }
  30. get_git_svn_id(){
  31. export SCM=git-svn
  32. export REVISION=`cd "$1"; git svn find-rev HEAD`
  33. export COMPARE="$1/.git/"
  34. if [ x = "x$REVISION" ] ; then
  35. # The extra M at the end is for Modified
  36. export REVISION=`cd "$1"; git svn find-rev \`git rev-list --max-count=1 --grep='git-svn-id: ' HEAD\``M
  37. else
  38. export REVISION="$REVISION"`cd "$1"; git diff --quiet HEAD || echo M`
  39. fi
  40. }
  41. get_svn_id(){
  42. export SCM=svn
  43. export REVISION=`cd "$1"; svnversion || svn info | sed -n 's/^Revision: \(.*\)/\1/p'`
  44. }
  45. HEADER="$2/autorevision.h"
  46. SCM=none
  47. if [ ! -f "$HEADER" ] ; then
  48. touch -t 197001010101 "$HEADER"
  49. fi
  50. # Extract the revision from SVN/git/etc
  51. if git rev-parse --git-dir > /dev/null 2>&1 ; then
  52. get_git_id "."
  53. elif [ -d "$1/.git/svn" ] ; then
  54. get_git_svn_id "$1"
  55. elif [ -d "$1/../.git/svn" ] ; then
  56. get_git_svn_id "$1/.."
  57. elif [ -d "$1/../../.git/svn" ] ; then
  58. get_git_svn_id "$1/../.."
  59. elif [ -d "$1/.svn" ] ; then
  60. COMPARE="$1/.svn"
  61. get_svn_id "$1"
  62. elif [ -d "$1/_svn" ] ; then
  63. COMPARE="$1/_svn"
  64. get_svn_id "$1"
  65. fi
  66. # Allow overriding both REVISION and DEVEL_VERSION
  67. if [ -f "$2/autorevision.conf" ] ; then
  68. SCM=manual
  69. . "$2/autorevision.conf"
  70. fi
  71. # Abort if the header is newer
  72. if [ "$COMPARE" -ot "$HEADER" ] ; then exit; fi
  73. # Set the development version string
  74. if [ x = "x$DEVEL_VERSION" ] ; then
  75. if [ x != "x$REVISION" ] ; then
  76. if [ $SCM = svn ] ; then
  77. DEVEL_VERSION="SVN r$REVISION"
  78. elif [ $SCM = git-svn ] ; then
  79. DEVEL_VERSION="SVN r$REVISION (via git)"
  80. elif [ $SCM = git ] ; then
  81. DEVEL_VERSION="Revision: ${REVISION}\\\\nBranch: ${BRANCH}\\\\nRevision ID: ${REVISION_ID}"
  82. elif [ $SCM = manual ] ; then
  83. DEVEL_VERSION="$REVISION (manually configured)"
  84. fi
  85. fi
  86. fi
  87. # Output the header
  88. if [ x != "x$DEVEL_VERSION" ] ; then
  89. printf "#define SHOW_EXTRA_INFO\n" > "$HEADER"
  90. printf "#define DEVEL_VERSION \"$DEVEL_VERSION\"\n" >> "$HEADER"
  91. fi