Skip to content

Commit

Permalink
refactor: reduce path.join while walking repo
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Sep 16, 2024
1 parent bf2bb89 commit f66099c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
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
34 changes: 18 additions & 16 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 := path.Join(c.RepoRoot, rel)

f, err := loadBuildFile(c, rel, dir, ents)
if err != nil {
log.Print(err)
Expand All @@ -158,31 +161,34 @@ 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) {
name := ent.Name()
entRel := path.Join(rel, name)

if wc.isExcluded(entRel) {
continue
}
ent := resolveFileInfo(wc, dir, rel, ent)

ent := resolveFileInfo(wc, dir, entRel, ent)
switch {
case ent == nil:
continue
case ent.IsDir():
subdirs = append(subdirs, base)
subdirs = append(subdirs, name)
default:
regularFiles = append(regularFiles, base)
regularFiles = append(regularFiles, name)
}
}

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 +338,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

0 comments on commit f66099c

Please sign in to comment.