diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 9dbe1a0..dfeb303 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -358,11 +358,14 @@ # go-revive # * File-based # * Executes if any .go files modified +# * Executes if revive.toml modified +# * Adds arg '-config=revive.toml' if present +# NOTE: Does nothing if ONLY revive.toml is modified (ie no .go files modified) # ============================================================================== - id: go-revive name: 'go-revive' entry: go-revive.sh - types: [go] + files: '(\.go$)|(\brevive\.toml$)' exclude: '(^|/)vendor/' language: 'script' description: "Run 'revive [$ARGS] $FILE' for each staged .go file" @@ -375,11 +378,13 @@ # * Targets first parent folder with a go.mod file # * Executes if any .go files modified # * Executes if go.mod modified +# * Executes if revive.toml modified +# * Adds arg '-config=revive.toml' if present # ============================================================================== - id: go-revive-mod name: 'go-revive-mod' entry: go-revive-mod.sh - files: '(\.go$)|(\bgo\.mod$)' + files: '(\.go$)|(\bgo\.mod$)|(\brevive\.toml$)' exclude: '(^|/)vendor/' language: 'script' description: "Run 'cd $(mod_root $FILE); revive [$ARGS] ./...' for each staged .go file" @@ -393,11 +398,13 @@ # * Targets ALL folders with a go.mod file # * Executes if any .go files modified # * Executes if go.mod modified +# * Executes if revive.toml modified +# * Adds arg '-config=revive.toml' if present # ============================================================================== - id: go-revive-repo-mod name: 'go-revive-repo-mod' entry: go-revive-repo-mod.sh - files: '(\.go$)|(\bgo\.mod$)' + files: '(\.go$)|(\bgo\.mod$)|(\brevive\.toml$)' exclude: '(^|/)vendor/' language: 'script' description: "Run 'cd $(mod_root); revive [$ARGS] ./...' for each module in the repo" diff --git a/README.md b/README.md index 8d92ec6..f1219b6 100644 --- a/README.md +++ b/README.md @@ -532,7 +532,7 @@ bingo install golang.org/x/lint/golint ------------- ### go-revive -~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint. +\~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint. | Hook ID | Description |-----------|------------ @@ -540,6 +540,19 @@ bingo install golang.org/x/lint/golint | `go-revive-mod` | Run `'cd $(mod_root $FILE); revive [$ARGS] ./...'` for each staged .go file | `go-revive-repo-mod` | Run `'cd $(mod_root); revive [$ARGS] ./...'` for each module in the repo +##### Support for Repository-Level Config +As of time of writing, revive only auto-checks for configs in `${HOME}/revive.toml`, and doesn't check the local folder (ie. `${REPO_ROOT}/revive.toml`). + +To make revive more useful, these hooks add built-in support for a repository-level config file. + +###### Auto-Configured +These hooks are configured to auto-add `-config=revive.toml` when the file is present in the repository root. + +###### Triggerred When Modified +These hooks are configured to run when the repo-level `revive.toml` file is modified (and staged). + +**NOTE:** Although configured to run, the file-based `go-revive` hook will, by default, effectively _do nothing_ if there are no staged `.go` files to run against. + ##### Install (via [bingo](https://github.com/TekWizely/bingo)) ``` bingo install github.com/mgechev/revive diff --git a/go-revive-mod.sh b/go-revive-mod.sh index 83225f1..dd5ac0b 100755 --- a/go-revive-mod.sh +++ b/go-revive-mod.sh @@ -1,3 +1,6 @@ #!/usr/bin/env bash cmd=(revive) +if [ -f "revive.toml" ]; then + cmd+=( "-config=revive.toml" ) +fi . "$(dirname "${0}")/lib/cmd-mod.bash" diff --git a/go-revive-repo-mod.sh b/go-revive-repo-mod.sh index 19c856c..061cc42 100755 --- a/go-revive-repo-mod.sh +++ b/go-revive-repo-mod.sh @@ -1,3 +1,6 @@ #!/usr/bin/env bash cmd=(revive) +if [ -f "revive.toml" ]; then + cmd+=( "-config=revive.toml" ) +fi . "$(dirname "${0}")/lib/cmd-repo-mod.bash" diff --git a/go-revive.sh b/go-revive.sh index 9892d71..0dbe715 100755 --- a/go-revive.sh +++ b/go-revive.sh @@ -1,3 +1,7 @@ #!/usr/bin/env bash cmd=(revive) +if [ -f "revive.toml" ]; then + cmd+=( "-config=revive.toml" ) +fi +ignore_file_pattern_array=( "revive.toml" ) . "$(dirname "${0}")/lib/cmd-files.bash" diff --git a/lib/common.bash b/lib/common.bash index ad28ab6..1e50b4c 100644 --- a/lib/common.bash +++ b/lib/common.bash @@ -2,6 +2,7 @@ : "${use_dot_dot_dot:=1}" : "${error_on_output:=0}" +: "${ignore_file_pattern_array:=}" ## # prepare_repo_hook_cmd @@ -38,7 +39,7 @@ function verify_hook_cmd { # parse_file_hook_args # Creates global vars: # OPTIONS: List of options to passed to comand -# FILES : List of files to process +# FILES : List of files to process, filtered against ignore_file_pattern_array # function parse_file_hook_args { OPTIONS=() @@ -72,14 +73,17 @@ function parse_file_hook_args { # all_files+=("$@") - # Filter out vendor entries + # Filter out vendor entries and ignore_file_pattern_array # FILES=() - local file + local file pattern + ignore_file_pattern_array+=( "vendor/*" "*/vendor/*" "*/vendor" ) for file in "${all_files[@]}"; do - if [[ "${file}" == "vendor/"* || "${file}" == *"/vendor/"* || "${file}" == *"/vendor" ]]; then - continue - fi + for pattern in "${ignore_file_pattern_array[@]}"; do + if [[ "${file}" == ${pattern} ]] ; then # pattern => unquoted + continue 2 + fi + done FILES+=("${file}") done }