Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reduce path.join while walking repo #1913

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions walk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func getWalkConfig(c *config.Config) *walkConfig {
return c.Exts[walkName].(*walkConfig)
}

func (wc *walkConfig) isExcluded(rel, base string) bool {
return matchAnyGlob(wc.excludes, path.Join(rel, base))
func (wc *walkConfig) isExcluded(p string) bool {
return matchAnyGlob(wc.excludes, p)
}

func (wc *walkConfig) shouldFollow(rel, base string) bool {
return matchAnyGlob(wc.follow, path.Join(rel, base))
func (wc *walkConfig) shouldFollow(p string) bool {
return matchAnyGlob(wc.follow, p)
}

var _ config.Configurer = (*Configurer)(nil)
Expand Down
26 changes: 13 additions & 13 deletions walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ func Walk(c *config.Config, cexts []config.Configurer, dirs []string, mode Mode,
log.Fatalf("error walking the file system: %v\n", err)
}

visit(c, cexts, knownDirectives, updateRels, trie, wf, c.RepoRoot, "", false)
visit(c, cexts, knownDirectives, updateRels, trie, wf, "", false)
}

func visit(c *config.Config, cexts []config.Configurer, knownDirectives map[string]bool, updateRels *UpdateFilter, trie *pathTrie, wf WalkFunc, dir, rel string, updateParent bool) {
func visit(c *config.Config, cexts []config.Configurer, knownDirectives map[string]bool, updateRels *UpdateFilter, trie *pathTrie, wf WalkFunc, rel string, updateParent bool) {
haveError := false

ents := make([]fs.DirEntry, 0, len(trie.children))
Expand All @@ -144,6 +144,9 @@ func visit(c *config.Config, cexts []config.Configurer, knownDirectives map[stri
return ents[i].Name() < ents[j].Name()
})

// Absolute path to the directory being visited
dir := filepath.Join(c.RepoRoot, rel)

f, err := loadBuildFile(c, rel, dir, ents)
if err != nil {
log.Print(err)
Expand All @@ -158,17 +161,18 @@ func visit(c *config.Config, cexts []config.Configurer, knownDirectives map[stri
c = configure(cexts, knownDirectives, c, rel, f)
wc := getWalkConfig(c)

if wc.isExcluded(rel, ".") {
if wc.isExcluded(rel) {
return
}

var subdirs, regularFiles []string
for _, ent := range ents {
base := ent.Name()
if wc.isExcluded(rel, base) {
entRel := path.Join(rel, base)
if wc.isExcluded(entRel) {
continue
}
ent := resolveFileInfo(wc, dir, rel, ent)
ent := resolveFileInfo(wc, dir, entRel, ent)
switch {
case ent == nil:
continue
Expand All @@ -182,7 +186,7 @@ func visit(c *config.Config, cexts []config.Configurer, knownDirectives map[stri
shouldUpdate := updateRels.shouldUpdate(rel, updateParent)
for _, sub := range subdirs {
if subRel := path.Join(rel, sub); updateRels.shouldVisit(subRel, shouldUpdate) {
visit(c, cexts, knownDirectives, updateRels, trie.children[sub], wf, path.Join(dir, sub), subRel, shouldUpdate)
visit(c, cexts, knownDirectives, updateRels, trie.children[sub], wf, subRel, shouldUpdate)
}
}

Expand Down Expand Up @@ -332,27 +336,23 @@ func findGenFiles(wc *walkConfig, f *rule.File) []string {

var genFiles []string
for _, s := range strs {
if !wc.isExcluded(f.Pkg, s) {
if !wc.isExcluded(path.Join(f.Pkg, s)) {
genFiles = append(genFiles, s)
}
}
return genFiles
}

func resolveFileInfo(wc *walkConfig, dir, rel string, ent fs.DirEntry) fs.DirEntry {
base := ent.Name()
if base == "" {
return nil
}
if ent.Type()&os.ModeSymlink == 0 {
// Not a symlink, use the original FileInfo.
return ent
}
if !wc.shouldFollow(rel, base) {
if !wc.shouldFollow(rel) {
// A symlink, but not one we should follow.
return nil
}
fi, err := os.Stat(path.Join(dir, base))
fi, err := os.Stat(path.Join(dir, ent.Name()))
if err != nil {
// A symlink, but not one we could resolve.
return nil
Expand Down