Skip to content

Commit 30e034c

Browse files
committed
Use git rev-parse to check for existence of base branches
And cache the result locally in CommitLoader.
1 parent 03e001e commit 30e034c

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

pkg/commands/git_commands/commit_loader.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ type CommitLoader struct {
2929
*common.Common
3030
cmd oscommands.ICmdObjBuilder
3131

32-
getRebaseMode func() (enums.RebaseMode, error)
33-
readFile func(filename string) ([]byte, error)
34-
walkFiles func(root string, fn filepath.WalkFunc) error
35-
dotGitDir string
32+
getRebaseMode func() (enums.RebaseMode, error)
33+
readFile func(filename string) ([]byte, error)
34+
walkFiles func(root string, fn filepath.WalkFunc) error
35+
dotGitDir string
36+
doBranchesExist map[string]bool
3637
}
3738

3839
// making our dependencies explicit for the sake of easier testing
@@ -43,12 +44,13 @@ func NewCommitLoader(
4344
getRebaseMode func() (enums.RebaseMode, error),
4445
) *CommitLoader {
4546
return &CommitLoader{
46-
Common: cmn,
47-
cmd: cmd,
48-
getRebaseMode: getRebaseMode,
49-
readFile: os.ReadFile,
50-
walkFiles: filepath.Walk,
51-
dotGitDir: dotGitDir,
47+
Common: cmn,
48+
cmd: cmd,
49+
getRebaseMode: getRebaseMode,
50+
readFile: os.ReadFile,
51+
walkFiles: filepath.Walk,
52+
dotGitDir: dotGitDir,
53+
doBranchesExist: make(map[string]bool),
5254
}
5355
}
5456

@@ -381,11 +383,15 @@ func (self *CommitLoader) getMergeBase(refName string) string {
381383
}
382384

383385
func (self *CommitLoader) branchExists(branchName string) bool {
384-
// This is not the proper way to test whether a branch exists; it could fail
385-
// in the future if git decides to store its branches in a different way.
386-
// However, making git calls to test if the branches exist is too expensive.
387-
_, err := os.Stat(filepath.Join(self.dotGitDir, "refs/heads", branchName))
388-
return err == nil
386+
branchExists, cacheEntryExists := self.doBranchesExist[branchName]
387+
if cacheEntryExists {
388+
return branchExists
389+
}
390+
391+
err := self.cmd.New(fmt.Sprintf("git rev-parse --verify --quiet refs/heads/%s", self.cmd.Quote(branchName))).DontLog().Run()
392+
branchExists = err == nil
393+
self.doBranchesExist[branchName] = branchExists
394+
return branchExists
389395
}
390396

391397
func ignoringWarnings(commandOutput string) string {

0 commit comments

Comments
 (0)