Skip to content

Commit 07c1788

Browse files
author
Jay Conrod
committed
cmd/go: move repository resolution from internal/get to internal/vcs
This is a refactoring intended to break the dependency from internal/modfetch to internal/get. No change in functionality is intended. Change-Id: If51aba7139cc0b62ecc9ba454c055c99e8f36f0f Reviewed-on: https://go-review.googlesource.com/c/go/+/254364 Run-TryBot: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> Reviewed-by: Michael Matloob <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 86ee84c commit 07c1788

File tree

7 files changed

+217
-205
lines changed

7 files changed

+217
-205
lines changed

src/cmd/go/internal/get/get.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"cmd/go/internal/load"
1919
"cmd/go/internal/search"
2020
"cmd/go/internal/str"
21+
"cmd/go/internal/vcs"
2122
"cmd/go/internal/web"
2223
"cmd/go/internal/work"
2324

@@ -406,7 +407,7 @@ func download(arg string, parent *load.Package, stk *load.ImportStack, mode int)
406407
// to make the first copy of or update a copy of the given package.
407408
func downloadPackage(p *load.Package) error {
408409
var (
409-
vcs *vcsCmd
410+
vcsCmd *vcs.Cmd
410411
repo, rootPath string
411412
err error
412413
blindRepo bool // set if the repo has unusual configuration
@@ -435,27 +436,27 @@ func downloadPackage(p *load.Package) error {
435436

436437
if p.Internal.Build.SrcRoot != "" {
437438
// Directory exists. Look for checkout along path to src.
438-
vcs, rootPath, err = vcsFromDir(p.Dir, p.Internal.Build.SrcRoot)
439+
vcsCmd, rootPath, err = vcs.FromDir(p.Dir, p.Internal.Build.SrcRoot)
439440
if err != nil {
440441
return err
441442
}
442443
repo = "<local>" // should be unused; make distinctive
443444

444445
// Double-check where it came from.
445-
if *getU && vcs.remoteRepo != nil {
446+
if *getU && vcsCmd.RemoteRepo != nil {
446447
dir := filepath.Join(p.Internal.Build.SrcRoot, filepath.FromSlash(rootPath))
447-
remote, err := vcs.remoteRepo(vcs, dir)
448+
remote, err := vcsCmd.RemoteRepo(vcsCmd, dir)
448449
if err != nil {
449450
// Proceed anyway. The package is present; we likely just don't understand
450451
// the repo configuration (e.g. unusual remote protocol).
451452
blindRepo = true
452453
}
453454
repo = remote
454455
if !*getF && err == nil {
455-
if rr, err := RepoRootForImportPath(importPrefix, IgnoreMod, security); err == nil {
456+
if rr, err := vcs.RepoRootForImportPath(importPrefix, vcs.IgnoreMod, security); err == nil {
456457
repo := rr.Repo
457-
if rr.vcs.resolveRepo != nil {
458-
resolved, err := rr.vcs.resolveRepo(rr.vcs, dir, repo)
458+
if rr.VCS.ResolveRepo != nil {
459+
resolved, err := rr.VCS.ResolveRepo(rr.VCS, dir, repo)
459460
if err == nil {
460461
repo = resolved
461462
}
@@ -469,13 +470,13 @@ func downloadPackage(p *load.Package) error {
469470
} else {
470471
// Analyze the import path to determine the version control system,
471472
// repository, and the import path for the root of the repository.
472-
rr, err := RepoRootForImportPath(importPrefix, IgnoreMod, security)
473+
rr, err := vcs.RepoRootForImportPath(importPrefix, vcs.IgnoreMod, security)
473474
if err != nil {
474475
return err
475476
}
476-
vcs, repo, rootPath = rr.vcs, rr.Repo, rr.Root
477+
vcsCmd, repo, rootPath = rr.VCS, rr.Repo, rr.Root
477478
}
478-
if !blindRepo && !vcs.isSecure(repo) && security != web.Insecure {
479+
if !blindRepo && !vcsCmd.IsSecure(repo) && security != web.Insecure {
479480
return fmt.Errorf("cannot download, %v uses insecure protocol", repo)
480481
}
481482

@@ -498,7 +499,7 @@ func downloadPackage(p *load.Package) error {
498499
}
499500
root := filepath.Join(p.Internal.Build.SrcRoot, filepath.FromSlash(rootPath))
500501

501-
if err := checkNestedVCS(vcs, root, p.Internal.Build.SrcRoot); err != nil {
502+
if err := vcs.CheckNested(vcsCmd, root, p.Internal.Build.SrcRoot); err != nil {
502503
return err
503504
}
504505

@@ -514,7 +515,7 @@ func downloadPackage(p *load.Package) error {
514515

515516
// Check that this is an appropriate place for the repo to be checked out.
516517
// The target directory must either not exist or have a repo checked out already.
517-
meta := filepath.Join(root, "."+vcs.cmd)
518+
meta := filepath.Join(root, "."+vcsCmd.Cmd)
518519
if _, err := os.Stat(meta); err != nil {
519520
// Metadata file or directory does not exist. Prepare to checkout new copy.
520521
// Some version control tools require the target directory not to exist.
@@ -535,12 +536,12 @@ func downloadPackage(p *load.Package) error {
535536
fmt.Fprintf(os.Stderr, "created GOPATH=%s; see 'go help gopath'\n", p.Internal.Build.Root)
536537
}
537538

538-
if err = vcs.create(root, repo); err != nil {
539+
if err = vcsCmd.Create(root, repo); err != nil {
539540
return err
540541
}
541542
} else {
542543
// Metadata directory does exist; download incremental updates.
543-
if err = vcs.download(root); err != nil {
544+
if err = vcsCmd.Download(root); err != nil {
544545
return err
545546
}
546547
}
@@ -549,20 +550,20 @@ func downloadPackage(p *load.Package) error {
549550
// Do not show tag sync in -n; it's noise more than anything,
550551
// and since we're not running commands, no tag will be found.
551552
// But avoid printing nothing.
552-
fmt.Fprintf(os.Stderr, "# cd %s; %s sync/update\n", root, vcs.cmd)
553+
fmt.Fprintf(os.Stderr, "# cd %s; %s sync/update\n", root, vcsCmd.Cmd)
553554
return nil
554555
}
555556

556557
// Select and sync to appropriate version of the repository.
557-
tags, err := vcs.tags(root)
558+
tags, err := vcsCmd.Tags(root)
558559
if err != nil {
559560
return err
560561
}
561562
vers := runtime.Version()
562563
if i := strings.Index(vers, " "); i >= 0 {
563564
vers = vers[:i]
564565
}
565-
if err := vcs.tagSync(root, selectTag(vers, tags)); err != nil {
566+
if err := vcsCmd.TagSync(root, selectTag(vers, tags)); err != nil {
566567
return err
567568
}
568569

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"time"
1414

1515
"cmd/go/internal/cfg"
16-
"cmd/go/internal/get"
1716
"cmd/go/internal/modfetch/codehost"
1817
"cmd/go/internal/par"
18+
"cmd/go/internal/vcs"
1919
web "cmd/go/internal/web"
2020

2121
"golang.org/x/mod/module"
@@ -261,13 +261,13 @@ func lookupDirect(path string) (Repo, error) {
261261
if allowInsecure(path) {
262262
security = web.Insecure
263263
}
264-
rr, err := get.RepoRootForImportPath(path, get.PreferMod, security)
264+
rr, err := vcs.RepoRootForImportPath(path, vcs.PreferMod, security)
265265
if err != nil {
266266
// We don't know where to find code for a module with this path.
267267
return nil, notExistError{err: err}
268268
}
269269

270-
if rr.VCS == "mod" {
270+
if rr.VCS.Name == "mod" {
271271
// Fetch module from proxy with base URL rr.Repo.
272272
return newProxyRepo(rr.Repo, path)
273273
}
@@ -279,8 +279,8 @@ func lookupDirect(path string) (Repo, error) {
279279
return newCodeRepo(code, rr.Root, path)
280280
}
281281

282-
func lookupCodeRepo(rr *get.RepoRoot) (codehost.Repo, error) {
283-
code, err := codehost.NewRepo(rr.VCS, rr.Repo)
282+
func lookupCodeRepo(rr *vcs.RepoRoot) (codehost.Repo, error) {
283+
code, err := codehost.NewRepo(rr.VCS.Cmd, rr.Repo)
284284
if err != nil {
285285
if _, ok := err.(*codehost.VCSError); ok {
286286
return nil, err
@@ -306,7 +306,7 @@ func ImportRepoRev(path, rev string) (Repo, *RevInfo, error) {
306306
if allowInsecure(path) {
307307
security = web.Insecure
308308
}
309-
rr, err := get.RepoRootForImportPath(path, get.IgnoreMod, security)
309+
rr, err := vcs.RepoRootForImportPath(path, vcs.IgnoreMod, security)
310310
if err != nil {
311311
return nil, nil, err
312312
}

src/cmd/go/internal/str/str_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package str
6+
7+
import "testing"
8+
9+
var foldDupTests = []struct {
10+
list []string
11+
f1, f2 string
12+
}{
13+
{StringList("math/rand", "math/big"), "", ""},
14+
{StringList("math", "strings"), "", ""},
15+
{StringList("strings"), "", ""},
16+
{StringList("strings", "strings"), "strings", "strings"},
17+
{StringList("Rand", "rand", "math", "math/rand", "math/Rand"), "Rand", "rand"},
18+
}
19+
20+
func TestFoldDup(t *testing.T) {
21+
for _, tt := range foldDupTests {
22+
f1, f2 := FoldDup(tt.list)
23+
if f1 != tt.f1 || f2 != tt.f2 {
24+
t.Errorf("foldDup(%q) = %q, %q, want %q, %q", tt.list, f1, f2, tt.f1, tt.f2)
25+
}
26+
}
27+
}

src/cmd/go/internal/get/discovery.go renamed to src/cmd/go/internal/vcs/discovery.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package get
5+
package vcs
66

77
import (
88
"encoding/xml"

src/cmd/go/internal/get/pkg_test.go renamed to src/cmd/go/internal/vcs/discovery_test.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,14 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package get
5+
package vcs
66

77
import (
8-
"cmd/go/internal/str"
98
"reflect"
109
"strings"
1110
"testing"
1211
)
1312

14-
var foldDupTests = []struct {
15-
list []string
16-
f1, f2 string
17-
}{
18-
{str.StringList("math/rand", "math/big"), "", ""},
19-
{str.StringList("math", "strings"), "", ""},
20-
{str.StringList("strings"), "", ""},
21-
{str.StringList("strings", "strings"), "strings", "strings"},
22-
{str.StringList("Rand", "rand", "math", "math/rand", "math/Rand"), "Rand", "rand"},
23-
}
24-
25-
func TestFoldDup(t *testing.T) {
26-
for _, tt := range foldDupTests {
27-
f1, f2 := str.FoldDup(tt.list)
28-
if f1 != tt.f1 || f2 != tt.f2 {
29-
t.Errorf("foldDup(%q) = %q, %q, want %q, %q", tt.list, f1, f2, tt.f1, tt.f2)
30-
}
31-
}
32-
}
33-
3413
var parseMetaGoImportsTests = []struct {
3514
in string
3615
mod ModuleMode

0 commit comments

Comments
 (0)