Skip to content

Commit 0096b04

Browse files
committed
fix: extract folder correctly from repo URLs with trailing slash (#381)
Closes #380 Closes coder/customers#690 (cherry picked from commit f50ec7a)
1 parent 3759642 commit 0096b04

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

integration/integration_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -844,15 +844,17 @@ func TestContainerEnv(t *testing.T) {
844844
require.NoError(t, err)
845845

846846
output := execContainer(t, ctr, "cat /env")
847-
require.Contains(t, strings.TrimSpace(output),
848-
`DEVCONTAINER=true
847+
want := `DEVCONTAINER=true
849848
DEVCONTAINER_CONFIG=/workspaces/empty/.devcontainer/devcontainer.json
850849
ENVBUILDER=true
851850
FROM_CONTAINER_ENV=bar
852851
FROM_DOCKERFILE=foo
853852
FROM_REMOTE_ENV=baz
854853
PATH=/usr/local/bin:/bin:/go/bin:/opt
855-
REMOTE_BAR=bar`)
854+
REMOTE_BAR=bar`
855+
if diff := cmp.Diff(want, strings.TrimSpace(output)); diff != "" {
856+
require.Failf(t, "env mismatch", "diff (-want +got):\n%s", diff)
857+
}
856858
}
857859

858860
func TestUnsetOptionsEnv(t *testing.T) {

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 == "/" || repo == "." || 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

+57
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,56 @@ func TestDefaultWorkspaceFolder(t *testing.T) {
3535
gitURL: "https://username:[email protected]/coder/envbuilder.git",
3636
expected: "/workspaces/envbuilder",
3737
},
38+
{
39+
name: "trailing",
40+
gitURL: "https://github.com/coder/envbuilder.git/",
41+
expected: "/workspaces/envbuilder",
42+
},
43+
{
44+
name: "trailing-x2",
45+
gitURL: "https://github.com/coder/envbuilder.git//",
46+
expected: "/workspaces/envbuilder",
47+
},
48+
{
49+
name: "no .git",
50+
gitURL: "https://github.com/coder/envbuilder",
51+
expected: "/workspaces/envbuilder",
52+
},
53+
{
54+
name: "trailing no .git",
55+
gitURL: "https://github.com/coder/envbuilder/",
56+
expected: "/workspaces/envbuilder",
57+
},
3858
{
3959
name: "fragment",
4060
gitURL: "https://github.com/coder/envbuilder.git#feature-branch",
4161
expected: "/workspaces/envbuilder",
4262
},
63+
{
64+
name: "fragment-trailing",
65+
gitURL: "https://github.com/coder/envbuilder.git/#refs/heads/feature-branch",
66+
expected: "/workspaces/envbuilder",
67+
},
68+
{
69+
name: "fragment-trailing no .git",
70+
gitURL: "https://github.com/coder/envbuilder/#refs/heads/feature-branch",
71+
expected: "/workspaces/envbuilder",
72+
},
73+
{
74+
name: "space",
75+
gitURL: "https://github.com/coder/env%20builder.git",
76+
expected: "/workspaces/env builder",
77+
},
78+
{
79+
name: "Unix path",
80+
gitURL: "/repo",
81+
expected: "/workspaces/repo",
82+
},
83+
{
84+
name: "Unix subpath",
85+
gitURL: "/path/to/repo",
86+
expected: "/workspaces/repo",
87+
},
4388
{
4489
name: "empty",
4590
gitURL: "",
@@ -65,6 +110,18 @@ func TestDefaultWorkspaceFolder(t *testing.T) {
65110
name: "website URL",
66111
invalidURL: "www.google.com",
67112
},
113+
{
114+
name: "Unix root",
115+
invalidURL: "/",
116+
},
117+
{
118+
name: "Path consists entirely of slash",
119+
invalidURL: "//",
120+
},
121+
{
122+
name: "Git URL with no path",
123+
invalidURL: "http://127.0.0.1:41073",
124+
},
68125
}
69126
for _, tt := range invalidTests {
70127
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)