Skip to content

Commit f69b840

Browse files
committed
Use --hook:path-prefix arg to make golangci-lint --path-prefix optional
1 parent d51e0d6 commit f69b840

6 files changed

+13
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
112112
# - Can be configured to replace MOST other hooks
113113
# - Supports repo config file for configuration
114114
# - https://github.com/golangci/golangci-lint
115+
# - Use arg `--hook:path-prefix` to indicate that the repository root
116+
# directory should be passed to the tool (for `*-mod` hooks)
115117
#
116118
- id: golangci-lint
117119
- id: golangci-lint-mod

golangci-lint-mod.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env bash
22
cmd=(golangci-lint run)
3-
cmd_pwd_arg="--path-prefix"
3+
cmd_cwd_arg="--path-prefix"
44
. "$(dirname "${0}")/lib/cmd-mod.bash"

golangci-lint-repo-mod.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env bash
22
cmd=(golangci-lint run)
3-
cmd_pwd_arg="--path-prefix"
3+
cmd_cwd_arg="--path-prefix"
44
. "$(dirname "${0}")/lib/cmd-repo-mod.bash"

lib/cmd-mod.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ error_code=0
1414
#
1515
for sub in $(find_module_roots "${FILES[@]}" | sort -u); do
1616
pushd "${sub}" > /dev/null || exit 1
17-
if [ "${cmd_pwd_arg:-}" ]; then
18-
OPTIONS+=("${cmd_pwd_arg}=${sub}")
17+
if [ "${use_path_prefix:-}" -eq 1 ] && [ "${cmd_cwd_arg:-}" ]; then
18+
OPTIONS+=("${cmd_cwd_arg}=${sub}")
1919
fi
2020
if [ "${error_on_output:-}" -eq 1 ]; then
2121
output=$(/usr/bin/env "${ENV_VARS[@]}" "${cmd[@]}" "${OPTIONS[@]}" 2>&1)

lib/cmd-repo-mod.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ error_code=0
1414
#
1515
for sub in $(find . -name go.mod -not -path '*/vendor/*' -exec dirname "{}" ';' | sort -u); do
1616
pushd "${sub}" > /dev/null || exit 1
17-
if [ "${cmd_pwd_arg:-}" ]; then
18-
OPTIONS+=("${cmd_pwd_arg}=${sub}")
17+
if [ "${use_path_prefix:-}" -eq 1 ] && [ "${cmd_cwd_arg:-}" ]; then
18+
OPTIONS+=("${cmd_cwd_arg}=${sub}")
1919
fi
2020
if [ "${error_on_output:-}" -eq 1 ]; then
2121
output=$(/usr/bin/env "${ENV_VARS[@]}" "${cmd[@]}" "${OPTIONS[@]}" 2>&1)

lib/common.bash

Lines changed: 5 additions & 0 deletions
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+
: "${use_path_prefix:=0}"
56
: "${ignore_file_pattern_array:=}"
67

78
##
@@ -66,6 +67,10 @@ function parse_file_hook_args {
6667
fi
6768
shift
6869
;;
70+
--hook:path-prefix)
71+
use_path_prefix=1
72+
shift
73+
;;
6974
--hook:*)
7075
printf "ERROR: Unknown hook option: '%s'\n" "${1}" >&2
7176
exit 1

0 commit comments

Comments
 (0)