跟踪 .git/hooks 中钩子的更改
有没有办法跟踪 git hook 的变化?我有三个钩子,它们只显示在我的机器上,而不是在我的其他开发人员获取时显示。尝试 git add 不起作用。
Is there a way to track git hook changes? I have three hooks that only show up on my machine, not when my other developers fetch. Trying to git add
doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
http://benjamin-meyer.blogspot.com/2008/10/git-hooks.html< /a>
http://benjamin-meyer.blogspot.com/2008/10/git-hooks.html
我意识到这个问题已经有很多年了,但我觉得对于像我这样最终来到这里的旅行者来说,这需要说:Hooks 不是按设计跟踪的! Brian 的答案是可行的,但要解决的问题是,你实际上信任与你一起工作的其他人,不会将恶意代码放入存储库中,然后你的钩子会毫无疑问地执行这些代码;这就是安全漏洞的定义。
I realise this question is years old, but I feel this needs to be said for travellers like myself who wind up here: Hooks are not tracked by design! Brian's answer will work, but the deal with that is that you are effectively trusting everyone else you are working with to not put malicious code in the repository, which your hooks then execute without question; this is the very definition of a security hole.
恢复这个旧线程,您可以有一个单独的版本控制目录,其中包含您的钩子,然后使用 git config core.hooksPath 来定位该目录。
Reviving this old thread, you could have a separate version controlled directory which contains your hooks then use the
git config
core.hooksPath
to target that directory.修改布莱恩的答案以考虑菲利普的重要观点:
如果您有任何具有写访问权限的用户使用“#!/bin/sh rm -rf ~”创建一个钩子(例如,提交后),那么您的主目录就会出现。 (或者可能是更良性但仍然愚蠢的东西。)
为了防止这种情况,最好不要符号链接目录,而是手动将它们复制到 git_hooks 目录或从 git_hooks 目录复制它们。是的,您必须记住在更新这些文件时手动复制这些文件,但总比没有好,而且您仍然不向某人授予对您计算机上的命令的用户级访问权限。
更新:如下所述,如果您计划大量更改挂钩,您可以制作一个包装器脚本来复制文件,然后运行提交。然而,自动化提交(使用 git add 并输入自动消息)确实很混乱。更好的想法是让您的钩子之一在提交之前将副本复制到此“git_hooks”目录。这样,恶意用户将无法提交将在下一个挂钩调用时运行的文件。
Modifying Brian's answer to take into account Philip's important point:
If you have any user with write access that creates a hook (say, post-commit) with '#!/bin/sh rm -rf ~' there goes your home directory. (Or maybe something more benign but still stupid.)
To protect against this, it would be best not to symlink the directory, but copy them manually to and from a git_hooks directory. Yes, you have to remember to manually copy theses files when you update them, but better than nothing, and still you don't give someone user-level access to commands on your machine.
UPDATE: As mentioned below, if you plan on changing your hooks a bunch, you could make a wrapper script which copies the files and then runs the commit. However automating a commit (with git add and inputting an automated message) is really messy. A better idea would be to have one of your hooks do the copy to this 'git_hooks' directory, before the commit. This way malicious user wouldn't be able to commit a file that will run on the next hook call.
使用模板目录。从 git 克隆中,您可以使用标志
--template=
将文件添加到$GIT_DIR
。您还需要创建一个保存模板的目录或使用 git 提供的位置/usr/share/git-core/templates
。通过使用模板目录,您可以准备好一个活动钩子,以便在创建时在钩子目录中运行。我不确定您的人员进行编辑的频率,但您可以将此模板保存在所有人都可以访问的存储库中。实际上,这应该是一个在存储库中运行文件的钩子,其中包含自动运行所需的所有代码。
就像其他人提到的那样,一旦您自动执行未知代码,您就会面临风险。
Use template dirs. From a git clone, you can us the flag
--template=<template_directory>
to add files to the$GIT_DIR
. You will also need to create a dir that holds your template or use gits provided location/usr/share/git-core/templates
. By using a template dir you can have an active hook ready to run in the hooks dir from creation.I'm not sure how often your people will be making edits, but you could save this template in a repo that all have access to. Really this should be one hook that runs a file in the repo that contains all of the code you need to automatically run.
Like others have mentioned, as soon as you're executing unknown code automatically you open yourself up to risk.
另一个解决方案(与跟踪/版本控制无关)可能是使用为您处理钩子的插件。
例如:
对于 Web 应用程序,想象一下您可以在 json 包中添加一个脚本来配置预提交挂钩!
这是一个很好的例子:
https://github.com/typicode/husky
最后这个脚本就可以进行版本控制了并且您不必处理 .git 内的 hooks 文件夹
Another solution (which is not related to tracking / versioning) could be the usage of a plugin that will handle hooks for you.
For example:
In the case of a web app, imagine that you can add one script in package json that will configure pre commit hooks !
Here is a good example:
https://github.com/typicode/husky
In the end, this script can be under version control and you don't have to deal with hooks folder inside .git