aboutsummaryrefslogtreecommitdiffstats
path: root/internal
diff options
context:
space:
mode:
authorJeremy Stribling <strib@users.noreply.github.com>2019-02-13 07:28:28 -0800
committerGitHub <noreply@github.com>2019-02-13 07:28:28 -0800
commit2ab6d5cd72b59cfd36b08078ddeebd1efb0d2254 (patch)
treea398b9df6d0d1afd13b6923393766a96b2a7af2d /internal
parentdcc9f375f4daedfef61e7ff2fbbfd851d42a72d8 (diff)
parent3889c6446da5f9d658b9bfe317429196e25aa4b7 (diff)
downloadgo-git-2ab6d5cd72b59cfd36b08078ddeebd1efb0d2254.tar.gz
Merge pull request #1066 from keybase/strib/909-git-push-speedup-when-local
remote: speed up pushes when the "remote" repo is local
Diffstat (limited to 'internal')
-rw-r--r--internal/url/url.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/internal/url/url.go b/internal/url/url.go
new file mode 100644
index 0000000..0f0d709
--- /dev/null
+++ b/internal/url/url.go
@@ -0,0 +1,37 @@
+package url
+
+import (
+ "regexp"
+)
+
+var (
+ isSchemeRegExp = regexp.MustCompile(`^[^:]+://`)
+ scpLikeUrlRegExp = regexp.MustCompile(`^(?:(?P<user>[^@]+)@)?(?P<host>[^:\s]+):(?:(?P<port>[0-9]{1,5})/)?(?P<path>[^\\].*)$`)
+)
+
+// MatchesScheme returns true if the given string matches a URL-like
+// format scheme.
+func MatchesScheme(url string) bool {
+ return isSchemeRegExp.MatchString(url)
+}
+
+// MatchesScpLike returns true if the given string matches an SCP-like
+// format scheme.
+func MatchesScpLike(url string) bool {
+ return scpLikeUrlRegExp.MatchString(url)
+}
+
+// FindScpLikeComponents returns the user, host, port and path of the
+// given SCP-like URL.
+func FindScpLikeComponents(url string) (user, host, port, path string) {
+ m := scpLikeUrlRegExp.FindStringSubmatch(url)
+ return m[1], m[2], m[3], m[4]
+}
+
+// IsLocalEndpoint returns true if the given URL string specifies a
+// local file endpoint. For example, on a Linux machine,
+// `/home/user/src/go-git` would match as a local endpoint, but
+// `https://github.com/src-d/go-git` would not.
+func IsLocalEndpoint(url string) bool {
+ return !MatchesScheme(url) && !MatchesScpLike(url)
+}