Skip to content

Commit

Permalink
Fixes args on -repo hooks; Fleshes out alias/names in docs; Removes '…
Browse files Browse the repository at this point in the history
…-' (#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 '--'
  • Loading branch information
TekWizely authored Aug 10, 2021
1 parent 0810ab1 commit 1fde58e
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 22 deletions.
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,51 @@ By default, hooks ONLY run when matching file types (usually `*.go`) are staged.
When configured to `"always_run"`, a hook is executed as if EVERY matching file were staged.
#### Aliases
Consider adding aliases to longer-named hooks for easier CLI usage.
#### Aliases / Names
pre-commit supports the ability to assign both an `alias` and a `name` to a configured hook:
| config | description
|--------|------------
| alias | (optional) allows the hook to be referenced using an additional id when using `pre-commit run <hookid>`
| name | (optional) override the name of the hook - shown during hook execution
These are beneficial for a couple of reasons:
* Creating short names for long-named hooks for easier CLI usage:
```
# ...
hooks:
- id: go-build-repo-mod
alias: build
```
_usage_
```
$ pre-commit run build
```
* Having variations of a given hook with different configurations:
```
# ...
hooks:
- id: go-fmt

# Add a second go-fmt hook with -w enabled
# Configure so it only runs when manually invoked
- id: go-fmt
args: [ -w ]
alias: go-fmtw-alias
name: go-fmtw-name
stages: [manual]
```
**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.
_usage: alias vs name_
```
$ pre-commit run --hook-stage manual go-fmtw-alias

go-fmtw-name................................Passed
```
#### Verbose Hook Output
When the `"verbose"` flag is enabled, all output generated by the hook will be displayed, even if there were no errors.
Expand Down
3 changes: 2 additions & 1 deletion go-fmt-repo.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
error_on_output=1
cmd=(gofmt -l -d .)
cmd=(gofmt -l -d)
target=(.)
. "$(dirname "${0}")/lib/cmd-repo.bash"
3 changes: 2 additions & 1 deletion go-fumpt-repo.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
error_on_output=1
cmd=(gofumpt -l -d .)
cmd=(gofumpt -l -d)
target=(.)
. "$(dirname "${0}")/lib/cmd-repo.bash"
3 changes: 2 additions & 1 deletion go-imports-repo.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
error_on_output=1
cmd=(goimports -l -d .)
cmd=(goimports -l -d)
target=(.)
. "$(dirname "${0}")/lib/cmd-repo.bash"
3 changes: 2 additions & 1 deletion go-returns-repo.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
error_on_output=1
cmd=(goreturns -l -d .)
cmd=(goreturns -l -d)
target=(.)
. "$(dirname "${0}")/lib/cmd-repo.bash"
5 changes: 5 additions & 0 deletions lib/cmd-repo.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

prepare_repo_hook_cmd "$@"

# Add target after options
#
if [[ ${#target[@]} -gt 0 ]]; then
OPTIONS+=("${target[@]}")
fi
if [ "${error_on_output:-}" -eq 1 ]; then
output=$("${cmd[@]}" "${OPTIONS[@]}" 2>&1)
if [ -n "${output}" ]; then
Expand Down
27 changes: 11 additions & 16 deletions lib/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function parse_file_hook_args {
OPTIONS=()
# If arg doesn't pass [ -f ] check, then it is assumed to be an option
#
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ] && [ ! -f "$1" ]; do
while [ $# -gt 0 ] && [ "$1" != "--" ] && [ ! -f "$1" ]; do
OPTIONS+=("$1")
shift
done
Expand All @@ -53,29 +53,24 @@ function parse_file_hook_args {
all_files=()
# Assume start of file list (may still be options)
#
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
while [ $# -gt 0 ] && [ "$1" != "--" ]; do
all_files+=("$1")
shift
done

# If '--' next, then files = options
#
if [ $# -gt 0 ]; then
if [ "$1" == "-" ] || [ "$1" == "--" ]; then
shift
# Append to previous options
#
OPTIONS=("${OPTIONS[@]}" "${all_files[@]}")
all_files=()
fi
if [ "$1" == "--" ]; then
shift
# Append to previous options
#
OPTIONS+=("${all_files[@]}")
all_files=()
fi

# Any remaining arguments are assumed to be files
#
while [ $# -gt 0 ]; do
all_files+=("$1")
shift
done
all_files+=("$@")

# Filter out vendor entries
#
Expand All @@ -91,11 +86,11 @@ function parse_file_hook_args {

##
# parse_repo_hook_args
# Build options list, ignoring '-', '--', and anything after
# Build options list, ignoring '--', and anything after
#
function parse_repo_hook_args {
OPTIONS=()
while [ $# -gt 0 ] && [ "$1" != "-" ] && [ "$1" != "--" ]; do
while [ $# -gt 0 ] && [ "$1" != "--" ]; do
OPTIONS+=("$1")
shift
done
Expand Down

0 comments on commit 1fde58e

Please sign in to comment.