Skip to content

Commit 04867cd

Browse files
aofeiBryan C. Mills
authored and
Bryan C. Mills
committed
cmd/go/internal/modconv: use modules to examine instead of using only direct source control entries
Since modules now support parsing multiple forms of versions (including commit hash and source control tag), I think modconv.ConvertLegacyConfig no longer needs modfetch.ImportRepoRev. So I suggest that we use modules to convert legacy config instead of using VCS directly. By doing this, we can make the module proxy participate in the conversion process and benefit from it (such as speeding up "go mod init" or breaking through the firewall). And since modconv.ConvertLegacyConfig is the only caller of modfetch.ImportRepoRev, I think modfetch.ImportRepoRev can be removed. Fixes #33767 Change-Id: Ic79b14fa805ed297ca1735a8498cfed2a5ddeec2 Reviewed-on: https://go-review.googlesource.com/c/go/+/191218 Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
1 parent cc39d80 commit 04867cd

File tree

3 files changed

+14
-64
lines changed

3 files changed

+14
-64
lines changed

src/cmd/go/internal/modconv/convert.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,19 @@ func ConvertLegacyConfig(f *modfile.File, file string, data []byte) error {
6666

6767
work.Do(10, func(item interface{}) {
6868
r := item.(module.Version)
69-
repo, info, err := modfetch.ImportRepoRev(r.Path, r.Version)
69+
var info *modfetch.RevInfo
70+
err := modfetch.TryProxies(func(proxy string) (err error) {
71+
info, err = modfetch.Stat(proxy, r.Path, r.Version)
72+
return err
73+
})
7074
if err != nil {
7175
fmt.Fprintf(os.Stderr, "go: converting %s: stat %s@%s: %v\n", base.ShortPath(file), r.Path, r.Version, err)
7276
return
7377
}
7478
mu.Lock()
75-
path := repo.ModulePath()
7679
// Don't use semver.Max here; need to preserve +incompatible suffix.
77-
if v, ok := need[path]; !ok || semver.Compare(v, info.Version) < 0 {
78-
need[path] = info.Version
80+
if v, ok := need[r.Path]; !ok || semver.Compare(v, info.Version) < 0 {
81+
need[r.Path] = info.Version
7982
}
8083
mu.Unlock()
8184
})

src/cmd/go/internal/modfetch/repo.go

-56
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,6 @@ type RevInfo struct {
161161
// and it can check that the path can be resolved to a target repository.
162162
// To avoid version control access except when absolutely necessary,
163163
// Lookup does not attempt to connect to the repository itself.
164-
//
165-
// The ImportRepoRev function is a variant of Import which is limited
166-
// to code in a source code repository at a particular revision identifier
167-
// (usually a commit hash or source code repository tag, not necessarily
168-
// a module version).
169-
// ImportRepoRev is used when converting legacy dependency requirements
170-
// from older systems into go.mod files. Those older systems worked
171-
// at either package or repository granularity, and most of the time they
172-
// recorded commit hashes, not tagged versions.
173164

174165
var lookupCache par.Cache
175166

@@ -279,53 +270,6 @@ func lookupCodeRepo(rr *get.RepoRoot) (codehost.Repo, error) {
279270
return code, nil
280271
}
281272

282-
// ImportRepoRev returns the module and version to use to access
283-
// the given import path loaded from the source code repository that
284-
// the original "go get" would have used, at the specific repository revision
285-
// (typically a commit hash, but possibly also a source control tag).
286-
func ImportRepoRev(path, rev string) (Repo, *RevInfo, error) {
287-
if cfg.BuildMod == "vendor" || cfg.BuildMod == "readonly" {
288-
return nil, nil, fmt.Errorf("repo version lookup disabled by -mod=%s", cfg.BuildMod)
289-
}
290-
291-
// Note: Because we are converting a code reference from a legacy
292-
// version control system, we ignore meta tags about modules
293-
// and use only direct source control entries (get.IgnoreMod).
294-
security := web.SecureOnly
295-
if get.Insecure {
296-
security = web.Insecure
297-
}
298-
rr, err := get.RepoRootForImportPath(path, get.IgnoreMod, security)
299-
if err != nil {
300-
return nil, nil, err
301-
}
302-
303-
code, err := lookupCodeRepo(rr)
304-
if err != nil {
305-
return nil, nil, err
306-
}
307-
308-
revInfo, err := code.Stat(rev)
309-
if err != nil {
310-
return nil, nil, err
311-
}
312-
313-
// TODO: Look in repo to find path, check for go.mod files.
314-
// For now we're just assuming rr.Root is the module path,
315-
// which is true in the absence of go.mod files.
316-
317-
repo, err := newCodeRepo(code, rr.Root, rr.Root)
318-
if err != nil {
319-
return nil, nil, err
320-
}
321-
322-
info, err := repo.(*codeRepo).convert(revInfo, rev)
323-
if err != nil {
324-
return nil, nil, err
325-
}
326-
return repo, info, nil
327-
}
328-
329273
func SortVersions(list []string) {
330274
sort.Slice(list, func(i, j int) bool {
331275
cmp := semver.Compare(list[i], list[j])

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
env GO111MODULE=on
22

3-
# modconv uses git directly to examine what old 'go get' would
4-
[!net] skip
5-
[!exec:git] skip
3+
# modconv uses modules to examine instead of using git directly
4+
[short] skip
65

76
# go build should populate go.mod from Gopkg.lock
87
cp go.mod1 go.mod
98
go build
109
stderr 'copying requirements from Gopkg.lock'
10+
stderr 'finding rsc.io/sampler v1.0.0'
1111
go list -m all
1212
! stderr 'copying requirements from Gopkg.lock'
13+
! stderr 'finding rsc.io/sampler v1.0.0'
1314
stdout 'rsc.io/sampler v1.0.0'
1415

1516
# go list should populate go.mod from Gopkg.lock
1617
cp go.mod1 go.mod
1718
go list
1819
stderr 'copying requirements from Gopkg.lock'
20+
! stderr 'finding rsc.io/sampler v1.0.0'
1921
go list
2022
! stderr 'copying requirements from Gopkg.lock'
23+
! stderr 'finding rsc.io/sampler v1.0.0'
2124
go list -m all
2225
stdout 'rsc.io/sampler v1.0.0'
2326

@@ -54,4 +57,4 @@ go $goversion
5457

5558
replace z v1.0.0 => rsc.io/quote v1.0.0
5659

57-
require rsc.io/quote v1.0.0
60+
require rsc.io/quote v1.0.0

0 commit comments

Comments
 (0)