Skip to content

Commit b159c99

Browse files
huww98gopherbot
authored andcommitted
cmd/go/internal/vcs: use git log to avoid unnecessary objects
"git show" by default shows the diff from the previous commit. "-s" suppress all output from the diff machinery. But it will still try to fetch the relevant objects, which may be unavailable if the repository is a partial clone. Use "git log" instead, which only needs the commit object. Fixes #65339 Change-Id: I766a680321cd22d5fdbd08d24cb777ef964bdb7c GitHub-Last-Rev: 87a8ba4 GitHub-Pull-Request: #65341 Reviewed-on: https://go-review.googlesource.com/c/go/+/559075 Auto-Submit: Bryan Mills <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent 8a57cc8 commit b159c99

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

src/cmd/go/internal/vcs/vcs.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -331,12 +331,12 @@ func gitStatus(vcsGit *Cmd, rootDir string) (Status, error) {
331331
}
332332
uncommitted := len(out) > 0
333333

334-
// "git status" works for empty repositories, but "git show" does not.
335-
// Assume there are no commits in the repo when "git show" fails with
334+
// "git status" works for empty repositories, but "git log" does not.
335+
// Assume there are no commits in the repo when "git log" fails with
336336
// uncommitted files and skip tagging revision / committime.
337337
var rev string
338338
var commitTime time.Time
339-
out, err = vcsGit.runOutputVerboseOnly(rootDir, "-c log.showsignature=false show -s --format=%H:%ct")
339+
out, err = vcsGit.runOutputVerboseOnly(rootDir, "-c log.showsignature=false log -1 --format=%H:%ct")
340340
if err != nil && !uncommitted {
341341
return Status{}, err
342342
} else if err == nil {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Regression test for https://go.dev/issue/65339.
2+
# Unnecessary git tree object required
3+
4+
[short] skip 'constructs a local git repo'
5+
[!git] skip
6+
7+
env GIT_AUTHOR_NAME='Go Gopher'
8+
env GIT_AUTHOR_EMAIL='[email protected]'
9+
env GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME
10+
env GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL
11+
12+
# Create 2 commit
13+
env GIT_COMMITTER_DATE=2024-01-30T10:52:00+08:00
14+
env GIT_AUTHOR_DATE=2024-01-30T10:52:00+08:00
15+
16+
cd $WORK/repo
17+
exec git init
18+
exec git add go.mod main.go
19+
exec git commit -m 'initial commit'
20+
21+
env GIT_COMMITTER_DATE=2024-01-30T10:53:00+08:00
22+
env GIT_AUTHOR_DATE=2024-01-30T10:53:00+08:00
23+
exec git add extra.go
24+
exec git commit -m 'add extra.go'
25+
26+
# Assume the tree object from initial commit is not available (e.g. partial clone)
27+
exec git log --pretty=%T
28+
cmp stdout $WORK/.git-trees
29+
30+
rm .git/objects/66/400c89b45cc96da36d232844dbf9ea5daa6bcf
31+
32+
# Build the module, which should succeed
33+
go build -v -buildvcs=true -o test
34+
go version -m test
35+
stdout '^\tbuild\tvcs.revision=fe3c8204d2332a731166269932dd23760c1b576a$'
36+
37+
-- $WORK/repo/go.mod --
38+
module github.com/golang/issue65339
39+
40+
go 1.20
41+
-- $WORK/repo/main.go --
42+
package main
43+
44+
func main() {
45+
println("hello, world")
46+
}
47+
-- $WORK/repo/extra.go --
48+
package main
49+
-- $WORK/.git-trees --
50+
ac724c6e5e3f86815e057ff58a639cab613abf28
51+
66400c89b45cc96da36d232844dbf9ea5daa6bcf

src/cmd/go/testdata/script/version_buildvcs_git.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ rm $GOBIN/d$GOEXE
119119
go list -x ./...
120120
stdout -count=3 '^example.com'
121121
stderr -count=1 '^git status'
122-
stderr -count=1 '^git -c log.showsignature=false show'
122+
stderr -count=1 '^git -c log.showsignature=false log'
123123

124124
-- $WORK/fakebin/git --
125125
#!/bin/sh

0 commit comments

Comments
 (0)