forked from go-git/go-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git: skip ignored files while walking worktree
Skip ignored files when walking through the worktree. This signigifantly improves the performance of `Status()`: In a repository with 3M ignored files `Status` now takes 27 s instead of 491 s.
- Loading branch information
Showing
3 changed files
with
112 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package gitignore | ||
|
||
import ( | ||
"slices" | ||
|
||
"github.com/go-git/go-git/v5/utils/merkletrie/noder" | ||
) | ||
|
||
var _ noder.Noder = matchNoder{} | ||
|
||
type matchNoder struct { | ||
ignore Matcher | ||
noder.Noder | ||
path []string | ||
children []noder.Noder | ||
} | ||
|
||
// IgnoreNoder returns a [noder.Noder] that filters out ignored nodes. | ||
// It cannot ignore itself. | ||
func IgnoreNoder(m Matcher, n noder.Noder) noder.Noder { | ||
if m == nil { | ||
return n | ||
} | ||
|
||
var path []string | ||
if name := n.Name(); name != "." { | ||
path = []string{name} | ||
} | ||
|
||
return matchNoder{ignore: m, Noder: n, path: path} | ||
} | ||
|
||
func (n matchNoder) Children() ([]noder.Noder, error) { | ||
if len(n.children) > 0 { | ||
return n.children, nil | ||
} | ||
|
||
children, err := n.Noder.Children() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
n.children = n.ignoreChildren(children) | ||
|
||
return n.children, nil | ||
} | ||
|
||
func (n matchNoder) ignoreChildren(children []noder.Noder) []noder.Noder { | ||
found := make([]noder.Noder, 0, len(children)) | ||
|
||
for _, child := range children { | ||
path := append(n.path, child.Name()) | ||
|
||
if n.ignore.Match(path, child.IsDir()) { | ||
continue | ||
} | ||
|
||
if child.IsDir() { | ||
found = append(found, matchNoder{ignore: n.ignore, Noder: child, path: slices.Clone(path)}) | ||
} else { | ||
found = append(found, child) | ||
} | ||
} | ||
|
||
return found | ||
} | ||
|
||
func (n matchNoder) NumChildren() (int, error) { | ||
children, err := n.Children() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return len(children), nil | ||
} | ||
|
||
func (n matchNoder) Skip() bool { | ||
return n.Noder.Skip() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters