@@ -61,6 +61,10 @@ func (oid OID) MarshalJSON() ([]byte, error) {
6161
6262type Repository struct {
6363 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
6468}
6569
6670// smartJoin returns the path that can be described as `relPath`
@@ -73,28 +77,36 @@ func smartJoin(path, relPath string) string {
7377 return filepath .Join (path , relPath )
7478}
7579
80+ // NewRepository creates a new repository object that can be used for
81+ // running `git` commands within that repository.
7682func 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" )
7892 out , err := cmd .Output ()
7993 if err != nil {
8094 switch err := err .(type ) {
8195 case * exec.Error :
8296 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 ,
8598 )
8699 case * exec.ExitError :
87100 return nil , fmt .Errorf (
88- "git rev-parse failed: %s" ,
89- err .Stderr ,
101+ "git rev-parse failed: %s" , err .Stderr ,
90102 )
91103 default :
92104 return nil , err
93105 }
94106 }
95107 gitDir := smartJoin (path , string (bytes .TrimSpace (out )))
96108
97- cmd = exec .Command ("git" , "rev-parse" , "--git-path" , "shallow" )
109+ cmd = exec .Command (gitBin , "rev-parse" , "--git-path" , "shallow" )
98110 cmd .Dir = gitDir
99111 out , err = cmd .Output ()
100112 if err != nil {
@@ -108,7 +120,10 @@ func NewRepository(path string) (*Repository, error) {
108120 return nil , errors .New ("this appears to be a shallow clone; full clone required" )
109121 }
110122
111- return & Repository {path : gitDir }, nil
123+ return & Repository {
124+ path : gitDir ,
125+ gitBin : gitBin ,
126+ }, nil
112127}
113128
114129func (repo * Repository ) gitCommand (callerArgs ... string ) * exec.Cmd {
@@ -124,7 +139,7 @@ func (repo *Repository) gitCommand(callerArgs ...string) *exec.Cmd {
124139
125140 args = append (args , callerArgs ... )
126141
127- cmd := exec .Command ("git" , args ... )
142+ cmd := exec .Command (repo . gitBin , args ... )
128143
129144 cmd .Env = append (
130145 os .Environ (),
0 commit comments