在服务器端复制 git 个客户端挂钩
Duplicating git client hooks on the server side
我在 .git/hooks
中定义了一个 post-commit 钩子,我也想在服务器端执行它(在我的例子中是 Gitlab.com)。
背景:我在 LaTeX 项目中使用 gitinfo2 和 post-commit 钩子来引用有关 pdf 中最新 git 标签的信息。这在我的电脑上运行良好,但当我将 repo 推送到 Gitlab 时失败。
它不会导致错误,但会给出以下警告,这基本上意味着 git 挂钩从未执行过。
Package gitinfo2 Warning: I can't find the file '.git/gitHeadInfo.gin'.
(gitinfo2) All git metadata has been set to '(None)'.
到目前为止,根据我在网上阅读的内容,客户端 git 挂钩不会在服务器上执行 - 但为什么不呢?在这种情况下,我希望挂钩在客户端和服务器上都执行。
所以,基本上,我希望事件的顺序是这样的:
- 我提交了 .tex 文件。
- 我将提交推送到 Gitlab。
- 在 Gitlab 中,执行 git 挂钩,导致在
.git
文件夹中创建名为 gitHeadInfo.gin
的文件。
- latex 文档是使用 Gitlab CI 构建的,
gitinfo
包帮助从 gitHeadInfo.gin
. 中提取 git 版本信息
- pdf 已部署到 Gitlab Pages。
除第 3 步外,我的所有工作都正常。因此,我目前的解决方法是在我的计算机上也构建 pdf 并提交它,而不是依赖 Gitlab CI。
git 挂钩的内容:
#!/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),您需要:
- 要么激活某种服务器端挂钩,目前仅在 GitHub 可用,GitHub actions。
- 或者设置一个监听器到 GitLab webhook: that webhook would call (on each push events) 你的监听器可以反过来获取最新的历史记录,做你需要的任何修改,创建新的提交并推回。
这是一个适合我的解决方案。它确实要求您在存储库的任何副本中执行一次两个短命令。
在您的存储库的任何副本中,执行以下操作:
- 复制您的 .git/hooks 文件夹 and/or 任何您想使用的挂钩脚本到 .githooks。当然,您可以选择与 .githooks 不同的文件夹名称。具体来说,在您的回购使用的顶级文件夹中
$ cp -a .git/hooks .githooks
- 将 .githooks 添加到 repo
$ git add .githooks
$ git commit -m 'added .githooks directory'
请注意,您只需在任何存储库中执行一次前两个步骤,而不是在每个本地副本中执行一次。
在存储库的每个本地副本中,您需要
- 更改本地存储库的配置以使用 .githooks 而不是 .git/hooks
$ git config --local core.hooksPath .githooks
- 此时本地存储库准备开始制作 .git/gitHeadInfo 文件,但尚未完成。任何一个
- 提交
- 手动执行其中一个 gitinfo2 挂钩(毕竟是脚本)。例如。,
$ .githooks/post-commit
我在 .git/hooks
中定义了一个 post-commit 钩子,我也想在服务器端执行它(在我的例子中是 Gitlab.com)。
背景:我在 LaTeX 项目中使用 gitinfo2 和 post-commit 钩子来引用有关 pdf 中最新 git 标签的信息。这在我的电脑上运行良好,但当我将 repo 推送到 Gitlab 时失败。
它不会导致错误,但会给出以下警告,这基本上意味着 git 挂钩从未执行过。
Package gitinfo2 Warning: I can't find the file '.git/gitHeadInfo.gin'.
(gitinfo2) All git metadata has been set to '(None)'.
到目前为止,根据我在网上阅读的内容,客户端 git 挂钩不会在服务器上执行 - 但为什么不呢?在这种情况下,我希望挂钩在客户端和服务器上都执行。
所以,基本上,我希望事件的顺序是这样的:
- 我提交了 .tex 文件。
- 我将提交推送到 Gitlab。
- 在 Gitlab 中,执行 git 挂钩,导致在
.git
文件夹中创建名为gitHeadInfo.gin
的文件。 - latex 文档是使用 Gitlab CI 构建的,
gitinfo
包帮助从gitHeadInfo.gin
. 中提取 git 版本信息
- pdf 已部署到 Gitlab Pages。
除第 3 步外,我的所有工作都正常。因此,我目前的解决方法是在我的计算机上也构建 pdf 并提交它,而不是依赖 Gitlab CI。
git 挂钩的内容:
#!/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),您需要:
- 要么激活某种服务器端挂钩,目前仅在 GitHub 可用,GitHub actions。
- 或者设置一个监听器到 GitLab webhook: that webhook would call (on each push events) 你的监听器可以反过来获取最新的历史记录,做你需要的任何修改,创建新的提交并推回。
这是一个适合我的解决方案。它确实要求您在存储库的任何副本中执行一次两个短命令。
在您的存储库的任何副本中,执行以下操作:
- 复制您的 .git/hooks 文件夹 and/or 任何您想使用的挂钩脚本到 .githooks。当然,您可以选择与 .githooks 不同的文件夹名称。具体来说,在您的回购使用的顶级文件夹中
$ cp -a .git/hooks .githooks
- 将 .githooks 添加到 repo
$ git add .githooks
$ git commit -m 'added .githooks directory'
请注意,您只需在任何存储库中执行一次前两个步骤,而不是在每个本地副本中执行一次。
在存储库的每个本地副本中,您需要
- 更改本地存储库的配置以使用 .githooks 而不是 .git/hooks
$ git config --local core.hooksPath .githooks
- 此时本地存储库准备开始制作 .git/gitHeadInfo 文件,但尚未完成。任何一个
- 提交
- 手动执行其中一个 gitinfo2 挂钩(毕竟是脚本)。例如。,
$ .githooks/post-commit