Debian Codegraph @

build-aux/git-version-gen

raw
1 #!/bin/sh
2 # Print a version string.
3 scriptversion=2018-08-31.20; # UTC
4
5 # Copyright (C) 2012-2026 Red Hat, Inc.
6 # Copyright (C) 2007-2016 Free Software Foundation, Inc.
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 # This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/.
22 # It may be run two ways:
23 # - from a git repository in which the "git describe" command below
24 # produces useful output (thus requiring at least one signed tag)
25 # - from a non-git-repo directory containing a .tarball-version file, which
26 # presumes this script is invoked like "./git-version-gen .tarball-version".
27
28 # In order to use intra-version strings in your project, you will need two
29 # separate generated version string files:
30 #
31 # .tarball-version - present only in a distribution tarball, and not in
32 # a checked-out repository. Created with contents that were learned at
33 # the last time autoconf was run, and used by git-version-gen. Must not
34 # be present in either $(srcdir) or $(builddir) for git-version-gen to
35 # give accurate answers during normal development with a checked out tree,
36 # but must be present in a tarball when there is no version control system.
37 # Therefore, it cannot be used in any dependencies. GNUmakefile has
38 # hooks to force a reconfigure at distribution time to get the value
39 # correct, without penalizing normal development with extra reconfigures.
40 #
41 # .version - present in a checked-out repository and in a distribution
42 # tarball. Usable in dependencies, particularly for files that don't
43 # want to depend on config.h but do want to track version changes.
44 # Delete this file prior to any autoconf run where you want to rebuild
45 # files to pick up a version string change; and leave it stale to
46 # minimize rebuild time after unrelated changes to configure sources.
47 #
48 # As with any generated file in a VC'd directory, you should add
49 # /.version to .gitignore, so that you don't accidentally commit it.
50 # .tarball-version is never generated in a VC'd directory, so needn't
51 # be listed there.
52 #
53 # In order to use git archive versions another two files has to be presented:
54 #
55 # .gitarchive-version - present in checked-out repository and git
56 # archive tarball, but not in the distribution tarball. Used as a last
57 # option for version. File must contain special string $Format:%d$,
58 # which is substitued by git on archive operation.
59 #
60 # .gitattributes - present in checked-out repository and git archive
61 # tarball, but not in the distribution tarball. Must set export-subst
62 # attribute for .gitarchive-version file.
63 #
64 # Use the following line in your configure.ac, so that $(VERSION) will
65 # automatically be up-to-date each time configure is run (and note that
66 # since configure.ac no longer includes a version string, Makefile rules
67 # should not depend on configure.ac for version updates).
68 #
69 # AC_INIT([GNU project],
70 # m4_esyscmd([build-aux/git-version-gen .tarball-version]),
71 # [bug-project@example])
72 #
73 # Then use the following lines in your Makefile.am, so that .version
74 # will be present for dependencies, and so that .version and
75 # .tarball-version will exist in distribution tarballs.
76 #
77 # EXTRA_DIST = $(top_srcdir)/.version
78 # BUILT_SOURCES = $(top_srcdir)/.version
79 # $(top_srcdir)/.version:
80 # echo $(VERSION) > $@-t && mv $@-t $@
81 # dist-hook:
82 # echo $(VERSION) > $(distdir)/.tarball-version
83
84
85 me=$0
86
87 version="git-version-gen $scriptversion
88
89 Copyright 2011 Free Software Foundation, Inc.
90 There is NO warranty. You may redistribute this software
91 under the terms of the GNU General Public License.
92 For more information about these matters, see the files named COPYING."
93
94 usage="\
95 Usage: $me [OPTION]... \$srcdir/.tarball-version [\$srcdir/.gitarchive-version] [TAG-NORMALIZATION-SED-SCRIPT]
96 Print a version string.
97
98 Options:
99
100 --prefix PREFIX prefix of git tags (default 'v')
101 --fallback VERSION
102 fallback version to use if \"git --version\" fails
103
104 --help display this help and exit
105 --version output version information and exit
106
107 Running without arguments will suffice in most cases."
108
109 prefix=v
110 fallback=
111
112 while test $# -gt 0; do
113 case $1 in
114 --help) echo "$usage"; exit 0;;
115 --version) echo "$version"; exit 0;;
116 --prefix) shift; prefix="$1";;
117 --fallback) shift; fallback="$1";;
118 -*)
119 echo "$0: Unknown option '$1'." >&2
120 echo "$0: Try '--help' for more information." >&2
121 exit 1;;
122 *)
123 if test "x$tarball_version_file" = x; then
124 tarball_version_file="$1"
125 elif test "x$gitarchive_version_file" = x; then
126 gitarchive_version_file="$1"
127 elif test "x$tag_sed_script" = x; then
128 tag_sed_script="$1"
129 else
130 echo "$0: extra non-option argument '$1'." >&2
131 exit 1
132 fi;;
133 esac
134 shift
135 done
136
137 if test "x$tarball_version_file" = x; then
138 echo "$usage"
139 exit 1
140 fi
141
142 tag_sed_script="${tag_sed_script:-s/x/x/}"
143
144 nl='
145 '
146
147 # Avoid meddling by environment variable of the same name.
148 v=
149 v_from_git=
150
151 # First see if there is a tarball-only version file.
152 # then try "git describe", then default.
153 if test -f $tarball_version_file
154 then
155 v=`cat $tarball_version_file` || v=
156 case $v in
157 *$nl*) v= ;; # reject multi-line output
158 [0-9]*) ;;
159 *) v= ;;
160 esac
161 test "x$v" = x \
162 && echo "$0: WARNING: $tarball_version_file is missing or damaged" 1>&2
163 fi
164
165 if test "x$v" != x
166 then
167 : # use $v
168 # Otherwise, if there is at least one git commit involving the working
169 # directory, and "git describe" output looks sensible, use that to
170 # derive a version string.
171 elif test "`git log -1 --pretty=format:x . 2>&1`" = x \
172 && v=`git describe --abbrev=4 --match="$prefix*" HEAD 2>/dev/null \
173 || git describe --abbrev=4 HEAD 2>/dev/null` \
174 && v=`printf '%s\n' "$v" | sed "$tag_sed_script"` \
175 && case $v in
176 $prefix[0-9]*) ;;
177 *) (exit 1) ;;
178 esac
179 then
180 # Is this a new git that lists number of commits since the last
181 # tag or the previous older version that did not?
182 # Newer: v6.10-77-g0f8faeb
183 # Older: v6.10-g0f8faeb
184 case $v in
185 *-*-*) : git describe is okay three part flavor ;;
186 *-*)
187 : git describe is older two part flavor
188 # Recreate the number of commits and rewrite such that the
189 # result is the same as if we were using the newer version
190 # of git describe.
191 vtag=`echo "$v" | sed 's/-.*//'`
192 commit_list=`git rev-list "$vtag"..HEAD 2>/dev/null` \
193 || { commit_list=failed;
194 echo "$0: WARNING: git rev-list failed" 1>&2; }
195 numcommits=`echo "$commit_list" | wc -l`
196 v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`;
197 test "$commit_list" = failed && v=UNKNOWN
198 ;;
199 esac
200
201 # Change the first '-' to a '.', so version-comparing tools work properly.
202 # Remove the "g" in git describe's output string, to save a byte.
203 v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`;
204 v_from_git=1
205 elif test "x$fallback" = x || git --version >/dev/null 2>&1; then
206 if test -f $gitarchive_version_file
207 then
208 v=`sed "s/^.*tag: \($prefix[0-9)][^,)]*\).*\$/\1/" $gitarchive_version_file \
209 | sed "$tag_sed_script"` || exit 1
210 case $v in
211 *$nl*) v= ;; # reject multi-line output
212 $prefix[0-9]*) ;;
213 *) v= ;;
214 esac
215
216 test -z "$v" \
217 && echo "$0: WARNING: $gitarchive_version_file doesn't contain valid version tag" 1>&2 \
218 && v=UNKNOWN
219 elif test "x$fallback" = x; then
220 v=UNKNOWN
221 else
222 v=$fallback
223 fi
224 else
225 v=$fallback
226 fi
227
228 if test "x$fallback" = x -a "$v" = "UNKNOWN"
229 then
230 echo "$0: ERROR: Can't find valid version. Please use valid git repository," \
231 "released tarball or version tagged archive" 1>&2
232
233 exit 1
234 fi
235
236 v=`echo "$v" |sed "s/^$prefix//"`
237
238 # Test whether to append the "-dirty" suffix only if the version
239 # string we're using came from git. I.e., skip the test if it's "UNKNOWN"
240 # or if it came from .tarball-version.
241 if test "x$v_from_git" != x; then
242 # Don't declare a version "dirty" merely because a time stamp has changed.
243 git update-index --refresh > /dev/null 2>&1
244
245 dirty=`exec 2>/dev/null;git diff-index --name-only HEAD` || dirty=
246 case "$dirty" in
247 '') ;;
248 *) # Append the suffix only if there isn't one already.
249 case $v in
250 *-dirty) ;;
251 *) v="$v-dirty" ;;
252 esac ;;
253 esac
254 fi
255
256 # Omit the trailing newline, so that m4_esyscmd can use the result directly.
257 printf %s "$v"
258
259 # Local variables:
260 # eval: (add-hook 'write-file-hooks 'time-stamp)
261 # time-stamp-start: "scriptversion="
262 # time-stamp-format: "%:y-%02m-%02d.%02H"
263 # time-stamp-time-zone: "UTC0"
264 # time-stamp-end: "; # UTC"
265 # End: