Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

use github mirror for golang.org packages when getting golang.org http meta failed #1857

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
if err != nil {
return errors.Wrap(err, "init failed: unable to create a source manager")
}

sm.UseDefaultSignalHandling()
defer sm.Release()

Expand Down
34 changes: 34 additions & 0 deletions gps/deduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
hgSchemes = []string{"https", "ssh", "http"}
svnSchemes = []string{"https", "http", "svn", "svn+ssh"}
gopkginSchemes = []string{"https", "http"}
stdlibPrefix = "golang.org/x"
)

const gopkgUnstableSuffix = "-unstable"
Expand Down Expand Up @@ -74,6 +75,7 @@ var (
jazzRegex = regexp.MustCompile(`^(?P<root>hub\.jazz\.net(/git/[a-z0-9]+/[A-Za-z0-9_.\-]+))((?:/[A-Za-z0-9_.\-]+)*)$`)
apacheRegex = regexp.MustCompile(`^(?P<root>git\.apache\.org(/[a-z0-9_.\-]+\.git))((?:/[A-Za-z0-9_.\-]+)*)$`)
vcsExtensionRegex = regexp.MustCompile(`^(?P<root>([a-z0-9.\-]+\.)+[a-z0-9.\-]+(:[0-9]+)?/[A-Za-z0-9_.\-/~]*?\.(?P<vcs>bzr|git|hg|svn))((?:/[A-Za-z0-9_.\-]+)*)$`)
stdlibRegex = regexp.MustCompile(`^(?P<root>golang\.org/x(/[A-Za-z0-9_.\-]+))((?:/[A-Za-z0-9_.\-]+)*)$`)
)

// Other helper regexes
Expand Down Expand Up @@ -467,6 +469,38 @@ func (m apacheDeducer) deduceSource(path string, u *url.URL) (maybeSources, erro
return mb, nil
}

type stdlibDeducer struct {
regexp *regexp.Regexp
}

func (m stdlibDeducer) deduceRoot(path string) (string, error) {
v := m.regexp.FindStringSubmatch(path)
if v == nil {
return "", fmt.Errorf("%s is not a valid path for a source on golang.org", path)
}
return stdlibPrefix + v[2], nil
}

func (m stdlibDeducer) deduceSource(path string, u *url.URL) (maybeSources, error) {
v := m.regexp.FindStringSubmatch(path)
if v == nil {
return nil, fmt.Errorf("%s is not a valid path for a source on hub.jazz.net", path)
}

u.Host = "github.com"
u.Path = "/golang" + v[2]

switch u.Scheme {
case "":
u.Scheme = "https"
fallthrough
case "https":
return maybeSources{maybeGitSource{url: u}}, nil
default:
return nil, fmt.Errorf("golang.org use https, %s is not allowed", u.String())
}
}

type vcsExtensionDeducer struct {
regexp *regexp.Regexp
}
Expand Down
7 changes: 7 additions & 0 deletions gps/source_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,13 @@ func (sm *SourceMgr) DeduceProjectRoot(ip string) (ProjectRoot, error) {
}

pd, err := sm.deduceCoord.deduceRootPath(context.TODO(), ip)

// as we can not deduce root path from golang.org,
// try github mirror and add golang.org/x rules to Trie tree
if err != nil && strings.HasPrefix(ip, stdlibPrefix) {
sm.deduceCoord.deducext.Insert(stdlibPrefix, stdlibDeducer{regexp: stdlibRegex})
pd, err = sm.deduceCoord.deduceKnownPaths(ip)
}
return ProjectRoot(pd.root), err
}

Expand Down