Skip to content

Commit 999da16

Browse files
committed
plumbing: use --exec-path to find pack executables
Suggested by smola. Issue: src-d#527
1 parent 93e6e73 commit 999da16

File tree

3 files changed

+50
-48
lines changed

3 files changed

+50
-48
lines changed

plumbing/transport/file/client.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
package file
33

44
import (
5+
"bufio"
6+
"errors"
57
"io"
68
"os"
79
"os/exec"
10+
"path/filepath"
11+
"strings"
812

913
"gopkg.in/src-d/go-git.v4/plumbing/transport"
1014
"gopkg.in/src-d/go-git.v4/plumbing/transport/internal/common"
@@ -30,6 +34,41 @@ func NewClient(uploadPackBin, receivePackBin string) transport.Transport {
3034
})
3135
}
3236

37+
func prefixExecPath(cmd string) (string, error) {
38+
// Use `git --exec-path` to find the exec path.
39+
execCmd := &command{cmd: exec.Command("git", "--exec-path")}
40+
41+
// Required, to populate stdErrCloser.
42+
_, err := execCmd.StderrPipe()
43+
if err != nil {
44+
return "", err
45+
}
46+
stdout, err := execCmd.StdoutPipe()
47+
if err != nil {
48+
return "", err
49+
}
50+
stdoutBuf := bufio.NewReader(stdout)
51+
52+
err = execCmd.Start()
53+
if err != nil {
54+
return "", err
55+
}
56+
execPathBytes, isPrefix, err := stdoutBuf.ReadLine()
57+
if err != nil {
58+
return "", err
59+
}
60+
if isPrefix {
61+
return "", errors.New("Couldn't read exec-path line all at once")
62+
}
63+
err = execCmd.Close()
64+
if err != nil {
65+
return "", err
66+
}
67+
execPath := string(execPathBytes)
68+
execPath = strings.TrimSpace(execPath)
69+
return filepath.Join(execPath, cmd), nil
70+
}
71+
3372
func (r *runner) Command(cmd string, ep transport.Endpoint, auth transport.AuthMethod,
3473
) (common.Command, error) {
3574

@@ -40,7 +79,17 @@ func (r *runner) Command(cmd string, ep transport.Endpoint, auth transport.AuthM
4079
cmd = r.ReceivePackBin
4180
}
4281

43-
return makeCommand(cmd, ep)
82+
_, err := exec.LookPath(cmd)
83+
if err != nil {
84+
if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound {
85+
cmd, err = prefixExecPath(cmd)
86+
if err != nil {
87+
return nil, err
88+
}
89+
}
90+
}
91+
92+
return &command{cmd: exec.Command(cmd, ep.Path())}, nil
4493
}
4594

4695
type command struct {

plumbing/transport/file/client_cmd_unix.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

plumbing/transport/file/client_cmd_windows.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)