在预提交中退出1不会中止git commit吗? [英] exit 1 in pre-commit doesn't abort git commit?
问题描述
如果满足特定条件,我将中止git commit.
I'm aborting git commit if a certain condition is met.
检查我的提交是否具有"import pdb"在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
代码运行正常,我可以看到日志"... has import pdb"
但是下一行exit 1
不会中止git commit.
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.
它曾经可以正常工作,但是在某些时候停止了工作. 我在git 1.9.1上
It used to work fine, but it stopped working at some point. I'm on git 1.9.1
输出
$ 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'
推荐答案
代码的第一部分内容类似
The first part of the code reads like
something | while ...; do ... exit 1; done
由于管道必须包含两个进程,因此外壳程序必须派生一个子外壳程序来执行您的while
循环.结果,exit 1
从状态为1的 subshell 存在,并返回到您的父shell,该父shell会忽略返回代码.
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.
您要在done
关键字后添加|| exit $?
,如下所示:
You want to add a || exit $?
after the done
keyword, like this:
something | while ...; do ... exit 1; done || exit $?
请注意,这实际上不是Git问题,而是shell问题.
Note that this is not actually a Git question, but a shell issue.
这篇关于在预提交中退出1不会中止git commit吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!