-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
parse short scp-like syntax into proper ssh urls
currently we're lelying on an underlying library (libssh2) to be able to handle the shorter `[user@]host:path` syntax, but we should be good citizens and convert it ourselves. one key thing to note is that we don't convert any syntax that omits the user (i.e. `host:path`). while git allows this and will use the current user (i.e. cnb), this is a behaviour we don't want people to rely on. Signed-off-by: Bohan Chen <danielchen97dc@gmail.com>
- Loading branch information
Showing
4 changed files
with
87 additions
and
2 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,26 @@ | ||
package git | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
var shortScpRegex = regexp.MustCompile(`^(ssh://)?(.*)@([[:alnum:]\.-]+):(.*)$`) | ||
|
||
// ParseURL converts a short scp-like SSH syntax to a proper SSH URL. | ||
// Git's ssh protocol supports a url like user@hostname:path syntax, which is | ||
// not a valid ssh url but is inherited from scp. Because the library we | ||
// use for git relies on the Golang SSH support, we need to convert it to a | ||
// proper SSH URL. | ||
// See https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols | ||
func parseURL(url string) string { | ||
parts := shortScpRegex.FindStringSubmatch(url) | ||
if len(parts) == 0 { | ||
return url | ||
} | ||
if parts[1] == "ssh://" { | ||
return url | ||
} | ||
|
||
return fmt.Sprintf("ssh://%v@%v/%v", parts[2], parts[3], parts[4]) | ||
} |
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,59 @@ | ||
package git | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sclevine/spec" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseURL(t *testing.T) { | ||
spec.Focus(t, "Test Parse Git URL", testParseURL) | ||
} | ||
|
||
func testParseURL(t *testing.T, when spec.G, it spec.S) { | ||
type entry struct { | ||
url string | ||
expected string | ||
} | ||
|
||
testURLs := func(table []entry) { | ||
for _, e := range table { | ||
assert.Equal(t, e.expected, parseURL(e.url)) | ||
} | ||
} | ||
|
||
// https: //stackoverflow.com/questions/31801271/what-are-the-supported-git-url-formats | ||
when("using the local protcol", func() { | ||
it("parses the url correctly", func() { | ||
testURLs([]entry{ | ||
{url: "/path/to/repo.git", expected: "/path/to/repo.git"}, | ||
{url: "file:///path/to/repo.git", expected: "file:///path/to/repo.git"}, | ||
}) | ||
}) | ||
}) | ||
when("using the https protcol", func() { | ||
it("parses the url correctly", func() { | ||
testURLs([]entry{ | ||
{url: "http://host.xz/path/to/repo.git", expected: "http://host.xz/path/to/repo.git"}, | ||
{url: "https://host.xz/path/to/repo.git", expected: "https://host.xz/path/to/repo.git"}, | ||
}) | ||
}) | ||
}) | ||
when("using the ssh protcol", func() { | ||
it("parses the url correctly", func() { | ||
testURLs([]entry{ | ||
{url: "ssh://user@host.xz:port/path/to/repo.git/", expected: "ssh://user@host.xz:port/path/to/repo.git/"}, | ||
{url: "ssh://user@host.xz/path/to/repo.git/", expected: "ssh://user@host.xz/path/to/repo.git/"}, | ||
{url: "user@host.xz:path/to/repo.git", expected: "ssh://user@host.xz/path/to/repo.git"}, | ||
}) | ||
}) | ||
}) | ||
when("using the git protcol", func() { | ||
it("parses the url correctly", func() { | ||
testURLs([]entry{ | ||
{url: "git://host.xz/path/to/repo.git", expected: "git://host.xz/path/to/repo.git"}, | ||
}) | ||
}) | ||
}) | ||
} |