Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit f563362

Browse files
committed
config: add a way to see if a "remote" URL is local or not
This factors out some URL-parsing code from the transport layer so it can be used by config as well. Issue: #909 Signed-off-by: Jeremy Stribling <strib@alum.mit.edu>
1 parent efe6c8b commit f563362

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sort"
99
"strconv"
1010

11+
"gopkg.in/src-d/go-git.v4/internal/url"
1112
format "gopkg.in/src-d/go-git.v4/plumbing/format/config"
1213
)
1314

@@ -399,3 +400,7 @@ func (c *RemoteConfig) marshal() *format.Subsection {
399400

400401
return c.raw
401402
}
403+
404+
func (c *RemoteConfig) IsFirstURLLocal() bool {
405+
return url.IsLocalEndpoint(c.URLs[0])
406+
}

internal/url/url.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package url
2+
3+
import (
4+
"regexp"
5+
)
6+
7+
var (
8+
isSchemeRegExp = regexp.MustCompile(`^[^:]+://`)
9+
scpLikeUrlRegExp = regexp.MustCompile(`^(?:(?P<user>[^@]+)@)?(?P<host>[^:\s]+):(?:(?P<port>[0-9]{1,5})/)?(?P<path>[^\\].*)$`)
10+
)
11+
12+
// MatchesScheme returns true if the given string matches a URL-like
13+
// format scheme.
14+
func MatchesScheme(url string) bool {
15+
return isSchemeRegExp.MatchString(url)
16+
}
17+
18+
// MatchesScpLike returns true if the given string matches an SCP-like
19+
// format scheme.
20+
func MatchesScpLike(url string) bool {
21+
return scpLikeUrlRegExp.MatchString(url)
22+
}
23+
24+
// FindScpLikeComponents returns the user, host, port and path of the
25+
// given SCP-like URL.
26+
func FindScpLikeComponents(url string) (user, host, port, path string) {
27+
m := scpLikeUrlRegExp.FindStringSubmatch(url)
28+
return m[1], m[2], m[3], m[4]
29+
}
30+
31+
// IsLocalEndpoint returns true if the given URL string specifies a
32+
// local file endpoint. For example, on a Linux machine,
33+
// `/home/user/src/go-git` would match as a local endpoint, but
34+
// `https://github.com/src-d/go-git` would not.
35+
func IsLocalEndpoint(url string) bool {
36+
return !MatchesScheme(url) && !MatchesScpLike(url)
37+
}

plumbing/transport/common.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import (
1919
"fmt"
2020
"io"
2121
"net/url"
22-
"regexp"
2322
"strconv"
2423
"strings"
2524

25+
giturl "gopkg.in/src-d/go-git.v4/internal/url"
2626
"gopkg.in/src-d/go-git.v4/plumbing"
2727
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp"
2828
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability"
@@ -224,34 +224,28 @@ func getPath(u *url.URL) string {
224224
return res
225225
}
226226

227-
var (
228-
isSchemeRegExp = regexp.MustCompile(`^[^:]+://`)
229-
scpLikeUrlRegExp = regexp.MustCompile(`^(?:(?P<user>[^@]+)@)?(?P<host>[^:\s]+):(?:(?P<port>[0-9]{1,5})/)?(?P<path>[^\\].*)$`)
230-
)
231-
232227
func parseSCPLike(endpoint string) (*Endpoint, bool) {
233-
if isSchemeRegExp.MatchString(endpoint) || !scpLikeUrlRegExp.MatchString(endpoint) {
228+
if giturl.MatchesScheme(endpoint) || !giturl.MatchesScpLike(endpoint) {
234229
return nil, false
235230
}
236231

237-
m := scpLikeUrlRegExp.FindStringSubmatch(endpoint)
238-
239-
port, err := strconv.Atoi(m[3])
232+
user, host, portStr, path := giturl.FindScpLikeComponents(endpoint)
233+
port, err := strconv.Atoi(portStr)
240234
if err != nil {
241235
port = 22
242236
}
243237

244238
return &Endpoint{
245239
Protocol: "ssh",
246-
User: m[1],
247-
Host: m[2],
240+
User: user,
241+
Host: host,
248242
Port: port,
249-
Path: m[4],
243+
Path: path,
250244
}, true
251245
}
252246

253247
func parseFile(endpoint string) (*Endpoint, bool) {
254-
if isSchemeRegExp.MatchString(endpoint) {
248+
if giturl.MatchesScheme(endpoint) {
255249
return nil, false
256250
}
257251

0 commit comments

Comments
 (0)