如何在 git 中执行 fetch 或 pull 命令后立即执行命令?

发布于 2024-11-01 01:39:09 字数 256 浏览 0 评论 0原文

我克隆了 GHC(格拉斯哥 Haskell 编译器)存储库。为了构建编译器,您需要几个库,所有这些库也都可以作为 git 存储库提供。为了方便人们的生活,GHC 黑客添加了一个脚本 sync-all,该脚本在执行时会更新所有依赖的存储库。

现在我的问题是:如何让 git 在自动执行 git pull 后执行 ./sync-all pull ?我听说过一些关于使用钩子的事情,但我真的不知道我必须做什么。

I cloned the GHC (Glasgow Haskell Compiler) repository. In order to build the compiler, you need several libraries, all of them are available as git repositories too. In order to ease ones live, the GHC hackers included a script sync-all that, when executed, updates all the dependent repositories.

Now to my question: How can I make git execute ./sync-all pull after I did a git pull automatically? I heard something about using hooks, but I don't really know, what I have to do.

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

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

发布评论

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

评论(2

不寐倦长更 2024-11-08 01:39:09

如果您需要在客户端(您的计算机)上执行 git pull 后执行脚本,那么没有可靠的解决方案。

合并后客户端挂钩 (此处提到)不会被每次拉动触发(例如,不在拉动变基中,不在快进合并的情况下),

因此包装两个命令的别名仍然是一个可能的解决方法。


原始答案(2011)

如果您需要在服务器端,您可以在每次推送到裸存储库(在服务器上)后检查裸存储库并从工作树(在服务器上)中提取:

A < code>post-receivepost-update 钩子通常用于此类任务,在每次推送后执行。
post-receivepost-update 挂钩之间的区别在于参数:post-receive 将同时获取旧的和新的除了名称之外,还有所有引用的值。

缺少的部分是:该钩子在哪里执行自身?

博客文章“缺少 git hooks 文档" 来自(一位伟大的)SO 贡献者 Mark Longair 对此做了一些说明:

当前工作目录将是 git 目录

  • 因此,如果这是一个名为“/src/git/test.git/”的裸存储库,那么它将是当前工作目录
    – 如果这是一个非裸存储库并且工作树的顶层是“/home/mark/test/”,那么当前工作目录将为“/home/mark/”测试/.git/

Chris Johnsen 详细信息在他的 SO 答案:“如果指定了 –-git-dirGIT_DIR 但没有指定指定了 -–work-treeGIT_WORK_TREEcore.worktree 时,当前工作目录被视为工作树的顶层目录”

换句话说,您的工作树也将是当前目录(.git 目录),这几乎肯定不是您想要的目录。想要

确保您的 hook/post-receive 脚本在创建并可执行后,将 GIT_WORK_TREE 设置在正确的路径,以便您的 ./sync-all pull 在正确的目录中执行(即不是“xxx.git”目录)。

If you need to execute a script after a git pull on the client side (your machine), then a there is no reliable solution.

The post-merge client-side hook (mentioned here) won't be triggered by every pull (for instance, not in a pull rebase, not in case of fast-forward merge)

So an alias wrapping both commands remains a possible workaround.


Original answer (2011)

If you need that on the server side, where you would checkout the bare repo and pull from a working tree (on the server) after each push to a bare repo (on the server):

A post-receive or post-update hook is generally used for this kind of task, executed after each push.
The difference between the post-receive and post-update hooks is in the arguments: a post-receive will get both the old and the new values of all the refs in addition to their names.

The missing piece is: where that hook executes itself?

The blog post "Missing git hooks documentation" from (a great) SO contributor Mark Longair sheds some light on this:

the current working directory will be the git directory.

  • So, if this is a bare repository called “/src/git/test.git/”, that will be the current working directory
    – if this is a non-bare repository and the top level of the working tree is “/home/mark/test/” then the current working directory will be “/home/mark/test/.git/

Chris Johnsen details in his SO answer: "If –-git-dir or GIT_DIR are specified but none of -–work-tree, GIT_WORK_TREE and core.worktree is specified, the current working directory is regarded as the top directory of your working tree."

In other words, your working tree will also be the current directory (the .git directory), which almost certainly isn’t what you want.

Make sure your hook/post-receive script, once created and made executable, sets the GIT_WORK_TREE at the right path, in order for your ./sync-all pull to be executed in the right directory (i.e. not "xxx.git" one).

暖心男生 2024-11-08 01:39:09

当您运行 git pull 时,git 实际上会先进行提取,然后进行合并。这意味着您可以使用 post-merge 挂钩在拉取完成后执行脚本。

要进行此设置,您只需在存储库的 .git/hooks/ 目录中创建一个名为 post-merge 的可执行脚本。

请注意,如果合并由于冲突而失败,则该脚本将不会运行。

When you run git pull, git actually does a fetch followed by a merge. This means you can use the post-merge hook to execute a script when a pull is completed.

To set this up you just need to create an executable script in your repository's .git/hooks/ directory called post-merge.

Note that this script will not be run if the merge fails due to conflicts.

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