Skip to content

adjust path extracted from file: url on Windows #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 18 additions & 1 deletion plumbing/transport/file/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/go-git/go-git/v5/plumbing/transport"
Expand Down Expand Up @@ -96,7 +97,23 @@ func (r *runner) Command(cmd string, ep *transport.Endpoint, auth transport.Auth
}
}

return &command{cmd: execabs.Command(cmd, ep.Path)}, nil
return &command{cmd: execabs.Command(cmd, adjustPathForWindows(ep.Path))}, nil
}

func isDriveLetter(c byte) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}

// On Windows, the path that results from a file: URL has a leading slash. This
// has to be removed if there's a drive letter
func adjustPathForWindows(p string) string {
if runtime.GOOS != "windows" {
return p
}
if len(p) >= 3 && p[0] == '/' && isDriveLetter(p[1]) && p[2] == ':' {
return p[1:]
}
return p
}

type command struct {
Expand Down
40 changes: 39 additions & 1 deletion repository_windows_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
package git

import "fmt"
import (
"fmt"
"strings"

"github.com/go-git/go-billy/v5/util"
"github.com/go-git/go-git/v5/storage/memory"
. "gopkg.in/check.v1"
)

// preReceiveHook returns the bytes of a pre-receive hook script
// that prints m before exiting successfully
func preReceiveHook(m string) []byte {
return []byte(fmt.Sprintf("#!C:/Program\\ Files/Git/usr/bin/sh.exe\nprintf '%s'\n", m))
}

func (s *RepositorySuite) TestCloneFileUrlWindows(c *C) {
dir, clean := s.TemporalDir()
defer clean()

r, err := PlainInit(dir, false)
c.Assert(err, IsNil)

err = util.WriteFile(r.wt, "foo", nil, 0755)
c.Assert(err, IsNil)

w, err := r.Worktree()
c.Assert(err, IsNil)

_, err = w.Add("foo")
c.Assert(err, IsNil)

_, err = w.Commit("foo", &CommitOptions{
Author: defaultSignature(),
Committer: defaultSignature(),
})
c.Assert(err, IsNil)

url := "file:///" + strings.ReplaceAll(dir, "\\", "/")
c.Assert(url, Matches, "file:///[A-Za-z]:/.*")
_, err = Clone(memory.NewStorage(), nil, &CloneOptions{
URL: url,
})

c.Assert(err, IsNil)
}