如何避免禁用预提交钩子?

huangapple go评论55阅读模式
英文:

How to avoid disabling a pre-commit hook?

问题

抱歉打扰,我确定这是一个重复的问题。我还没有找到解决我的用例的方法。我创建了一个预提交挂钩,用于阻止提交新开发的代码存在 Sonar 问题。因为这是一个预提交挂钩,开发人员可以使用 --no-verify 选项禁用此挂钩,我想避免这种情况。

注意:在这种用例中,我不能使用预接收挂钩,因为新开发的代码将位于开发人员的机器上,因此挂钩应作为客户端挂钩触发。

有没有办法在提交之前强制执行此挂钩(超出开发人员的控制)而不跳过它呢?我找到了一个类似的 Stack Overflow 问题,答案建议使用预接收挂钩,它适用于 push 事件而不是 commit 事件。现在我只想在没有开发人员控制的情况下运行预提交挂钩。如果这不可能,有没有其他方法?这样我就可以在推送代码之前确认挂钩的执行。

我知道 --no-verify 选项是 Git 的内部命令,我们很少有控制它的机会。我想知道预接收挂钩是否可以帮助我确保在推送事件之前执行预提交挂钩。谢谢提前。

英文:

I am sure it is a repeated question and apologies to bother. I didn't find solution to my usecase yet. I created a pre-commit hook that prevents commit when newly developed code has sonar issues. Since it is a pre-commit hook there might be a chance that developer can disable this hook using --no-verify option and I want to avoid it.

NOTE: I can't use pre-receive hook in this usecase because newly developed code will be on developer's machine and hence the hook should be triggered as client-side hook.

Is there anyway to enforce this hook to be executed before commit (out of developer control) without skipping it?
I found a similar stack overflow question where the answers suggests to use pre-receive hook which works on push event not on commit event. Now I just want to run pre-commit hook without developer control. If this is not possible, Is there any alternative? so that I can confirm the hook execution before pushing the code.

I know that --no-verify option is git internal command and we have less chances to control it. I want to know if pre-receive hook can help me in ensuring the execution of pre-commit hook before push event.
Thanks in advance

答案1

得分: 1

没有这样的选项。用户始终可以完全控制在他们自己的机器上执行哪些钩子。

git clone之后,用户的提交钩子也不会被创建,安装钩子也是开发者自行决定的。

你可以做一件事,就是在构建和/或测试脚本中添加一些代码来确保存储库配置。然后添加两件事:

  1. 添加预提交钩子以确保代码已经经过测试。
  2. 当传入--no-verify标志时,添加一个替代git commit并抛出异常的git别名

要知道这段代码是否运行,唯一的方法就是在提交中添加一些内容。可以是你添加到提交消息中的内容,或者可以向提交添加一个注释:

git notes add -m "Pre-commit hook added this marker"

这样你可以在服务器上的预接收钩子中检查那个注释。

这仍然不能保证提交实际上已经被测试或构建。要添加这样的保证,你只能使用拉取请求工作流,并将构建和测试作为检查的一部分运行。

英文:

No there is no such option. The user always has full control over which hooks are executed on their own machine.

You commit-hook also won't be created for a user after doing a git clone, installing the hooks is also up to the developer.

One thing you could do is to add a bit of code that will ensue the repo configuration in the build and/or test scripts. Then add two things:

  1. The pre-commit hook to ensure the code was tested
  2. A git alias that replaces git commit and throws when the --no-verify flag is passed in.

The only way to know whether this code as ran is to add something to the commit. Can be a something you add to the commit message or you coud add a note to the commit:

git notes add -m "Pre-commit hook added this marker"

That way you can check for that note on the server from a pre-receive hook.

It is still no guarantee the commit was actually tested or built. To add such a guarantee you can only use a pull-request workflow and run the build and tests as part of the checks.

huangapple
  • 本文由 发表于 2023年2月19日 21:41:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定