【问题标题】:Local executing hook after a git push?git push 后的本地执行钩子?
【发布时间】:2010-12-20 07:18:05
【问题描述】:

我查看了the githooks manpage,但除非我遗漏了一些东西,否则我看不到本地、推送后 git 挂钩的选项。在将主分支推送到 GitHub 存储库后,我想要一个更新我的 Web 服务器上的 api 文档(我已经有一个脚本)。当然,我可以自己编写脚本,结合 git push 和 api 文档运行,但感觉有点不雅。

【问题讨论】:

  • post-update 钩子可以在这种情况下触发,对吧?

标签: git githooks


【解决方案1】:

Git 不支持这种类型的钩子。它不属于valid reasons for a Git hook 由 Git 的维护者给出。

上述链接消息中的介绍性评论几乎直接说明了您的情况:

我不是特别喜欢那种钩子 行动开始后行动 在本地并仅根据本地数据采取行动。 这可能是因为我还在考虑 git工具构建块适用于 比其他更高级别的脚本 人们会这样做。

附注“单推”提示

  • 有太多的警告需要完整的解释,但如果你能弄清楚,你应该能够处理细节。

一个额外的pushurl 到带有“备用”对象存储的本地 repo 可以为您提供一种低开销的方式来本地执行推送挂钩。但实际上,付出的努力远不止git push upstream && update-web-server(可能在shell 别名、git 别名或脚本中)。

【讨论】:

【解决方案2】:

我最近遇到了同样的问题。我想要一个钩子,以便来自我的 git 子模块的推送将在“超级项目”中提交新的子模块引用。

正如 Chris 所说,最好的方法是使用 git 别名,如下所示:

$ git config alias.xpush '!git push $1 $2 && update-server.sh'
# (remember the backslash before the ! if your shell requires it)

这会将以下内容添加到您的 .git/config 文件中:

[alias]
  xpush = !git push $1 $2 && update-server.sh

现在,如果你输入:

$ git xpush

您的更改将被推送,然后 update-server.sh 将被执行。

【讨论】:

  • 嗯,如果你在分行呢?你做那个xpush?不应该像 !git push $1 $2 && update-server.sh 这样你就可以指定原始分支?
  • 请注意,使用 git 别名无法覆盖现有命令,请参阅 stackoverflow.com/questions/3538774/…
【解决方案3】:

这个问题的另一个解决方案是有一个git push 的包装器,它在git push 调用之前和之后执行.git/hooks/pre-push.git/hooks/post-push 脚本。可能的包装器可能如下所示:

#!/bin/sh

GIT_DIR_="$(git rev-parse --git-dir)"
BRANCH="$(git rev-parse --symbolic --abbrev-ref $(git symbolic-ref HEAD))"

PRE_PUSH="$GIT_DIR_/hooks/pre-push"
POST_PUSH="$GIT_DIR_/hooks/post-push"

test -x "$PRE_PUSH" &&
    exec "$PRE_PUSH" "$BRANCH" "$@"

git push "$@"

test $? -eq 0 && test -x "$POST_PUSH" &&
    exec "$POST_PUSH" "$BRANCH" "$@"

PATH 中的某处保存为git-push-wh,如果你想用钩子推送,它可以被称为git push-wh

【讨论】:

  • 这太棒了...我使用单独的遥控器:一个我托管我的代码的地方,一个在我的生产服务器中的裸仓库,所以我将this code 添加到我的~/.functions.sh 并使用** push origin ** ** 推送生产 ** 一旦推送完成,它会在我的浏览器中自动启动网站(因为这是在推送中定义的)。谢谢!
  • 当前代码在 git 工作树中不起作用,要修复它,只需更改 3 行:` GIT_DIR_="$(git rev-parse --git-path hooks)" PRE_PUSH="${GIT_DIR_ }/pre-push" POST_PUSH="${GIT_DIR_}/post-push" `
【解决方案4】:

从 Git 1.8.2 开始,在推送操作之前调用了一个新的钩子:pre-push 如果脚本返回的不是 0,推送操作将被取消。

在发行说明中提及:https://github.com/git/git/blob/master/Documentation/RelNotes/1.8.2.txt#L167

示例:https://github.com/git/git/blob/87c86dd14abe8db7d00b0df5661ef8cf147a72a3/templates/hooks--pre-push.sample

【讨论】:

  • 我不认为这个答案是好的答案,它讲述了 pre-push hook,而最初的问题是关于 post-push hook。
  • Levente Holló,pre-push hook在用户运行git-push之后,git实际推送commit/s之前执行。Git中没有集成其他push相关的hook。
【解决方案5】:

我为此使用了一个函数:

current_branch() {
    local ref=$(git symbolic-ref HEAD 2> /dev/null) || return
    echo ${ref#refs/heads/}
}

gp() {
    local post_push="$(git rev-parse --git-dir)/hooks/post-push"
    git push "$@" && {
        [[ -x "$post_push" ]] && "$post_push" "$(current_branch)" "$@"
    }
}
compdef _git gp=git-push

compdef 部分用于 ZSH。

【讨论】:

    【解决方案6】:

    只需创建一个顶部带有 sleep 的 pre-push 钩子。根据您的网络连接速度,确保睡眠时间足够长,以便将提交上传到上游服务器。理想情况下添加& 以在后台运行您的脚本:

    (sleep 30 && .git/hooks/post-push) &
    

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 2015-11-19
      • 2014-02-15
      • 1970-01-01
      • 2014-07-08
      相关资源
      最近更新 更多