1
#!/bin/sh
2
3
-GVF=GIT-VERSION-FILE
3
DEF_VER=v2.47.GIT
4
5
LF='
6
'
7
8
+if test "$#" -ne 3
9
+then
10
+ echo >&2 "USAGE: $0 <SOURCE_DIR> <INPUT> <OUTPUT>"
11
+ exit 1
12
+fi
13
+
14
+SOURCE_DIR="$1"
15
+INPUT="$2"
16
+OUTPUT="$3"
17
+
18
+if ! test -f "$INPUT"
19
+then
20
+ echo >&2 "Input is not a file: $INPUT"
21
+ exit 1
22
+fi
23
+
24
+# Protect us from reading Git version information outside of the Git directory
25
+# in case it is not a repository itself, but embedded in an unrelated
26
+# repository.
27
+GIT_CEILING_DIRECTORIES="$SOURCE_DIR/.."
28
+export GIT_CEILING_DIRECTORIES
29
+
30
# First see if there is a version file (included in release tarballs),
31
# then try git-describe, then default.
11
-if test -f version
32
+if test -f "$SOURCE_DIR"/version
33
then
13
- VN=$(cat version) || VN="$DEF_VER"
14
-elif { test -d "${GIT_DIR:-.git}" || test -f .git; } &&
15
- VN=$(git describe --match "v[0-9]*" HEAD 2>/dev/null) &&
34
+ VN=$(cat "$SOURCE_DIR"/version) || VN="$DEF_VER"
35
+elif {
36
+ test -d "$SOURCE_DIR/.git" ||
37
+ test -d "${GIT_DIR:-.git}" ||
38
+ test -f "$SOURCE_DIR"/.git;
39
+ } &&
40
+ VN=$(git -C "$SOURCE_DIR" describe --match "v[0-9]*" HEAD 2>/dev/null) &&
41
case "$VN" in
42
*$LF*) (exit 1) ;;
43
v[0-9]*)
19
- git update-index -q --refresh
20
- test -z "$(git diff-index --name-only HEAD --)" ||
44
+ git -C "$SOURCE_DIR" update-index -q --refresh
45
+ test -z "$(git -C "$SOURCE_DIR" diff-index --name-only HEAD --)" ||
46
VN="$VN-dirty" ;;
47
esac
48
then
51
VN="$DEF_VER"
52
fi
53
29
-VN=$(expr "$VN" : v*'\(.*\)')
54
+GIT_VERSION=$(expr "$VN" : v*'\(.*\)')
55
+
56
+read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION trailing <<EOF
57
+$(echo "$GIT_VERSION" 0 0 0 | tr '.a-zA-Z-' ' ')
58
+EOF
59
+
60
+sed -e "s|@GIT_VERSION@|$GIT_VERSION|" \
61
+ -e "s|@GIT_MAJOR_VERSION@|$GIT_MAJOR_VERSION|" \
62
+ -e "s|@GIT_MINOR_VERSION@|$GIT_MINOR_VERSION|" \
63
+ -e "s|@GIT_MICRO_VERSION@|$GIT_MICRO_VERSION|" \
64
+ "$INPUT" >"$OUTPUT"+
65
31
-if test -r $GVF
66
+if ! test -f "$OUTPUT" || ! cmp "$OUTPUT"+ "$OUTPUT" >/dev/null
67
then
33
- VC=$(sed -e 's/^GIT_VERSION = //' <$GVF)
68
+ mv "$OUTPUT"+ "$OUTPUT"
69
else
35
- VC=unset
70
+ rm "$OUTPUT"+
71
fi
37
-test "$VN" = "$VC" || {
38
- echo >&2 "GIT_VERSION = $VN"
39
- echo "GIT_VERSION = $VN" >$GVF
40
-}