]> git.ipfire.org Git - thirdparty/git.git/blame - GIT-VERSION-GEN
The sixth batch
[thirdparty/git.git] / GIT-VERSION-GEN
CommitLineData
9b88fcef
JH
1#!/bin/sh
2
f9aa0eed 3DEF_VER=v2.50.GIT
9b88fcef 4
c96c2909
JH
5LF='
6'
7
f6a2efdc 8if test "$#" -lt 2 || test "$#" -gt 3
4838deab 9then
f6a2efdc 10 echo >&2 "USAGE: $0 <SOURCE_DIR> (--format=<STRING>|<INPUT>) [<OUTPUT>]"
4838deab
PS
11 exit 1
12fi
13
14SOURCE_DIR="$1"
4838deab 15
f6a2efdc
PS
16case "$2" in
17--format=*)
18 INPUT="${2#--format=}"
19 ;;
20*)
21 if ! test -f "$2"
22 then
23 echo >&2 "Input is not a file: $2"
24 exit 1
25 fi
26 INPUT=$(cat "$2")
27 ;;
28esac
29
30OUTPUT="$3"
4838deab
PS
31
32# Protect us from reading Git version information outside of the Git directory
33# in case it is not a repository itself, but embedded in an unrelated
34# repository.
35GIT_CEILING_DIRECTORIES="$SOURCE_DIR/.."
36export GIT_CEILING_DIRECTORIES
37
992bc561 38if test -z "$GIT_VERSION"
0b8b051c 39then
992bc561
PS
40 # First see if there is a version file (included in release tarballs),
41 # then try git-describe, then default.
42 if test -f "$SOURCE_DIR"/version
43 then
44 VN=$(cat "$SOURCE_DIR"/version) || VN="$DEF_VER"
45 elif {
46 test -d "$SOURCE_DIR/.git" ||
47 test -d "${GIT_DIR:-.git}" ||
48 test -f "$SOURCE_DIR"/.git;
49 } &&
e40622a6 50 VN=$(git -C "$SOURCE_DIR" describe --dirty --match="v[0-9]*" 2>/dev/null) &&
992bc561
PS
51 case "$VN" in
52 *$LF*) (exit 1) ;;
992bc561
PS
53 esac
54 then
55 VN=$(echo "$VN" | sed -e 's/-/./g');
56 else
57 VN="$DEF_VER"
58 fi
59
60 GIT_VERSION=$(expr "$VN" : v*'\(.*\)')
374dfaa2 61fi
181129d2 62
cfa01e6d 63if test -z "$GIT_BUILT_FROM_COMMIT"
204d4092 64then
cfa01e6d
PS
65 GIT_BUILT_FROM_COMMIT=$(git -C "$SOURCE_DIR" rev-parse -q --verify HEAD 2>/dev/null)
66fi
67
68if test -z "$GIT_DATE"
69then
70 GIT_DATE=$(git -C "$SOURCE_DIR" show --quiet --format='%as' 2>/dev/null)
374dfaa2 71fi
181129d2 72
0c8d3395
PS
73if test -z "$GIT_USER_AGENT"
74then
75 GIT_USER_AGENT="git/$GIT_VERSION"
76fi
4838deab 77
9bb10d27
PS
78# While released Git versions only have three numbers, development builds also
79# have a fourth number that corresponds to the number of patches since the last
80# release.
81read GIT_MAJOR_VERSION GIT_MINOR_VERSION GIT_MICRO_VERSION GIT_PATCH_LEVEL trailing <<EOF
82$(echo "$GIT_VERSION" 0 0 0 0 | tr '.a-zA-Z-' ' ')
4838deab
PS
83EOF
84
5acfacc2 85REPLACED=$(printf "%s\n" "$INPUT" | sed -e "s|@GIT_VERSION@|$GIT_VERSION|" \
4838deab
PS
86 -e "s|@GIT_MAJOR_VERSION@|$GIT_MAJOR_VERSION|" \
87 -e "s|@GIT_MINOR_VERSION@|$GIT_MINOR_VERSION|" \
88 -e "s|@GIT_MICRO_VERSION@|$GIT_MICRO_VERSION|" \
9bb10d27 89 -e "s|@GIT_PATCH_LEVEL@|$GIT_PATCH_LEVEL|" \
0c8d3395
PS
90 -e "s|@GIT_BUILT_FROM_COMMIT@|$GIT_BUILT_FROM_COMMIT|" \
91 -e "s|@GIT_USER_AGENT@|$GIT_USER_AGENT|" \
f6a2efdc
PS
92 -e "s|@GIT_DATE@|$GIT_DATE|"
93)
eb858c60 94
f6a2efdc 95if test -z "$OUTPUT"
9b88fcef 96then
f6a2efdc 97 printf "%s\n" "$REPLACED"
9b88fcef 98else
f6a2efdc
PS
99 printf "%s\n" "$REPLACED" >"$OUTPUT".$$+
100 if ! test -f "$OUTPUT" || ! cmp "$OUTPUT".$$+ "$OUTPUT" >/dev/null
101 then
102 mv "$OUTPUT".$$+ "$OUTPUT"
103 else
104 rm "$OUTPUT".$$+
105 fi
9b88fcef 106fi