Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions options/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package options

import (
"fmt"
"path"
"strings"

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

Expand Down
39 changes: 39 additions & 0 deletions options/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,41 @@ func TestDefaultWorkspaceFolder(t *testing.T) {
gitURL: "https://github.com/coder/envbuilder.git#feature-branch",
expected: "/workspaces/envbuilder",
},
{
name: "trailing",
gitURL: "https://github.com/coder/envbuilder.git/",
expected: "/workspaces/envbuilder",
},
{
name: "trailing-x2",
gitURL: "https://github.com/coder/envbuilder.git//",
expected: "/workspaces/envbuilder",
},
{
name: "fragment-trailing",
gitURL: "https://github.com/coder/envbuilder.git/#refs/heads/feature-branch",
expected: "/workspaces/envbuilder",
},
{
name: "space",
gitURL: "https://github.com/coder/env%20builder.git",
expected: "/workspaces/env builder",
},
{
name: "no .git",
gitURL: "https://github.com/coder/envbuilder",
expected: "/workspaces/envbuilder",
},
{
name: "Unix path",
gitURL: "/repo",
expected: "/workspaces/repo",
},
{
name: "Unix subpath",
gitURL: "/path/to/repo",
expected: "/workspaces/repo",
},
{
name: "empty",
gitURL: "",
Expand All @@ -65,6 +100,10 @@ func TestDefaultWorkspaceFolder(t *testing.T) {
name: "website URL",
invalidURL: "www.google.com",
},
{
name: "Unix root",
invalidURL: "/",
},
}
for _, tt := range invalidTests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading