在预提交中退出1';放弃git提交?

在预提交中退出1';放弃git提交?,git,githooks,Git,Githooks,如果满足某个条件,我将中止git提交 代码运行良好,我可以看到日志“…已导入pdb” 但是下一行退出1不会中止git提交 它过去工作得很好,但有时会停止工作。 我在Git1.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'

如果满足某个条件,我将中止git提交

代码运行良好,我可以看到日志“…已导入pdb” 但是下一行退出1不会中止git提交

它过去工作得很好,但有时会停止工作。 我在Git1.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'

代码的第一部分如下所示

something | while ...; do ... exit 1; done
由于管道必然涉及两个进程,shell必须派生一个子shell来执行
while
循环。因此,
exit 1
从状态为1的子shell中存在,并返回到父shell,而父shell将忽略返回代码

您想在
done
关键字后添加
| | exit$?
,如下所示:

something | while ...; do ... exit 1; done || exit $?

请注意,这实际上不是一个Git问题,而是一个shell问题。

请将您的问题独立起来,或者使用您链接到的问题中的注释来改进答案。另外,您可以发布您的确切脚本和您运行的确切命令吗?否则,我们无法调试您的问题…@MatthieuMoy right。。我添加了代码。你能添加你运行的确切命令吗?此外,您还可以尝试运行
GIT\u TRACE=2 GIT commit
并查看输出。我添加了
GIT\u TRACE=2 GIT commit
的输出。正如您所看到的,pre-commit钩子运行良好,但不管退出1,它都会继续运行,您也更改了代码,现在我明白了为什么它不起作用。
something | while ...; do ... exit 1; done || exit $?