From cf144a139fadb344d1a57baae1028c6eb44ea802 Mon Sep 17 00:00:00 2001 From: creatorHead Date: Wed, 29 Apr 2026 15:16:25 +0530 Subject: [PATCH] fix: skip ignored directories early during file walk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, ignore patterns were only checked against files — the walk still descended into every directory unconditionally. This meant a directory like .venv with thousands of files was fully traversed before each file was individually filtered out, causing significant slowdowns. Now, when a directory itself matches an ignore pattern, filepath.SkipDir is returned immediately, pruning the entire subtree in one step regardless of how many files are inside. --- addlicense/main.go | 4 ++++ cmd/headers.go | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/addlicense/main.go b/addlicense/main.go index fe7cf93..e082bb1 100644 --- a/addlicense/main.go +++ b/addlicense/main.go @@ -307,6 +307,10 @@ func walk(ch chan<- *file, start string, logger *log.Logger) error { return nil } if fi.IsDir() { + if path != start && FileMatches(path, ignorePatterns) { + logger.Printf("[DEBUG] skipping directory: %s", path) + return filepath.SkipDir + } return nil } // Skip symlinks — broken symlinks cause os.ReadFile to fail with diff --git a/cmd/headers.go b/cmd/headers.go index eb0bf67..abe394d 100644 --- a/cmd/headers.go +++ b/cmd/headers.go @@ -259,7 +259,13 @@ func updateExistingHeaders(cmd *cobra.Command, ignoredPatterns []string, dryRun return nil } - if err != nil || d.IsDir() { + if err != nil { + return nil + } + if d.IsDir() { + if path != "." && addlicense.FileMatches(path, ignoredPatterns) { + return filepath.SkipDir + } return nil }