Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a0190a6

Browse files
committed
lfshttp: don't strip off initial slash on SSH commands
When we process an SSH URL, we intentionally strip off the slash at the beginning of the URL. While that was convenient for git-lfs-authenticate, it also prevents us from handling an absolute path in git-lfs-transfer, since the path will have its leading slash stripped off and will therefore be relative. Instead, let's adopt Git's behavior, which is to not remove the leading slash. This is an incompatible change, but we're about to do a major release, so it's a good time to make it. This will affect both git-lfs-transfer and git-lfs-authenticate commands, but at least GitHub already supports the proper syntax. Note that since we process the non-URL form of SSH remotes by converting them to a URL and then parsing, let's strip off the leading slash when we process that form, since there we do have the ability to distinguish between absolute and relative paths. Update the lfs-ssh-echo binary to handle this new format.
1 parent a487e7c commit a0190a6

5 files changed

Lines changed: 26 additions & 29 deletions

File tree

lfsapi/endpoint_finder_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func TestSSHEndpointAddsLfsSuffix(t *testing.T) {
155155
e := finder.Endpoint("download", "")
156156
assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url)
157157
assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost)
158-
assert.Equal(t, "foo/bar", e.SSHMetadata.Path)
158+
assert.Equal(t, "/foo/bar", e.SSHMetadata.Path)
159159
assert.Equal(t, "", e.SSHMetadata.Port)
160160
}
161161

@@ -167,7 +167,7 @@ func TestSSHCustomPortEndpointAddsLfsSuffix(t *testing.T) {
167167
e := finder.Endpoint("download", "")
168168
assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url)
169169
assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost)
170-
assert.Equal(t, "foo/bar", e.SSHMetadata.Path)
170+
assert.Equal(t, "/foo/bar", e.SSHMetadata.Path)
171171
assert.Equal(t, "9000", e.SSHMetadata.Port)
172172
}
173173

@@ -179,7 +179,7 @@ func TestGitSSHEndpointAddsLfsSuffix(t *testing.T) {
179179
e := finder.Endpoint("download", "")
180180
assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url)
181181
assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost)
182-
assert.Equal(t, "foo/bar", e.SSHMetadata.Path)
182+
assert.Equal(t, "/foo/bar", e.SSHMetadata.Path)
183183
assert.Equal(t, "", e.SSHMetadata.Port)
184184
}
185185

@@ -191,7 +191,7 @@ func TestGitSSHCustomPortEndpointAddsLfsSuffix(t *testing.T) {
191191
e := finder.Endpoint("download", "")
192192
assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url)
193193
assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost)
194-
assert.Equal(t, "foo/bar", e.SSHMetadata.Path)
194+
assert.Equal(t, "/foo/bar", e.SSHMetadata.Path)
195195
assert.Equal(t, "9000", e.SSHMetadata.Port)
196196
}
197197

@@ -203,7 +203,7 @@ func TestSSHGitEndpointAddsLfsSuffix(t *testing.T) {
203203
e := finder.Endpoint("download", "")
204204
assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url)
205205
assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost)
206-
assert.Equal(t, "foo/bar", e.SSHMetadata.Path)
206+
assert.Equal(t, "/foo/bar", e.SSHMetadata.Path)
207207
assert.Equal(t, "", e.SSHMetadata.Port)
208208
}
209209

@@ -215,7 +215,7 @@ func TestSSHGitCustomPortEndpointAddsLfsSuffix(t *testing.T) {
215215
e := finder.Endpoint("download", "")
216216
assert.Equal(t, "https://example.com/foo/bar.git/info/lfs", e.Url)
217217
assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost)
218-
assert.Equal(t, "foo/bar", e.SSHMetadata.Path)
218+
assert.Equal(t, "/foo/bar", e.SSHMetadata.Path)
219219
assert.Equal(t, "9000", e.SSHMetadata.Port)
220220
}
221221

