4 # Copyright 2011-2016 Nest Labs Inc. All Rights Reserved.
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
21 # This file attempts to find and generate a package version
22 # including, if necessary, the number of commits from the last
23 # GIT tag and the current GIT hash corresponding to HEAD for the
26 # This is largely cobbled together from similar scripts in other
27 # packages that are maintained in GIT (linux, u-boot, parted, etc.).
29 # This can produce version information such as:
33 # 1.0.1-00032-gab50dbb
34 # 1.0.1-00032-gab50dbb-dirty
42 DATADIR=${ROOTDIR}share
43 DEVICEDIR=${ROOTDIR}dev
46 LIBEXECDIR=${ROOTDIR}libexec
50 SBINDIR=${ROOTDIR}sbin
53 USRBINDIR=${USRDIR}/bin
54 USRDATADIR=${USRDIR}/share
55 USRLIBDIR=${USRDIR}/lib
56 USRLIBEXECDIR=${USRDIR}/libexec
57 USRSBINDIR=${USRDIR}/sbin
60 BASENAME=${USRBINDIR}/basename
63 NULL=${DEVICEDIR}/null
64 PRINTF=${USRBINDIR}/printf
74 # This routine prints out the proper command line usage for this
75 # program and then exits with the specified status.
78 # status - Exit status to exit the program with.
81 # This subroutine does not return.
84 local name=`${BASENAME} ${0}`
86 ${ECHO} "Usage: ${name} [options] [ <project root> ]"
88 if [ ${1} -ne 0 ]; then
89 ${ECHO} "Try '${name} -h' for more information."
92 if [ ${1} -ne 1 ]; then
94 -b, --build-version=VERSION Specify VERSION as the build version to generate
95 extra build information against.
96 -h, --help Print this help, then exit.
104 # gitversion <string> <directory> <version>
107 # This routine prints out any GIT version information appended to the
108 # end of the package version, including the number of commits from
109 # the last GIT tag and the current GIT hash corresponding to HEAD
110 # for the current branch.
113 # string - The current version string which may be empty.
114 # directory - The current directory.
115 # version - The optional current package version.
129 # Retrieve the shortened, unique GIT hash associated with the
132 head=`test -d .git && git rev-parse --verify --short HEAD 2> ${NULL}`
134 # If we found a hash, we are actually in a GIT repository; continue.
136 if [ -n "${head}" ]; then
137 # Check to see if we have a position in GIT that is
138 # exactly at an existing tag (e.g. 1.0.2). If we are,
139 # just use it and add a dirty qualifier. Otherwise,
140 # work through the logic to determine how far off the
143 exact="`git describe --exact-match 2> ${NULL}`"
145 if [ -z "${exact}" ] || [ -n "${version}" ] && [ "${version}" != "${exact}" ]; then
146 dtag="`git describe 2> ${NULL}`"
148 # If we are n commits away from a tag, then
149 # print n and a shortened version of the
150 # hash. Otherwise, just print the hash.
152 # If we are at an exact version, then there
153 # won't be a delta or a hash, just use the
156 if [ -n "${dtag}" ]; then
157 if [ "${dtag}" == "${exact}" ]; then
160 gitversion=`${PRINTF} "${dtag}" | ${AWK} -F '-' '{printf("%s-%05d-%s", $(NF-2),$(NF-1),$(NF))}' 2> ${NULL}`
164 gitversion=`${PRINTF} "g${head}"`
169 gitversion="${exact}"
173 # Update the index if we are in a writable directory
174 # so that we can successfully check for a dirty (has
175 # uncommitted changes or unresolved merges) tree.
177 if [ -w "${dir}" ]; then
178 git update-index --refresh --unmerged > ${NULL}
181 # Now check for such a dirty tree and add to the "string"
184 if git diff-index --name-only HEAD | read dummy; then
185 if [ -n "${gitversion}" ]; then
186 gitversion="${gitversion}-dirty"
193 gitversion="${version}"
197 if [ -n "${string}" ] && [ -n "${gitversion}" ]; then
198 string="${string}-${gitversion}"
200 string="${gitversion}"
203 ${PRINTF} "${string}"
210 while [ ${#} -gt 0 ]; do
211 if [ ${1:0:1} == "-" ]; then
212 if [ "${1}" == "-h" ] || [ "${1}" == "--help" ]; then
215 elif [ "${1}" == "-b" ] || [ "${1}" == "--build-version" ]; then
220 ${ECHO} "Unknown argument '${1}'."
231 if [ ${#} -gt 1 ]; then
233 elif [ ${#} -eq 1 ]; then
239 if [ "${tree}" != "." ]; then
243 VERSION="`gitversion \"${VERSION}\" . ${version}`"
245 ${PRINTF} "${VERSION}"