1 # This test checks that VCS information is stamped into Go binaries by default,
2 # controlled with -buildvcs. This test focuses on Git. Other tests focus on
3 # other VCS tools but may not cover common functionality.
4
5 [!git] skip
6 [short] skip
7 env GOBIN=$WORK/gopath/bin
8 env oldpath=$PATH
9 cd repo/a
10
11 # If there's no local repository, there's no VCS info.
12 go install
13 go version -m $GOBIN/a$GOEXE
14 ! stdout vcs.revision
15 rm $GOBIN/a$GOEXE
16
17 # If there's an orphan .git file left by a git submodule, it's not a git
18 # repository, and there's no VCS info.
19 cd ../gitsubmodule
20 go install
21 go version -m $GOBIN/gitsubmodule$GOEXE
22 ! stdout vcs.revision
23 rm $GOBIN/gitsubmodule$GOEXE
24
25 # If there is a repository, but it can't be used for some reason,
26 # there should be an error. It should hint about -buildvcs=false.
27 # Also ensure that multiple errors are collected by "go list -e".
28 cd ..
29 mkdir .git
30 env PATH=$WORK${/}fakebin${:}$oldpath
31 chmod 0755 $WORK/fakebin/git
32 ! exec git help
33 cd a
34 ! go install
35 stderr '^error obtaining VCS status: exit status 1\n\tUse -buildvcs=false to disable VCS stamping.$'
36 go list -e -f '{{.ImportPath}}: {{.Error}}' ./...
37 stdout -count=1 '^example\.com/a: error obtaining VCS status'
38 stdout -count=1 '^example\.com/a/library: <nil>'
39 stdout -count=1 '^example\.com/a/othermain: error obtaining VCS status'
40 cd ..
41 env PATH=$oldpath
42 rm .git
43
44 # If there is an empty repository in a parent directory, only "uncommitted" is tagged.
45 exec git init
46 exec git config user.email gopher@golang.org
47 exec git config user.name 'J.R. Gopher'
48 cd a
49 go install
50 go version -m $GOBIN/a$GOEXE
51 stdout '^\tbuild\tvcs=git$'
52 stdout '^\tbuild\tvcs.modified=true$'
53 ! stdout vcs.revision
54 ! stdout vcs.time
55 rm $GOBIN/a$GOEXE
56
57 # Revision and commit time are tagged for repositories with commits.
58 exec git add -A
59 exec git commit -m 'initial commit'
60 go install
61 go version -m $GOBIN/a$GOEXE
62 stdout '^\tbuild\tvcs.revision='
63 stdout '^\tbuild\tvcs.time='
64 stdout '^\tbuild\tvcs.modified=false$'
65 rm $GOBIN/a$GOEXE
66
67 # Building with -buildvcs=false suppresses the info.
68 go install -buildvcs=false
69 go version -m $GOBIN/a$GOEXE
70 ! stdout vcs.revision
71 rm $GOBIN/a$GOEXE
72
73 # An untracked file is shown as uncommitted, even if it isn't part of the build.
74 cp ../../outside/empty.txt .
75 go install
76 go version -m $GOBIN/a$GOEXE
77 stdout '^\tbuild\tvcs.modified=true$'
78 rm empty.txt
79 rm $GOBIN/a$GOEXE
80
81 # An edited file is shown as uncommitted, even if it isn't part of the build.
82 cp ../../outside/empty.txt ../README
83 go install
84 go version -m $GOBIN/a$GOEXE
85 stdout '^\tbuild\tvcs.modified=true$'
86 exec git checkout ../README
87 rm $GOBIN/a$GOEXE
88
89 # If the build doesn't include any packages from the repository,
90 # there should be no VCS info.
91 go install example.com/cmd/a@v1.0.0
92 go version -m $GOBIN/a$GOEXE
93 ! stdout vcs.revision
94 rm $GOBIN/a$GOEXE
95
96 go mod edit -require=example.com/c@v0.0.0
97 go mod edit -replace=example.com/c@v0.0.0=../../outside/c
98 go install example.com/c
99 go version -m $GOBIN/c$GOEXE
100 ! stdout vcs.revision
101 rm $GOBIN/c$GOEXE
102 exec git checkout go.mod
103
104 # If the build depends on a package in the repository, but it's not in the
105 # main module, there should be no VCS info.
106 go mod edit -require=example.com/b@v0.0.0
107 go mod edit -replace=example.com/b@v0.0.0=../b
108 go mod edit -require=example.com/d@v0.0.0
109 go mod edit -replace=example.com/d@v0.0.0=../../outside/d
110 go install example.com/d
111 go version -m $GOBIN/d$GOEXE
112 ! stdout vcs.revision
113 exec git checkout go.mod
114 rm $GOBIN/d$GOEXE
115
116 # If we're loading multiple main packages,
117 # but they share the same VCS repository,
118 # we only need to execute VCS status commands once.
119 go list -x ./...
120 stdout -count=3 '^example.com'
121 stderr -count=1 '^git status'
122 stderr -count=1 '^git -c log.showsignature=false log'
123
124 -- $WORK/fakebin/git --
125 #!/bin/sh
126 exit 1
127 -- $WORK/fakebin/git.bat --
128 exit 1
129 -- repo/README --
130 Far out in the uncharted backwaters of the unfashionable end of the western
131 spiral arm of the Galaxy lies a small, unregarded yellow sun.
132 -- repo/a/go.mod --
133 module example.com/a
134
135 go 1.18
136 -- repo/a/a.go --
137 package main
138
139 func main() {}
140 -- repo/a/library/f.go --
141 package library
142 -- repo/a/othermain/f.go --
143 package main
144
145 func main() {}
146 -- repo/b/go.mod --
147 module example.com/b
148
149 go 1.18
150 -- repo/b/b.go --
151 package b
152 -- repo/gitsubmodule/.git --
153 gitdir: ../.git/modules/gitsubmodule
154 -- repo/gitsubmodule/go.mod --
155 module example.com/gitsubmodule
156
157 go 1.18
158 -- repo/gitsubmodule/main.go --
159 package main
160
161 func main() {}
162 -- outside/empty.txt --
163 -- outside/c/go.mod --
164 module example.com/c
165
166 go 1.18
167 -- outside/c/main.go --
168 package main
169
170 func main() {}
171 -- outside/d/go.mod --
172 module example.com/d
173
174 go 1.18
175
176 require example.com/b v0.0.0
177 -- outside/d/main.go --
178 package main
179
180 import _ "example.com/b"
181
182 func main() {}
183
View as plain text