Minor tweak for Solaris /bin/sh compatibility.
[sbcl.git] / generate-version.sh
1 #!/bin/sh
2 # Not a shell script, but something intended to be sourced from shell scripts
3 git_available_p() {
4     # Check that (1) we have git (2) this is a git tree.
5     if ( which git >/dev/null 2>/dev/null && git describe >/dev/null 2>/dev/null )
6     then
7         echo "ok"
8     else
9         echo ""
10     fi
11 }
12
13 generate_version() {
14     AVAILABLE=`git_available_p`
15     if [ -f version.lisp-expr -a -z "$AVAILABLE" ]
16     then
17         # Relase tarball, leave version.lisp-expr alone.
18         return
19     elif [ -z "$AVAILABLE" ]
20     then
21         echo "Can't run 'git describe' and version.lisp-expr is missing." >&2
22         echo "To fix this, either install git or create a fake version.lisp-expr file." >&2
23         echo "You can create a fake version.lisp-expr file like this:" >&2
24         echo "    \$ echo '\"1.0.99.999\"' > version.lisp-expr" >&2
25         exit 1
26     fi
27     # Build it.
28     version_head=`git rev-parse HEAD`
29     if [ -z "$SBCL_BUILDING_RELEASE_FROM" ]
30     then
31         version_root="origin/master"
32     else
33         version_root="$SBCL_BUILDING_RELEASE_FROM"
34     fi
35     version_base=`git rev-parse "$version_root"`
36     version_tag=`git describe --tags --match="sbcl*" --abbrev=0 $version_base`
37     version_release=`echo $version_tag | sed -e 's/sbcl[_-]//' | sed -e 's/_/\./g'`
38     # Using wc -l instead of --count argument to rev-list because
39     # pre-1.7.2 Gits are still common out in the wilderness.
40     version_n_root=`git rev-list $version_base --not $version_tag | wc -l`
41     version_n_branch=`git rev-list HEAD --not $version_base | wc -l`
42     if [ -z "$NO_GIT_HASH_IN_VERSION" ]
43     then
44         version_hash="-`git rev-parse --short $version_head`"
45     else
46         version_hash=""
47     fi
48     if git diff HEAD --no-ext-diff --quiet --exit-code
49     then
50         version_dirty=""
51     else
52         version_dirty="-dirty"
53     fi
54     # Now that we have all the pieces, put them together.
55     cat >version.lisp-expr <<EOF
56 ;;; This file is auto-generated using generate-version.sh. Every time
57 ;;; you re-run make.sh, this file will be overwritten if you are
58 ;;; working from a Git checkout.
59 EOF
60     if [ "$version_base" = "$version_head" ]
61     then
62         if [ "0" = "$version_n_root" ]
63         then
64             printf "\"%s%s\"\n" \
65                 $version_release $version_dirty >>version.lisp-expr
66         else
67             printf "\"%s.%s%s%s\"\n" \
68                 $version_release $version_n_root \
69                 $version_hash $version_dirty >>version.lisp-expr
70         fi
71     else
72         echo "base=$version_base"
73         echo "head=$version_head"
74         version_branchname=`git describe --contains --all HEAD`
75         printf "\"%s.%s.%s.%s%s%s\"\n" \
76             $version_release $version_n_root \
77             $version_branchname $version_n_branch \
78             $version_hash $version_dirty >>version.lisp-expr
79     fi
80 }