Skip to content

Commit

Permalink
Merge pull request #2167 from crazy-max/fix-git-root-wsl
Browse files Browse the repository at this point in the history
gitutil: sanitize root dir on WSL
  • Loading branch information
crazy-max authored Dec 20, 2023
2 parents 7694f0b + ac5b324 commit 1cdefbe
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
6 changes: 5 additions & 1 deletion util/gitutil/gitutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ func (c *Git) IsDirty() bool {
}

func (c *Git) RootDir() (string, error) {
return c.clean(c.run("rev-parse", "--show-toplevel"))
root, err := c.clean(c.run("rev-parse", "--show-toplevel"))
if err != nil {
return "", err
}
return sanitizePath(root), nil
}

func (c *Git) GitDir() (string, error) {
Expand Down
17 changes: 17 additions & 0 deletions util/gitutil/gitpath_unix.go → util/gitutil/path_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

"github.com/moby/sys/mountinfo"
)
Expand Down Expand Up @@ -40,3 +42,18 @@ func gitPath(wd string) (string, error) {
}
return exec.LookPath("git")
}

var windowsPathRegex = regexp.MustCompile(`^[A-Za-z]:[\\/].*$`)

func sanitizePath(path string) string {
// If we're running in WSL, we need to convert Windows paths to Unix paths.
// This is because the git binary can be invoked through `git.exe` and
// therefore returns Windows paths.
if os.Getenv("WSL_DISTRO_NAME") != "" && windowsPathRegex.MatchString(path) {
unixPath := strings.ReplaceAll(path, "\\", "/")
drive := strings.ToLower(string(unixPath[0]))
rest := filepath.Clean(unixPath[3:])
return filepath.Join("/mnt", drive, rest)
}
return filepath.Clean(path)
}
19 changes: 19 additions & 0 deletions util/gitutil/path_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build !windows
// +build !windows

package gitutil

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSanitizePathUnix(t *testing.T) {
assert.Equal(t, "/home/foobar", sanitizePath("/home/foobar"))
}

func TestSanitizePathWSL(t *testing.T) {
t.Setenv("WSL_DISTRO_NAME", "Ubuntu")
assert.Equal(t, "/mnt/c/Users/foobar", sanitizePath("C:\\Users\\foobar"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package gitutil

import (
"os/exec"
"path/filepath"
)

func gitPath(wd string) (string, error) {
return exec.LookPath("git.exe")
}

func sanitizePath(path string) string {
return filepath.ToSlash(filepath.Clean(path))
}
11 changes: 11 additions & 0 deletions util/gitutil/path_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gitutil

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSanitizePathWindows(t *testing.T) {
assert.Equal(t, "C:\\Users\\foobar", sanitizePath("C:/Users/foobar"))
}

0 comments on commit 1cdefbe

Please sign in to comment.