From f37c7ca7acceb8c42ca5b6ff6f0f25ab7e7c98c2 Mon Sep 17 00:00:00 2001 From: Euan Kemp Date: Mon, 11 Jun 2018 01:14:02 -0700 Subject: [PATCH] devcam hook: ignore vendor in whitespace check While I was there, I also switched gofmt's vendor ignoring logic to use git's builtin pathspec filtering. Change-Id: Iab8093d795e7f967cceef351c6a593ba776d9790 --- dev/devcam/hook.go | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/dev/devcam/hook.go b/dev/devcam/hook.go index b66fab8e6..03fac054f 100644 --- a/dev/devcam/hook.go +++ b/dev/devcam/hook.go @@ -236,7 +236,8 @@ func (c *hookCmd) hookGofmt() error { } func (c *hookCmd) hookTrailingSpace() error { - out, _ := cmdOutputDirErr(".", "git", "diff-index", "--check", "--diff-filter=ACM", "--cached", "HEAD", "--") + // see 'pathspec' for the ':!' syntax to ignore a directory. + out, _ := cmdOutputDirErr(".", "git", "diff-index", "--check", "--diff-filter=ACM", "--cached", "HEAD", "--", ":!/vendor/") if out != "" { printf("\n%s", out) printf("Trailing whitespace detected, you need to clean it up manually.\n") @@ -256,11 +257,11 @@ func (c *hookCmd) runGofmt() (files []string, err error) { repo += string(filepath.Separator) } - out, err := cmdOutputDirErr(".", "git", "diff-index", "--name-only", "--diff-filter=ACM", "--cached", "HEAD", "--") + out, err := cmdOutputDirErr(".", "git", "diff-index", "--name-only", "--diff-filter=ACM", "--cached", "HEAD", "--", ":!/vendor/", ":(glob)**/*.go") if err != nil { return nil, err } - indexFiles := addRoot(repo, filter(gofmtRequired, nonBlankLines(out))) + indexFiles := addRoot(repo, nonBlankLines(out)) if len(indexFiles) == 0 { return } @@ -330,23 +331,6 @@ func filter(f func(string) bool, list []string) []string { return out } -// gofmtRequired reports whether the specified file should be checked -// for gofmt'dness by the pre-commit hook. -// The file name is relative to the repo root. -func gofmtRequired(file string) bool { - if !strings.HasSuffix(file, ".go") { - return false - } - // vendor files should be imported as-is - if strings.HasPrefix(file, "vendor/") { - return false - } - if !strings.HasPrefix(file, "test/") { - return true - } - return strings.HasPrefix(file, "test/bench/") || file == "test/run.go" -} - func commandString(command string, args []string) string { return strings.Join(append([]string{command}, args...), " ") }