Merge pull request #3230 from bk2204/ssh-shorthands

lfsapi: handle SSH hostnames and aliases without users
This commit is contained in:
brian m. carlson 2018-09-06 19:56:31 +00:00 committed by GitHub
commit b25cf00fd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 3 deletions

@ -188,8 +188,16 @@ func (e *endpointGitFinder) NewEndpoint(rawurl string) Endpoint {
case "":
return endpointFromBareSshUrl(u.String())
default:
// Just passthrough to preserve
return Endpoint{Url: rawurl}
if strings.HasPrefix(rawurl, u.Scheme+"::") {
// Looks like a remote helper; just pass it through.
return Endpoint{Url: rawurl}
}
// We probably got here because the "scheme" that was parsed is
// a hostname (whether FQDN or single word) and the URL parser
// didn't know what to do with it. Do what Git does and treat
// it as an SSH URL. This ensures we handle SSH config aliases
// properly.
return endpointFromBareSshUrl(u.String())
}
}

@ -376,3 +376,85 @@ func TestDeleteAccessWithEmptyString(t *testing.T) {
finder.SetAccess("http://example.com", Access(""))
assert.Equal(t, NoneAccess, finder.AccessFor("http://example.com"))
}
type EndpointParsingTestCase struct {
Given string
Expected Endpoint
}
func (c *EndpointParsingTestCase) Assert(t *testing.T) {
finder := NewEndpointFinder(NewContext(nil, nil, map[string]string{
"url.https://github.com/.insteadof": "gh:",
}))
actual := finder.NewEndpoint(c.Given)
assert.Equal(t, c.Expected, actual, "lfsapi: expected endpoint for %q to be %#v (was %#v)", c.Given, c.Expected, actual)
}
func TestEndpointParsing(t *testing.T) {
// Note that many of these tests will produce silly or completely broken
// values for the Url, and that's okay: they work nevertheless.
for desc, c := range map[string]EndpointParsingTestCase{
"simple bare ssh": {
"git@github.com:git-lfs/git-lfs.git",
Endpoint{
Url: "https://github.com/git-lfs/git-lfs.git",
SshUserAndHost: "git@github.com",
SshPath: "git-lfs/git-lfs.git",
SshPort: "",
Operation: "",
},
},
"port bare ssh": {
"[git@ssh.github.com:443]:git-lfs/git-lfs.git",
Endpoint{
Url: "https://ssh.github.com/git-lfs/git-lfs.git",
SshUserAndHost: "git@ssh.github.com",
SshPath: "git-lfs/git-lfs.git",
SshPort: "443",
Operation: "",
},
},
"no user bare ssh": {
"github.com:git-lfs/git-lfs.git",
Endpoint{
Url: "https://github.com/git-lfs/git-lfs.git",
SshUserAndHost: "github.com",
SshPath: "git-lfs/git-lfs.git",
SshPort: "",
Operation: "",
},
},
"bare word bare ssh": {
"github:git-lfs/git-lfs.git",
Endpoint{
Url: "https://github/git-lfs/git-lfs.git",
SshUserAndHost: "github",
SshPath: "git-lfs/git-lfs.git",
SshPort: "",
Operation: "",
},
},
"insteadof alias": {
"gh:git-lfs/git-lfs.git",
Endpoint{
Url: "https://github.com/git-lfs/git-lfs.git",
SshUserAndHost: "",
SshPath: "",
SshPort: "",
Operation: "",
},
},
"remote helper": {
"remote::git-lfs/git-lfs.git",
Endpoint{
Url: "remote::git-lfs/git-lfs.git",
SshUserAndHost: "",
SshPath: "",
SshPort: "",
Operation: "",
},
},
} {
t.Run(desc, c.Assert)
}
}

@ -137,7 +137,7 @@ begin_test "url alias must be prefix"
git config url."http://actual-url/".insteadOf alias:
git config lfs.url badalias:rest
git lfs env | tee env.log
grep "Endpoint=badalias:rest (auth=none)" env.log
grep "SSH=badalias:rest" env.log
)
end_test