问题
I'm aborting git commit if a certain condition is met.
check if my commit has 'import pdb' in emacs/git?
#!/bin/sh
git diff --cached --no-renames --name-status --diff-filter=AM |
while read st file; do
case "$file" in
*.py)
if git show ":$file" |
grep -E "^[^#]*\bimport[[:space:]]+pdb\b"; then
echo "$file: has import pdb"
exit 1
fi;;
esac
done
git diff --cached --name-status --diff-filter=ACM | while read st file; do
if grep "#[[:space:]]*@test" "$file"; then
echo "$file: has @test"
exit 1
fi
done
The code runs fine and I can see the log "... has import pdb"
But the next line exit 1
won't abort the git commit.
It used to work fine, but it stopped working at some point. I'm on git 1.9.1
output
$ GIT_TRACE=2 git commit
trace: built-in: git 'commit'
trace: run_command: '.git/hooks/pre-commit'
trace: built-in: git 'diff' '--cached' '--no-renames' '--name-status' '--diff-filter=AM'
trace: built-in: git 'show' ':momsite/apps/custom_push_notifications/utils.py'
import pdb; pdb.set_trace()
momsite/apps/custom_push_notifications/utils.py: has import pdb
trace: built-in: git 'diff' '--cached' '--name-status' '--diff-filter=ACM'
trace: run_command: 'emacs' '/home/eugenekim/Documents/zibann/.git/COMMIT_EDITMSG'
trace: exec: 'emacs' '/home/eugenekim/Documents/zibann/.git/COMMIT_EDITMSG'
回答1:
The first part of the code reads like
something | while ...; do ... exit 1; done
Since a pipe necessarily involves two processes, the shell has to fork a subshell to execute your while
loop. As a result, exit 1
exists from the subshell with status 1, and returns to your parent shell, which ignores the return code.
You want to add a || exit $?
after the done
keyword, like this:
something | while ...; do ... exit 1; done || exit $?
Note that this is not actually a Git question, but a shell issue.
来源:https://stackoverflow.com/questions/29969093/exit-1-in-pre-commit-doesnt-abort-git-commit