-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support SSH endpoints and basic auth for HTTP endpoints #98
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must have deleted this comment on accident, whoops!
I'm wondering if this should be configuration driven in additon to being interactive? Interactive is nice, but if you're pulling down a few it would be nice to able to define these in the conf file
Here's what that could look like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh very nice, yeah I think this would be great. Would you be alright with merging this as a first pass, and then adding config support as a fast follow? I'm happy to take a stab at the config work, too! But I'd also like to get this in :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good!