-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support SSH endpoints and basic auth for HTTP endpoints (#98)
* Add support for cloning over SSH * Add interactive prompt for HTTP auth when cloning a private repo
- Loading branch information
1 parent
571e366
commit 5f2ed78
Showing
4 changed files
with
238 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Package url contains functions for parsing remote urls | ||
package url | ||
|
||
import ( | ||
"regexp" | ||
) | ||
|
||
var ( | ||
isSchemeRegExp = regexp.MustCompile(`^[^:]+://`) | ||
|
||
// Ref: https://github.com/git/git/blob/master/Documentation/urls.txt#L37 | ||
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) | ||
} | ||
|
||
// IsRemoteEndpoint returns true if the giver URL string specifies | ||
// a remote endpoint. For example, on a Linux machine, | ||
// `https://github.com/src-d/go-git` would match as a remote | ||
// endpoint, but `/home/user/src/go-git` would not. | ||
func IsRemoteEndpoint(url string) bool { | ||
return MatchesScheme(url) || MatchesScpLike(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 !IsRemoteEndpoint(url) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package url | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMatchesScpLike(t *testing.T) { | ||
// See https://github.com/git/git/blob/master/Documentation/urls.txt#L37 | ||
examples := []string{ | ||
// Most-extended case | ||
"[email protected]:james/bond", | ||
// Most-extended case with port | ||
"[email protected]:22:james/bond", | ||
// Most-extended case with numeric path | ||
"[email protected]:007/bond", | ||
// Most-extended case with port and numeric "username" | ||
"[email protected]:22:007/bond", | ||
// Single repo path | ||
"[email protected]:bond", | ||
// Single repo path with port | ||
"[email protected]:22:bond", | ||
// Single repo path with port and numeric repo | ||
"[email protected]:22:007", | ||
// Repo path ending with .git and starting with _ | ||
"[email protected]:22:_007.git", | ||
"[email protected]:_007.git", | ||
"[email protected]:_james.git", | ||
"[email protected]:_james/bond.git", | ||
} | ||
|
||
for _, url := range examples { | ||
t.Run(url, func(t *testing.T) { | ||
assert.Equal(t, true, MatchesScpLike(url)) | ||
}) | ||
} | ||
} | ||
|
||
func TestFindScpLikeComponents(t *testing.T) { | ||
testCases := []struct { | ||
url, user, host, port, path string | ||
}{ | ||
{ | ||
// Most-extended case | ||
url: "[email protected]:james/bond", user: "git", host: "github.com", port: "", path: "james/bond", | ||
}, | ||
{ | ||
// Most-extended case with port | ||
url: "[email protected]:22:james/bond", user: "git", host: "github.com", port: "22", path: "james/bond", | ||
}, | ||
{ | ||
// Most-extended case with numeric path | ||
url: "[email protected]:007/bond", user: "git", host: "github.com", port: "", path: "007/bond", | ||
}, | ||
{ | ||
// Most-extended case with port and numeric path | ||
url: "[email protected]:22:007/bond", user: "git", host: "github.com", port: "22", path: "007/bond", | ||
}, | ||
{ | ||
// Single repo path | ||
url: "[email protected]:bond", user: "git", host: "github.com", port: "", path: "bond", | ||
}, | ||
{ | ||
// Single repo path with port | ||
url: "[email protected]:22:bond", user: "git", host: "github.com", port: "22", path: "bond", | ||
}, | ||
{ | ||
// Single repo path with port and numeric path | ||
url: "[email protected]:22:007", user: "git", host: "github.com", port: "22", path: "007", | ||
}, | ||
{ | ||
// Repo path ending with .git and starting with _ | ||
url: "[email protected]:22:_007.git", user: "git", host: "github.com", port: "22", path: "_007.git", | ||
}, | ||
{ | ||
// Repo path ending with .git and starting with _ | ||
url: "[email protected]:_007.git", user: "git", host: "github.com", port: "", path: "_007.git", | ||
}, | ||
{ | ||
// Repo path ending with .git and starting with _ | ||
url: "[email protected]:_james.git", user: "git", host: "github.com", port: "", path: "_james.git", | ||
}, | ||
{ | ||
// Repo path ending with .git and starting with _ | ||
url: "[email protected]:_james/bond.git", user: "git", host: "github.com", port: "", path: "_james/bond.git", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
user, host, port, path := FindScpLikeComponents(tc.url) | ||
|
||
assert.Equal(t, tc.user, user) | ||
assert.Equal(t, tc.host, host) | ||
assert.Equal(t, tc.port, port) | ||
assert.Equal(t, tc.path, path) | ||
} | ||
} |