Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 8f601b5

Browse files
wkingrphillips
authored andcommitted
build/git-version: Only call 'git' once (#951)
Simplify the logic from 04848d1 (Add release build process, 2016-05-27, #47) to: * Move from Bash to a generic POSIX shell. This makes the script more portable (it will work on POSIX systems without Bash). It does mean we need to drop the 'set' (which is not in POSIX [1]), but that's not a major problem because: * We can use &&-chaining instead of 'set -e'. * We do only have the one locally-defined variable, so we don't need 'set -u'. * The simpler logic has no pipes, so 'set -o pipefail' would no longer matter. * Replace the previous multiple Git calls with a single call to 'git describe'. * Use --dirty so we can drop the previous diff call. * Drop --tags, because we don't want to use lightweight (non-annotated) tags. We want to use annotated tags, because we want to use signed release tags. There's no code check for tag signatures here, but RELEASING.md requests 'tag -s vX.Y.Z', which will create signed, annotated tags. * Git's SHA-1 hashes are 40 hex digits, but I've used --abbrev=100 to get unabbreviated hashes even in the face of future hash transitions [2]. This is a bit of a hack compared to the previous approach, because at some point Git may have >100-char hashes. But if that happens, it will be far enough in the future that I think the simplification we get here is worth it (and this constant is easy to bump if we need to). * The new call always returns the most recent tag, and (for commits which are not exact tag matches) also includes the abbreviated commit. To isolate the hash (and possible dirty-mark) for commits that are not exact tags, I use POSIX's ${parameter##[word]} prefix-pattern removal [3]. Exact tags are unlikely to include '-g' (unless it is in the tag itself), so for them the prefix-removal is a no-op. Again, this is a bit of a hack, since a tag might include '-g', but I think that's unlikely enough (as mentioned above, RELEASING.md requests vX.Y.Z for tags) that the simplification we get here is worth it. [1]: http://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html [2]: https://github.com/git/git/blob/v2.16.3/Documentation/technical/hash-function-transition.txt [3]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
1 parent 101f003 commit 8f601b5

File tree

1 file changed

+3
-21
lines changed

1 file changed

+3
-21
lines changed

build/git-version.sh

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
1-
#!/usr/bin/env bash
2-
set -euo pipefail
1+
#!/bin/sh
32

4-
# parse the current git commit hash
5-
COMMIT=$(git rev-parse HEAD)
6-
7-
# check if the current commit has a matching tag
8-
TAG=$(git describe --exact-match --abbrev=0 --tags ${COMMIT} 2> /dev/null || true)
9-
10-
# use the matching tag as the version, if available
11-
if [ -z "$TAG" ]; then
12-
VERSION=$COMMIT
13-
else
14-
VERSION=$TAG
15-
fi
16-
17-
# check for changed files (not untracked files)
18-
if [ -n "$(git diff --shortstat 2> /dev/null | tail -n1)" ]; then
19-
VERSION="${VERSION}-dirty"
20-
fi
21-
22-
echo $VERSION
3+
DESCRIPTION=$(git describe --abbrev=100 --dirty) &&
4+
echo "${DESCRIPTION##*-g}"

0 commit comments

Comments
 (0)