Skip to content

Commit a5bd84e

Browse files
authored
fix: Regression from (at least) v1.99.0 which incorrectly handle pre-commit run -a, that causes multiply hooks runs. terraform_trivy from its introduction could always be Passed for pre-commit run -a (#909)
### Description of your changes Probably introduced in #875 or even earlier. There were issue that regex in `.pre-commit-hooks.yaml` specified for Python `re` library, but we also used it with `grep -e`. `grep -e` working slightly different than we expect, so I switched to `grep -E` which have less differences with Python `re`. (step 3 below) Then I found that there is no [required `--exit-code=1`](https://trivy.dev/latest/docs/configuration/others/#exit-code) in `run_hook_on_whole_repo` from hook introduction 2 years ago #606. Fixed it too. From above, I assume that `pre-commit run -a` for Fix #908 ### How can we test changes 1. Clone https://github.com/pre-commit-terraform/GH-908-reproduce. 2. Run `pre-commit run -a` - you'll see 2 occurrences of same error. 3. (Optional) Change `.pre-commit-config.yaml` to ```yaml repos: - repo: https://github.com/antonbabenko/pre-commit-terraform rev: 48525b2 hooks: - id: terraform_trivy args: # https://trivy.dev/latest/docs/configuration/others/#exit-code # It wasn't set, when it should be set by default in hook. Another issue. - --args=--exit-code=1 ``` and run `pre-commit run -a` 4. Change `.pre-commit-config.yaml` to ```yaml repos: - repo: https://github.com/antonbabenko/pre-commit-terraform rev: bafa663 hooks: - id: terraform_trivy ``` and run `pre-commit run -a`
1 parent 6c84595 commit a5bd84e

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

hooks/_common.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ function common::is_hook_run_on_whole_repo {
176176
local all_files_that_can_be_checked
177177

178178
if [ -z "$excluded_files" ]; then
179-
all_files_that_can_be_checked=$(git ls-files | sort | grep -e "$included_files" | tr '\n' ' ')
179+
all_files_that_can_be_checked=$(git ls-files | sort | grep -E -- "$included_files" | tr '\n' ' ')
180180
else
181-
all_files_that_can_be_checked=$(git ls-files | sort | grep -e "$included_files" | grep -v -e "$excluded_files" | tr '\n' ' ')
181+
all_files_that_can_be_checked=$(git ls-files | sort | grep -E -- "$included_files" | grep -v -E -- "$excluded_files" | tr '\n' ' ')
182182
fi
183183

184184
if [ "$files_to_check" == "$all_files_that_can_be_checked" ]; then

hooks/terraform_docs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ function terraform_docs {
187187
config_file_no_color="$config_file$(date +%s).yml"
188188

189189
if [ "$PRE_COMMIT_COLOR" = "never" ] &&
190-
[[ $(grep -e '^formatter:' "$config_file") == *"pretty"* ]] &&
190+
[[ $(grep -E '^formatter:' "$config_file") == *"pretty"* ]] &&
191191
[[ $(grep ' color: ' "$config_file") != *"false"* ]]; then
192192

193193
cp "$config_file" "$config_file_no_color"

hooks/terraform_trivy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function run_hook_on_whole_repo {
6565
local -a -r args=("$@")
6666

6767
# pass the arguments to hook
68-
trivy conf "$(pwd)" "${args[@]}"
68+
trivy conf "$(pwd)" --exit-code=1 "${args[@]}"
6969

7070
# return exit code to common::per_dir_hook
7171
local exit_code=$?

0 commit comments

Comments
 (0)