go/vcs: match go-import package prefixes by slash
This is related to golang/go#15947. This ports https://golang.org/cl/23732 (June 2016) from cmd/go to this repository. Change-Id: I9960556bd19775b433d4eecc0ecca3f09ce1368b Reviewed-on: https://go-review.googlesource.com/119935 Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
53440098a4
commit
fcb3cb6c24
|
@ -603,15 +603,28 @@ type metaImport struct {
|
||||||
// errNoMatch is returned from matchGoImport when there's no applicable match.
|
// errNoMatch is returned from matchGoImport when there's no applicable match.
|
||||||
var errNoMatch = errors.New("no import match")
|
var errNoMatch = errors.New("no import match")
|
||||||
|
|
||||||
|
// pathPrefix reports whether sub is a prefix of s,
|
||||||
|
// only considering entire path components.
|
||||||
|
func pathPrefix(s, sub string) bool {
|
||||||
|
// strings.HasPrefix is necessary but not sufficient.
|
||||||
|
if !strings.HasPrefix(s, sub) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// The remainder after the prefix must either be empty or start with a slash.
|
||||||
|
rem := s[len(sub):]
|
||||||
|
return rem == "" || rem[0] == '/'
|
||||||
|
}
|
||||||
|
|
||||||
// matchGoImport returns the metaImport from imports matching importPath.
|
// matchGoImport returns the metaImport from imports matching importPath.
|
||||||
// An error is returned if there are multiple matches.
|
// An error is returned if there are multiple matches.
|
||||||
// errNoMatch is returned if none match.
|
// errNoMatch is returned if none match.
|
||||||
func matchGoImport(imports []metaImport, importPath string) (_ metaImport, err error) {
|
func matchGoImport(imports []metaImport, importPath string) (_ metaImport, err error) {
|
||||||
match := -1
|
match := -1
|
||||||
for i, im := range imports {
|
for i, im := range imports {
|
||||||
if !strings.HasPrefix(importPath, im.Prefix) {
|
if !pathPrefix(importPath, im.Prefix) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if match != -1 {
|
if match != -1 {
|
||||||
err = fmt.Errorf("multiple meta tags match import path %q", importPath)
|
err = fmt.Errorf("multiple meta tags match import path %q", importPath)
|
||||||
return
|
return
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
package vcs
|
package vcs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
@ -184,3 +185,96 @@ func TestValidateRepoRoot(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMatchGoImport(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
imports []metaImport
|
||||||
|
path string
|
||||||
|
mi metaImport
|
||||||
|
err error
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
imports: []metaImport{
|
||||||
|
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
},
|
||||||
|
path: "example.com/user/foo",
|
||||||
|
mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
imports: []metaImport{
|
||||||
|
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
},
|
||||||
|
path: "example.com/user/foo/",
|
||||||
|
mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
imports: []metaImport{
|
||||||
|
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
},
|
||||||
|
path: "example.com/user/foo",
|
||||||
|
mi: metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
imports: []metaImport{
|
||||||
|
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
},
|
||||||
|
path: "example.com/user/fooa",
|
||||||
|
mi: metaImport{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
imports: []metaImport{
|
||||||
|
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
},
|
||||||
|
path: "example.com/user/foo/bar",
|
||||||
|
err: errors.New("should not be allowed to create nested repo"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
imports: []metaImport{
|
||||||
|
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
},
|
||||||
|
path: "example.com/user/foo/bar/baz",
|
||||||
|
err: errors.New("should not be allowed to create nested repo"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
imports: []metaImport{
|
||||||
|
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
},
|
||||||
|
path: "example.com/user/foo/bar/baz/qux",
|
||||||
|
err: errors.New("should not be allowed to create nested repo"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
imports: []metaImport{
|
||||||
|
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
},
|
||||||
|
path: "example.com/user/foo/bar/baz/",
|
||||||
|
err: errors.New("should not be allowed to create nested repo"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
imports: []metaImport{
|
||||||
|
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
|
||||||
|
},
|
||||||
|
path: "example.com",
|
||||||
|
err: errors.New("pathologically short path"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
mi, err := matchGoImport(test.imports, test.path)
|
||||||
|
if mi != test.mi {
|
||||||
|
t.Errorf("unexpected metaImport; got %v, want %v", mi, test.mi)
|
||||||
|
}
|
||||||
|
|
||||||
|
got := err
|
||||||
|
want := test.err
|
||||||
|
if (got == nil) != (want == nil) {
|
||||||
|
t.Errorf("unexpected error; got %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue