How to avoid git commit if one sub command in parallel a command fails #961
-
⚡ SummaryI want to run multiple linters in parallel in pre-commit. But I want to fail the pre-commit if one of the liners fails and git commit won't happen in this case. Consider the following config file: pre-commit:
parallel: true
commands:
format-non-md:
glob: "*.{js,json,yml,yaml,css,html}"
run: yarn prettier --ignore-unknown --write {staged_files}
yarn-filecheck:
glob: "*.{svg,png,jpeg,jpg,gif}"
run: yarn filecheck {staged_files}
markdown:
glob: "*.md"
run: |
node scripts/front-matter_linter.js --fix true {staged_files}
yarn markdownlint-cli2 --fix {staged_files}
yarn prettier --write {staged_files}
yarn node scripts/update-moved-file-links.js --check
fail_text: "Please fix the lining errors before committing." In this case, if The equivalent shell script of what I want is the following: # 'set -e' tells the shell to exit if any of the foreground commands fails
set -eu
pids=()
node scripts/front-matter_linter.js --fix true {staged_files}
pids+=($!)
yarn markdownlint-cli2 --fix {staged_files}
pids+=($!)
yarn prettier --write {staged_files}
pids+=($!)
yarn node scripts/update-moved-file-links.js --check
pids+=($!)
# Wait for each specific process to terminate.
# Instead of this loop, a single call to 'wait' would wait for all the jobs
# to terminate, but it would not give us their exit status.
for pid in "${pids[@]}"; do
# Waiting on a specific PID makes the wait command return with the exit
# status of that process. Because of the 'set -e' setting, any exit status
# other than zero causes the current shell to terminate with that exit
# status as well.
wait "$pid"
done Is there an option or setting combination to achieve the above? Or do I have to handle this in a shell script and run the script in the command? I've tried the following, but I am not sure it is full proof: markdown:
glob: "*.md"
run: |
node scripts/front-matter_linter.js --fix true {staged_files}; status1=$?
yarn markdownlint-cli2 --fix {staged_files}; status2=$?
yarn prettier --write {staged_files}; status3=$?
yarn node scripts/update-moved-file-links.js --check; status4=$?
exit $((status1 + status2 + status3 + status4))
stage_fixed: true
fail_text: "Please fix the errors before committing." Is the above approach correct? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
@OnkarRuikar , I think you can use pre-commit:
parallel: true
jobs:
- name: format-non-md
glob: "*.{js,json,yml,yaml,css,html}"
run: yarn prettier --ignore-unknown --write {staged_files}
- name: yarn-filecheck
glob: "*.{svg,png,jpeg,jpg,gif}"
run: yarn filecheck {staged_files}
- name: markdown
glob: "*.md"
group:
piped: true
jobs:
- run: node scripts/front-matter_linter.js --fix true {staged_files}
- run: yarn markdownlint-cli2 --fix {staged_files}
- run: yarn prettier --write {staged_files}
- run: yarn node scripts/update-moved-file-links.js --check In this case if one job fails the whole group will fail and the pre-commit hook will also fail |
Beta Was this translation helpful? Give feedback.
-
@mrexox Thanks for the info!
Using pre-commit:
parallel: true
jobs:
- name: format-non-md
glob: "*.{js,json,yml,yaml,css,html}"
run: yarn prettier --ignore-unknown --write {staged_files}
- name: yarn-filecheck
glob: "*.{svg,png,jpeg,jpg,gif}"
run: yarn filecheck {staged_files}
- name: markdown
glob: "*.md"
group:
parallel: true <---------- switched from piped to parallel
jobs:
- run: node scripts/front-matter_linter.js --fix true {staged_files}
- run: yarn markdownlint-cli2 --fix {staged_files}
- run: yarn prettier --write {staged_files}
- run: yarn node scripts/update-moved-file-links.js --check Logs:
|
Beta Was this translation helpful? Give feedback.
@OnkarRuikar , I think you can use
jobs
for this and split your multi-commands into separate jobs: