1#!/bin/sh
2
3# This script outputs additional MPFR version information for a
4# Git working tree (Git branch or "(none)", total commit count,
5# commit id, and whether the current HEAD is modified). It is
6# called in tests/Makefile.am for "make check", but may be used
7# by other tools that need such information (other tests may be
8# needed; see tests/Makefile.am as an example of use).
9# Note that this does not replace version information found in
10# the VERSION file, which may still need to be output in addition
11# to the output of this script.
12
13set -e
14
15if [ "x`git rev-parse --is-inside-work-tree 2> /dev/null`" != xtrue ]; then
16  echo "$0: This script should be executed from a Git working tree." >&2
17  exit 1
18fi
19
20# Normally passed by tests/Makefile.am with "GREP=$(GREP) SED=$(SED)".
21GREP=${GREP:-grep}
22SED=${SED:-sed}
23
24# Note: for the branch detection, in the case of a detached HEAD state,
25# the commit may appear in multiple branches, i.e. which diverge after
26# the commit; thus we exclude branches created after this commit, based
27# on <branch>-root tags (such a tag should be added by the user when
28# creating a branch, so that "git diff <branch>-root" shows commits done
29# in the branch since its creation, etc.). If $gitb contains multiple
30# branches, this means that something is probably wrong with the tags
31# or the branches (merged branches should be deleted).
32
33git tag --contains | $SED -n 's/-root$//p' > excluded-branches
34gitb=`git branch --format='%(refname:short)' --contains | \
35        $SED 's,(HEAD detached at origin/\(.*\)),\1,' | \
36        $GREP -v '^(' | $GREP -v -F -f excluded-branches -x || true`
37rm excluded-branches
38gitc=`git rev-list --count HEAD`
39gith=`git rev-parse --short HEAD`
40gitm=`git update-index -q --refresh; git diff-index --name-only HEAD`
41echo "${gitb:-(none)}-$gitc-$gith${gitm:+ (modified)}"
42
43# References:
44#   https://stackoverflow.com/q/3882838/3782797
45#   https://stackoverflow.com/a/3899339/3782797
46#     for the "git diff-index --name-only HEAD" solution, but this
47#     is not sufficient, because autogen.sh modifies the "INSTALL"
48#     and "doc/texinfo.tex" files (due to "autoreconf -f -i"), and
49#     restores them. On needs:
50#   https://stackoverflow.com/q/3882838/3782797#comment121636904_3899339
51#     suggesting "git update-index -q --refresh" first.
52