跳过 Git 提交挂钩

发布于 2024-12-02 02:11:18 字数 117 浏览 1 评论 0 原文

我正在查看一个 Git 挂钩,它在 Python 代码中查找打印语句。如果找到 print 语句,它会阻止 Git 提交。

我想覆盖这个钩子,有人告诉我有一个命令可以这样做。我一直没能找到它。有什么想法吗?

I'm looking at a Git hook which looks for print statements in Python code. If a print statement is found, it prevents the Git commit.

I want to override this hook and I was told that there is a command to do so. I haven't been able to find it. Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

何必那么矫情 2024-12-09 02:11:18

也许(来自 git commit 手册页):

git commit --no-verify -m "commit message"
           ^^^^^^^^^^^
-n  
--no-verify

此选项绕过预提交和提交消息挂钩。另请参阅 githooks(5)

正如 Blaise 所评论的,-n 对于某些命令可以具有不同的角色。
例如, git push -n 实际上是一个干 -运行推送。
只有 git push --no-verify 会跳过该钩子。


注意:Git 2.14.x/2.15 改进了 --no-verify 行为:

请参阅 提交 680ee55(2017 年 8 月 14 日)作者:凯文·威尔福德 (``)
(由 Junio C Hamano -- gitster -- 合并于 提交c3e034f,2017 年 8 月 23 日)

commit:如果没有pre-commit钩子,则跳过丢弃索引

git commit”用于丢弃索引并从文件系统重新读取
以防万一 pre-commit 钩子在中间更新了它;这
当我们知道我们不运行 pre-commit 钩子时,就已经被优化了。


Davi Lima 指出在评论中 gitcherry-pick 确实 支持 --no-verify。
因此,如果cherry-pick触发了预提交挂钩,您可能会像这篇博文,必须以某种方式评论/禁用该挂钩才能让您的 gitcherry-pick 继续进行。

在解决合并冲突后,如果发生 git rebase --continue,则需要执行相同的过程。


在 Git 2.36(2022 年第 2 季度)中,run_commit_hook() 的调用者可以了解它“成功”是因为挂钩成功还是因为没有任何挂钩。

请参阅 提交 a8cc594 (已修复 提交 4369e3a1), 提交 9f6e63b(2022 年 3 月 7 日),作者:Ævar Arnfjörð Bjarmason (avar)
(由 Junio C Hamano -- gitster -- 合并于 提交7431379,2022 年 3 月 16 日)

hooks:修复一个晦涩的< a href="https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use" rel="noreferrer">TOCTOU“我们刚刚运行了一个钩子吗?”种族

签字人:Ævar Arnfjörð Bjarmason

