Skip to content

Commit

Permalink
Fixing case where there are multiple go-import statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattfarina committed Oct 21, 2015
1 parent b677986 commit 0f2158e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.1.3 (2015-10-21)

- Fixing issue where there are multiple go-import statements for go redirects

# 1.1.2 (2015-10-20)

- Fixes #7: hg not checking out code when Get is called
Expand Down
56 changes: 21 additions & 35 deletions vcs_remote_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ func parseImportFromBody(ur *url.URL, r io.ReadCloser) (tp Type, u string, err e
t, err = d.Token()
if err != nil {
if err == io.EOF {
err = nil
// When the end is reached it could not detect a VCS if it
// got here.
err = ErrCannotDetectVCS
}
return
}
Expand All @@ -287,45 +289,29 @@ func parseImportFromBody(ur *url.URL, r io.ReadCloser) (tp Type, u string, err e
continue
}
if f := strings.Fields(attrValue(e.Attr, "content")); len(f) == 3 {

// If this the second time a go-import statement has been detected
// return an error. There should only be one import statement per
// html file. We don't simply return the first found in order to
// detect pages including more than one.
// Should this be a different error?
if tp != "" || u != "" {
tp = NoVCS
u = ""
err = ErrCannotDetectVCS
return
}

// If the prefix supplied by the remote system isn't a prefix to the
// url we're fetching return an error. This will work for exact
// matches and prefixes. For example, golang.org/x/net as a prefix
// will match for golang.org/x/net and golang.org/x/net/context.
// Should this be a different error?
// url we're fetching continue to look for other imports.
// This will work for exact matches and prefixes. For example,
// golang.org/x/net as a prefix will match for golang.org/x/net and
// golang.org/x/net/context.
vcsURL := ur.Host + ur.Path
if !strings.HasPrefix(vcsURL, f[0]) {
err = ErrCannotDetectVCS
continue
} else {
switch Type(f[1]) {
case Git:
tp = Git
case Svn:
tp = Svn
case Bzr:
tp = Bzr
case Hg:
tp = Hg
}

u = f[2]
return
}

// We check to make sure the string in the html document is one of
// the VCS we support. Do not want to blindly trust a string value
// in an HTML doc.
switch Type(f[1]) {
case Git:
tp = Git
case Svn:
tp = Svn
case Bzr:
tp = Bzr
case Hg:
tp = Hg
}

u = f[2]
}
}
}
Expand Down
1 change: 1 addition & 0 deletions vcs_remote_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestVCSLookup(t *testing.T) {
"https://example.com/foo/bar/baz.hg": {work: true, t: Hg},
"https://gopkg.in/tomb.v1": {work: true, t: Git},
"https://golang.org/x/net": {work: true, t: Git},
"https://speter.net/go/exp/math/dec/inf": {work: true, t: Git},
}

for u, c := range urlList {
Expand Down

0 comments on commit 0f2158e

Please sign in to comment.