Skip to content

Commit 1147e09

Browse files
committed
Cache the submodule list rather than recursing through it each time.
I doubt this saves much CPU, but it avoids printing "submodule not initialized" errors repeatedly in troublesome repos.
1 parent 58c18b1 commit 1147e09

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

subtrac.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ type Cache struct {
4848
autoexclude bool // --auto-exclude enabled
4949
excludes map[plumbing.Hash]bool // specifically excluded objects
5050
tracs map[plumbing.Hash]*Trac // object lookup cache
51+
srPaths []string // subrepo paths cache
52+
srRepos []*git.Repository // subrepo object cache
5153
}
5254

5355
func NewCache(rdir string, r *git.Repository, excludes []string,
@@ -373,6 +375,10 @@ func commitPath(path string, sub int) string {
373375
// Recursively open all submodule repositories, starting at c.repo, and
374376
// return a list of them.
375377
func (c *Cache) allSubrepos() (paths []string, repos []*git.Repository, err error) {
378+
if c.srPaths != nil && c.srRepos != nil {
379+
return c.srPaths, c.srRepos, nil
380+
}
381+
376382
var recurse func(string, *git.Repository) error
377383
recurse = func(path string, r *git.Repository) error {
378384
wt, err := r.Worktree()
@@ -419,6 +425,10 @@ func (c *Cache) allSubrepos() (paths []string, repos []*git.Repository, err erro
419425
if err != nil {
420426
return nil, nil, err
421427
}
428+
429+
// Cache entries for next time
430+
c.srPaths = paths
431+
c.srRepos = repos
422432
return paths, repos, nil
423433
}
424434

0 commit comments

Comments
 (0)