修复检查时间到时间-在 (TOCTOU) 竞赛href="https://github.com/git/git/commit/680ee550d72150f27cdb3235462eee355a20038b" rel="noreferrer">680ee55 ("commit: 如果没有 pre 则跳过丢弃索引-commit 钩子”,2017-08-14,Git v2.15.0-rc0 -- 合并第 3 批)。

如果我们运行“pre-commit”挂钩并修改了索引,则可能会发生这种模糊的竞争条件,但 hook_exists() 稍后返回 false(例如,因为钩子本身消失了,目录变得不可读等)。
那么我们就不会在应该调用的时候调用 discard_cache()

竞争条件本身可能并不重要,用户在实践中不太可能遇到它。
680ee55 已讨论,但尚未修复。

我们还可以为“push-to-checkout”挂钩更改此设置。
现在,我们将始终尝试推送结帐,而不是检查挂钩是否存在以及执行推送结帐或推送部署。
如果挂钩不存在,我们将依靠推送来部署。
与之前相同的行为,但没有 TOCTOU 竞赛。
请参阅 0855331 ("receive-pack: 支持推送-checkout 钩子”,2014-12-01,Git v2.4.0-rc0 -- 合并)用于引入先前的行为。

这使得 hook_exists() 在两个重要的地方使用。
“reference-transaction”检查 refs.c< /a>,参见6754159(“refs:实现参考事务挂钩”,2020-06 -19, Git v2.28.0-rc0 -- 合并 batch #7) 和“prepare-commit-msg”挂钩,请参阅 66618a5 ("sequencer: 运行 'prepare-commit-msg' 钩子”,2018-01-24,Git v2.17.0-rc0 -- 合并第 2 批)。

在这两种情况下,我们通过不为钩子准备数据来节省 CPU 时间,如果没有钩子,我们将不执行任何操作。
因此,在这些情况下使用这个“invoked_hook”模式没有意义。

reference-transaction”和“prepare-commit-msg”钩子也不够活泼。
在这些情况下,如果我们添加了新的钩子,我们将跳过钩子运行,而在此处修复的 TOCTOU 竞赛中,我们错误地跳过了所需的后钩子逻辑。

Maybe (from git commit man page):

git commit --no-verify -m "commit message"
           ^^^^^^^^^^^
-n  
--no-verify

This option bypasses the pre-commit and commit-msg hooks. See also githooks(5).

As commented by Blaise, -n can have a different role for certain commands.
For instance, git push -n is actually a dry-run push.
Only git push --no-verify would skip the hook.


Note: Git 2.14.x/2.15 improves the --no-verify behavior:

See commit 680ee55 (14 Aug 2017) by Kevin Willford (``).
(Merged by Junio C Hamano -- gitster -- in commit c3e034f, 23 Aug 2017)

commit: skip discarding the index if there is no pre-commit hook

"git commit" used to discard the index and re-read from the filesystem
just in case the pre-commit hook has updated it in the middle; this
has been optimized out when we know we do not run the pre-commit hook.


Davi Lima points out in the comments the git cherry-pick does not support --no-verify.
So if a cherry-pick triggers a pre-commit hook, you might, as in this blog post, have to comment/disable somehow that hook in order for your git cherry-pick to proceed.

The same process would be necessary in case of a git rebase --continue, after a merge conflict resolution.


With Git 2.36 (Q2 2022), the callers of run_commit_hook() to learn if it got "success" because the hook succeeded or because there wasn't any hook.

See commit a8cc594 (fixed with commit 4369e3a1), commit 9f6e63b (07 Mar 2022) by Ævar Arnfjörð Bjarmason (avar).
(Merged by Junio C Hamano -- gitster -- in commit 7431379, 16 Mar 2022)

hooks: fix an obscure TOCTOU "did we just run a hook?" race

Signed-off-by: Ævar Arnfjörð Bjarmason

Fix a Time-of-check to time-of-use (TOCTOU) race in code added in 680ee55 ("commit: skip discarding the index if there is no pre-commit hook", 2017-08-14, Git v2.15.0-rc0 -- merge listed in batch #3).

This obscure race condition can occur if we e.g. ran the "pre-commit" hook and it modified the index, but hook_exists() returns false later on (e.g., because the hook itself went away, the directory became unreadable, etc.).
Then we won't call discard_cache() when we should have.

The race condition itself probably doesn't matter, and users would have been unlikely to run into it in practice.
This problem has been noted on-list when 680ee55 was discussed, but had not been fixed.

Let's also change this for the push-to-checkout hook.
Now instead of checking if the hook exists and either doing a push to checkout or a push to deploy we'll always attempt a push to checkout.
If the hook doesn't exist we'll fall back on push to deploy.
The same behavior as before, without the TOCTOU race.
See 0855331 ("receive-pack: support push-to-checkout hook", 2014-12-01, Git v2.4.0-rc0 -- merge) for the introduction of the previous behavior.

This leaves uses of hook_exists() in two places that matter.
The "reference-transaction" check in refs.c, see 6754159 ("refs: implement reference transaction hook", 2020-06-19, Git v2.28.0-rc0 -- merge listed in batch #7), and the "prepare-commit-msg" hook, see 66618a5 ("sequencer: run 'prepare-commit-msg' hook", 2018-01-24, Git v2.17.0-rc0 -- merge listed in batch #2).

In both of those cases we're saving ourselves CPU time by not preparing data for the hook that we'll then do nothing with if we don't have the hook.
So using this "invoked_hook" pattern doesn't make sense in those cases.

The "reference-transaction" and "prepare-commit-msg" hook also aren't racy.
In those cases we'll skip the hook runs if we race with a new hook being added, whereas in the TOCTOU races being fixed here we were incorrectly skipping the required post-hook logic.

素染倾城色 2024-12-09 02:11:18

使用 commentno verify 没有任何进一步的问题:

git commit -m "Some comments" --no-verify

With both comment and no verify without any further issue:

git commit -m "Some comments" --no-verify
心碎无痕… 2024-12-09 02:11:18

--no-verify 有效,但就我而言,我不想一直将参数放在终端上。所以我选择了更具侵略性的东西。

如果你想全局禁用 git hooks,你可以尝试运行这个:

git config --global core.hooksPath /dev/null

但是,如果你想保持原来的状态,只需在终端中运行以下命令:

git config --global --unset core.hooksPath

如果你不想要它要成为全局的,只需删除参数: --global

我用 Git 2.16.3 测试了它。

--no-verify works, but in my case, I didn't want to put the parameter all the time on the terminal. So I opted for something a little more aggressive.

If you want to disable git hooks globally, you could try running this:

git config --global core.hooksPath /dev/null

But, if you want to leave it as it was before, just run the following command in your terminal:

git config --global --unset core.hooksPath

If you do not want it to be global, just remove the argument: --global

I tested it with Git 2.16.3.

一袭白衣梦中忆 2024-12-09 02:11:18

来自 man githooks

预提交
该钩子由 git commit 调用,可以使用 --no-verify 选项绕过。它
不带任何参数,并在获取建议的提交日志消息之前调用
并做出承诺。从此脚本中以非零状态退出会导致 git
承诺中止。

From man githooks:

pre-commit
This hook is invoked by git commit, and can be bypassed with --no-verify option. It
takes no parameter, and is invoked before obtaining the proposed commit log message
and making a commit. Exiting with non-zero status from this script causes the git
commit to abort.

汐鸠 2024-12-09 02:11:18

例如,-n--no-verify 对于“git merge --continue”之后的提交不起作用。

这是另一个更粗略的想法。

  1. 只需使用符号“#”文件中的注释行 .git/hooks/pre-commit 即可。
  2. 运行单个或多个命令
  3. 取消注释
  4. 利润。

-n or --no-verify does not work for a commit after 'git merge --continue' for example.

So here is another rougher idea.

  1. Just comment lines in file .git/hooks/pre-commit with symbol '#'.
  2. Run single or many commands
  3. Uncomment
  4. Profit.
独孤求败 2024-12-09 02:11:18

由于某种原因,--no-verify 对于这个特定的钩子不起作用:prepare-commit-msg

如果您也遇到了这个问题,请尝试:

SKIP=prepare-commit-msg git commit

For some reason, --no-verify does not work for me with this particular hook: prepare-commit-msg

If you too are running into this issue, try:

SKIP=prepare-commit-msg git commit
○愚か者の日 2024-12-09 02:11:18

SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^(.>).$/- 签署人:\1/p')
grep -qs "^$SOB" "$1" ||回声“$SOB”>> “1美元”

SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^(.>).$/- signed-off-by: \1/p')
grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文