1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
package vcs
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
)
type vcsInfo struct {
host string
pattern string
vcs Type
addCheck func(m map[string]string, u *url.URL) (Type, error)
regex *regexp.Regexp
}
// scpSyntaxRe matches the SCP-like addresses used by Git to access
// repositories by SSH.
var scpSyntaxRe = regexp.MustCompile(`^([a-zA-Z0-9_]+)@([a-zA-Z0-9._-]+):(.*)$`)
var vcsList = []*vcsInfo{
{
host: "github.com",
vcs: Git,
pattern: `^(github\.com[/|:][A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`,
},
{
host: "bitbucket.org",
pattern: `^(bitbucket\.org/(?P<name>[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+))(/[A-Za-z0-9_.\-]+)*$`,
addCheck: checkBitbucket,
},
{
host: "launchpad.net",
pattern: `^(launchpad\.net/(([A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)?|~[A-Za-z0-9_.\-]+/(\+junk|[A-Za-z0-9_.\-]+)/[A-Za-z0-9_.\-]+))(/[A-Za-z0-9_.\-]+)*$`,
vcs: Bzr,
},
{
host: "git.launchpad.net",
vcs: Git,
pattern: `^(git\.launchpad\.net/(([A-Za-z0-9_.\-]+)|~[A-Za-z0-9_.\-]+/(\+git|[A-Za-z0-9_.\-]+)/[A-Za-z0-9_.\-]+))$`,
},
{
host: "hub.jazz.net",
vcs: Git,
pattern: `^(hub\.jazz\.net/git/[a-z0-9]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`,
},
{
host: "go.googlesource.com",
vcs: Git,
pattern: `^(go\.googlesource\.com/[A-Za-z0-9_.\-]+/?)$`,
},
{
host: "git.openstack.org",
vcs: Git,
pattern: `^(git\.openstack\.org/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)$`,
},
// If none of the previous detect the type they will fall to this looking for the type in a generic sense
// by the extension to the path.
{
addCheck: checkURL,
pattern: `\.(?P<type>git|hg|svn|bzr)$`,
},
}
func init() {
// Precompile the regular expressions used to check VCS locations.
for _, v := range vcsList {
v.regex = regexp.MustCompile(v.pattern)
}
}
// This function is really a hack around Go redirects rather than around
// something VCS related. Should this be moved to the glide project or a
// helper function?
func detectVcsFromRemote(vcsURL string) (Type, string, error) {
t, e := detectVcsFromURL(vcsURL)
if e == nil {
return t, vcsURL, nil
} else if e != ErrCannotDetectVCS {
return NoVCS, "", e
}
// Pages like https://golang.org/x/net provide an html document with
// meta tags containing a location to work with. The go tool uses
// a meta tag with the name go-import which is what we use here.
// godoc.org also has one call go-source that we do not need to use.
// The value of go-import is in the form "prefix vcs repo". The prefix
// should match the vcsURL and the repo is a location that can be
// checked out. Note, to get the html document you you need to add
// ?go-get=1 to the url.
u, err := url.Parse(vcsURL)
if err != nil {
return NoVCS, "", err
}
if u.RawQuery == "" {
u.RawQuery = "go-get=1"
} else {
u.RawQuery = u.RawQuery + "+go-get=1"
}
checkURL := u.String()
resp, err := http.Get(checkURL)
if err != nil {
return NoVCS, "", ErrCannotDetectVCS
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
if resp.StatusCode == 404 {
return NoVCS, "", NewRemoteError(fmt.Sprintf("%s Not Found", vcsURL), nil, "")
} else if resp.StatusCode == 401 || resp.StatusCode == 403 {
return NoVCS, "", NewRemoteError(fmt.Sprintf("%s Access Denied", vcsURL), nil, "")
}
return NoVCS, "", ErrCannotDetectVCS
}
t, nu, err := parseImportFromBody(u, resp.Body)
if err != nil {
// TODO(mattfarina): Log the parsing error
return NoVCS, "", ErrCannotDetectVCS
} else if t == "" || nu == "" {
return NoVCS, "", ErrCannotDetectVCS
}
return t, nu, nil
}
// From a remote vcs url attempt to detect the VCS.
func detectVcsFromURL(vcsURL string) (Type, error) {
var u *url.URL
var err error
if m := scpSyntaxRe.FindStringSubmatch(vcsURL); m != nil {
// Match SCP-like syntax and convert it to a URL.
// Eg, "git@github.com:user/repo" becomes
// "ssh://git@github.com/user/repo".
u = &url.URL{
Scheme: "ssh",
User: url.User(m[1]),
Host: m[2],
Path: "/" + m[3],
}
} else {
u, err = url.Parse(vcsURL)
if err != nil {
return "", err
}
}
// Detect file schemes
if u.Scheme == "file" {
return DetectVcsFromFS(u.Path)
}
if u.Host == "" {
return "", ErrCannotDetectVCS
}
// Try to detect from the scheme
switch u.Scheme {
case "git+ssh":
return Git, nil
case "git":
return Git, nil
case "bzr+ssh":
return Bzr, nil
case "svn+ssh":
return Svn, nil
}
// Try to detect from known hosts, such as Github
for _, v := range vcsList {
if v.host != "" && v.host != u.Host {
continue
}
// Make sure the pattern matches for an actual repo location. For example,
// we should fail if the VCS listed is github.com/masterminds as that's
// not actually a repo.
uCheck := u.Host + u.Path
m := v.regex.FindStringSubmatch(uCheck)
if m == nil {
if v.host != "" {
return "", ErrCannotDetectVCS
}
continue
}
// If we are here the host matches. If the host has a singular
// VCS type, such as Github, we can return the type right away.
if v.vcs != "" {
return v.vcs, nil
}
// Run additional checks to determine try and determine the repo
// for the matched service.
info := make(map[string]string)
for i, name := range v.regex.SubexpNames() {
if name != "" {
info[name] = m[i]
}
}
t, err := v.addCheck(info, u)
if err != nil {
switch err.(type) {
case *RemoteError:
return "", err
}
return "", ErrCannotDetectVCS
}
return t, nil
}
// Attempt to ascertain from the username passed in.
if u.User != nil {
un := u.User.Username()
if un == "git" {
return Git, nil
} else if un == "hg" {
return Hg, nil
}
}
// Unable to determine the vcs from the url.
return "", ErrCannotDetectVCS
}
// Figure out the type for Bitbucket by the passed in information
// or via the public API.
func checkBitbucket(i map[string]string, ul *url.URL) (Type, error) {
// Fast path for ssh urls where we may not even be able to
// anonymously get details from the API.
if ul.User != nil {
un := ul.User.Username()
if un == "git" {
return Git, nil
} else if un == "hg" {
return Hg, nil
}
}
// The part of the response we care about.
var response struct {
SCM Type `json:"scm"`
}
u := expand(i, "https://api.bitbucket.org/1.0/repositories/{name}")
data, err := get(u)
if err != nil {
return "", err
}
if err := json.Unmarshal(data, &response); err != nil {
return "", fmt.Errorf("Decoding error %s: %v", u, err)
}
return response.SCM, nil
}
// Expect a type key on i with the exact type detected from the regex.
func checkURL(i map[string]string, u *url.URL) (Type, error) {
return Type(i["type"]), nil
}
func get(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
if resp.StatusCode == 404 {
return nil, NewRemoteError("Not Found", err, resp.Status)
} else if resp.StatusCode == 401 || resp.StatusCode == 403 {
return nil, NewRemoteError("Access Denied", err, resp.Status)
}
return nil, fmt.Errorf("%s: %s", url, resp.Status)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("%s: %v", url, err)
}
return b, nil
}
func expand(match map[string]string, s string) string {
for k, v := range match {
s = strings.Replace(s, "{"+k+"}", v, -1)
}
return s
}
func parseImportFromBody(ur *url.URL, r io.ReadCloser) (tp Type, u string, err error) {
d := xml.NewDecoder(r)
d.CharsetReader = charsetReader
d.Strict = false
var t xml.Token
for {
t, err = d.Token()
if err != nil {
if err == io.EOF {
// When the end is reached it could not detect a VCS if it
// got here.
err = ErrCannotDetectVCS
}
return
}
if e, ok := t.(xml.StartElement); ok && strings.EqualFold(e.Name.Local, "body") {
return
}
if e, ok := t.(xml.EndElement); ok && strings.EqualFold(e.Name.Local, "head") {
return
}
e, ok := t.(xml.StartElement)
if !ok || !strings.EqualFold(e.Name.Local, "meta") {
continue
}
if attrValue(e.Attr, "name") != "go-import" {
continue
}
if f := strings.Fields(attrValue(e.Attr, "content")); len(f) == 3 {
// If the prefix supplied by the remote system isn't a prefix to the
// 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]) {
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
}
}
}
}
func charsetReader(charset string, input io.Reader) (io.Reader, error) {
switch strings.ToLower(charset) {
case "ascii":
return input, nil
default:
return nil, fmt.Errorf("can't decode XML document using charset %q", charset)
}
}
func attrValue(attrs []xml.Attr, name string) string {
for _, a := range attrs {
if strings.EqualFold(a.Name.Local, name) {
return a.Value
}
}
return ""
}
|