git - 如何在 git 中获取或 pull 命令后立即执行命令?

标签 git ghc githooks

我克隆了 GHC(Glasgow Haskell 编译器)存储库。为了构建编译器,您需要几个库,它们都可以作为 git 存储库使用。为了缓解这些问题,GHC 黑客包含了一个脚本 sync-all,该脚本在执行时会更新所有依赖的存储库。

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

最佳答案

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

post-merge client-side hook ( mentioned here ) 不会被每次 pull 触发(例如,不是在 pull rebase 中,不是在快进 merge 的情况下)

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


原始答案(2011 年)

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

post-receivepost-update Hook 通常用于此类任务,执行每次推送后。
post-receivepost-update Hook 的区别在于参数:post-receive 将同时获取旧的和新的所有 refs 的值以及它们的名称。

缺少的部分是:那个钩子(Hook)在哪里执行?

博文“Missing git hooks documentation”来自(一位伟大的)SO 贡献者 Mark Longair阐明了这一点:

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.

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

关于git - 如何在 git 中获取或 pull 命令后立即执行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5623208/

相关文章:

php - 在 webhook 触发后尝试在服务器上运行 git pull,但无法正常工作

ruby - 没有注释的 Git diff

git - 如何访问 Azure Web Apps 中的部署历史记录?

gcc - Hackage 包中的 undefined symbol `double-conversion`

php - 从 GIT post-update hook 执行 PHP

bash - 如何实时从远程 git 仓库获取推送通知?

windows - 如果分支名称在 Windows 上无效,则 Git 无法 pull/获取

haskell - 为什么通用函数以特殊的方式处理列表头?

haskell - ghc中的字节串链接

git Hook : is there a clone hook?