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

transport: add local transport #145

Merged
merged 4 commits into from
Nov 29, 2016
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
Binary file not shown.
5 changes: 5 additions & 0 deletions fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ var fixtures = Fixtures{{
Tags: []string{"packfile", "diff-tree"},
URL: "https://github.com/toqueteos/ts3.git",
PackfileHash: plumbing.NewHash("21b33a26eb7ffbd35261149fe5d886b9debab7cb"),
}, {
Tags: []string{"empty", ".git"},
URL: "https://github.com/git-fixtures/empty.git",
DotGitHash: plumbing.NewHash("4abe340d8d378baf7c2bfb2854c0fa498642bac3"),
ObjectsCount: 0,
}}

func All() Fixtures {
Expand Down
5 changes: 4 additions & 1 deletion plumbing/transport/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/file"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
)
Expand All @@ -13,6 +14,7 @@ var Protocols = map[string]transport.Client{
"http": http.DefaultClient,
"https": http.DefaultClient,
"ssh": ssh.DefaultClient,
"file": file.DefaultClient,
}

// InstallProtocol adds or modifies an existing protocol.
Expand All @@ -21,7 +23,8 @@ func InstallProtocol(scheme string, c transport.Client) {
}

// NewClient returns the appropriate client among of the set of known protocols:
// HTTP, SSH. See `InstallProtocol` to add or modify protocols.
// http://, https://, ssh:// and file://.
// See `InstallProtocol` to add or modify protocols.
func NewClient(endpoint transport.Endpoint) (transport.Client, error) {
f, ok := Protocols[endpoint.Scheme]
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion plumbing/transport/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (s *ClientSuite) TestNewClientSSH(c *C) {

output, err := NewClient(e)
c.Assert(err, IsNil)
c.Assert(typeAsString(output), Equals, "*ssh.client")
c.Assert(output, NotNil)
}

func (s *ClientSuite) TestNewClientUnknown(c *C) {
Expand Down
3 changes: 2 additions & 1 deletion plumbing/transport/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ var (
)

const (
UploadPackServiceName = "git-upload-pack"
UploadPackServiceName = "git-upload-pack"
ReceivePackServiceName = "git-receive-pack"
)

// Client can initiate git-fetch-pack and git-send-pack processes.
Expand Down
72 changes: 72 additions & 0 deletions plumbing/transport/file/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package file

import (
"io"
"os/exec"

"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/internal/common"
)

// DefaultClient is the default local client.
var DefaultClient = NewClient(
transport.UploadPackServiceName,
transport.ReceivePackServiceName,
)

type runner struct {
UploadPackBin string
ReceivePackBin string
}

// NewClient returns a new local client using the given git-upload-pack and
// git-receive-pack binaries.
func NewClient(uploadPackBin, receivePackBin string) transport.Client {
return common.NewClient(&runner{
UploadPackBin: uploadPackBin,
ReceivePackBin: receivePackBin,
})
}

func (r *runner) Command(cmd string, ep transport.Endpoint) (common.Command, error) {
return &command{cmd: exec.Command(cmd, ep.Path)}, nil
}

type command struct {
cmd *exec.Cmd
closed bool
}

func (c *command) SetAuth(auth transport.AuthMethod) error {
if auth != nil {
return transport.ErrInvalidAuthMethod
}

return nil
}

func (c *command) Start() error {
return c.cmd.Start()
}

func (c *command) StderrPipe() (io.Reader, error) {
return c.cmd.StderrPipe()
}

func (c *command) StdinPipe() (io.WriteCloser, error) {
return c.cmd.StdinPipe()
}

func (c *command) StdoutPipe() (io.Reader, error) {
return c.cmd.StdoutPipe()
}

// Close waits for the command to exit.
func (c *command) Close() error {
return c.cmd.Process.Kill()
}

func (c *command) Wait() error {
defer func() { c.closed = true }()
return c.cmd.Wait()
}
9 changes: 9 additions & 0 deletions plumbing/transport/file/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package file

import (
"testing"

. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }
48 changes: 48 additions & 0 deletions plumbing/transport/file/fetch_pack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package file

import (
"fmt"
"os/exec"

"gopkg.in/src-d/go-git.v4/fixtures"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/test"

. "gopkg.in/check.v1"
)

type FetchPackSuite struct {
fixtures.Suite
test.FetchPackSuite
}

var _ = Suite(&FetchPackSuite{})

func (s *FetchPackSuite) SetUpSuite(c *C) {
s.Suite.SetUpSuite(c)

if err := exec.Command("git", "--version").Run(); err != nil {
c.Skip("git command not found")
}

s.FetchPackSuite.Client = DefaultClient

fixture := fixtures.Basic().One()
path := fixture.DotGit().Base()
url := fmt.Sprintf("file://%s", path)
ep, err := transport.NewEndpoint(url)
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)
c.Assert(err, IsNil)
s.EmptyEndpoint = ep

url = fmt.Sprintf("file://%s/%s", fixtures.DataFolder, "non-existent")
ep, err = transport.NewEndpoint(url)
c.Assert(err, IsNil)
s.NonExistentEndpoint = ep
}
Loading