@@ -672,7 +672,7 @@ func TestInsteadOf(t *testing.T) {
672672
Url: "https://example.com/git-lfs/git-lfs.git/info/lfs",
673673
SSHMetadata: ssh.SSHMetadata{
674674
UserAndHost: "example.com",
675-
Path: "git-lfs/git-lfs.git",
675+
Path: "/git-lfs/git-lfs.git",
676676
Port: "",
677677
},
678678
Operation: "upload",
@@ -685,7 +685,7 @@ func TestInsteadOf(t *testing.T) {
685685
Url: "https://example.com/git-lfs/git-lfs.git/info/lfs",
686686
SSHMetadata: ssh.SSHMetadata{
687687
UserAndHost: "example.com",
688-
Path: "git-lfs/git-lfs.git",
688+
Path: "/git-lfs/git-lfs.git",
689689
Port: "",
690690
},
691691
Operation: "download",
@@ -698,7 +698,7 @@ func TestInsteadOf(t *testing.T) {
698698
Url: "https://example.com/git-lfs/git-lfs.git/info/lfs",
699699
SSHMetadata: ssh.SSHMetadata{
700700
UserAndHost: "example.com",
701-
Path: "git-lfs/git-lfs.git",
701+
Path: "/git-lfs/git-lfs.git",
702702
Port: "",
703703
},
704704
Operation: "upload",

lfshttp/endpoint.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,7 @@ func EndpointFromSshUrl(u *url.URL) Endpoint {
5454
endpoint.SSHMetadata.Port = match[2]
5555
}
5656

57-
// u.Path includes a preceding '/', strip off manually
58-
// rooted paths in the URL will be '//path/to/blah'
59-
// this is just how Go's URL parsing works
60-
if strings.HasPrefix(u.Path, "/") {
61-
endpoint.SSHMetadata.Path = u.Path[1:]
62-
} else {
63-
endpoint.SSHMetadata.Path = u.Path
64-
}
57+
endpoint.SSHMetadata.Path = u.Path
6558

6659
// Fallback URL for using HTTPS while still using SSH for git
6760
// u.Host includes host & port so can't use SSH port
@@ -98,7 +91,11 @@ func EndpointFromBareSshUrl(rawurl string) Endpoint {
9891
return Endpoint{Url: UrlUnknown}
9992
}
10093

101-
return EndpointFromSshUrl(newu)
94+
endpoint := EndpointFromSshUrl(newu)
95+
if strings.HasPrefix(endpoint.SSHMetadata.Path, "/") {
96+
endpoint.SSHMetadata.Path = endpoint.SSHMetadata.Path[1:]
97+
}
98+
return endpoint
10299
}
103100

104101
// Construct a new endpoint from a HTTP URL

ssh/ssh_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,11 @@ func TestSSHGetLFSExeAndArgsWithCustomSSH(t *testing.T) {
439439
t.Logf("ENDPOINT: %+v", e)
440440
assert.Equal(t, "12345", e.SSHMetadata.Port)
441441
assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost)
442-
assert.Equal(t, "repo", e.SSHMetadata.Path)
442+
assert.Equal(t, "/repo", e.SSHMetadata.Path)
443443

444444
exe, args := ssh.GetLFSExeAndArgs(cli.OSEnv(), cli.GitEnv(), &e.SSHMetadata, "git-lfs-authenticate", "download", false)
445445
assert.Equal(t, "not-ssh", exe)
446-
assert.Equal(t, []string{"-p", "12345", "[email protected]", "git-lfs-authenticate repo download"}, args)
446+
assert.Equal(t, []string{"-p", "12345", "[email protected]", "git-lfs-authenticate /repo download"}, args)
447447
}
448448

449449
func TestSSHGetLFSExeAndArgsInvalidOptionsAsHost(t *testing.T) {
@@ -457,11 +457,11 @@ func TestSSHGetLFSExeAndArgsInvalidOptionsAsHost(t *testing.T) {
457457
e := lfshttp.EndpointFromSshUrl(u)
458458
t.Logf("ENDPOINT: %+v", e)
459459
assert.Equal(t, "-oProxyCommand=gnome-calculator", e.SSHMetadata.UserAndHost)
460-
assert.Equal(t, "repo", e.SSHMetadata.Path)
460+
assert.Equal(t, "/repo", e.SSHMetadata.Path)
461461

462462
exe, args := ssh.GetLFSExeAndArgs(cli.OSEnv(), cli.GitEnv(), &e.SSHMetadata, "git-lfs-authenticate", "download", false)
463463
assert.Equal(t, "ssh", exe)
464-
assert.Equal(t, []string{"--", "-oProxyCommand=gnome-calculator", "git-lfs-authenticate repo download"}, args)
464+
assert.Equal(t, []string{"--", "-oProxyCommand=gnome-calculator", "git-lfs-authenticate /repo download"}, args)
465465
}
466466

467467
func TestSSHGetLFSExeAndArgsInvalidOptionsAsHostWithCustomSSH(t *testing.T) {
@@ -478,11 +478,11 @@ func TestSSHGetLFSExeAndArgsInvalidOptionsAsHostWithCustomSSH(t *testing.T) {
478478
e := lfshttp.EndpointFromSshUrl(u)
479479
t.Logf("ENDPOINT: %+v", e)
480480
assert.Equal(t, "--oProxyCommand=gnome-calculator", e.SSHMetadata.UserAndHost)
481-
assert.Equal(t, "repo", e.SSHMetadata.Path)
481+
assert.Equal(t, "/repo", e.SSHMetadata.Path)
482482

483483
exe, args := ssh.GetLFSExeAndArgs(cli.OSEnv(), cli.GitEnv(), &e.SSHMetadata, "git-lfs-authenticate", "download", false)
484484
assert.Equal(t, "not-ssh", exe)
485-
assert.Equal(t, []string{"oProxyCommand=gnome-calculator", "git-lfs-authenticate repo download"}, args)
485+
assert.Equal(t, []string{"oProxyCommand=gnome-calculator", "git-lfs-authenticate /repo download"}, args)
486486
}
487487

488488
func TestSSHGetExeAndArgsInvalidOptionsAsHost(t *testing.T) {
@@ -515,7 +515,7 @@ func TestSSHGetExeAndArgsInvalidOptionsAsPath(t *testing.T) {
515515
e := lfshttp.EndpointFromSshUrl(u)
516516
t.Logf("ENDPOINT: %+v", e)
517517
assert.Equal(t, "[email protected]", e.SSHMetadata.UserAndHost)
518-
assert.Equal(t, "-oProxyCommand=gnome-calculator", e.SSHMetadata.Path)
518+
assert.Equal(t, "/-oProxyCommand=gnome-calculator", e.SSHMetadata.Path)
519519

520520
exe, args, needShell := ssh.GetExeAndArgs(cli.OSEnv(), cli.GitEnv(), &e.SSHMetadata, false)
521521
assert.Equal(t, "ssh", exe)

t/cmd/lfs-ssh-echo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ func main() {
100100
Href: fmt.Sprintf("http://127.0.0.1:%s/%s.git/info/lfs", os.Args[2], repo),
101101
}
102102
switch repo {
103-
case "ssh-expired-absolute":
103+
case "/ssh-expired-absolute":
104104
r.ExpiresAt = time.Now().Add(-5 * time.Minute)
105-
case "ssh-expired-relative":
105+
case "/ssh-expired-relative":
106106
r.ExpiresIn = -5
107-
case "ssh-expired-both":
107+
case "/ssh-expired-both":
108108
r.ExpiresAt = time.Now().Add(-5 * time.Minute)
109109
r.ExpiresIn = -5
110110
}

t/t-locks.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ begin_test "list a single lock (SSH)"
7272
[ $(wc -l < locks.log) -eq 1 ]
7373
grep "f.dat" locks.log
7474
grep "Git LFS Tests" locks.log
75-
grep "lfs-ssh-echo.*git-lfs-authenticate $reponame download" trace.log
75+
grep "lfs-ssh-echo.*git-lfs-authenticate /$reponame download" trace.log
7676
)
7777
end_test
7878

0 commit comments

Comments
 (0)