de0b97e1a3b80adee1febc9f65e61a0bb8abd90e
[asterisk/asterisk.git] / build_tools / make_version
1 #!/bin/sh
2
3 if [ -f ${1}/.version ]; then
4     cat ${1}/.version
5 elif [ -d ${1}/.svn ]; then
6     PARTS=`LANG=C svn info ${1} | ${GREP} URL | ${AWK} '{print $2;}' | sed -e 's:^.*/svn/asterisk/::' | sed -e 's:/: :g'`
7     BRANCH=0
8     TEAM=0
9     TAG=0
10     FEATURE=0
11
12     REV=`svnversion -c ${1} | cut -d: -f2`
13
14     INTEGRATED=`LANG=C svn pg automerge-propname ${1}`
15     if [ -z "${INTEGRATED}" ] ; then
16         INTEGRATED=svnmerge-integrated
17     fi
18
19     BASE=`LANG=C svn pg ${INTEGRATED} ${1} | cut -d: -f1`
20
21     if [ "${PARTS}" = "trunk" ] ; then
22         echo SVN-trunk-r${REV}
23         exit 0
24     fi
25
26     for PART in $PARTS ; do
27         if [ ${TAG} != 0 ] ; then
28             if [ "${PART}" = "autotag_for_be" ] ; then
29                 continue
30             fi
31             if [ "${PART}" = "autotag_for_sx00i" ] ; then
32                 continue
33             fi
34             RESULT="${PART}"
35             break
36         fi
37
38         if [ ${BRANCH} != 0 ] ; then
39             RESULT="${RESULT}-${PART}"
40             if [ ${FEATURE} != 0 ] ; then
41                 RESULT="${RESULT}-${FEATURE_NAME}"
42             fi
43             break
44         fi
45
46         if [ ${TEAM} != 0 ] ; then
47             if [ -z "${RESULT}" ] ; then
48                 RESULT="${PART}"
49             else
50                 RESULT="${RESULT}-${PART}"
51             fi
52             continue
53         fi
54
55         if [ "${PART}" = "certified" ] ; then
56             FEATURE=1
57             FEATURE_NAME="cert"
58             continue
59         fi
60
61         if [ "${PART}" = "branches" ] ; then
62             BRANCH=1
63             RESULT="branch"
64             continue
65         fi
66
67         if [ "${PART}" = "tags" ] ; then
68             TAG=1
69             continue
70         fi
71
72         if [ "${PART}" = "team" ] ; then
73             TEAM=1
74             continue
75         fi
76     done
77
78     if [ ${TAG} != 0 ] ; then
79         echo ${RESULT}
80     else
81         echo SVN-${RESULT}-r${REV}${BASE:+-${BASE}}
82     fi
83 elif [ -d ${1}/.git ]; then
84     if [ -z ${GIT} ]; then
85         GIT="git"
86     fi
87
88     if ! command -v ${GIT} >/dev/null 2>&1; then
89         echo "UNKNOWN__and_probably_unsupported"
90         exit 1
91     fi
92     # If the first log commit messages indicates that this is checked into
93     # subversion, we'll just use the SVN- form of the revision.
94     MODIFIED=""
95     SVN_REV=`${GIT} log --pretty=full -1 | grep -F "git-svn-id:" | sed -e "s/.*\@\([^\s]*\)\s.*/\1/g"`
96     if [ -z "$SVN_REV" ]; then
97         VERSION=GIT-`${GIT} describe --long --always --tags --dirty=M 2> /dev/null`
98         if [ $? -ne 0 ]; then
99             if [ "`${GIT} ls-files -m | wc -l`" != "0" ]; then
100                 MODIFIED="M"
101             fi
102             # Some older versions of git do not support all the above
103             # options.
104             VERSION=GIT-`${GIT} rev-parse --short --verify HEAD`${MODIFIED}
105         fi
106         echo ${VERSION}
107     else
108         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'`
109         BRANCH=0
110         TEAM=0
111         TAG=0
112         FEATURE=0
113
114         if [ "`${GIT} ls-files -m | wc -l`" != "0" ]; then
115             MODIFIED="M"
116         fi
117
118         for PART in $PARTS ; do
119             if [ ${TAG} != 0 ] ; then
120                 if [ "${PART}" = "autotag_for_be" ] ; then
121                     continue
122                 fi
123                 if [ "${PART}" = "autotag_for_sx00i" ] ; then
124                     continue
125                 fi
126                 RESULT="${PART}"
127                 break
128             fi
129
130             if [ ${BRANCH} != 0 ] ; then
131                 RESULT="${RESULT}-${PART}"
132                 if [ ${FEATURE} != 0 ] ; then
133                     RESULT="${RESULT}-${FEATURE_NAME}"
134                 fi
135                 break
136             fi
137
138             if [ ${TEAM} != 0 ] ; then
139                 if [ -z "${RESULT}" ] ; then
140                     RESULT="${PART}"
141                 else
142                     RESULT="${RESULT}-${PART}"
143                 fi
144                 continue
145             fi
146
147             if [ "${PART}" = "certified" ] ; then
148                 FEATURE=1
149                 FEATURE_NAME="cert"
150                 continue
151             fi
152
153             if [ "${PART}" = "branches" ] ; then
154                 BRANCH=1
155                 RESULT="branch"
156                 continue
157             fi
158
159             if [ "${PART}" = "tags" ] ; then
160                 TAG=1
161                 continue
162             fi
163
164             if [ "${PART}" = "team" ] ; then
165                 TEAM=1
166                 continue
167             fi
168
169             if [ "${PART}" = "trunk" ]; then
170                 echo SVN-trunk-r${SVN_REV}${MODIFIED}
171                 exit 0
172             fi
173         done
174
175         if [ ${TAG} != 0 ] ; then
176             echo ${RESULT}
177         else
178             echo SVN-${RESULT##-}-r${SVN_REV}${MODIFIED}
179         fi
180     fi
181 else
182     echo "UNKNOWN__and_probably_unsupported"
183 fi