Skip to content

Commit 39102df

Browse files
committed
findGitBin(): memoize the result
1 parent 1d75c74 commit 39102df

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

git/git_bin.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,41 @@ package git
22

33
import (
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`.
1525
func 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
}

0 commit comments

Comments
 (0)