| 1 |
// Package file implements the file transport protocol. |
| 2 |
package file |
| 3 |
|
| 4 |
import ( |
| 5 |
"bufio" |
| 6 |
"errors" |
| 7 |
"io" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"runtime" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"github.com/go-git/go-git/v5/plumbing/transport" |
| 14 |
"github.com/go-git/go-git/v5/plumbing/transport/internal/common" |
| 15 |
"golang.org/x/sys/execabs" |
| 16 |
) |
| 17 |
|
| 18 |
// DefaultClient is the default local client. |
| 19 |
var DefaultClient = NewClient( |
| 20 |
transport.UploadPackServiceName, |
| 21 |
transport.ReceivePackServiceName, |
| 22 |
) |
| 23 |
|
| 24 |
type runner struct { |
| 25 |
UploadPackBin string |
| 26 |
ReceivePackBin string |
| 27 |
} |
| 28 |
|
| 29 |
// NewClient returns a new local client using the given git-upload-pack and |
| 30 |
// git-receive-pack binaries. |
| 31 |
func NewClient(uploadPackBin, receivePackBin string) transport.Transport { |
| 32 |
return common.NewClient(&runner{ |
| 33 |
UploadPackBin: uploadPackBin, |
| 34 |
ReceivePackBin: receivePackBin, |
| 35 |
}) |
| 36 |
} |
| 37 |
|
| 38 |
func prefixExecPath(cmd string) (string, error) { |
| 39 |
// Use `git --exec-path` to find the exec path. |
| 40 |
execCmd := execabs.Command("git", "--exec-path") |
| 41 |
|
| 42 |
stdout, err := execCmd.StdoutPipe() |
| 43 |
if err != nil { |
| 44 |
return "", err |
| 45 |
} |
| 46 |
stdoutBuf := bufio.NewReader(stdout) |
| 47 |
|
| 48 |
err = execCmd.Start() |
| 49 |
if err != nil { |
| 50 |
return "", err |
| 51 |
} |
| 52 |
|
| 53 |
execPathBytes, isPrefix, err := stdoutBuf.ReadLine() |
| 54 |
if err != nil { |
| 55 |
return "", err |
| 56 |
} |
| 57 |
if isPrefix { |
| 58 |
return "", errors.New("couldn't read exec-path line all at once") |
| 59 |
} |
| 60 |
|
| 61 |
err = execCmd.Wait() |
| 62 |
if err != nil { |
| 63 |
return "", err |
| 64 |
} |
| 65 |
execPath := string(execPathBytes) |
| 66 |
execPath = strings.TrimSpace(execPath) |
| 67 |
cmd = filepath.Join(execPath, cmd) |
| 68 |
|
| 69 |
// Make sure it actually exists. |
| 70 |
_, err = execabs.LookPath(cmd) |
| 71 |
if err != nil { |
| 72 |
return "", err |
| 73 |
} |
| 74 |
return cmd, nil |
| 75 |
} |
| 76 |
|
| 77 |
func (r *runner) Command(cmd string, ep *transport.Endpoint, auth transport.AuthMethod, |
| 78 |
) (common.Command, error) { |
| 79 |
|
| 80 |
switch cmd { |
| 81 |
case transport.UploadPackServiceName: |
| 82 |
cmd = r.UploadPackBin |
| 83 |
case transport.ReceivePackServiceName: |
| 84 |
cmd = r.ReceivePackBin |
| 85 |
} |
| 86 |
|
| 87 |
_, err := execabs.LookPath(cmd) |
| 88 |
if err != nil { |
| 89 |
if e, ok := err.(*execabs.Error); ok && e.Err == execabs.ErrNotFound { |
| 90 |
cmd, err = prefixExecPath(cmd) |
| 91 |
if err != nil { |
| 92 |
return nil, err |
| 93 |
} |
| 94 |
} else { |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return &command{cmd: execabs.Command(cmd, adjustPathForWindows(ep.Path))}, nil |
| 100 |
} |
| 101 |
|
| 102 |
func isDriveLetter(c byte) bool { |
| 103 |
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') |
| 104 |
} |
| 105 |
|
| 106 |
// On Windows, the path that results from a file: URL has a leading slash. This |
| 107 |
// has to be removed if there's a drive letter |
| 108 |
func adjustPathForWindows(p string) string { |
| 109 |
if runtime.GOOS != "windows" { |
| 110 |
return p |
| 111 |
} |
| 112 |
if len(p) >= 3 && p[0] == '/' && isDriveLetter(p[1]) && p[2] == ':' { |
| 113 |
return p[1:] |
| 114 |
} |
| 115 |
return p |
| 116 |
} |
| 117 |
|
| 118 |
type command struct { |
| 119 |
cmd *execabs.Cmd |
| 120 |
stderrCloser io.Closer |
| 121 |
closed bool |
| 122 |
} |
| 123 |
|
| 124 |
func (c *command) Start() error { |
| 125 |
return c.cmd.Start() |
| 126 |
} |
| 127 |
|
| 128 |
func (c *command) StderrPipe() (io.Reader, error) { |
| 129 |
// Pipe returned by Command.StderrPipe has a race with Read + Command.Wait. |
| 130 |
// We use an io.Pipe and close it after the command finishes. |
| 131 |
r, w := io.Pipe() |
| 132 |
c.cmd.Stderr = w |
| 133 |
c.stderrCloser = r |
| 134 |
return r, nil |
| 135 |
} |
| 136 |
|
| 137 |
func (c *command) StdinPipe() (io.WriteCloser, error) { |
| 138 |
return c.cmd.StdinPipe() |
| 139 |
} |
| 140 |
|
| 141 |
func (c *command) StdoutPipe() (io.Reader, error) { |
| 142 |
return c.cmd.StdoutPipe() |
| 143 |
} |
| 144 |
|
| 145 |
func (c *command) Kill() error { |
| 146 |
c.cmd.Process.Kill() |
| 147 |
return c.Close() |
| 148 |
} |
| 149 |
|
| 150 |
// Close waits for the command to exit. |
| 151 |
func (c *command) Close() error { |
| 152 |
if c.closed { |
| 153 |
return nil |
| 154 |
} |
| 155 |
|
| 156 |
defer func() { |
| 157 |
c.closed = true |
| 158 |
_ = c.stderrCloser.Close() |
| 159 |
|
| 160 |
}() |
| 161 |
|
| 162 |
err := c.cmd.Wait() |
| 163 |
if _, ok := err.(*os.PathError); ok { |
| 164 |
return nil |
| 165 |
} |
| 166 |
|
| 167 |
// When a repository does not exist, the command exits with code 128. |
| 168 |
if _, ok := err.(*execabs.ExitError); ok { |
| 169 |
return nil |
| 170 |
} |
| 171 |
|
| 172 |
return err |
| 173 |
} |