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