From 4a670e130804d630fa056e4a60101698dc7a9af1 Mon Sep 17 00:00:00 2001 From: "Santiago M. Mola" Date: Fri, 28 Apr 2017 15:20:51 +0200 Subject: do not convert local paths to URL * Do not convert local paths to URLs, just keep them as they are. * This way we add support for Windows without taking care of Windows path-to-URL conversion. --- plumbing/transport/common.go | 29 ++++++++++++++-- plumbing/transport/common_test.go | 52 +++++++++++++++++++++++++++-- plumbing/transport/file/client_test.go | 7 ++-- plumbing/transport/file/server.go | 4 +-- plumbing/transport/file/server_test.go | 5 +-- plumbing/transport/file/upload_pack_test.go | 12 +++---- plumbing/transport/server/loader_test.go | 7 ++-- 7 files changed, 91 insertions(+), 25 deletions(-) (limited to 'plumbing/transport') diff --git a/plumbing/transport/common.go b/plumbing/transport/common.go index 0ff9e89..d6594ca 100644 --- a/plumbing/transport/common.go +++ b/plumbing/transport/common.go @@ -114,6 +114,10 @@ func NewEndpoint(endpoint string) (Endpoint, error) { return e, nil } + if e, ok := parseFile(endpoint); ok { + return e, nil + } + u, err := url.Parse(endpoint) if err != nil { return nil, plumbing.NewPermanentError(err) @@ -201,9 +205,21 @@ func (e *scpEndpoint) String() string { return fmt.Sprintf("%s%s:%s", user, e.host, e.path) } +type fileEndpoint struct { + path string +} + +func (e *fileEndpoint) Protocol() string { return "file" } +func (e *fileEndpoint) User() string { return "" } +func (e *fileEndpoint) Password() string { return "" } +func (e *fileEndpoint) Host() string { return "" } +func (e *fileEndpoint) Port() int { return 0 } +func (e *fileEndpoint) Path() string { return e.path } +func (e *fileEndpoint) String() string { return e.path } + var ( - isSchemeRegExp = regexp.MustCompile("^[^:]+://") - scpLikeUrlRegExp = regexp.MustCompile("^(?:(?P[^@]+)@)?(?P[^:]+):/?(?P.+)$") + isSchemeRegExp = regexp.MustCompile(`^[^:]+://`) + scpLikeUrlRegExp = regexp.MustCompile(`^(?:(?P[^@]+)@)?(?P[^:\s]+):(?P[^\\].*)$`) ) func parseSCPLike(endpoint string) (Endpoint, bool) { @@ -219,6 +235,15 @@ func parseSCPLike(endpoint string) (Endpoint, bool) { }, true } +func parseFile(endpoint string) (Endpoint, bool) { + if isSchemeRegExp.MatchString(endpoint) { + return nil, false + } + + path := endpoint + return &fileEndpoint{path}, true +} + // UnsupportedCapabilities are the capabilities not supported by any client // implementation var UnsupportedCapabilities = []capability.Capability{ diff --git a/plumbing/transport/common_test.go b/plumbing/transport/common_test.go index ce41045..ec617bd 100644 --- a/plumbing/transport/common_test.go +++ b/plumbing/transport/common_test.go @@ -74,8 +74,56 @@ func (s *SuiteCommon) TestNewEndpointSCPLike(c *C) { c.Assert(e.String(), Equals, "git@github.com:user/repository.git") } -func (s *SuiteCommon) TestNewEndpointWrongForgat(c *C) { - e, err := NewEndpoint("foo") +func (s *SuiteCommon) TestNewEndpointFileAbs(c *C) { + e, err := NewEndpoint("/foo.git") + c.Assert(err, IsNil) + c.Assert(e.Protocol(), Equals, "file") + c.Assert(e.User(), Equals, "") + c.Assert(e.Password(), Equals, "") + c.Assert(e.Host(), Equals, "") + c.Assert(e.Port(), Equals, 0) + c.Assert(e.Path(), Equals, "/foo.git") + c.Assert(e.String(), Equals, "/foo.git") +} + +func (s *SuiteCommon) TestNewEndpointFileRel(c *C) { + e, err := NewEndpoint("foo.git") + c.Assert(err, IsNil) + c.Assert(e.Protocol(), Equals, "file") + c.Assert(e.User(), Equals, "") + c.Assert(e.Password(), Equals, "") + c.Assert(e.Host(), Equals, "") + c.Assert(e.Port(), Equals, 0) + c.Assert(e.Path(), Equals, "foo.git") + c.Assert(e.String(), Equals, "foo.git") +} + +func (s *SuiteCommon) TestNewEndpointFileWindows(c *C) { + e, err := NewEndpoint("C:\\foo.git") + c.Assert(err, IsNil) + c.Assert(e.Protocol(), Equals, "file") + c.Assert(e.User(), Equals, "") + c.Assert(e.Password(), Equals, "") + c.Assert(e.Host(), Equals, "") + c.Assert(e.Port(), Equals, 0) + c.Assert(e.Path(), Equals, "C:\\foo.git") + c.Assert(e.String(), Equals, "C:\\foo.git") +} + +func (s *SuiteCommon) TestNewEndpointFileURL(c *C) { + e, err := NewEndpoint("file:///foo.git") + c.Assert(err, IsNil) + c.Assert(e.Protocol(), Equals, "file") + c.Assert(e.User(), Equals, "") + c.Assert(e.Password(), Equals, "") + c.Assert(e.Host(), Equals, "") + c.Assert(e.Port(), Equals, 0) + c.Assert(e.Path(), Equals, "/foo.git") + c.Assert(e.String(), Equals, "file:///foo.git") +} + +func (s *SuiteCommon) TestNewEndpointInvalidURL(c *C) { + e, err := NewEndpoint("http://\\") c.Assert(err, NotNil) c.Assert(e, IsNil) } diff --git a/plumbing/transport/file/client_test.go b/plumbing/transport/file/client_test.go index 220df3d..030175e 100644 --- a/plumbing/transport/file/client_test.go +++ b/plumbing/transport/file/client_test.go @@ -1,9 +1,9 @@ package file import ( - "fmt" "io" "os" + "path/filepath" "strings" "testing" @@ -20,13 +20,12 @@ filemode = true bare = true` func prepareRepo(c *C, path string) transport.Endpoint { - url := fmt.Sprintf("file://%s", path) - ep, err := transport.NewEndpoint(url) + ep, err := transport.NewEndpoint(path) c.Assert(err, IsNil) // git-receive-pack refuses to update refs/heads/master on non-bare repo // so we ensure bare repo config. - config := fmt.Sprintf("%s/config", path) + config := filepath.Join(path, "config") if _, err := os.Stat(config); err == nil { f, err := os.OpenFile(config, os.O_TRUNC|os.O_WRONLY, 0) c.Assert(err, IsNil) diff --git a/plumbing/transport/file/server.go b/plumbing/transport/file/server.go index 74085c2..61dd42d 100644 --- a/plumbing/transport/file/server.go +++ b/plumbing/transport/file/server.go @@ -14,7 +14,7 @@ import ( // and error. This is meant to be used when implementing a git-upload-pack // command. func ServeUploadPack(path string) error { - ep, err := transport.NewEndpoint(fmt.Sprintf("file://%s", path)) + ep, err := transport.NewEndpoint(path) if err != nil { return err } @@ -32,7 +32,7 @@ func ServeUploadPack(path string) error { // input and error. This is meant to be used when implementing a // git-receive-pack command. func ServeReceivePack(path string) error { - ep, err := transport.NewEndpoint(fmt.Sprintf("file://%s", path)) + ep, err := transport.NewEndpoint(path) if err != nil { return err } diff --git a/plumbing/transport/file/server_test.go b/plumbing/transport/file/server_test.go index a7b4e34..176d6ee 100644 --- a/plumbing/transport/file/server_test.go +++ b/plumbing/transport/file/server_test.go @@ -1,7 +1,6 @@ package file import ( - "fmt" "os" "os/exec" @@ -15,7 +14,6 @@ type ServerSuite struct { RemoteName string SrcPath string DstPath string - DstURL string } var _ = Suite(&ServerSuite{}) @@ -30,9 +28,8 @@ func (s *ServerSuite) SetUpSuite(c *C) { fixture = fixtures.ByTag("empty").One() s.DstPath = fixture.DotGit().Base() - s.DstURL = fmt.Sprintf("file://%s", s.DstPath) - cmd := exec.Command("git", "remote", "add", s.RemoteName, s.DstURL) + cmd := exec.Command("git", "remote", "add", s.RemoteName, s.DstPath) cmd.Dir = s.SrcPath c.Assert(cmd.Run(), IsNil) } diff --git a/plumbing/transport/file/upload_pack_test.go b/plumbing/transport/file/upload_pack_test.go index e5915f0..f013683 100644 --- a/plumbing/transport/file/upload_pack_test.go +++ b/plumbing/transport/file/upload_pack_test.go @@ -1,8 +1,8 @@ package file import ( - "fmt" "os" + "path/filepath" "github.com/src-d/go-git-fixtures" "gopkg.in/src-d/go-git.v4/plumbing/transport" @@ -25,20 +25,18 @@ func (s *UploadPackSuite) SetUpSuite(c *C) { fixture := fixtures.Basic().One() path := fixture.DotGit().Base() - url := fmt.Sprintf("file://%s", path) - ep, err := transport.NewEndpoint(url) + ep, err := transport.NewEndpoint(path) c.Assert(err, IsNil) s.Endpoint = ep fixture = fixtures.ByTag("empty").One() path = fixture.DotGit().Base() - url = fmt.Sprintf("file://%s", path) - ep, err = transport.NewEndpoint(url) + ep, err = transport.NewEndpoint(path) c.Assert(err, IsNil) s.EmptyEndpoint = ep - url = fmt.Sprintf("file://%s/%s", fixtures.DataFolder, "non-existent") - ep, err = transport.NewEndpoint(url) + path = filepath.Join(fixtures.DataFolder, "non-existent") + ep, err = transport.NewEndpoint(path) c.Assert(err, IsNil) s.NonExistentEndpoint = ep } diff --git a/plumbing/transport/server/loader_test.go b/plumbing/transport/server/loader_test.go index b4a8c37..5f32cb0 100644 --- a/plumbing/transport/server/loader_test.go +++ b/plumbing/transport/server/loader_test.go @@ -1,7 +1,6 @@ package server import ( - "fmt" "os/exec" "path/filepath" @@ -33,7 +32,7 @@ func (s *LoaderSuite) endpoint(c *C, url string) transport.Endpoint { } func (s *LoaderSuite) TestLoadNonExistent(c *C) { - sto, err := DefaultLoader.Load(s.endpoint(c, "file:///does-not-exist")) + sto, err := DefaultLoader.Load(s.endpoint(c, "does-not-exist")) c.Assert(err, Equals, transport.ErrRepositoryNotFound) c.Assert(sto, IsNil) } @@ -45,13 +44,13 @@ func (s *LoaderSuite) TestLoadNonExistentIgnoreHost(c *C) { } func (s *LoaderSuite) TestLoad(c *C) { - sto, err := DefaultLoader.Load(s.endpoint(c, fmt.Sprintf("file://%s", s.RepoPath))) + sto, err := DefaultLoader.Load(s.endpoint(c, s.RepoPath)) c.Assert(err, IsNil) c.Assert(sto, NotNil) } func (s *LoaderSuite) TestLoadIgnoreHost(c *C) { - sto, err := DefaultLoader.Load(s.endpoint(c, fmt.Sprintf("file://%s", s.RepoPath))) + sto, err := DefaultLoader.Load(s.endpoint(c, s.RepoPath)) c.Assert(err, IsNil) c.Assert(sto, NotNil) } -- cgit