@@ -29,10 +29,11 @@ type CommitLoader struct {
29
29
* common.Common
30
30
cmd oscommands.ICmdObjBuilder
31
31
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
36
37
}
37
38
38
39
// making our dependencies explicit for the sake of easier testing
@@ -43,12 +44,13 @@ func NewCommitLoader(
43
44
getRebaseMode func () (enums.RebaseMode , error ),
44
45
) * CommitLoader {
45
46
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 ),
52
54
}
53
55
}
54
56
@@ -381,11 +383,15 @@ func (self *CommitLoader) getMergeBase(refName string) string {
381
383
}
382
384
383
385
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
389
395
}
390
396
391
397
func ignoringWarnings (commandOutput string ) string {
0 commit comments