Skip to content

Commit 298e76e

Browse files
takecyclaude
andcommitted
refactor(syncer): filter hidden directories before the IsRepo check
ListDirs used `strings.Index(f.Name(), ".") != 0` to drop hidden entries — a slightly indirect way of asking the same question that strings.HasPrefix asks more idiomatically. Switch to HasPrefix. While here, reorder the filter chain so the cheap HasPrefix check runs before IsRepo. IsRepo now performs an os.Stat per call, so short-circuiting on hidden directories avoids a syscall for each .something entry in the parent. Rewrite the conjunction as a sequence of early-continue guards so the intent of each step reads top-down. Pure refactor — for non-empty names, "name starts with ." and "strings.Index(name, \".\") == 0" are exactly the same predicate, so ListDirs returns the same set of paths it always did. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f38ae87 commit 298e76e

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

syncer/dir.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import (
66
"strings"
77
)
88

9-
// ListDirs returns target directories
9+
// ListDirs returns the names of direct child directories that look like git
10+
// repositories, skipping hidden entries (those whose name starts with ".").
11+
// The order matters for efficiency: HasPrefix is a cheap string check, so we
12+
// filter out hidden directories before paying the os.Stat cost in IsRepo.
1013
func ListDirs() (dirs []string, err error) {
1114
files, err := os.ReadDir("./")
1215
if err != nil {
@@ -15,11 +18,17 @@ func ListDirs() (dirs []string, err error) {
1518

1619
dirs = make([]string, 0, len(files))
1720
for _, f := range files {
18-
if f.IsDir() && IsRepo(f.Name()) && strings.Index(f.Name(), ".") != 0 {
19-
dirs = append(dirs, f.Name())
21+
if !f.IsDir() {
22+
continue
2023
}
24+
if strings.HasPrefix(f.Name(), ".") {
25+
continue
26+
}
27+
if !IsRepo(f.Name()) {
28+
continue
29+
}
30+
dirs = append(dirs, f.Name())
2131
}
22-
2332
return
2433
}
2534

0 commit comments

Comments
 (0)