...
Text file
src/cmd/go/testdata/script/build_version_stamping_git.txt
1# Test that the version of a binary is stamped using git tag information.
2# See https://go.dev/issue/50603
3
4[short] skip 'constructs a local git repo'
5[!git] skip
6
7# Redirect git to a test-specific .gitconfig.
8# GIT_CONFIG_GLOBAL suffices for git 2.32.0 and newer.
9# For older git versions we also set $HOME.
10env GIT_CONFIG_GLOBAL=$WORK${/}home${/}gopher${/}.gitconfig
11env HOME=$WORK${/}home${/}gopher
12exec git config --global --show-origin user.name
13stdout 'Go Gopher'
14
15cd $WORK/repo
16# Use devel when git information is missing.
17go build
18go version -m example$GOEXE
19stdout '\s+mod\s+example\s+\(devel\)'
20rm example$GOEXE
21
22env GIT_AUTHOR_NAME='Go Gopher'
23env GIT_AUTHOR_EMAIL='gopher@golang.org'
24env GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME
25env GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL
26
27exec git init
28env GIT_COMMITTER_DATE=2022-07-19T11:07:00-04:00
29env GIT_AUTHOR_DATE=2022-07-19T11:07:00-04:00
30exec git add .
31exec git commit -m 'initial commit'
32exec git branch -m main
33
34# Use a 0.0.0 pseudo-version when no tags are present.
35go build
36go version -m example$GOEXE
37stdout '\s+mod\s+example\s+v0.0.0-20220719150700-e7537ba8fd6d\s+'
38rm example$GOEXE
39
40# Use a 0.0.0 pseudo-version if the current tag is not a valid semantic version.
41exec git tag 1.0.1
42go build
43go version -m example$GOEXE
44stdout '\s+mod\s+example\s+v0.0.0-20220719150700-e7537ba8fd6d\s+'
45rm example$GOEXE
46
47# Use the current tag which has a valid semantic version to stamp the version.
48exec git tag v1.0.1
49go build
50go version -m example$GOEXE
51stdout '\s+mod\s+example\s+v1.0.1\s+'
52rm example$GOEXE
53
54# Use tag+dirty when there are uncommitted changes present.
55cp $WORK/copy/README $WORK/repo/README
56go build
57go version -m example$GOEXE
58stdout '\s+mod\s+example\s+v1.0.1\+dirty\s+'
59rm example$GOEXE
60
61env GIT_COMMITTER_DATE=2022-07-19T11:07:01-04:00
62env GIT_AUTHOR_DATE=2022-07-19T11:07:01-04:00
63exec git add .
64exec git commit -m 'commit 2'
65
66# Use the updated tag to stamp the version.
67exec git tag v1.0.2
68go build
69go version -m example$GOEXE
70stdout '\s+mod\s+example\s+v1.0.2\s+'
71rm example$GOEXE
72
73env GIT_COMMITTER_DATE=2022-07-19T11:07:02-04:00
74env GIT_AUTHOR_DATE=2022-07-19T11:07:02-04:00
75mv README README2
76exec git add .
77exec git commit -m 'commit 3'
78
79# Use a pseudo-version when current commit doesn't match a tagged version.
80go build
81go version -m example$GOEXE
82stdout '\s+mod\s+example\s+v1.0.3-0.20220719150702-b0226f18a7ae\s+'
83rm example$GOEXE
84
85# Use pseudo+dirty when uncommitted changes are present.
86mv README2 README3
87go build
88go version -m example$GOEXE
89stdout '\s+mod\s+example\s+v1.0.3-0.20220719150702-b0226f18a7ae\+dirty\s+'
90rm example$GOEXE
91
92# Make sure we always use the previously tagged version to generate the pseudo-version at a untagged revision.
93env GIT_COMMITTER_DATE=2022-07-19T11:07:03-04:00
94env GIT_AUTHOR_DATE=2022-07-19T11:07:03-04:00
95exec git add .
96exec git commit -m 'commit 4'
97
98mv README3 README4
99env GIT_COMMITTER_DATE=2022-07-19T11:07:04-04:00
100env GIT_AUTHOR_DATE=2022-07-19T11:07:04-04:00
101exec git add .
102exec git commit -m 'commit 5'
103exec git tag v1.0.4
104# Jump back to commit 4 which is untagged.
105exec git checkout ':/commit 4'
106go build
107go version -m example$GOEXE
108stdout '\s+mod\s+example\s+v1.0.3-0.20220719150703-2ebc76937b49\s+'
109rm example$GOEXE
110
111# Create +incompatible module
112exec git checkout v1.0.4
113exec git rm go.mod
114exec git commit -m 'commit 6'
115exec git tag v2.0.0
116exec git checkout HEAD^ go.mod
117# And make the tree +dirty
118mv README4 README5
119go build
120go version -m example$GOEXE
121stdout '\s+mod\s+example\s+v2.0.0\+incompatible.dirty\s+'
122rm example$GOEXE
123
124# Make sure v2 works as expected.
125exec git checkout v1.0.4
126go mod edit -module example/v2
127exec git add .
128exec git commit -m 'commit 7'
129exec git tag v2.1.1
130go build
131go version -m example$GOEXE
132stdout '\s+mod\s+example/v2\s+v2.1.1\s+'
133rm example$GOEXE
134
135# v2+dirty
136mv README5 README6
137go build
138go version -m example$GOEXE
139stdout '\s+mod\s+example/v2\s+v2.1.1\+dirty\s+'
140rm example$GOEXE
141
142# v2+pseudo
143exec git add .
144exec git commit -m 'commit 8'
145go build
146go version -m example$GOEXE
147stdout '\s+mod\s+example/v2\s+v2.1.2-0.20220719150704-0ebeb94ecde2\s+'
148rm example$GOEXE
149
150# v2+pseudo+dirty
151mv README6 README7
152go build
153go version -m example$GOEXE
154stdout '\s+mod\s+example/v2\s+v2.1.2-0.20220719150704-0ebeb94ecde2\+dirty\s+'
155rm example$GOEXE
156
157# modules in subdirectories should be stamped with the correct tag
158exec git add .
159cd subdir
160exec git commit -m 'commit 9'
161go build
162go version -m subdir$GOEXE
163# missing tag creates a pseudo version with v2.0.0
164stdout '\s+mod\s+example/subdir/v2\s+v2.0.0-20220719150704-fbef6799938f\s+'
165rm subdir$GOEXE
166# tag with subdir
167exec git tag subdir/v2.1.0
168go build
169go version -m subdir$GOEXE
170stdout '\s+mod\s+example/subdir/v2\s+v2.1.0\s+'
171# v2+dirty
172mv ../README7 README8
173go build
174go version -m subdir$GOEXE
175stdout '\s+mod\s+example/subdir/v2\s+v2.1.0\+dirty\s+'
176rm subdir$GOEXE
177
178# modules in a subdirectory without a go.mod in the root should result in (devel)
179rm ../go.mod
180go build
181go version -m subdir$GOEXE
182stdout '\s+mod\s+example/subdir/v2\s+\(devel\)\s+'
183rm subdir$GOEXE
184
185-- $WORK/repo/go.mod --
186module example
187
188go 1.18
189-- $WORK/repo/main.go --
190package main
191
192func main() {
193}
194-- $WORK/copy/README --
195hello
196
197-- $WORK/repo/subdir/go.mod --
198module example/subdir/v2
199
200go 1.18
201
202-- $WORK/repo/subdir/main.go --
203package main
204
205func main() {
206}
207
208-- $WORK/home/gopher/.gitconfig --
209[user]
210 name = Go Gopher
211 email = gopher@golang.org
View as plain text