Skip to content

Commit

Permalink
plumbing: use --exec-path to find pack executables
Browse files Browse the repository at this point in the history
Suggested by smola.

Issue: src-d#527
  • Loading branch information
strib committed Aug 2, 2017
1 parent 93e6e73 commit 4605107
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 48 deletions.
48 changes: 47 additions & 1 deletion plumbing/transport/file/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
package file

import (
"bufio"
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"strings"

"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/internal/common"
Expand All @@ -30,6 +34,38 @@ func NewClient(uploadPackBin, receivePackBin string) transport.Transport {
})
}

func prefixExecPath(cmd string) (string, error) {
// Use `git --exec-path` to find the exec path.
execCmd := exec.Command("git", "--exec-path")

err := execCmd.Start()
if err != nil {
return "", err
}

stdout, err := execCmd.StdoutPipe()
if err != nil {
return "", err
}
stdoutBuf := bufio.NewReader(stdout)

execPathBytes, isPrefix, err := stdoutBuf.ReadLine()
if err != nil {
return "", err
}
if isPrefix {
return "", errors.New("Couldn't read exec-path line all at once")
}

err = execCmd.Wait()
if err != nil {
return "", err
}
execPath := string(execPathBytes)
execPath = strings.TrimSpace(execPath)
return filepath.Join(execPath, cmd), nil
}

func (r *runner) Command(cmd string, ep transport.Endpoint, auth transport.AuthMethod,
) (common.Command, error) {

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

return makeCommand(cmd, ep)
_, err := exec.LookPath(cmd)
if err != nil {
if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound {
cmd, err = prefixExecPath(cmd)
if err != nil {
return nil, err
}
}
}

return &command{cmd: exec.Command(cmd, ep.Path())}, nil
}

type command struct {
Expand Down
18 changes: 0 additions & 18 deletions plumbing/transport/file/client_cmd_unix.go

This file was deleted.

29 changes: 0 additions & 29 deletions plumbing/transport/file/client_cmd_windows.go

This file was deleted.

0 comments on commit 4605107

Please sign in to comment.