Skip to content

Commit 1fde58e

Browse files
authored
Fixes args on -repo hooks; Fleshes out alias/names in docs; Removes '-' (#16)
* Fixes formatter `-repo` hooks to allow hook args * Fleshes out alias/names section in README for - Short alias for easier CLI usage - Using alias/name for hook variations * Removes '-' as an alias for '--' BREAKING CHANGE: No longer treats '-' as an alias for '--'
1 parent 0810ab1 commit 1fde58e

File tree

7 files changed

+69
-22
lines changed

7 files changed

+69
-22
lines changed

README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,51 @@ By default, hooks ONLY run when matching file types (usually `*.go`) are staged.
196196
197197
When configured to `"always_run"`, a hook is executed as if EVERY matching file were staged.
198198
199-
#### Aliases
200-
Consider adding aliases to longer-named hooks for easier CLI usage.
199+
#### Aliases / Names
200+
201+
pre-commit supports the ability to assign both an `alias` and a `name` to a configured hook:
202+
203+
| config | description
204+
|--------|------------
205+
| alias | (optional) allows the hook to be referenced using an additional id when using `pre-commit run <hookid>`
206+
| name | (optional) override the name of the hook - shown during hook execution
207+
208+
These are beneficial for a couple of reasons:
209+
210+
* Creating short names for long-named hooks for easier CLI usage:
211+
```
212+
# ...
213+
hooks:
214+
- id: go-build-repo-mod
215+
alias: build
216+
```
217+
_usage_
218+
```
219+
$ pre-commit run build
220+
```
221+
222+
* Having variations of a given hook with different configurations:
223+
```
224+
# ...
225+
hooks:
226+
- id: go-fmt
227+
228+
# Add a second go-fmt hook with -w enabled
229+
# Configure so it only runs when manually invoked
230+
- id: go-fmt
231+
args: [ -w ]
232+
alias: go-fmtw-alias
233+
name: go-fmtw-name
234+
stages: [manual]
235+
```
236+
**NOTE:** When creating variations, take note that the `alias` is used to execute the hook, but the the `name` is used in the hook report.
237+
238+
_usage: alias vs name_
239+
```
240+
$ pre-commit run --hook-stage manual go-fmtw-alias
241+
242+
go-fmtw-name................................Passed
243+
```
201244
202245
#### Verbose Hook Output
203246
When the `"verbose"` flag is enabled, all output generated by the hook will be displayed, even if there were no errors.

go-fmt-repo.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
22
error_on_output=1
3-
cmd=(gofmt -l -d .)
3+
cmd=(gofmt -l -d)
4+
target=(.)
45
. "$(dirname "${0}")/lib/cmd-repo.bash"

go-fumpt-repo.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
22
error_on_output=1
3-
cmd=(gofumpt -l -d .)
3+
cmd=(gofumpt -l -d)
4+
target=(.)
45
. "$(dirname "${0}")/lib/cmd-repo.bash"

go-imports-repo.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
22
error_on_output=1
3-
cmd=(goimports -l -d .)
3+
cmd=(goimports -l -d)
4+
target=(.)
45
. "$(dirname "${0}")/lib/cmd-repo.bash"

go-returns-repo.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
22
error_on_output=1
3-
cmd=(goreturns -l -d .)
3+
cmd=(goreturns -l -d)
4+
target=(.)
45
. "$(dirname "${0}")/lib/cmd-repo.bash"

lib/cmd-repo.bash

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
prepare_repo_hook_cmd "$@"
66

7+
# Add target after options
8+
#
9+
if [[ ${#target[@]} -gt 0 ]]; then
10+
OPTIONS+=("${target[@]}")
11+
fi
712
if [ "${error_on_output:-}" -eq 1 ]; then
813
output=$("${cmd[@]}" "${OPTIONS[@]}" 2>&1)
914
if [ -n "${output}" ]; then

lib/common.bash

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function parse_file_hook_args {
4444
OPTIONS=()
4545
# If arg doesn't pass [ -f ] check, then it is assumed to be an option
4646
#
47-
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ] && [ ! -f "$1" ]; do
47+
while [ $# -gt 0 ] && [ "$1" != "--" ] && [ ! -f "$1" ]; do
4848
OPTIONS+=("$1")
4949
shift
5050
done
@@ -53,29 +53,24 @@ function parse_file_hook_args {
5353
all_files=()
5454
# Assume start of file list (may still be options)
5555
#
56-
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
56+
while [ $# -gt 0 ] && [ "$1" != "--" ]; do
5757
all_files+=("$1")
5858
shift
5959
done
6060

6161
# If '--' next, then files = options
6262
#
63-
if [ $# -gt 0 ]; then
64-
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
65-
shift
66-
# Append to previous options
67-
#
68-
OPTIONS=("${OPTIONS[@]}" "${all_files[@]}")
69-
all_files=()
70-
fi
63+
if [ "$1" == "--" ]; then
64+
shift
65+
# Append to previous options
66+
#
67+
OPTIONS+=("${all_files[@]}")
68+
all_files=()
7169
fi
7270

7371
# Any remaining arguments are assumed to be files
7472
#
75-
while [ $# -gt 0 ]; do
76-
all_files+=("$1")
77-
shift
78-
done
73+
all_files+=("$@")
7974

8075
# Filter out vendor entries
8176
#
@@ -91,11 +86,11 @@ function parse_file_hook_args {
9186

9287
##
9388
# parse_repo_hook_args
94-
# Build options list, ignoring '-', '--', and anything after
89+
# Build options list, ignoring '--', and anything after
9590
#
9691
function parse_repo_hook_args {
9792
OPTIONS=()
98-
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
93+
while [ $# -gt 0 ] && [ "$1" != "--" ]; do
9994
OPTIONS+=("$1")
10095
shift
10196
done

0 commit comments

Comments
 (0)