forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
url.go
194 lines (174 loc) · 5.03 KB
/
url.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
package gitserver
import (
"fmt"
"net/url"
"path/filepath"
"regexp"
"runtime"
"strings"
)
// According to git-clone(1), a "Git URL" can be in one of three broad types:
// 1) A standards-compliant URL.
// a) The scheme may be followed by '://',
// e.g. https://github.com/openshift/origin, file:///foo/bar, etc. In
// this case, note that among other things, a standards-compliant file URL
// must have an empty host part, an absolute path and no backslashes. The
// Git for Windows URL parser accepts many non-compliant URLs, but we
// don't.
// b) Alternatively, the scheme may be followed by '::', in which case it is
// treated as an transport/opaque address pair, e.g.
// http::http://github.com/openshift/origin.git .
// 2) The "alternative scp-like syntax", including a ':' with no preceding '/',
// but not of the form C:... on Windows, e.g.
// git@github.com:openshift/origin, etc.
// 3) An OS-specific relative or absolute local file path, e.g. foo/bar,
// C:\foo\bar, etc.
//
// We extend all of the above URL types to additionally accept an optional
// appended #fragment, which is given to specify a git reference.
//
// The git client allows Git URL rewriting rules to be defined. The meaning of
// a Git URL cannot be 100% guaranteed without consulting the rewriting rules.
// URLType indicates the type of the URL (see above)
type URLType int
const (
// URLTypeURL is the URL type (see above)
URLTypeURL URLType = iota
// URLTypeSCP is the SCP type (see above)
URLTypeSCP
// URLTypeLocal is the local type (see above)
URLTypeLocal
)
// String returns a string representation of the URLType
func (t URLType) String() string {
switch t {
case URLTypeURL:
return "URLTypeURL"
case URLTypeSCP:
return "URLTypeSCP"
case URLTypeLocal:
return "URLTypeLocal"
}
panic("unknown URLType")
}
// GoString returns a Go string representation of the URLType
func (t URLType) GoString() string {
return t.String()
}
// URL represents a "Git URL"
type URL struct {
URL url.URL
Type URLType
}
var urlSchemeRegexp = regexp.MustCompile("(?i)^[a-z][-a-z0-9+.]*:") // matches scheme: according to RFC3986
var dosDriveRegexp = regexp.MustCompile("(?i)^[a-z]:")
var scpRegexp = regexp.MustCompile("^" +
"(?:([^@/]*)@)?" + // user@ (optional)
"([^/]*):" + // host:
"(.*)" + // path
"$")
func splitOnByte(s string, c byte) (string, string) {
if i := strings.IndexByte(s, c); i != -1 {
return s[:i], s[i+1:]
}
return s, ""
}
// ParseURL parses a "Git URL"
func ParseURL(rawurl string) (*URL, error) {
if urlSchemeRegexp.MatchString(rawurl) &&
(runtime.GOOS != "windows" || !dosDriveRegexp.MatchString(rawurl)) {
u, err := url.Parse(rawurl)
if err != nil {
return nil, err
}
if u.Scheme == "file" && u.Opaque == "" {
if u.Host != "" {
return nil, fmt.Errorf("file url %q has non-empty host %q", rawurl, u.Host)
}
if runtime.GOOS == "windows" && (len(u.Path) == 0 || !filepath.IsAbs(u.Path[1:])) {
return nil, fmt.Errorf("file url %q has non-absolute path %q", rawurl, u.Path)
}
}
return &URL{
URL: *u,
Type: URLTypeURL,
}, nil
}
s, fragment := splitOnByte(rawurl, '#')
if m := scpRegexp.FindStringSubmatch(s); m != nil &&
(runtime.GOOS != "windows" || !dosDriveRegexp.MatchString(s)) {
u := &url.URL{
Host: m[2],
Path: m[3],
Fragment: fragment,
}
if m[1] != "" {
u.User = url.User(m[1])
}
return &URL{
URL: *u,
Type: URLTypeSCP,
}, nil
}
return &URL{
URL: url.URL{
Path: s,
Fragment: fragment,
},
Type: URLTypeLocal,
}, nil
}
// MustParse parses a "Git URL" and panics on failure
func MustParse(rawurl string) *URL {
u, err := ParseURL(rawurl)
if err != nil {
panic(err)
}
return u
}
// String returns a string representation of the URL
func (u URL) String() string {
var s string
switch u.Type {
case URLTypeURL:
return u.URL.String()
case URLTypeSCP:
if u.URL.User != nil {
s = u.URL.User.Username() + "@"
}
s += u.URL.Host + ":" + u.URL.Path
case URLTypeLocal:
s = u.URL.Path
}
if u.URL.RawQuery != "" {
s += "?" + u.URL.RawQuery
}
if u.URL.Fragment != "" {
s += "#" + u.URL.Fragment
}
return s
}
// StringNoFragment returns a string representation of the URL without its
// fragment
func (u URL) StringNoFragment() string {
u.URL.Fragment = ""
return u.String()
}
// IsLocal returns true if the Git URL refers to a local repository
func (u URL) IsLocal() bool {
return u.Type == URLTypeLocal || (u.Type == URLTypeURL && u.URL.Scheme == "file" && u.URL.Opaque == "")
}
// LocalPath returns the path to a local repository in OS-native format. It is
// assumed that IsLocal() is true
func (u URL) LocalPath() string {
switch {
case u.Type == URLTypeLocal:
return u.URL.Path
case u.Type == URLTypeURL && u.URL.Scheme == "file" && u.URL.Opaque == "":
if runtime.GOOS == "windows" && len(u.URL.Path) > 0 && u.URL.Path[0] == '/' {
return filepath.FromSlash(u.URL.Path[1:])
}
return filepath.FromSlash(u.URL.Path)
}
panic("LocalPath called on non-local URL")
}