File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,26 +2,41 @@ package git
22
33import (
44 "path/filepath"
5+ "sync"
56
67 "github.com/cli/safeexec"
78)
89
10+ // This variable will be used to memoize the result of `findGitBin()`,
11+ // since its return value only depends on the environment.
12+ var gitBinMemo struct {
13+ once sync.Once
14+
15+ gitBin string
16+ err error
17+ }
18+
919// findGitBin finds the `git` binary in PATH that should be used by
1020// the rest of `git-sizer`. It uses `safeexec` to find the executable,
1121// because on Windows, `exec.Cmd` looks not only in PATH, but also in
1222// the current directory. This is a potential risk if the repository
1323// being scanned is hostile and non-bare because it might possibly
1424// contain an executable file named `git`.
1525func findGitBin () (string , error ) {
16- gitBin , err := safeexec .LookPath ("git" )
17- if err != nil {
18- return "" , err
19- }
26+ gitBinMemo .once .Do (func () {
27+ p , err := safeexec .LookPath ("git" )
28+ if err != nil {
29+ gitBinMemo .err = err
30+ return
31+ }
2032
21- gitBin , err = filepath .Abs (gitBin )
22- if err != nil {
23- return "" , err
24- }
33+ p , err = filepath .Abs (p )
34+ if err != nil {
35+ gitBinMemo .err = err
36+ return
37+ }
2538
26- return gitBin , nil
39+ gitBinMemo .gitBin = p
40+ })
41+ return gitBinMemo .gitBin , gitBinMemo .err
2742}
You can’t perform that action at this time.
0 commit comments