-
Notifications
You must be signed in to change notification settings - Fork 7
/
vcsurl.go
298 lines (245 loc) · 6.27 KB
/
vcsurl.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
package vcsurl
import (
"errors"
"fmt"
"net/url"
"path/filepath"
"regexp"
"strings"
)
// Errors returned by Parse function.
var (
ErrUnknownURL = errors.New("unknown URL format")
ErrUnableParse = errors.New("unable to determine name or full name")
ErrEmptyURL = errors.New("empty URL")
ErrEmptyPath = errors.New("empty path in URL")
ErrUnknownProtocol = errors.New("remote protocol should be SSH or HTTPS")
ErrUnsupportedProtocol = errors.New("unsupported remote protocol")
)
// Host VCS provider.
type Host string
// Supported VCS host provider.
const (
GitHub Host = "github.com"
Bitbucket Host = "bitbucket.org"
GitLab Host = "gitlab.com"
gitHubAPI Host = "api.github.com"
)
// Kind of VCS
type Kind string
// Supported VCS kinds.
const (
Git Kind = "git"
)
// Protocol of remote
type Protocol string
// Supported VCS protocols.
const (
SSH Protocol = "ssh"
HTTPS Protocol = "https"
)
var knownHosts = map[Host]struct{}{
GitHub: struct{}{},
GitLab: struct{}{},
Bitbucket: struct{}{},
}
var kindByHost = map[Host]Kind{
GitHub: Git,
gitHubAPI: Git,
GitLab: Git,
Bitbucket: Git,
}
// VCS describes a VCS repository.
type VCS struct {
// ID unique repository identification.
ID string
// Kind of VCS.
Kind Kind
// Host is the public web of the repository.
Host Host
// Username of repo owner on repo hosting site.
Username string
// Name base name of repo on repo hosting site.
Name string
// FullName full name of repo on repo hosting site.
FullName string
// Committish is a reference to an object that can be recursively
// dereferenced to a commit object. They can be commits, tags or branches.
Committish string
// Raw is the original parsed URL.
Raw string
}
var (
removeDotGit = regexp.MustCompile(`\.git$`)
gitPreprocessRE = regexp.MustCompile("^git@([a-zA-Z0-9-_\\.]+)\\:(.*)$")
)
// Parse parses a string that resembles a VCS repository URL. See TestParse for
// a list of supported URL formats.
func Parse(raw string) (*VCS, error) {
if len(raw) == 0 {
return nil, fmt.Errorf("parse %q: %w", raw, ErrEmptyURL)
}
spec := raw
if parts := gitPreprocessRE.FindStringSubmatch(spec); len(parts) == 3 {
spec = fmt.Sprintf("git://%s/%s", parts[1], parts[2])
}
parsedURL, err := url.Parse(spec)
if err != nil {
return nil, err
}
if parsedURL.Scheme == "" {
spec = "https://" + spec
if parsedURL, err = url.Parse(spec); err != nil {
return nil, err
}
}
vcs := &VCS{}
vcs.Raw = raw
vcs.Host = Host(parsedURL.Host)
vcs.Kind = kindByHost[vcs.Host]
switch vcs.Host {
case GitHub, gitHubAPI:
err = vcs.parseGitHub(parsedURL)
case Bitbucket:
err = vcs.parseBitbucket(parsedURL)
case GitLab:
err = vcs.parseGitlab(parsedURL)
default:
err = vcs.parseDefault(parsedURL)
}
if err != nil {
return nil, fmt.Errorf("parse %q: %w", raw, err)
}
if vcs.ID == "" {
vcs.ID = fmt.Sprintf("%s/%s", string(vcs.Host), vcs.FullName)
}
return vcs, nil
}
func (v *VCS) parseGitHub(url *url.URL) error {
parts := strings.Split(url.Path, "/")
if v.Host == gitHubAPI {
v.Host = GitHub
if len(parts) < 2 || parts[1] != "repos" {
return ErrUnknownURL
}
parts = parts[1:]
}
if len(parts) < 3 {
return ErrUnknownURL
}
v.Username = parts[1]
v.Name = removeDotGit.ReplaceAllLiteralString(parts[2], "")
v.FullName = v.Username + "/" + v.Name
if len(parts) < 5 {
return nil
}
if _, ok := githubCommittishParts[parts[3]]; ok {
v.Committish = strings.Join(parts[4:], "/")
return nil
}
if len(parts) >= 6 && parts[3] == "releases" {
v.Committish = parts[5]
}
return nil
}
var githubCommittishParts = map[string]struct{}{
"commits": struct{}{},
"commit": struct{}{},
"tree": struct{}{},
"branches": struct{}{},
}
func (v *VCS) parseBitbucket(url *url.URL) error {
parts := strings.Split(url.Path, "/")
if len(parts) < 3 {
return ErrUnknownURL
}
v.Username = parts[1]
v.Name = removeDotGit.ReplaceAllLiteralString(parts[2], "")
v.FullName = v.Username + "/" + v.Name
if len(parts) >= 5 && (parts[3] == "src" || parts[3] == "commits" || parts[3] == "branch") {
v.Committish = parts[4]
}
return nil
}
func (v *VCS) parseGitlab(url *url.URL) error {
parts := strings.Split(url.Path, "/")
if len(parts) < 3 {
return ErrUnknownURL
}
var last int
for _, p := range parts {
if p == "-" {
break
}
last++
}
v.Username = strings.Join(parts[1:last-1], "/")
v.Name = removeDotGit.ReplaceAllLiteralString(parts[last-1], "")
v.FullName = v.Username + "/" + v.Name
if len(parts) >= (last + 2) {
object := parts[last+1]
if object == "tags" || object == "commit" || object == "tree" {
v.Committish = strings.Join(parts[last+2:], "/")
}
}
return nil
}
func (v *VCS) parseDefault(url *url.URL) error {
path := url.Path
if len(path) == 0 {
return ErrEmptyPath
}
path = path[1:] // remove leading slash
path = removeDotGit.ReplaceAllLiteralString(path, "")
v.FullName = path
v.Name = filepath.Base(path)
if strings.Contains(url.String(), "git") {
v.Kind = Git
}
if v.Name == "" || v.FullName == "" {
return ErrUnableParse
}
return nil
}
// Remote returns a remote URL in the given protocol. ErrUnsupportedProtocol
// is returned if the protocol it's not supported by the VCS.
func (v *VCS) Remote(p Protocol) (string, error) {
if _, ok := knownHosts[v.Host]; !ok {
return v.remoteUnknownHost(p)
}
switch p {
case SSH:
return v.sshRemote(), nil
case HTTPS:
return v.httpsRemote(), nil
}
return "", ErrUnknownProtocol
}
func (v *VCS) remoteUnknownHost(p Protocol) (string, error) {
switch p {
case SSH:
if gitPreprocessRE.MatchString(v.Raw) {
return v.Raw, nil
}
case HTTPS:
parsed, _ := url.Parse(v.Raw)
if parsed.Scheme == "https" {
return v.Raw, nil
}
default:
return "", ErrUnknownProtocol
}
return "", ErrUnsupportedProtocol
}
// [email protected]:commento/docs.git
// [email protected]:go-git/go-git.git
// git clone [email protected]:mcuadros/discovery-rest.git
func (v *VCS) sshRemote() string {
return fmt.Sprintf("git@%s:%s/%s.git", v.Host, v.Username, v.Name)
}
// https://[email protected]/mcuadros/discovery-rest.git
// https://gitlab.com/commento/docs.git
// https://github.com/go-git/go-git.git
func (v *VCS) httpsRemote() string {
return fmt.Sprintf("https://%s/%s/%s.git", v.Host, v.Username, v.Name)
}