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

Commit 4605107

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

File tree

3 files changed

+47
-48
lines changed

3 files changed

+47
-48
lines changed

plumbing/transport/file/client.go

Lines changed: 47 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,38 @@ 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 := exec.Command("git", "--exec-path")
40+
41+
err := execCmd.Start()
42+
if err != nil {
43+
return "", err
44+
}
45+
46+
stdout, err := execCmd.StdoutPipe()
47+
if err != nil {
48+
return "", err
49+
}
50+
stdoutBuf := bufio.NewReader(stdout)
51+
52+
execPathBytes, isPrefix, err := stdoutBuf.ReadLine()
53+
if err != nil {
54+
return "", err
55+
}
56+
if isPrefix {
57+
return "", errors.New("Couldn't read exec-path line all at once")
58+
}
59+
60+
err = execCmd.Wait()
61+
if err != nil {
62+
return "", err
63+
}
64+
execPath := string(execPathBytes)
65+
execPath = strings.TrimSpace(execPath)
66+
return filepath.Join(execPath, cmd), nil
67+
}
68+
3369
func (r *runner) Command(cmd string, ep transport.Endpoint, auth transport.AuthMethod,
3470
) (common.Command, error) {
3571

@@ -40,7 +76,17 @@ func (r *runner) Command(cmd string, ep transport.Endpoint, auth transport.AuthM
4076
cmd = r.ReceivePackBin
4177
}
4278

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

4692
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)