Skip to content

Commit 0525768

Browse files
committed
fix: extract folder correctly from repo URLs with trailing slash
Closes #380 Closes coder/customers#690
1 parent 46c97a4 commit 0525768

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

options/defaults.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package options
22

33
import (
44
"fmt"
5+
"path"
56
"strings"
67

78
"github.com/go-git/go-billy/v5/osfs"
@@ -25,12 +26,14 @@ func DefaultWorkspaceFolder(repoURL string) string {
2526
if err != nil {
2627
return EmptyWorkspaceDir
2728
}
28-
name := strings.Split(parsed.Path, "/")
29-
hasOwnerAndRepo := len(name) >= 2
30-
if !hasOwnerAndRepo {
29+
repo := path.Base(parsed.Path)
30+
// Giturls parsing never actually fails since ParseLocal never
31+
// errors and places the entire URL in the Path field. This check
32+
// ensures it's at least a Unix path containing forwardslash.
33+
if repo == repoURL || repo == "" {
3134
return EmptyWorkspaceDir
3235
}
33-
repo := strings.TrimSuffix(name[len(name)-1], ".git")
36+
repo = strings.TrimSuffix(repo, ".git")
3437
return fmt.Sprintf("/workspaces/%s", repo)
3538
}
3639

options/defaults_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,41 @@ func TestDefaultWorkspaceFolder(t *testing.T) {
4040
gitURL: "https://github.com/coder/envbuilder.git#feature-branch",
4141
expected: "/workspaces/envbuilder",
4242
},
43+
{
44+
name: "trailing",
45+
gitURL: "https://github.com/coder/envbuilder.git/",
46+
expected: "/workspaces/envbuilder",
47+
},
48+
{
49+
name: "trailing-x2",
50+
gitURL: "https://github.com/coder/envbuilder.git//",
51+
expected: "/workspaces/envbuilder",
52+
},
53+
{
54+
name: "fragment-trailing",
55+
gitURL: "https://github.com/coder/envbuilder.git/#refs/heads/feature-branch",
56+
expected: "/workspaces/envbuilder",
57+
},
58+
{
59+
name: "space",
60+
gitURL: "https://github.com/coder/env%20builder.git",
61+
expected: "/workspaces/env builder",
62+
},
63+
{
64+
name: "no .git",
65+
gitURL: "https://github.com/coder/envbuilder",
66+
expected: "/workspaces/envbuilder",
67+
},
68+
{
69+
name: "Unix path",
70+
gitURL: "/repo",
71+
expected: "/workspaces/repo",
72+
},
73+
{
74+
name: "Unix subpath",
75+
gitURL: "/path/to/repo",
76+
expected: "/workspaces/repo",
77+
},
4378
{
4479
name: "empty",
4580
gitURL: "",
@@ -65,6 +100,10 @@ func TestDefaultWorkspaceFolder(t *testing.T) {
65100
name: "website URL",
66101
invalidURL: "www.google.com",
67102
},
103+
{
104+
name: "Unix root",
105+
invalidURL: "/",
106+
},
68107
}
69108
for _, tt := range invalidTests {
70109
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)