]> sourceware.org Git - systemtap.git/blob - git_version.sh
Version bumps for 0.9.6 release
[systemtap.git] / git_version.sh
1 #!/bin/sh
2 #
3 # Generate some basic versioning information which can be piped to a header.
4 #
5 # Copyright (c) 2006-2007 Luc Verhaegen <libv@skynet.be>
6 # Copyright (C) 2007 Hans Ulrich Niedermann <hun@n-dimensional.de>
7 #
8 # Permission is hereby granted, free of charge, to any person obtaining a
9 # copy of this software and associated documentation files (the "Software"),
10 # to deal in the Software without restriction, including without limitation
11 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 # and/or sell copies of the Software, and to permit persons to whom the
13 # Software is furnished to do so, subject to the following conditions:
14 #
15 # The above copyright notice and this permission notice shall be included in
16 # all copies or substantial portions of the Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 # THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 # OTHER DEALINGS IN THE SOFTWARE.
25 #
26 # This script is based on the one written for xf86-video-unichrome by
27 # Luc Verhaegen, but was rewritten almost completely by Hans Ulrich
28 # Niedermann. The script contains a few bug fixes from Egbert Eich,
29 # Matthias Hopf, Joerg Sonnenberger, and possibly others.
30 #
31 # The author thanks the nice people on #git for the assistance.
32 #
33 # Simple testing of this script:
34 # /sbin/busybox sh git_version.sh --example > moo.c \
35 # && gcc -Wall -Wextra -Wno-unused -o moo moo.c \
36 # && ./moo
37 # (bash should also do)
38 #
39 # For how to hook this up to your automake- and/or imake-based build
40 # system, best take a look at how the RadeonHD.am and/or RadeonHD.tmpl
41 # work in the xf86-video-radeonhd build system. For non-recursive make,
42 # you can probably make things a little bit simpler.
43 #
44 # KNOWN BUGS:
45 # * Uses hyphenated ("git-foo-bar") program names, which git upstream
46 # have declared deprecated.
47 #
48
49 # Help messages
50 USAGE="[<option>...]"
51 LONG_USAGE="\
52 Options:
53 -h, --help Print this help message.
54
55 -k, --keep-if-no-repo Keep old output file if no git repo found.
56 -o, --output FILENAME Set output file name.
57 -q, --quiet Quiet output.
58 -s, --srcdir DIRNAME Set source tree dir name.
59 -x, --example Print complete example program."
60
61 # The caller may have set these for us
62 SED="${SED-sed}"
63
64 # Initialize
65 working_dir="$(pwd)"
66
67 # Who am I?
68 self="$(basename "$0")"
69
70 # Defaults
71 ifndef_symbol="GIT_VERSION_H"
72 outfile="-"
73 print_example=false
74 keep_if_no_repo=no
75 quiet=false
76 srcdir="$(pwd)"
77
78 # Parse command line parameter, affecting defaults
79 while [ "x$1" != "x" ]
80 do
81 case "$1" in
82 -x|--example)
83 print_example=:
84 ;;
85 -o|--output)
86 if shift; then
87 outfile="$1"
88 if [ "x$outfile" = "x-" ]; then
89 : # keep default ifndef_symbol
90 else
91 ifndef_symbol=`basename "$outfile" | $SED 's|\.|_|g; s|[^A-Za-z0-9_]||g' | tr a-z A-Z`
92 fi
93 else
94 echo "$self: Fatal: \"$1\" option requires parameter." >&2
95 exit 1
96 fi
97 ;;
98 -q|--quiet)
99 quiet=:
100 ;;
101 -h|--help)
102 echo "Usage: ${self} $USAGE"
103 [ -n "$LONG_USAGE" ] && echo "$LONG_USAGE"
104 exit
105 ;;
106 -k|--keep-if-no-repo)
107 keep_if_no_repo=yes
108 ;;
109 -s|--srcdir)
110 if shift; then
111 if test -d "$1"; then
112 srcdir="$1"
113 else
114 echo "$self: Fatal: \"$1\" not a directory." >&2
115 exit 1
116 fi
117 else
118 echo "$self: Fatal: \"$1\" option requires directory parameter." >&2
119 exit 1
120 fi
121 ;;
122 *)
123 echo "$self: Fatal: Invalid command line paramenter: \"$1\"" >&2
124 exit 1
125 ;;
126 esac
127 shift
128 done
129
130 # If not printing to stdout, redirect stdout to output file
131 rename_new_output=false
132 if [ "x$outfile" = "x-" ]
133 then
134 : # keep using stdout
135 else
136 exec 1> "${outfile}.new"
137 fi
138
139 # Done with creating output files, so we can change to source dir
140 abs_srcdir="$(cd "$srcdir" && pwd)"
141 cd "$srcdir"
142
143 # Write program header
144 cat<<EOF
145 /*
146 * Basic versioning gathered from the git repository.
147 * Automatically generated by $0.
148 */
149
150 #ifndef ${ifndef_symbol}
151 #define ${ifndef_symbol} 1
152
153 /* whether this is a dist tarball or not */
154 #undef GIT_IS_DIST
155
156 EOF
157
158 # Backwards compatibility hack for git 1.6.0 (and people running the
159 # latest pre-release version of git.)
160 #
161 # For now, we'll do this to support git 1.6, with minimal changes to
162 # the rest of this script, but no guarantees how long this will work.
163 # The hyphenated git-foo-bar names really are deprecated, and may
164 # disappear in the future as more of git gets rewritten as built-in C
165 # programs. Google summer of code students and other git developers
166 # are hard at work doing this, in order to make git more
167 # portable/usable for Windows users. As a result, some of the
168 # git-foo-bar programs, which will be moved to the exec-dir directory
169 # in git 1.6, may disappear altogether in the future. Hence the only
170 # truly safe and future-compatible way of running commands such as
171 # git-diff-files, git-rev-parse, etc., are "git diff-files" and
172 # "git rev-parse". Here endeth the git portability sermon, which
173 # I suspect will have as much effect as abstinence-only sex ed
174 # classes. :-) TYT, 2008-07-08
175
176 execdir=$(git --exec-path 2> /dev/null)
177 if test -n "$execdir"; then
178 PATH=$PATH:$execdir
179 fi
180
181 # Detect git tools (should work with old and new git versions)
182 git_found=yes
183 for git_tool in git-symbolic-ref git-rev-parse git-diff-files git-diff-index git git-describe
184 do
185 if [ x`which $git_tool 2>/dev/null` = "x" ]; then
186 git_found="'$git_tool' not found"
187 break
188 fi
189 done
190
191 # Determine git specific defines
192 unset git_errors ||:
193 if [ "x$git_found" = "xyes" ]; then
194 git_version=`git --version`
195 if [ "x$git_version" = "x" ]; then
196 git_errors="${git_errors+${git_errors}; }error running 'git --version'"
197 fi
198 fi
199
200 git_repo=no
201 # "git-rev-parse --git-dir" since git-0.99.7
202 git_repo_dir="$(git-rev-parse --git-dir 2> /dev/null || true)"
203 abs_repo_dir="$(cd "$git_repo_dir" && pwd)"
204 # Only accept the found git repo iff it is in our top srcdir, as determined
205 # by comparing absolute pathnames creaged by running pwd in the respective dir.
206 if [ "x$git_repo_dir" != "x" ] && [ "x${abs_repo_dir}" = "x${abs_srcdir}/.git" ]; then
207 git_repo=yes
208 if [ "x$git_found" = "xyes" ]; then
209 # git-1.4 and probably earlier understand "git-rev-parse HEAD"
210 git_shaid=`git-describe`
211 if [ "x$git_shaid" = "x" ]; then
212 git_errors="${git_errors+${git_errors}; }error running 'git-rev-parse HEAD'"
213 fi
214 # git-1.4 and probably earlier understand "git-symbolic-ref HEAD"
215 git_branch=`git-symbolic-ref HEAD | $SED -n 's|^refs/heads/||p'`
216 if [ "x$git_branch" = "x" ]; then
217 # This happens, is OK, and "(no branch)" is what "git branch" prints.
218 git_branch="(no branch)"
219 fi
220 git_dirty=yes
221 # git-1.4 does not understand "git-diff-files --quiet"
222 # git-1.4 does not understand "git-diff-index --cached --quiet HEAD"
223 if [ "x$(git-diff-files)" = "x" ] && [ "x$(git-diff-index --cached HEAD)" = "x" ]; then
224 git_dirty=no
225 fi
226 fi
227 fi
228
229 # Write git specific defines
230 if [ "x$git_errors" = "x" ]; then
231 echo "/* No errors occured while running git */"
232 echo "#undef GIT_ERRORS"
233 else
234 echo "/* Some errors occured while running git */"
235 echo "#define GIT_ERRORS \"${git_errors}\""
236 fi
237 echo ""
238
239 if [ "x$git_found" = "xyes" ]; then
240 echo "/* git utilities found */"
241 echo "#undef GIT_NOT_FOUND"
242 echo "#define GIT_VERSION \"${git_version}\""
243 else
244 echo "/* git utilities not found */"
245 echo "#define GIT_NOT_FOUND \"${git_found}\""
246 echo "#undef GIT_VERSION"
247 fi
248 echo ""
249
250 if [ "x$git_repo" = "xno" ]; then
251 echo "/* No git repo found, probably building from dist tarball */"
252 echo "#undef GIT_REPO"
253 else
254 echo "/* git repo found */"
255 echo "#define GIT_REPO 1"
256 echo ""
257 if [ "x$git_found" = "xyes" ]; then
258 echo "/* Git SHA ID of last commit */"
259 echo "#define GIT_SHAID \"${git_shaid}\""
260 echo ""
261
262 echo "/* Branch this tree is on */"
263 echo "#define GIT_BRANCH \"$git_branch\""
264 echo ""
265
266 # Any uncommitted changes we should know about?
267 # Or technically: Are the working tree or index dirty?
268 if [ "x$git_dirty" = "xno" ]; then
269 echo "/* SHA-ID uniquely defines the state of this code */"
270 echo "#undef GIT_DIRTY"
271 else
272 echo "/* Local changes might be breaking things */"
273 echo "#define GIT_DIRTY 1"
274 fi
275 fi
276 fi
277
278 # Define a few immediately useful message strings
279 cat<<EOF
280
281 /* Define GIT_MESSAGE such that
282 * printf("%s: built from %s", argv[0], GIT_MESSAGE);
283 * forms a proper sentence.
284 */
285
286 #ifdef GIT_DIRTY
287 # define GIT_DIRTY_MSG " + changes"
288 #else /* !GIT_DIRTY */
289 # define GIT_DIRTY_MSG ""
290 #endif /* GIT_DIRTY */
291
292 #ifdef GIT_ERRORS
293 # define GIT_ERROR_MSG " with error: " GIT_ERRORS
294 #else /* !GIT_ERRORS */
295 # define GIT_ERROR_MSG ""
296 #endif /* GIT_ERRORS */
297
298 #ifdef GIT_IS_DIST
299 # define GIT_DIST_MSG "dist of "
300 #else /* !GIT_IS_DIST */
301 # define GIT_DIST_MSG ""
302 #endif /* GIT_IS_DIST */
303
304 #ifdef GIT_REPO
305 # ifdef GIT_NOT_FOUND
306 # define GIT_MESSAGE GIT_DIST_MSG "git sources without git: " GIT_NOT_FOUND
307 # else /* !GIT_NOT_FOUND */
308 # define GIT_MESSAGE \\
309 GIT_DIST_MSG \\
310 "commit " GIT_SHAID GIT_DIRTY_MSG \\
311 GIT_ERROR_MSG
312 # endif /* GIT_NOT_FOUND */
313 #else /* !GIT_REPO */
314 # define GIT_MESSAGE GIT_DIST_MSG "non-git sources" GIT_ERROR_MSG
315 #endif /* GIT_REPO */
316
317 #endif /* ${ifndef_symbol} */
318 EOF
319
320 # Example program
321 if "$print_example"
322 then
323 cat<<EOF
324
325 /* example program demonstrating the use of git_version.sh output */
326 #include <stdio.h>
327 #include <string.h>
328
329 #ifdef HAVE_CONFIG_H
330 # include "config.h"
331 #endif
332
333 int main(int argc, char *argv[])
334 {
335 const char *const idx = strrchr(argv[0], '/');
336 const char *const prog = (idx)?(idx+1):(argv[0]);
337 #ifdef PACKAGE_VERSION
338 printf("%s: version %s, built from %s\n", prog, PACKAGE_VERSION, GIT_MESSAGE);
339 #elif defined(GIT_MESSAGE)
340 printf("%s: built from %s\n", prog, GIT_MESSAGE);
341 #endif
342 return 0;
343 }
344 EOF
345 fi
346
347 # Change back to working dir for the remaining output file manipulations.
348 cd "$working_dir"
349
350 # If necessary, overwrite outdated output file with new one
351 if [ "x$outfile" != "x-" ]
352 then
353 if [ -f "$outfile" ]; then
354 if [ "x$keep_if_no_repo" = "xyes" ] && [ "x$git_repo" = "xno" ]; then
355 "$quiet" || echo "$self: Not a git repo, keeping existing $outfile" >&2
356 rm -f "$outfile.new"
357 elif cmp "$outfile" "$outfile.new" > /dev/null; then
358 "$quiet" || echo "$self: Output is unchanged, keeping $outfile" >&2
359 rm -f "$outfile.new"
360 else
361 echo "$self: Output has changed, updating $outfile" >&2
362 mv -f "$outfile.new" "$outfile"
363 fi
364 else
365 echo "$self: Output is new file, creating $outfile" >&2
366 mv -f "$outfile.new" "$outfile"
367 fi
368 fi
369
370 # THE END.
This page took 0.057367 seconds and 5 git commands to generate.