build_tools/make_version: Teach version string about git.
[dahdi/tools.git] / build_tools / make_version
1 #!/bin/sh
2
3 if [ -f ${1}/.version ]; then
4         cat ${1}/.version
5 elif [ -f ${1}/.svnrevision ]; then
6         echo SVN-`cat ${1}/.svnbranch`-r`cat ${1}/.svnrevision`
7 elif [ -d ${1}/.svn ]; then
8     PARTS=`LANG=C svn info ${1} | grep URL | awk '{print $2;}' | sed -e s:^.*/svn/${2}/:: | sed -e 's:/: :g'`
9     BRANCH=0
10     TEAM=0
11     
12     REV=`svnversion -c ${1} | cut -d: -f2`
13     
14     if [ "${PARTS}" = "trunk" ]
15         then
16         echo SVN-'trunk'-r${REV}
17         exit 0
18     fi
19     
20     for PART in $PARTS
21       do
22       if [ ${BRANCH} != 0 ]
23           then
24           RESULT="${RESULT}-${PART}"
25           break
26       fi
27       
28       if [ ${TEAM} != 0 ]
29           then
30           RESULT="${RESULT}-${PART}"
31           continue
32       fi
33       
34       if [ "${PART}" = "branches" ]
35           then
36           BRANCH=1
37           RESULT="branch"
38           continue
39       fi
40       
41       if [ "${PART}" = "tags" ]
42           then
43           BRANCH=1
44           RESULT="tag"
45           continue
46       fi
47       
48       if [ "${PART}" = "team" ]
49           then
50           TEAM=1
51           continue
52       fi
53     done
54     
55     echo SVN-${RESULT##-}-r${REV}
56 elif [ -d ${1}/.git ]; then
57     # If the first log commit messages indicates that this is checked into
58     # subversion, we'll just use the SVN- form of the revision.
59     MODIFIED=""
60     SVN_REV=`git log --pretty=full -1 | grep -F "git-svn-id:" | sed -e "s/.*\@\([^\s]*\)\s.*/\1/g"`
61     if [ -z "$SVN_REV" ]; then
62         VERSION=`git describe --tags --dirty=M 2> /dev/null | sed -e "s/^v\([0-9]\)/\1/"`
63         if [ $? -ne 0 ]; then
64             if [ "`git ls-files -m | wc -l`" != "0" ]; then
65                 MODIFIED="M"
66             fi
67             # Some older versions of git do not support all the above
68             # options.
69             VERSION=GIT-`git rev-parse --short --verify HEAD`${MODIFIED}
70         fi
71         echo ${VERSION}
72     else
73         PARTS=`LANG=C git log --pretty=full | grep -F "git-svn-id:" | head -1 | awk '{print $2;}' | sed -e s:^.*/svn/$2/:: | sed -e 's:/: :g' | sed -e 's/@.*$//g'`
74         BRANCH=0
75         TEAM=0
76
77         if [ "`git ls-files -m | wc -l`" != "0" ]; then
78             MODIFIED="M"
79         fi
80
81         if [ "${PARTS}" = "trunk" ]; then
82             echo SVN-'trunk'-r${SVN_REV}${MODIFIED}
83             exit 0
84         fi
85
86         for PART in $PARTS
87           do
88               if [ ${BRANCH} != 0 ]; then
89               RESULT="${RESULT}-${PART}"
90               break
91           fi
92
93           if [ ${TEAM} != 0 ]; then
94               RESULT="${RESULT}-${PART}"
95               continue
96           fi
97
98           if [ "${PART}" = "branches" ]; then
99               BRANCH=1
100               RESULT="branch"
101               continue
102           fi
103
104           if [ "${PART}" = "tags" ]; then
105               BRANCH=1
106               RESULT="tag"
107               continue
108           fi
109
110           if [ "${PART}" = "team" ]; then
111               TEAM=1
112               continue
113           fi
114         done
115
116         echo SVN-${RESULT##-}-r${SVN_REV}${MODIFIED}
117     fi
118 else
119         # Use the directory information in the absence of any other version
120         # information
121         pwd -P
122 fi