Git 远程/共享预提交挂钩
将一个官方存储库作为远程存储库,并从其克隆多个本地存储库,是否可以在该主存储库上编写预提交挂钩脚本并在其所有克隆上强制执行?
With a one official repository as the remote, and multiple local repositories cloned from it, can a pre-commit hook be scripted on that main repository and be enforced on all clones of it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我不这么认为,因为钩子不会被克隆。
可能是如果该挂钩脚本本身是版本化的,然后链接到克隆服务器中的(符号链接)(前提是它们的操作系统支持该链接功能)。
或者,如果挂钩是 git 模板的一部分用于创建克隆的目录(这只能确保它们存在于克隆存储库中,不能保证它们实际被使用和执行)。
但我不认为有任何“核心”方式来执行提交。
正如 Jefromi 在评论中更清楚地解释的那样(强调我的):
我同意这一评论,并且只看到了强制执行的方法在给定的专门存储库中本地应用的规则。
例如,您不会直接推送到中央存储库,而是首先推送到 QA 存储库,该存储库仅在遵循某些规则时才会接受您的提交。如果是,那么 QA 存储库会将您的提交推送到中央存储库。
直接源自我刚才提到的另一个例子是“无服务器连续与 Git 集成”,这是一种在将它们推送到任何地方之前强制执行本地私有构建的方法。
I don't think so, as hooks are not cloned.
May be if that hook script is itself versioned, and then link to (symbolic link) in the clone servers (provided their OS support that link feature).
Or maybe if the hooks are part of a git template directory used for creating the clones (that would only ensure their presences in the clone repo, that would not guarantee they are actually used and executed).
But I don't think there is any "central" way to enforce a commit.
As Jefromi explains even more clearly in the comments (emphasis mine):
I agree with that comment, and have only seen ways to enforce rules applied locally, in a given specialized repo.
For instance, you wouldn't push to the central repo directly, but would first push to a QA repo which would accept your commit only if it follows certain rules. If it does, then the QA repo will push your commit to the central repo.
Another illustration directly derived from what I just mentioned would be "Serverless Continuous Integration with Git", a way to enforce locally private build that works before pushing them anywhere.
您不能在人们的本地存储库上强制使用预提交挂钩,但在您的中央存储库中您仍然可以运行预接收挂钩。
F. ex 我需要确保提交消息遵守某些规则(用于 trac 集成等)
所以我使用了以下预接收钩子,它检查推送到中央存储库的每个提交消息,如果不正确,将拒绝推送。
有关更多信息,请参阅 f.ex https://git-scm。 com/book/en/v2/Customizing-Git-Git-Hooks
You can not have the pre-commit hook forced on peoples local repositories, but in your central repo you can still run a pre-receive hook.
F. ex I needed to be sure the commit messages obeyed certain rules (for trac integration etc)
so I used following pre-receive hook, which checks every commit messages being pushed to the central repository, and will deny the push if it is not welformed.
for more info see f.ex https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
两个选项:
1. 使用 Husky
如果您正在编写 JavaScript,最好的方法是使用 Husky。 Husky 有一个 postInstall 脚本,可以设置和管理你的 githooks。然后,您可以在 package.json 或 husky 点文件中配置预提交和预推送脚本。
您可以使用它来运行任意脚本。我通常会预推送
yarn lint
和yarn test
。2. 在 git.config 中设置 core/hooksPath,然后在您的存储库中包含 /hooks
如果您不使用 JavaScript,或者您无法使用 Husky,您可以将提交钩子克隆到开发人员计算机上并将其签入一个存储库,但你不能强迫开发人员运行它们。
要签入您的钩子,请在存储库中的某个位置创建一个
hooks
目录。然后将您的钩子放在那里,而不是通常的 .git/hooks 目录。这是您可以强制执行的部分。另一部分取决于开发商的商誉。要将 hooks 文件夹设置为 hooksPath,每个开发人员都必须运行:
git config core.hooksPath hooks
现在,hooks 文件夹中的所有钩子都将按照您的预期运行。
Two options:
1. Use Husky
If you are writing JavaScript, the best way to do this is with Husky. Husky has a postInstall script that will set up and manage your githooks. You can then configure precommit and prepush scripts in your package.json or a husky dotfile.
You can use this to run arbitrary scripts. I typically
yarn lint
andyarn test
prepush.2. Set core/hooksPath in git.config, then include /hooks in your repo
If you are not using JavaScript, or you cannot use Husky, you can clone commit hooks onto developer machines and check them into a repo, but you cannot force developers to run them.
To check in your hooks, create a
hooks
directory somewhere in your repo. Then put your hooks there instead of the usual.git/hooks
directory. This is the part you can enforce.The other part depends on developer goodwill. To set your hooks folder as the hooksPath, each developer must run:
git config core.hooksPath hooks
Now all the hooks in the hooks folder will run as you would expect.
来自
githooks(5)
:由于该钩子很容易被绕过,看来你的问题的答案是“否”。
另外,由于 .git/hooks 目录没有被克隆,因此似乎没有一种机制将其推送到客户端。
From
githooks(5)
:Since the hook can easily be bypassed, it seems the answer to your question is "no".
Also, since the .git/hooks directory is not cloned, there does not seem to be a mechanism to push it to the client.
这里有非常非常的答案。如今这绝对是可能的。
从 Git 2.9 左右开始,配置
core.hooksPath
可用。根据 git 文档:这意味着您可以在客户端计算机之间共享 Git 挂钩(可能使用项目内的非 .git 文件夹,或使用本地克隆的专用 Git 存储库,或使用 OneDrive 或 Dropbox 等同步软件)只需将 core.hooksPath 配置设置为该(共享)文件夹即可。
Very very old answers here. This is definitely possible these days.
Since Git 2.9 or so, the config
core.hooksPath
is available. Per the git documentation:This means that you can share Git hooks between client machines (perhaps using a non-
.git
folder inside the project, or using a dedicated Git repository locally cloned, or using sync software such as OneDrive or Dropbox) by just setting thecore.hooksPath
config to that (shared) folder.假设您的 git 存储库中的源代码具有与其关联的构建系统,您可以配置构建系统来设置预提交挂钩,即通过移动或链接版本控制的预提交挂钩。
我还没有尝试过这个。我在谷歌搜索更好的解决方案时来到这里。
Assuming you have source code in your git repo that has a build system associated with it, you could configure the build system to set up the pre-commit hook, i.e. by moving or linking a pre-commit hook that ~is versioned.
I have not tried this yet. I came here while googling for a better solution.
它不是全自动的,但您可以在要添加到 git 的
hooks/
目录中添加存储库的钩子脚本,然后要求开发人员在克隆后对钩子脚本进行符号链接存储库:在项目自述文件中记录可能效果很好。
It's not fully automatic, but you can add hook scripts for a repo inside a
hooks/
directory that you'd add to git, and then ask developers to symlink the hook script(s) once after they clone the repo:Documenting in a project README might work well enough.
我创建了一个新文件:
pre-commit-hook.sh
这就是我提交到 Git 的方式:
I create a new file:
pre-commit-hook.sh
And this is how I commit to Git: