summaryrefslogtreecommitdiffstats
path: root/GIT-VERSION-GEN
blob: 2fee5e7e80a0580f48a45943c66bbb6e0c9a0b9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/sh

DEF_VER=v2.47.GIT

LF='
'

if test "$#" -ne 3
then
    echo >&2 "USAGE: $0 <SOURCE_DIR> <INPUT> <OUTPUT>"
    exit 1
fi

SOURCE_DIR="$1"
INPUT="$2"
OUTPUT="$3"

if ! test -f "$INPUT"
then
	echo >&2 "Input is not a file: $INPUT"
	exit 1
fi

# Protect us from reading Git version information outside of the Git directory
# in case it is not a repository itself, but embedded in an unrelated
# repository.
GIT_CEILING_DIRECTORIES="$SOURCE_DIR/.."
export GIT_CEILING_DIRECTORIES

if test -z "$GIT_VERSION"
then
	# First see if there is a version file (included in release tarballs),
	# then try git-describe, then default.
	if test -f "$SOURCE_DIR"/version
	then
		VN=$(cat "$SOURCE_DIR"/version) || VN="$DEF_VER"
	elif {
			test -d "$SOURCE_DIR/.git" ||
			test -d "${GIT_DIR:-.git}" ||
			test -f "$SOURCE_DIR"/.git;
		} &&
		VN=$(git -C "$SOURCE_DIR" describe --match "v[0-9]*" HEAD 2>/dev/null) &&
		case "$VN" in
		*$LF*) (exit 1) ;;
		v[0-9]*)
			git -C "$SOURCE_DIR" update-index -q --refresh
			test -z "$(git -C "$SOURCE_DIR" diff-index --name-only HEAD --)" ||
			VN="$VN-dirty" ;;
		esac
	then
		VN=$(echo "$VN" | sed -e 's/-/./g');
	else
		VN="$DEF_VER"
	fi

	GIT_VERSION=$(expr "$VN" : v*'\(.*\)')
fi

GIT_BUILT_FROM_COMMIT=$(git -C "$SOURCE_DIR" rev-parse -q --verify HEAD 2>/dev/null)
GIT_DATE=$(git -C "$SOURCE_DIR" show --quiet --format='%as' 2>/dev/null)
if test -z "$GIT_USER_AGENT"
then
	GIT_USER_AGENT="git/$GIT_VERSION"
fi

# While released Git versions only have three numbers, development builds also
# have a fourth number that corresponds to the number of patches since the last
# release.
read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION GIT_PATCH_LEVEL trailing <<EOF
$(echo "$GIT_VERSION" 0 0 0 0 | tr '.a-zA-Z-' ' ')
EOF

sed -e "s|@GIT_VERSION@|$GIT_VERSION|" \
	-e "s|@GIT_MAJOR_VERSION@|$GIT_MAJOR_VERSION|" \
	-e "s|@GIT_MINOR_VERSION@|$GIT_MINOR_VERSION|" \
	-e "s|@GIT_MICRO_VERSION@|$GIT_MICRO_VERSION|" \
	-e "s|@GIT_PATCH_LEVEL@|$GIT_PATCH_LEVEL|" \
	-e "s|@GIT_BUILT_FROM_COMMIT@|$GIT_BUILT_FROM_COMMIT|" \
	-e "s|@GIT_USER_AGENT@|$GIT_USER_AGENT|" \
	-e "s|@GIT_DATE@|$GIT_DATE|" \
	"$INPUT" >"$OUTPUT"+

if ! test -f "$OUTPUT" || ! cmp "$OUTPUT"+ "$OUTPUT" >/dev/null
then
	mv "$OUTPUT"+ "$OUTPUT"
else
	rm "$OUTPUT"+
fi