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

问题描述 投票:0回答:2

我创建了一个预提交挂钩,可以在新开发的代码出现声纳问题时阻止提交。由于它是一个预提交挂钩,开发人员可能有机会使用 --no-verify 选项禁用此挂钩,我想避免它。

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

有没有强制这个钩子在提交之前执行(不受开发人员控制)而不跳过它? 我发现了一个类似的堆栈溢出问题,其中的答案建议使用预接收挂钩,它适用于 push 事件而不是 commit 事件。现在我只想在没有开发人员控制的情况下运行预提交挂钩。如果这不可能,还有其他选择吗?这样我就可以在推送代码之前确认挂钩的执行情况。 提前致谢

git githooks
2个回答
0
投票

不,没有这样的选择。用户始终可以完全控制在自己的机器上执行哪些挂钩。

执行 git clone 后,您也不会为用户创建提交挂钩,安装挂钩也取决于开发人员。


0
投票

不幸的是,如果你想在提交之前强制执行预提交挂钩,而不允许开发人员使用 --no-verify 选项跳过它,Git 中没有内置机制来实现这一点。

pre-commit hook 是为了在客户端执行,所以由开发者决定是否跳过它。但是,您可以考虑一些选项来解决此问题:

Use a Git pre-commit hook manager: You can use a pre-commit hook manager like pre-commit to manage your pre-commit hooks. This tool can help you ensure that all developers use the same pre-commit hook configuration and that the hooks are always executed before a commit, even if a developer tries to skip them.

Educate your developers: You can educate your developers about the importance of the pre-commit hook and the risks of bypassing it using the --no-verify option. This can help promote a culture of responsible code development and ensure that all developers are committed to quality code.

Use Git commit hooks as a notification mechanism: You can use the pre-commit hook as a notification mechanism rather than a prevention mechanism. In other words, instead of preventing a commit, you can configure the hook to send a notification to a team lead or manager whenever it detects an issue. This can help you catch problems early on and address them before they become more serious.

Use Git commit hooks with CI/CD pipeline: You can set up a CI/CD pipeline that includes a pre-commit hook. The pipeline can run the pre-commit hook and any other checks you want to perform, and then automatically build and test the code. If everything passes, the code can be automatically committed and pushed to the repository. This way, the pre-commit hook is always executed before a commit, and there is no option to skip it.
© www.soinside.com 2019 - 2024. All rights reserved.