From 1809517d67741ad0c79e45504d889bd595926356 Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Wed, 18 May 2022 19:59:13 +0200 Subject: [PATCH] lint: enable the CODEOWNERS linter on non-clean workdirs Prior to this patch, the linter would bark if there were spurious files in otherwise-empty unowned directories (e.g. pkg/). This patch fixes it. Release note: None --- pkg/internal/codeowners/lint.go | 51 ++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/pkg/internal/codeowners/lint.go b/pkg/internal/codeowners/lint.go index 52d17c53ace6..755e31164a09 100644 --- a/pkg/internal/codeowners/lint.go +++ b/pkg/internal/codeowners/lint.go @@ -66,6 +66,11 @@ func LintEverythingIsOwned( ".gitignore": {}, "README.md": {}, } + skipGlobs := []string{ + "#*#", // editor backup files. + "*~", // editor backup files. + ".*", // .DS_Store, vim temp files, etc. + } // Map of (unowned dir relative to walkRoot) -> (triggering file relative to walkRoot). // For example, kv/kvserver -> kv/kvserver/foo.go. @@ -91,6 +96,7 @@ func LintEverythingIsOwned( // the file itself as unowned, but most of the time we have // one owner for the directory and also the failures get less // noisy by tracking per-directory. + debug("found unowned file: %s", path) parts := strings.Split(path, string(filepath.Separator)) var ok bool for i := range parts { @@ -102,7 +108,7 @@ func LintEverythingIsOwned( } } if !ok { - debug("unowned: %s", path) + debug("adding unowned: %s", path) unowned[filepath.Dir(path)] = path } } @@ -125,20 +131,39 @@ func LintEverythingIsOwned( return err } - if _, ok := skip[relPath]; ok { - debug("skipping %s", relPath) - if info.IsDir() { - return filepath.SkipDir + if relPath != "." { + // We only apply filtering to relPath entries that are not ".". + // Directory-level matching for directory a/b is handled when entry "b" + // is matched inside directory "a". + + if _, ok := skip[relPath]; ok { + debug("skipping %s", relPath) + if info.IsDir() { + return filepath.SkipDir + } + return nil } - return nil - } - fname := filepath.Base(relPath) - if _, ok := skip[fname]; ok { - debug("skipping %s", relPath) - if info.IsDir() { - return filepath.SkipDir + for _, g := range skipGlobs { + ok, err := filepath.Match(g, relPath) + if err != nil { + return err + } + if ok { + debug("skipping %s", relPath) + if info.IsDir() { + return filepath.SkipDir + } + return nil + } + } + fname := filepath.Base(relPath) + if _, ok := skip[fname]; ok { + debug("skipping %s", relPath) + if info.IsDir() { + return filepath.SkipDir + } + return nil } - return nil } if info.IsDir() {