Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

do not convert local paths to URL #368

Merged
merged 1 commit into from
May 4, 2017
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
2 changes: 1 addition & 1 deletion _examples/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func createBareRepository(dir string) string {

func setEmptyRemote(dir string) string {
remote := createBareRepository(tempFolder())
setRemote(dir, fmt.Sprintf("file://%s", remote))
setRemote(dir, remote)
return dir
}

Expand Down
4 changes: 1 addition & 3 deletions common_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package git

import (
"fmt"
"testing"

"gopkg.in/src-d/go-git.v4/plumbing"
Expand Down Expand Up @@ -95,8 +94,7 @@ func (s *BaseSuite) GetBasicLocalRepositoryURL() string {
}

func (s *BaseSuite) GetLocalRepositoryURL(f *fixtures.Fixture) string {
path := f.DotGit().Base()
return fmt.Sprintf("file://%s", path)
return f.DotGit().Base()
}

type SuiteCommon struct{}
Expand Down
29 changes: 27 additions & 2 deletions plumbing/transport/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<user>[^@]+)@)?(?P<host>[^:]+):/?(?P<path>.+)$")
isSchemeRegExp = regexp.MustCompile(`^[^:]+://`)
scpLikeUrlRegExp = regexp.MustCompile(`^(?:(?P<user>[^@]+)@)?(?P<host>[^:\s]+):(?P<path>[^\\].*)$`)
)

func parseSCPLike(endpoint string) (Endpoint, bool) {
Expand All @@ -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{
Expand Down
52 changes: 50 additions & 2 deletions plumbing/transport/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,56 @@ func (s *SuiteCommon) TestNewEndpointSCPLike(c *C) {
c.Assert(e.String(), Equals, "[email protected]: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)
}
Expand Down
7 changes: 3 additions & 4 deletions plumbing/transport/file/client_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package file

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions plumbing/transport/file/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions plumbing/transport/file/server_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package file

import (
"fmt"
"os"
"os/exec"

Expand All @@ -15,7 +14,6 @@ type ServerSuite struct {
RemoteName string
SrcPath string
DstPath string
DstURL string
}

var _ = Suite(&ServerSuite{})
Expand All @@ -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)
}
Expand Down
12 changes: 5 additions & 7 deletions plumbing/transport/file/upload_pack_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}
Expand Down
7 changes: 3 additions & 4 deletions plumbing/transport/server/loader_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package server

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

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (s *ReferencesSuite) TestObjectNotFoundError(c *C) {
url := fixtures.ByURL("https://github.com/git-fixtures/basic.git").One().DotGit().Base()
storer := memory.NewStorage()
r, err := Clone(storer, nil, &CloneOptions{
URL: "file://" + url,
URL: url,
})
c.Assert(err, IsNil)

Expand Down
29 changes: 14 additions & 15 deletions remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package git

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
Expand All @@ -26,9 +25,9 @@ type RemoteSuite struct {
var _ = Suite(&RemoteSuite{})

func (s *RemoteSuite) TestFetchInvalidEndpoint(c *C) {
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux"})
err := r.Fetch(&FetchOptions{})
c.Assert(err, ErrorMatches, ".*invalid endpoint.*")
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "http://\\"})
err := r.Fetch(&FetchOptions{RemoteName: "foo"})
c.Assert(err, ErrorMatches, ".*invalid character.*")
}

func (s *RemoteSuite) TestFetchNonExistentEndpoint(c *C) {
Expand Down Expand Up @@ -215,7 +214,7 @@ func (s *RemoteSuite) TestPushToEmptyRepository(c *C) {
c.Assert(err, IsNil)

dstFs := fixtures.ByTag("empty").One().DotGit()
url := fmt.Sprintf("file://%s", dstFs.Base())
url := dstFs.Base()

r := newRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
Expand Down Expand Up @@ -255,7 +254,7 @@ func (s *RemoteSuite) TestPushTags(c *C) {
c.Assert(err, IsNil)

dstFs := fixtures.ByTag("empty").One().DotGit()
url := fmt.Sprintf("file://%s", dstFs.Base())
url := dstFs.Base()

r := newRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
Expand Down Expand Up @@ -298,7 +297,7 @@ func (s *RemoteSuite) TestPushNoErrAlreadyUpToDate(c *C) {
f := fixtures.Basic().One()
sto, err := filesystem.NewStorage(f.DotGit())
c.Assert(err, IsNil)
url := fmt.Sprintf("file://%s", f.DotGit().Base())
url := f.DotGit().Base()
r := newRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
URL: url,
Expand All @@ -320,7 +319,7 @@ func (s *RemoteSuite) TestPushRejectNonFastForward(c *C) {
dstSto, err := filesystem.NewStorage(dstFs)
c.Assert(err, IsNil)

url := fmt.Sprintf("file://%s", dstFs.Base())
url := dstFs.Base()
r := newRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
URL: url,
Expand Down Expand Up @@ -349,7 +348,7 @@ func (s *RemoteSuite) TestPushForce(c *C) {
dstSto, err := filesystem.NewStorage(dstFs)
c.Assert(err, IsNil)

url := fmt.Sprintf("file://%s", dstFs.Base())
url := dstFs.Base()
r := newRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
URL: url,
Expand Down Expand Up @@ -378,7 +377,7 @@ func (s *RemoteSuite) TestPushNewReference(c *C) {
dstSto, err := filesystem.NewStorage(dstFs)
c.Assert(err, IsNil)

url := fmt.Sprintf("file://%s", dstFs.Base())
url := dstFs.Base()
r := newRemote(sto, &config.RemoteConfig{
Name: DefaultRemoteName,
URL: url,
Expand All @@ -399,9 +398,9 @@ func (s *RemoteSuite) TestPushNewReference(c *C) {
}

func (s *RemoteSuite) TestPushInvalidEndpoint(c *C) {
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux"})
err := r.Push(&PushOptions{})
c.Assert(err, ErrorMatches, ".*invalid endpoint.*")
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "http://\\"})
err := r.Push(&PushOptions{RemoteName: "foo"})
c.Assert(err, ErrorMatches, ".*invalid character.*")
}

func (s *RemoteSuite) TestPushNonExistentEndpoint(c *C) {
Expand All @@ -426,7 +425,7 @@ func (s *RemoteSuite) TestPushInvalidFetchOptions(c *C) {
func (s *RemoteSuite) TestPushInvalidRefSpec(c *C) {
r := newRemote(nil, &config.RemoteConfig{
Name: DefaultRemoteName,
URL: "file:///some-url",
URL: "some-url",
})

rs := config.RefSpec("^*$**")
Expand All @@ -439,7 +438,7 @@ func (s *RemoteSuite) TestPushInvalidRefSpec(c *C) {
func (s *RemoteSuite) TestPushWrongRemoteName(c *C) {
r := newRemote(nil, &config.RemoteConfig{
Name: DefaultRemoteName,
URL: "file:///some-url",
URL: "some-url",
})

err := r.Push(&PushOptions{
Expand Down
Loading