aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/transport/file/client.go
blob: 54840096d3ad7d29b47912e3eb96913766b1c6fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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.Transport {
	return common.NewClient(&runner{
		UploadPackBin:  uploadPackBin,
		ReceivePackBin: receivePackBin,
	})
}

func (r *runner) Command(cmd string, ep transport.Endpoint) (common.Command, error) {
	switch cmd {
	case transport.UploadPackServiceName:
		cmd = r.UploadPackBin
	case transport.ReceivePackServiceName:
		cmd = r.ReceivePackBin
	}
	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 {
	if c.closed {
		return nil
	}

	return c.cmd.Process.Kill()
}

func (c *command) Wait() error {
	defer func() { c.closed = true }()
	return c.cmd.Wait()
}