在服务器端复制git客户端挂钩

问题描述 投票:8回答:2

我在.git/hooks中定义了一个post-commit钩子,我想在服务器端执行(在我的例子中是Gitlab.com)。

背景:我在一个LaTeX项目中使用gitinfo2和一个post-commit钩子来引用有关pdf中最新git标签的信息。这在我的计算机上运行良好,但是当我将回购推送到Gitlab时失败了。

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

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

从我到目前为止在线阅读,客户端git钩子不在服务器上执行 - 但为什么不呢?在这种情况下,我希望钩子在客户端和服务器上执行。

所以,基本上,我希望事件序列如下:

  1. 我提交了.tex文件。
  2. 我将提交推送到Gitlab。
  3. 一旦它在Gitlab上,执行git钩子,导致在gitHeadInfo.gin文件夹中创建一个名为.git的文件。
  4. 乳胶文档是使用Gitlab CI构建的,gitinfo包帮助从gitHeadInfo.gin中提取git版本信息。
  5. 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
git latex gitlab githooks
2个回答
7
投票

客户端git hooks不在服务器上执行 - 但为什么不呢?

一般来说,你正在推动一个裸仓库(没有工作树的仓库,你不能直接进行任何提交) 因此,server-side commits更多地是关于执行策略而不是创建新提交。

如果你真的需要在服务器端创建新内容(特别是你没有直接控制的内容,比如GitLab.com),你需要:

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

4
投票

这是一个适合我的解决方案。它确实要求您在repo的任何副本中执行一次两个短命令。

在您的回购的任何副本中,执行以下操作:

  1. 将.git / hooks文件夹和/或要使用的任何钩子脚本复制到.githooks。当然,您可以选择与.githooks不同的文件夹名称。具体来说,在您的repo使用的顶级文件夹中 $ cp -a .git / hooks .githooks
  2. 将.githooks添加到repo $ git add .githooks $ git commit -m'添加.githooks目录'

请注意,您只需在任何存储库中执行前两个步骤,而不是在每个本地副本中执行。

在回购的每个本地副本中,您需要

  1. 更改本地repo的配置以使用.githooks而不是.git / hooks $ git config --local core.hooksPath .githooks
  2. 此时本地仓库准备开始制作.git / gitHeadInfo文件,但还没有这样做。或 提交 手动执行gitinfo2挂钩之一(毕竟是脚本)。例如。, $ .githooks / post-commit
© www.soinside.com 2019 - 2024. All rights reserved.