Skip to content

Commit bd69b81

Browse files
authored
feat(revive): Adds built-in support for repo-level revive.toml (#20)
Modifies revive hooks: * Auto-adds '-config=revive.toml' argument if file is present in repository root * Triggers hooks when repo-level revive.toml is modified * Updates README NOTE: Although the file-based go-revive hook is configured to trigger when revive.toml is modified, it will essentially do nothing if there are no .go files staged. We might consider adding some logic to run against ALL .go files in the repo, but I'm really not sure thats a good idea, and I probably didn't need to add a trigger to the file-level hook at all.
1 parent 4d78ac2 commit bd69b81

6 files changed

+44
-10
lines changed

.pre-commit-hooks.yaml

+10-3
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,14 @@
358358
# go-revive
359359
# * File-based
360360
# * Executes if any .go files modified
361+
# * Executes if revive.toml modified
362+
# * Adds arg '-config=revive.toml' if present
363+
# NOTE: Does nothing if ONLY revive.toml is modified (ie no .go files modified)
361364
# ==============================================================================
362365
- id: go-revive
363366
name: 'go-revive'
364367
entry: go-revive.sh
365-
types: [go]
368+
files: '(\.go$)|(\brevive\.toml$)'
366369
exclude: '(^|/)vendor/'
367370
language: 'script'
368371
description: "Run 'revive [$ARGS] $FILE' for each staged .go file"
@@ -375,11 +378,13 @@
375378
# * Targets first parent folder with a go.mod file
376379
# * Executes if any .go files modified
377380
# * Executes if go.mod modified
381+
# * Executes if revive.toml modified
382+
# * Adds arg '-config=revive.toml' if present
378383
# ==============================================================================
379384
- id: go-revive-mod
380385
name: 'go-revive-mod'
381386
entry: go-revive-mod.sh
382-
files: '(\.go$)|(\bgo\.mod$)'
387+
files: '(\.go$)|(\bgo\.mod$)|(\brevive\.toml$)'
383388
exclude: '(^|/)vendor/'
384389
language: 'script'
385390
description: "Run 'cd $(mod_root $FILE); revive [$ARGS] ./...' for each staged .go file"
@@ -393,11 +398,13 @@
393398
# * Targets ALL folders with a go.mod file
394399
# * Executes if any .go files modified
395400
# * Executes if go.mod modified
401+
# * Executes if revive.toml modified
402+
# * Adds arg '-config=revive.toml' if present
396403
# ==============================================================================
397404
- id: go-revive-repo-mod
398405
name: 'go-revive-repo-mod'
399406
entry: go-revive-repo-mod.sh
400-
files: '(\.go$)|(\bgo\.mod$)'
407+
files: '(\.go$)|(\bgo\.mod$)|(\brevive\.toml$)'
401408
exclude: '(^|/)vendor/'
402409
language: 'script'
403410
description: "Run 'cd $(mod_root); revive [$ARGS] ./...' for each module in the repo"

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -532,14 +532,27 @@ bingo install golang.org/x/lint/golint
532532
533533
-------------
534534
### go-revive
535-
~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint.
535+
\~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint.
536536
537537
| Hook ID | Description
538538
|-----------|------------
539539
| `go-revive` | Run `'revive [$ARGS] $FILE'` for each staged .go file
540540
| `go-revive-mod` | Run `'cd $(mod_root $FILE); revive [$ARGS] ./...'` for each staged .go file
541541
| `go-revive-repo-mod` | Run `'cd $(mod_root); revive [$ARGS] ./...'` for each module in the repo
542542
543+
##### Support for Repository-Level Config
544+
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`).
545+
546+
To make revive more useful, these hooks add built-in support for a repository-level config file.
547+
548+
###### Auto-Configured
549+
These hooks are configured to auto-add `-config=revive.toml` when the file is present in the repository root.
550+
551+
###### Triggerred When Modified
552+
These hooks are configured to run when the repo-level `revive.toml` file is modified (and staged).
553+
554+
**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.
555+
543556
##### Install (via [bingo](https://github.com/TekWizely/bingo))
544557
```
545558
bingo install github.com/mgechev/revive

go-revive-mod.sh

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#!/usr/bin/env bash
22
cmd=(revive)
3+
if [ -f "revive.toml" ]; then
4+
cmd+=( "-config=revive.toml" )
5+
fi
36
. "$(dirname "${0}")/lib/cmd-mod.bash"

go-revive-repo-mod.sh

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#!/usr/bin/env bash
22
cmd=(revive)
3+
if [ -f "revive.toml" ]; then
4+
cmd+=( "-config=revive.toml" )
5+
fi
36
. "$(dirname "${0}")/lib/cmd-repo-mod.bash"

go-revive.sh

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
#!/usr/bin/env bash
22
cmd=(revive)
3+
if [ -f "revive.toml" ]; then
4+
cmd+=( "-config=revive.toml" )
5+
fi
6+
ignore_file_pattern_array=( "revive.toml" )
37
. "$(dirname "${0}")/lib/cmd-files.bash"

lib/common.bash

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
: "${use_dot_dot_dot:=1}"
44
: "${error_on_output:=0}"
5+
: "${ignore_file_pattern_array:=}"
56

67
##
78
# prepare_repo_hook_cmd
@@ -38,7 +39,7 @@ function verify_hook_cmd {
3839
# parse_file_hook_args
3940
# Creates global vars:
4041
# OPTIONS: List of options to passed to comand
41-
# FILES : List of files to process
42+
# FILES : List of files to process, filtered against ignore_file_pattern_array
4243
#
4344
function parse_file_hook_args {
4445
OPTIONS=()
@@ -72,14 +73,17 @@ function parse_file_hook_args {
7273
#
7374
all_files+=("$@")
7475

75-
# Filter out vendor entries
76+
# Filter out vendor entries and ignore_file_pattern_array
7677
#
7778
FILES=()
78-
local file
79+
local file pattern
80+
ignore_file_pattern_array+=( "vendor/*" "*/vendor/*" "*/vendor" )
7981
for file in "${all_files[@]}"; do
80-
if [[ "${file}" == "vendor/"* || "${file}" == *"/vendor/"* || "${file}" == *"/vendor" ]]; then
81-
continue
82-
fi
82+
for pattern in "${ignore_file_pattern_array[@]}"; do
83+
if [[ "${file}" == ${pattern} ]] ; then # pattern => unquoted
84+
continue 2
85+
fi
86+
done
8387
FILES+=("${file}")
8488
done
8589
}

0 commit comments

Comments
 (0)