aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortomqwpl <42344626+tomqwpl@users.noreply.github.com>2024-08-09 09:37:28 +0100
committerGitHub <noreply@github.com>2024-08-09 09:37:28 +0100
commit4fcb86f647af479e96576e9bdc8b4fe5526f17ec (patch)
tree44e15c704d4121cea23c398957dba0571d50fed2
parent728f1494421b0f32403ec18767ac2ab1cde68ff0 (diff)
downloadgo-git-4fcb86f647af479e96576e9bdc8b4fe5526f17ec.tar.gz
adjust path extracted from file: url on Windows (#416)
* adjust path extracted from file: url on Windows Fixes #415
-rw-r--r--plumbing/transport/file/client.go19
-rw-r--r--repository_windows_test.go40
2 files changed, 57 insertions, 2 deletions
diff --git a/plumbing/transport/file/client.go b/plumbing/transport/file/client.go
index 38714e2..d921d0a 100644
--- a/plumbing/transport/file/client.go
+++ b/plumbing/transport/file/client.go
@@ -7,6 +7,7 @@ import (
"io"
"os"
"path/filepath"
+ "runtime"
"strings"
"github.com/go-git/go-git/v5/plumbing/transport"
@@ -95,7 +96,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 {
diff --git a/repository_windows_test.go b/repository_windows_test.go
index bec0acd..e7c1ac7 100644
--- a/repository_windows_test.go
+++ b/repository_windows_test.go
@@ -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)
+}