11#! /bin/sh
22set -eu
3- rootdir=` dirname " $0 " ` /..
4- prog=` basename " $0 " `
3+ rootdir=$( dirname " $0 " ) /..
4+ prog=$( basename " $0 " )
5+
6+ # Remarks:
7+ # - This script assumes the version tag matches with "^v[0-9].*$".
8+ # The version string after the prefix "v" must be in the format in
9+ # semantic versioning.
10+ # - The option -C <path> affects only the dirty flag of $revision
11+ # (and the related variable: $version_desc) and $date,
12+ # if the referred Git repository is the same.
513
614print_usage () {
715 cat << END
@@ -14,6 +22,7 @@ Options:
1422 -r, --raw raw output (default)
1523 -c, --c C output
1624 -t, --tex TeX output
25+ -s, --shell shell script output
1726 -v, --only-version only-version output
1827 -o <file>, --output <file> output to <file>
1928 --date-format <format> date format (default: '%b %e %Y')
@@ -72,6 +81,9 @@ for a in "$@"; do
7281 -t|--tex)
7382 mode=tex
7483 ;;
84+ -s|--shell)
85+ mode=shell
86+ ;;
7587 -v|--only-version)
7688 mode=only-version
7789 ;;
@@ -92,52 +104,62 @@ if [ -n "$next" ]; then
92104 exit 1
93105fi
94106
107+ # Same as git -C "$refdir" ... but works with git < 1.8.5.
95108git_C () {
96109 (cd " $refdir " && git " $@ " )
97110}
98111
99112# Extract the version number from the latest tag, e.g.,
100113# v1.0.0-xxx-yyy-zzz -> 1.0.0
101- version_tag=` git_C describe --match ' v[0-9]*' --tags HEAD`
102- version_tmp=` echo " $version_tag " | sed ' s/^v//' `
103- version_num=` echo " $version_tmp " | sed ' s/-.*//' `
114+ version_tag=$( git_C describe --match ' v[0-9]*' --tags HEAD)
115+ version_tmp=$( echo " $version_tag " | sed ' s/^v//' )
116+ version_num=$( echo " $version_tmp " | sed ' s/-.*//' )
104117
105118version=$version_num
119+ version_suffix=
106120
107121# Support typical pre-release versions (e.g., v1.0.0-alpha-xxx-yyy-zzz) for
108122# -alpha, -alpha.1, -beta, -beta.1, -rc, -rc.1
109123case $version_tmp in
110124 * -alpha* |* -beta* |* -rc* )
111- version_tmp=` echo " $version_tmp " | sed ' s/^[^-]*-//' | sed ' s/-.*//' `
125+ version_tmp=$( echo " $version_tmp " | sed ' s/^[^-]*-//' | sed ' s/-.*//' )
112126 case $version_tmp in
113127 alpha* |beta* |rc* )
114128 version=" $version -$version_tmp "
129+ version_suffix=" -$version_tmp "
115130 ;;
116131 esac
117132 ;;
118133esac
119134
120135if [ " $mode " != " only-version" ]; then
121136 # Get the revision identifier by git-describe.
122- revision=` git_C describe --tags --always --abbrev=7 HEAD`
137+ revision=$( git_C describe --tags --always --abbrev=7 HEAD)
123138 # Check if the working tree is dirty.
124139 git_C update-index -q --refresh
125140 if git_C diff-index --quiet HEAD . ; then
126141 # If the working tree is not dirty, use the latest commit date.
127- isodate=` git_C log -1 --pretty=%ci .`
128- date=` LANG=C TZ=UTC fmt_isodate " $isodate " " $date_format " `
142+ isodate=$( git_C log -1 --pretty=%ci .)
143+ date=$( LANG=C TZ=UTC fmt_isodate " $isodate " " $date_format " )
129144 else
130145 # If the working tree is dirty, suffix "-dirty" to the revision identifier
131146 # and use the current date time.
132147 revision=" $revision -dirty"
133- date=` LANG=C TZ=UTC date +" $date_format " `
148+ date=$( LANG=C TZ=UTC date +" $date_format " )
149+ fi
150+ # Version description.
151+ # Examples: "4.3.0", "v4.3.0-1-g7c9706c"
152+ if [ " v$version " = " $revision " ]; then
153+ version_desc=$version
154+ else
155+ version_desc=$revision
134156 fi
135157 # Extract MAJOR.MINOR.PATCH from the version number.
136- major_version=` expr " $version_num " : ' \([0-9]\+\)' || :`
137- version_num=` expr " $version_num " : ' [0-9]\+\.\?\(.*\)' || :`
138- minor_version=` expr " $version_num " : ' \([0-9]\+\)' || :`
139- version_num=` expr " $version_num " : ' [0-9]\+\.\?\(.*\)' || :`
140- patch_version=` expr " $version_num " : ' \([0-9]\+\)' || :`
158+ major_version=$( expr " $version_num " : ' \([0-9]\+\)' || :)
159+ version_num=$( expr " $version_num " : ' [0-9]\+\.\?\(.*\)' || :)
160+ minor_version=$( expr " $version_num " : ' \([0-9]\+\)' || :)
161+ version_num=$( expr " $version_num " : ' [0-9]\+\.\?\(.*\)' || :)
162+ patch_version=$( expr " $version_num " : ' \([0-9]\+\)' || :)
141163 [ -z " $major_version " ] && major_version=0
142164 [ -z " $minor_version " ] && minor_version=0
143165 [ -z " $patch_version " ] && patch_version=0
@@ -148,31 +170,49 @@ print_versions() {
148170 raw)
149171 cat << END
150172$version
173+ $version_desc
151174$revision
152175$date
153176$major_version
154177$minor_version
155178$patch_version
179+ $version_suffix
156180END
157181 ;;
158182 c)
159183 cat << END
160- #define REPO_VERSION "$version "
161- #define REPO_REVISION "$revision "
162- #define REPO_DATE "$date "
163- #define REPO_MAJOR_VERSION $major_version
164- #define REPO_MINOR_VERSION $minor_version
165- #define REPO_PATCH_VERSION $patch_version
184+ #define REPO_VERSION "$version "
185+ #define REPO_VERSION_DESC "$version_desc "
186+ #define REPO_REVISION "$revision "
187+ #define REPO_DATE "$date "
188+ #define REPO_MAJOR_VERSION $major_version
189+ #define REPO_MINOR_VERSION $minor_version
190+ #define REPO_PATCH_VERSION $patch_version
191+ #define REPO_VERSION_SUFFIX "$version_suffix "
166192END
167193 ;;
168194 tex)
169195 cat << END
170196\def\repoversion{$version }
197+ \def\repoversiondesc{$version_desc }
171198\def\reporevision{$revision }
172199\def\repodate{$date }
173200\def\repomajorversion{$major_version }
174201\def\repominorversion{$minor_version }
175202\def\repopatchversion{$patch_version }
203+ \def\repoversionsuffix{$version_suffix }
204+ END
205+ ;;
206+ shell)
207+ cat << END
208+ repo_version='$version '
209+ repo_version_desc='$version_desc '
210+ repo_revision='$revision '
211+ repo_date='$date '
212+ repo_major_version='$major_version '
213+ repo_minor_version='$minor_version '
214+ repo_patch_version='$patch_version '
215+ repo_version_suffix='$version_suffix '
176216END
177217 ;;
178218 only-version)
@@ -197,7 +237,7 @@ if [ -z "$output_file" ]; then
197237else
198238 # To the output file. Write only if any changes required.
199239 if [ -f " $output_file " ]; then
200- out=` print_versions`
240+ out=$( print_versions)
201241 # NOTE: using echo instead of say at the next line may have problems
202242 # for --tex. POSIX says echo should process '\\', but some shells
203243 # don't without -e option. Here we shouldn't process them.
0 commit comments