git - 在服务器端复制 git 客户端 Hook

标签 git latex gitlab githooks

我在 .git/hooks 中定义了一个提交后 Hook ,我也想在服务器端执行它(在我的例子中是 Gitlab.com)。

背景:我在 LaTeX 项目中使用 gitinfo2 和提交后 Hook 来引用有关 pdf 中最新 git 标签的信息。这在我的电脑上运行良好,但当我将 repo 推送到 Gitlab 时失败。

它不会导致错误,但会给出以下警告,这基本上意味着 git 钩子(Hook)从未执行过。

Package gitinfo2 Warning: I can't find the file '.git/gitHeadInfo.gin'.
(gitinfo2)                All git metadata has been set to '(None)'.

到目前为止,根据我在网上阅读的内容,客户端 git Hook 不会在服务器上执行 - 但为什么不呢?在这种情况下,我希望 Hook 在客户端和服务器上都执行。

所以,基本上,我希望事件的顺序是这样的:

  1. 我提交了 .tex 文件。
  2. 我将提交推送到 Gitlab。
  3. 一旦进入 Gitlab,git 钩子(Hook)就会执行,导致在 .git 文件夹中创建一个名为 gitHeadInfo.gin 的文件。
  4. latex 文档是使用 Gitlab CI 构建的,gitinfo 包有助于从 gitHeadInfo.gin 中提取 git 版本信息。
  5. pdf 已部署到 Gitlab Pages .

除第 3 步外,我的所有工作都正常。因此,我目前的解决方法是在我的计算机上也构建 pdf 并提交它,而不是依赖 Gitlab CI。

git 钩子(Hook)的内容:

#!/bin/sh
# Copyright 2015 Brent Longborough
# Part of gitinfo2 package Version 2
# Release 2.0.7 2015-11-22
# Please read gitinfo2.pdf for licencing and other details
# -----------------------------------------------------
# Post-{commit,checkout,merge} hook for the gitinfo2 package
#
# Get the first tag found in the history from the current HEAD
FIRSTTAG=$(git describe --tags --always --dirty='-*' 2>/dev/null)
# Get the first tag in history that looks like a Release
RELTAG=$(git describe --tags --long --always --dirty='-*' --match '[0-9]*.*' 2>/dev/null)
# Hoover up the metadata
git --no-pager log -1 --date=short --decorate=short \
    --pretty=format:"\usepackage[%
        shash={%h},
        lhash={%H},
        authname={%an},
        authemail={%ae},
        authsdate={%ad},
        authidate={%ai},
        authudate={%at},
        commname={%cn},
        commemail={%ce},
        commsdate={%cd},
        commidate={%ci},
        commudate={%ct},
        refnames={%d},
        firsttagdescribe={$FIRSTTAG},
        reltag={$RELTAG}
    ]{gitexinfo}" HEAD > .git/gitHeadInfo.gin

最佳答案

client side git hooks don't execute on the server - but why not?

通常,您正在推送到一个裸仓库(一个没有工作树的仓库,您不能直接在其中进行任何提交)
所以server-side commits更多的是关于执行策略而不是创建新的提交。

如果您真的需要在服务器端创建新内容(尤其是您无法直接控制的内容,例如 GitLab.com),您需要:

  • 要么激活某种服务器端 Hook ,目前只能在 GitHub 上使用 GitHub actions .
  • 或者为 GitLab webhook 设置监听器: 该 webhook 会调用(在 each push events 上)您的监听器,它可以反过来获取最新的历史记录,进行您需要的任何修改,创建新的提交并推回。

关于git - 在服务器端复制 git 客户端 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54226297/

相关文章:

r - 如何在 PDF 中使用带有 bookdown 的自定义 block

r - 在 Revealjs 演示文稿中的 kable 表中使用 LaTeX 表达式

gitlab 无法向新用户发送电子邮件

git - git父编号是如何分配的?

git - 如何在 GitHub Actions 中使用子模块

latex - 将 LaTeX 表达式转换为中缀表达式

git - GitLab HTTP URL 上的 504 网关超时

gitlab - Argocd 与 GITLAB 存储库的连接

ruby - ruby 中两次提交之间新行的代码覆盖率

git - 无法克隆 GitHub SSH 存储库 : CreateProcessW failed error:193