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