Skip to content

Commit 1804bba

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modfetch/codehost: work around an apparent bug in 'git fetch --unshallow'
When 'git fetch' is passed the '--unshallow' flag, it assumes that the local and remote refs are equal.¹ However, we were fetching an expanded set of refs explicitly in the same command, violating that assumption. Now we first expand the set of refs, then unshallow the repo in a separate fetch. Empirically, this seems to work, whereas the opposite order does not. ¹https://github.com/git/git/blob/4c86140027f4a0d2caaa3ab4bd8bfc5ce3c11c8a/transport.c#L1303-L1309 Fixes #34266 Change-Id: Ie97eb7c1223f944003a1e31d0ec9e69aad0efc0d Reviewed-on: https://go-review.googlesource.com/c/go/+/196961 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 47d27a8 commit 1804bba

File tree

2 files changed

+35
-53
lines changed

2 files changed

+35
-53
lines changed

src/cmd/go/internal/modfetch/codehost/git.go

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,6 @@ func (r *gitRepo) findRef(hash string) (ref string, ok bool) {
265265
return "", false
266266
}
267267

268-
func unshallow(gitDir string) []string {
269-
if _, err := os.Stat(filepath.Join(gitDir, "shallow")); err == nil {
270-
return []string{"--unshallow"}
271-
}
272-
return []string{}
273-
}
274-
275268
// minHashDigits is the minimum number of digits to require
276269
// before accepting a hex digit sequence as potentially identifying
277270
// a specific commit in a git repo. (Of course, users can always
@@ -421,29 +414,27 @@ func (r *gitRepo) stat(rev string) (*RevInfo, error) {
421414
// fetchRefsLocked requires that r.mu remain locked for the duration of the call.
422415
func (r *gitRepo) fetchRefsLocked() error {
423416
if r.fetchLevel < fetchAll {
424-
if err := r.fetchUnshallow("refs/heads/*:refs/heads/*", "refs/tags/*:refs/tags/*"); err != nil {
417+
// NOTE: To work around a bug affecting Git clients up to at least 2.23.0
418+
// (2019-08-16), we must first expand the set of local refs, and only then
419+
// unshallow the repository as a separate fetch operation. (See
420+
// golang.org/issue/34266 and
421+
// https://github.com/git/git/blob/4c86140027f4a0d2caaa3ab4bd8bfc5ce3c11c8a/transport.c#L1303-L1309.)
422+
423+
if _, err := Run(r.dir, "git", "fetch", "-f", r.remote, "refs/heads/*:refs/heads/*", "refs/tags/*:refs/tags/*"); err != nil {
425424
return err
426425
}
426+
427+
if _, err := os.Stat(filepath.Join(r.dir, "shallow")); err == nil {
428+
if _, err := Run(r.dir, "git", "fetch", "--unshallow", "-f", r.remote); err != nil {
429+
return err
430+
}
431+
}
432+
427433
r.fetchLevel = fetchAll
428434
}
429435
return nil
430436
}
431437

432-
func (r *gitRepo) fetchUnshallow(refSpecs ...string) error {
433-
// To work around a protocol version 2 bug that breaks --unshallow,
434-
// add -c protocol.version=0.
435-
// TODO(rsc): The bug is believed to be server-side, meaning only
436-
// on Google's Git servers. Once the servers are fixed, drop the
437-
// protocol.version=0. See Google-internal bug b/110495752.
438-
var protoFlag []string
439-
unshallowFlag := unshallow(r.dir)
440-
if len(unshallowFlag) > 0 {
441-
protoFlag = []string{"-c", "protocol.version=0"}
442-
}
443-
_, err := Run(r.dir, "git", protoFlag, "fetch", unshallowFlag, "-f", r.remote, refSpecs)
444-
return err
445-
}
446-
447438
// statLocal returns a RevInfo describing rev in the local git repository.
448439
// It uses version as info.Version.
449440
func (r *gitRepo) statLocal(version, rev string) (*RevInfo, error) {
@@ -563,39 +554,10 @@ func (r *gitRepo) ReadFileRevs(revs []string, file string, maxSize int64) (map[s
563554
}
564555
defer unlock()
565556

566-
var refs []string
567-
var protoFlag []string
568-
var unshallowFlag []string
569-
for _, tag := range redo {
570-
refs = append(refs, "refs/tags/"+tag+":refs/tags/"+tag)
571-
}
572-
if len(refs) > 1 {
573-
unshallowFlag = unshallow(r.dir)
574-
if len(unshallowFlag) > 0 {
575-
// To work around a protocol version 2 bug that breaks --unshallow,
576-
// add -c protocol.version=0.
577-
// TODO(rsc): The bug is believed to be server-side, meaning only
578-
// on Google's Git servers. Once the servers are fixed, drop the
579-
// protocol.version=0. See Google-internal bug b/110495752.
580-
protoFlag = []string{"-c", "protocol.version=0"}
581-
}
582-
}
583-
if _, err := Run(r.dir, "git", protoFlag, "fetch", unshallowFlag, "-f", r.remote, refs); err != nil {
557+
if err := r.fetchRefsLocked(); err != nil {
584558
return nil, err
585559
}
586560

587-
// TODO(bcmills): after the 1.11 freeze, replace the block above with:
588-
// if r.fetchLevel <= fetchSome {
589-
// r.fetchLevel = fetchSome
590-
// var refs []string
591-
// for _, tag := range redo {
592-
// refs = append(refs, "refs/tags/"+tag+":refs/tags/"+tag)
593-
// }
594-
// if _, err := Run(r.dir, "git", "fetch", "--update-shallow", "-f", r.remote, refs); err != nil {
595-
// return nil, err
596-
// }
597-
// }
598-
599561
if _, err := r.readFileRevs(redo, file, files); err != nil {
600562
return nil, err
601563
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Regression test for golang.org/issue/34092: with an empty module cache,
2+
# 'GOPROXY=direct go get golang.org/x/tools/gopls@master' did not correctly
3+
# resolve the pseudo-version for its dependency on golang.org/x/tools.
4+
5+
[short] skip
6+
[!net] skip
7+
[!exec:git] skip
8+
9+
env GO111MODULE=on
10+
env GOPROXY=direct
11+
env GOSUMDB=off
12+
13+
go list -m cloud.google.com/go@master
14+
! stdout 'v0.0.0-'
15+
16+
-- go.mod --
17+
module example.com
18+
19+
go 1.14
20+
-- go.sum --

0 commit comments

Comments
 (0)