@@ -61,6 +61,10 @@ func (oid OID) MarshalJSON() ([]byte, error) {
61
61
62
62
type Repository struct {
63
63
path string
64
+
65
+ // gitBin is the path of the `git` executable that should be used
66
+ // when running commands in this repository.
67
+ gitBin string
64
68
}
65
69
66
70
// smartJoin returns the path that can be described as `relPath`
@@ -73,28 +77,36 @@ func smartJoin(path, relPath string) string {
73
77
return filepath .Join (path , relPath )
74
78
}
75
79
80
+ // NewRepository creates a new repository object that can be used for
81
+ // running `git` commands within that repository.
76
82
func NewRepository (path string ) (* Repository , error ) {
77
- cmd := exec .Command ("git" , "-C" , path , "rev-parse" , "--git-dir" )
83
+ // Find the `git` executable to be used:
84
+ gitBin , err := findGitBin ()
85
+ if err != nil {
86
+ return nil , fmt .Errorf (
87
+ "could not find 'git' executable (is it in your PATH?): %v" , err ,
88
+ )
89
+ }
90
+
91
+ cmd := exec .Command (gitBin , "-C" , path , "rev-parse" , "--git-dir" )
78
92
out , err := cmd .Output ()
79
93
if err != nil {
80
94
switch err := err .(type ) {
81
95
case * exec.Error :
82
96
return nil , fmt .Errorf (
83
- "could not run git (is it in your PATH?): %s" ,
84
- err .Err ,
97
+ "could not run '%s': %v" , gitBin , err .Err ,
85
98
)
86
99
case * exec.ExitError :
87
100
return nil , fmt .Errorf (
88
- "git rev-parse failed: %s" ,
89
- err .Stderr ,
101
+ "git rev-parse failed: %s" , err .Stderr ,
90
102
)
91
103
default :
92
104
return nil , err
93
105
}
94
106
}
95
107
gitDir := smartJoin (path , string (bytes .TrimSpace (out )))
96
108
97
- cmd = exec .Command ("git" , "rev-parse" , "--git-path" , "shallow" )
109
+ cmd = exec .Command (gitBin , "rev-parse" , "--git-path" , "shallow" )
98
110
cmd .Dir = gitDir
99
111
out , err = cmd .Output ()
100
112
if err != nil {
@@ -108,7 +120,10 @@ func NewRepository(path string) (*Repository, error) {
108
120
return nil , errors .New ("this appears to be a shallow clone; full clone required" )
109
121
}
110
122
111
- return & Repository {path : gitDir }, nil
123
+ return & Repository {
124
+ path : gitDir ,
125
+ gitBin : gitBin ,
126
+ }, nil
112
127
}
113
128
114
129
func (repo * Repository ) gitCommand (callerArgs ... string ) * exec.Cmd {
@@ -124,7 +139,7 @@ func (repo *Repository) gitCommand(callerArgs ...string) *exec.Cmd {
124
139
125
140
args = append (args , callerArgs ... )
126
141
127
- cmd := exec .Command ("git" , args ... )
142
+ cmd := exec .Command (repo . gitBin , args ... )
128
143
129
144
cmd .Env = append (
130
145
os .Environ (),
0 commit comments