forked from Masterminds/vcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcs_remote_lookup.go
335 lines (296 loc) · 8.54 KB
/
vcs_remote_lookup.go
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
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) (Type, error)
regex *regexp.Regexp
}
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: "go.googlesource.com",
vcs: Git,
pattern: `^(go\.googlesource\.com/[A-Za-z0-9_.\-]+/?)$`,
},
// TODO: Once Google Code becomes fully deprecated this can be removed.
{
host: "code.google.com",
addCheck: checkGoogle,
pattern: `^(code\.google\.com/[pr]/(?P<project>[a-z0-9\-]+)(\.(?P<repo>[a-z0-9\-]+))?)(/[A-Za-z0-9_.\-]+)*$`,
},
// Alternative Google setup. This is the previous structure but it still works... until Google Code goes away.
{
addCheck: checkURL,
pattern: `^([a-z0-9_\-.]+)\.googlecode\.com/(?P<type>git|hg|svn)(/.*)?$`,
},
// 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
}
// Need to test for vanity or paths like golang.org/x/
// TODO: Test for 3xx redirect codes and handle appropriately.
// 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()
t, nu, err := parseImportFromBody(u, resp.Body)
if err != nil {
return NoVCS, "", err
} 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) {
u, err := url.Parse(vcsURL)
if err != nil {
return "", err
}
// If there is no host found we cannot detect the VCS from
// the url. Note, URIs beginning with git@github using the ssh
// syntax fail this check.
if u.Host == "" {
return "", ErrCannotDetectVCS
}
// 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)
if err != nil {
return "", ErrCannotDetectVCS
}
return t, nil
}
// Unable to determine the vcs from the url.
return "", ErrCannotDetectVCS
}
// Bitbucket provides an API for checking the VCS.
func checkBitbucket(i map[string]string) (Type, error) {
// 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
}
// Google supports Git, Hg, and Svn. The SVN style is only
// supported through their legacy setup at <project>.googlecode.com.
// I wonder if anyone is actually using SVN support.
func checkGoogle(i map[string]string) (Type, error) {
// To figure out which of the VCS types is used in Google Code you need
// to parse a web page and find it. Ugh. I mean... ugh.
var hack = regexp.MustCompile(`id="checkoutcmd">(hg|git|svn)`)
d, err := get(expand(i, "https://code.google.com/p/{project}/source/checkout?repo={repo}"))
if err != nil {
return "", err
}
if m := hack.FindSubmatch(d); m != nil {
if vcs := string(m[1]); vcs != "" {
if vcs == "svn" {
// While Google supports SVN it can only be used with the legacy
// urls of <project>.googlecode.com. I considered creating a new
// error for this problem but Google Code is going away and there
// is support for the legacy structure.
return "", ErrCannotDetectVCS
}
return Type(vcs), nil
}
}
return "", ErrCannotDetectVCS
}
// Expect a type key on i with the exact type detected from the regex.
func checkURL(i map[string]string) (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 {
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 ""
}