Reveal guile-tools's inner simplicity...
[bpt/guile.git] / build-aux / git-version-gen
CommitLineData
5ad8e59f
LC
1#!/bin/sh
2# Print a version string.
dd36ce77 3scriptversion=2011-02-19.19; # UTC
5ad8e59f 4
49114fd4 5# Copyright (C) 2007-2011 Free Software Foundation, Inc.
5ad8e59f
LC
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20# This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/.
21# It may be run two ways:
22# - from a git repository in which the "git describe" command below
23# produces useful output (thus requiring at least one signed tag)
24# - from a non-git-repo directory containing a .tarball-version file, which
25# presumes this script is invoked like "./git-version-gen .tarball-version".
26
27# In order to use intra-version strings in your project, you will need two
28# separate generated version string files:
29#
30# .tarball-version - present only in a distribution tarball, and not in
31# a checked-out repository. Created with contents that were learned at
32# the last time autoconf was run, and used by git-version-gen. Must not
33# be present in either $(srcdir) or $(builddir) for git-version-gen to
34# give accurate answers during normal development with a checked out tree,
35# but must be present in a tarball when there is no version control system.
36# Therefore, it cannot be used in any dependencies. GNUmakefile has
37# hooks to force a reconfigure at distribution time to get the value
38# correct, without penalizing normal development with extra reconfigures.
39#
40# .version - present in a checked-out repository and in a distribution
41# tarball. Usable in dependencies, particularly for files that don't
42# want to depend on config.h but do want to track version changes.
43# Delete this file prior to any autoconf run where you want to rebuild
44# files to pick up a version string change; and leave it stale to
45# minimize rebuild time after unrelated changes to configure sources.
46#
47# It is probably wise to add these two files to .gitignore, so that you
48# don't accidentally commit either generated file.
49#
50# Use the following line in your configure.ac, so that $(VERSION) will
51# automatically be up-to-date each time configure is run (and note that
52# since configure.ac no longer includes a version string, Makefile rules
53# should not depend on configure.ac for version updates).
54#
55# AC_INIT([GNU project],
56# m4_esyscmd([build-aux/git-version-gen .tarball-version]),
57# [bug-project@example])
58#
59# Then use the following lines in your Makefile.am, so that .version
60# will be present for dependencies, and so that .tarball-version will
61# exist in distribution tarballs.
62#
63# BUILT_SOURCES = $(top_srcdir)/.version
64# $(top_srcdir)/.version:
65# echo $(VERSION) > $@-t && mv $@-t $@
66# dist-hook:
67# echo $(VERSION) > $(distdir)/.tarball-version
68
69case $# in
70 1|2) ;;
71 *) echo 1>&2 "Usage: $0 \$srcdir/.tarball-version" \
72 '[TAG-NORMALIZATION-SED-SCRIPT]'
73 exit 1;;
74esac
75
76tarball_version_file=$1
77tag_sed_script="${2:-s/x/x/}"
78nl='
79'
80
0977e175
TTN
81# Avoid meddling by environment variable of the same name.
82v=
dd36ce77 83v_from_git=
0977e175 84
5ad8e59f
LC
85# First see if there is a tarball-only version file.
86# then try "git describe", then default.
87if test -f $tarball_version_file
88then
49114fd4 89 v=`cat $tarball_version_file` || v=
5ad8e59f 90 case $v in
49114fd4
LC
91 *$nl*) v= ;; # reject multi-line output
92 [0-9]*) ;;
93 *) v= ;;
5ad8e59f
LC
94 esac
95 test -z "$v" \
49114fd4 96 && echo "$0: WARNING: $tarball_version_file is missing or damaged" 1>&2
5ad8e59f
LC
97fi
98
99if test -n "$v"
100then
101 : # use $v
0f00f2c3
LC
102# Otherwise, if there is at least one git commit involving the working
103# directory, and "git describe" output looks sensible, use that to
104# derive a version string.
105elif test "`git log -1 --pretty=format:x . 2>&1`" = x \
5ad8e59f 106 && v=`git describe --abbrev=4 --match='v*' HEAD 2>/dev/null \
49114fd4 107 || git describe --abbrev=4 HEAD 2>/dev/null` \
5ad8e59f
LC
108 && v=`printf '%s\n' "$v" | sed "$tag_sed_script"` \
109 && case $v in
49114fd4
LC
110 v[0-9]*) ;;
111 *) (exit 1) ;;
5ad8e59f
LC
112 esac
113then
114 # Is this a new git that lists number of commits since the last
115 # tag or the previous older version that did not?
116 # Newer: v6.10-77-g0f8faeb
117 # Older: v6.10-g0f8faeb
118 case $v in
49114fd4
LC
119 *-*-*) : git describe is okay three part flavor ;;
120 *-*)
121 : git describe is older two part flavor
122 # Recreate the number of commits and rewrite such that the
123 # result is the same as if we were using the newer version
124 # of git describe.
125 vtag=`echo "$v" | sed 's/-.*//'`
126 commit_list=`git rev-list "$vtag"..HEAD 2>/dev/null` \
127 || { commit_list=failed;
128 echo "$0: WARNING: git rev-list failed" 1>&2; }
129 numcommits=`echo "$commit_list" | wc -l`
130 v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`;
131 test "$commit_list" = failed && v=UNKNOWN
132 ;;
5ad8e59f
LC
133 esac
134
135 # Change the first '-' to a '.', so version-comparing tools work properly.
136 # Remove the "g" in git describe's output string, to save a byte.
137 v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`;
dd36ce77 138 v_from_git=1
5ad8e59f
LC
139else
140 v=UNKNOWN
141fi
142
143v=`echo "$v" |sed 's/^v//'`
144
dd36ce77
MW
145# Test whether to append the "-dirty" suffix only if the version
146# string we're using came from git. I.e., skip the test if it's "UNKNOWN"
147# or if it came from .tarball-version.
148if test -n "$v_from_git"; then
149 # Don't declare a version "dirty" merely because a time stamp has changed.
150 git update-index --refresh > /dev/null 2>&1
5ad8e59f 151
dd36ce77
MW
152 dirty=`exec 2>/dev/null;git diff-index --name-only HEAD` || dirty=
153 case "$dirty" in
154 '') ;;
155 *) # Append the suffix only if there isn't one already.
156 case $v in
157 *-dirty) ;;
158 *) v="$v-dirty" ;;
159 esac ;;
160 esac
161fi
5ad8e59f
LC
162
163# Omit the trailing newline, so that m4_esyscmd can use the result directly.
164echo "$v" | tr -d "$nl"
165
166# Local variables:
167# eval: (add-hook 'write-file-hooks 'time-stamp)
168# time-stamp-start: "scriptversion="
169# time-stamp-format: "%:y-%02m-%02d.%02H"
170# time-stamp-time-zone: "UTC"
171# time-stamp-end: "; # UTC"
172# End: