From 999722b722849702696b92e62fff8dfcda55c092 Mon Sep 17 00:00:00 2001 From: Varun Patro Date: Sun, 3 Dec 2017 14:15:55 +0800 Subject: [PATCH 1/2] Run `golint` for each file that has changed If I change one file `main.go`, then the `GoLint` hook runs `golint main.go` which is desirable and correct. However, when I change files that belong to multiple packages such as: 1. `main.go` 2. `lib/util.go` then the `GoLint` hook runs `golint main.go lib/util.go` which will always return the error (regardless of the presence of any lint errors) > lib/util.go is in package lib, not main This is because `golint` can only be used in the following way (from `golint --help`: > Usage of golint: > golint [flags] # runs on package in current directory > golint [flags] [packages] > golint [flags] [directories] # where a '/...' suffix includes all sub-directories > golint [flags] [files] # all must belong to a single package This means `golint` shouldn't be passed files that belong to different packages. This commit fixes this behaviour by running `golint` separately on each file that has changed. And the concatenating all their outputs. --- lib/overcommit/hook/pre_commit/go_lint.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/overcommit/hook/pre_commit/go_lint.rb b/lib/overcommit/hook/pre_commit/go_lint.rb index e5b93c3b..9b79c6f1 100644 --- a/lib/overcommit/hook/pre_commit/go_lint.rb +++ b/lib/overcommit/hook/pre_commit/go_lint.rb @@ -4,8 +4,17 @@ module Overcommit::Hook::PreCommit # @see https://github.com/golang/lint class GoLint < Base def run - result = execute(command, args: applicable_files) - output = result.stdout + result.stderr + output = '' + success = true + + # golint doesn't accept multiple file arguments if + # they belong to different packages + applicable_files.each do |gofile| + result = execute(command, args: Array(gofile)) + output += result.stdout + success &&= result.success? + end + # Unfortunately the exit code is always 0 return :pass if output.empty? From c0aa43da140680a6af9232c9c6a667c830c8e917 Mon Sep 17 00:00:00 2001 From: Varun Patro Date: Sun, 3 Dec 2017 14:35:32 +0800 Subject: [PATCH 2/2] Remove trailing space and redundant variables --- lib/overcommit/hook/pre_commit/go_lint.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/overcommit/hook/pre_commit/go_lint.rb b/lib/overcommit/hook/pre_commit/go_lint.rb index 9b79c6f1..3e0dfc3c 100644 --- a/lib/overcommit/hook/pre_commit/go_lint.rb +++ b/lib/overcommit/hook/pre_commit/go_lint.rb @@ -5,16 +5,14 @@ module Overcommit::Hook::PreCommit class GoLint < Base def run output = '' - success = true # golint doesn't accept multiple file arguments if # they belong to different packages applicable_files.each do |gofile| result = execute(command, args: Array(gofile)) - output += result.stdout - success &&= result.success? + output += result.stdout + result.stderr end - + # Unfortunately the exit code is always 0 return :pass if output.empty?