From 95000af3de2109c44f0d497d493a5831d23bf220 Mon Sep 17 00:00:00 2001 From: Igor Foox Date: Thu, 25 Jul 2019 14:25:51 -0400 Subject: [PATCH 1/5] Add git pre-commit hooks that can be setup to go fmt your code before submission. --- .githooks/pre-commit | 22 ++++++++++++++++++++++ setup-githooks.sh | 8 ++++++++ 2 files changed, 30 insertions(+) create mode 100755 .githooks/pre-commit create mode 100755 setup-githooks.sh diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 000000000..d17b96158 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,22 @@ +#!/bin/sh + + +######################################################################## +# Check that all Go files have been gofmt'd. +######################################################################## +# Source: https://golang.org/misc/git/pre-commit + +gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$') +[ -z "$gofiles" ] && exit 0 + +unformatted=$(gofmt -l $gofiles) +[ -z "$unformatted" ] && exit 0 + +# Some files are not gofmt'd. Print message and fail. + +echo >&2 "Go files must be formatted with gofmt. Please run:" +for fn in $unformatted; do + echo >&2 " gofmt -w $PWD/$fn" +done + +exit 1 diff --git a/setup-githooks.sh b/setup-githooks.sh new file mode 100755 index 000000000..4baabf5b7 --- /dev/null +++ b/setup-githooks.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Copies git hooks from .githooks/ to .git/hooks/, overriding any hooks you may +# have previously had there. +cp .githooks/* .git/hooks/ +for h in `ls .githooks/* | xargs basename`; do + chmod ug+x .git/hooks/$h +done From 6ee4248fcac7197cc0b7389f815d27277dd12aa1 Mon Sep 17 00:00:00 2001 From: Igor Foox Date: Thu, 25 Jul 2019 14:49:43 -0400 Subject: [PATCH 2/5] Move go fmt into it's own script, reference from pre-commit hook and bazelci setup. --- .bazelci/presubmit.yml | 3 +++ .githooks/pre-commit | 16 +--------------- check-gofmt.sh | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 15 deletions(-) create mode 100755 check-gofmt.sh diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 65cb8cb81..ddc39c814 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -2,6 +2,9 @@ validate_config: 1 tasks: ubuntu1804: + shell_commands: + - "echo +++ Check go format" + - "bash check-gofmt.sh" build_targets: - "//..." test_targets: diff --git a/.githooks/pre-commit b/.githooks/pre-commit index d17b96158..c9654f14e 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -4,19 +4,5 @@ ######################################################################## # Check that all Go files have been gofmt'd. ######################################################################## -# Source: https://golang.org/misc/git/pre-commit -gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$') -[ -z "$gofiles" ] && exit 0 - -unformatted=$(gofmt -l $gofiles) -[ -z "$unformatted" ] && exit 0 - -# Some files are not gofmt'd. Print message and fail. - -echo >&2 "Go files must be formatted with gofmt. Please run:" -for fn in $unformatted; do - echo >&2 " gofmt -w $PWD/$fn" -done - -exit 1 +source ./check-gofmt.sh diff --git a/check-gofmt.sh b/check-gofmt.sh new file mode 100755 index 000000000..1ae39bf94 --- /dev/null +++ b/check-gofmt.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# Source: https://golang.org/misc/git/pre-commit + +gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$') +[ -z "$gofiles" ] && exit 0 + +unformatted=$(gofmt -l $gofiles) +[ -z "$unformatted" ] && exit 0 + +# Some files are not gofmt'd. Print message and fail. + +echo >&2 "Go files must be formatted with gofmt. Please run:" +for fn in $unformatted; do + echo >&2 " gofmt -w $PWD/$fn" +done + +exit 1 From 1ba89a52313ac5c04775c914d0f34e716e4697cc Mon Sep 17 00:00:00 2001 From: Igor Foox Date: Thu, 25 Jul 2019 15:02:56 -0400 Subject: [PATCH 3/5] Add file that is not properly formatted. --- go/cmd/rexec/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/cmd/rexec/main.go b/go/cmd/rexec/main.go index 96c4c2c9e..1b6b94aca 100644 --- a/go/cmd/rexec/main.go +++ b/go/cmd/rexec/main.go @@ -51,7 +51,7 @@ func initFlags(cmd *command.Command, opt *command.ExecutionOptions) { } func main() { - cmd := &command.Command{InputSpec: &command.InputSpec{}, Identifiers: &command.Identifiers{}} + cmd := &command.Command{InputSpec: &command.InputSpec{}, Identifiers: &command.Identifiers{}} opt := &command.ExecutionOptions{} initFlags(cmd, opt) flag.Usage = func() { From 7b691f79a81cd3b409ddf4360c86a3b7abccd7db Mon Sep 17 00:00:00 2001 From: Igor Foox Date: Thu, 25 Jul 2019 15:26:49 -0400 Subject: [PATCH 4/5] Change git hook to look at only changed files, CI hook to look at all go files. --- .bazelci/presubmit.yml | 2 +- .githooks/pre-commit | 5 ++++- check-gofmt.sh | 5 +---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index ddc39c814..5b2ccc191 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -4,7 +4,7 @@ tasks: ubuntu1804: shell_commands: - "echo +++ Check go format" - - "bash check-gofmt.sh" + - "./check-gofmt.sh `find go -name '*.go'`" build_targets: - "//..." test_targets: diff --git a/.githooks/pre-commit b/.githooks/pre-commit index c9654f14e..47ee025ce 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -4,5 +4,8 @@ ######################################################################## # Check that all Go files have been gofmt'd. ######################################################################## +# Find all change go files. +gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$') +[ -z "$gofiles" ] && exit 0 -source ./check-gofmt.sh +source ./check-gofmt.sh "$gofiles" diff --git a/check-gofmt.sh b/check-gofmt.sh index 1ae39bf94..c9e1df014 100755 --- a/check-gofmt.sh +++ b/check-gofmt.sh @@ -2,10 +2,7 @@ # Source: https://golang.org/misc/git/pre-commit -gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$') -[ -z "$gofiles" ] && exit 0 - -unformatted=$(gofmt -l $gofiles) +unformatted=$(gofmt -l "$@") [ -z "$unformatted" ] && exit 0 # Some files are not gofmt'd. Print message and fail. From 6064431feb1ded545ca666f039b324e91c8187ba Mon Sep 17 00:00:00 2001 From: Igor Foox Date: Thu, 25 Jul 2019 15:30:16 -0400 Subject: [PATCH 5/5] Revert "Add file that is not properly formatted." This reverts commit 1ba89a52313ac5c04775c914d0f34e716e4697cc. --- go/cmd/rexec/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/cmd/rexec/main.go b/go/cmd/rexec/main.go index 1b6b94aca..96c4c2c9e 100644 --- a/go/cmd/rexec/main.go +++ b/go/cmd/rexec/main.go @@ -51,7 +51,7 @@ func initFlags(cmd *command.Command, opt *command.ExecutionOptions) { } func main() { - cmd := &command.Command{InputSpec: &command.InputSpec{}, Identifiers: &command.Identifiers{}} + cmd := &command.Command{InputSpec: &command.InputSpec{}, Identifiers: &command.Identifiers{}} opt := &command.ExecutionOptions{} initFlags(cmd, opt) flag.Usage = func() {