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 13, 2024
1 parent 0890963 commit f49eb13
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
11 changes: 4 additions & 7 deletions walk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,12 @@ func getWalkConfig(c *config.Config) *walkConfig {
return c.Exts[walkName].(*walkConfig)
}

func (wc *walkConfig) isExcluded(rel, base string) bool {
if base == ".git" {
return true
}
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
36 changes: 20 additions & 16 deletions walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,19 @@ func Walk(c *config.Config, cexts []config.Configurer, dirs []string, mode Mode,
log.Printf("error loading .bazelignore: %v", err)
}

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

func visit(c *config.Config, cexts []config.Configurer, isBazelIgnored isIgnoredFunc, knownDirectives map[string]bool, updateRels *UpdateFilter, wf WalkFunc, dir, rel string, updateParent bool) {
func visit(c *config.Config, cexts []config.Configurer, isBazelIgnored isIgnoredFunc, knownDirectives map[string]bool, updateRels *UpdateFilter, wf WalkFunc, rel string, updateParent bool) {
if isBazelIgnored(rel) {
return
}

haveError := false

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

// TODO: OPT: ReadDir stats all the files, which is slow. We just care about
// names and modes, so we should use something like
// golang.org/x/tools/internal/fastwalk to speed this up.
Expand All @@ -155,31 +158,36 @@ func visit(c *config.Config, cexts []config.Configurer, isBazelIgnored isIgnored
c = configure(cexts, knownDirectives, c, rel, f)
wc := getWalkConfig(c)

if wc.isExcluded(rel, ".") {
// Don't recurse into excluded directories
if wc.isExcluded(rel) {
return
}

var subdirs, regularFiles []string
for _, ent := range ents {
base := ent.Name()
if isBazelIgnored(path.Join(rel, base)) || wc.isExcluded(rel, base) {
name := ent.Name()
entRel := path.Join(rel, name)

// Skip blank entry names, .git directories, ignored and excluded.
if name == "" || name == ".git" || isBazelIgnored(entRel) || 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, isBazelIgnored, knownDirectives, updateRels, wf, filepath.Join(dir, sub), subRel, shouldUpdate)
visit(c, cexts, isBazelIgnored, knownDirectives, updateRels, wf, subRel, shouldUpdate)
}
}

Expand Down Expand Up @@ -329,27 +337,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 f49eb13

Please sign in to comment.