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

时间:2019-01-16 22:22:12

标签: git latex gitlab githooks

我在.git/hooks中定义了一个提交后钩子,我也想在服务器端执行(在我的情况下,Gitlab.com)。

背景:我正在使用gitinfo2以及LaTeX项目中的提交后钩子来引用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 hook,从而在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

2 个答案:

答案 0 :(得分:7)

  

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

通常,您将推送至裸仓库(没有工作树的仓库,您不能直接进行任何提交)
因此,server-side commits与其说是执行新提交,不如说是执行政策。

如果您确实需要在服务器端创建新内容(特别是您没有直接控制权的内容,例如GitLab.com),则需要:

  • 或者使用GitHub actions激活某种类型的服务器端挂钩(目前仅在GitHub上可用)。
  • 或设置GitLab webhook的监听器:该webhook会调用each push events上的监听器,从而可以获取最新历史记录,进行所需的任何修改,创建新的提交并回推。

答案 1 :(得分:4)

这是对我有用的解决方案。它确实需要您在存储库的任何副本中一次执行两个短命令。

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

  1. 将.git / hooks文件夹和/或要使用的任何钩子脚本复制到.githooks。当然,您可以选择与.githooks不同的文件夹名称。具体来说,在回购使用的顶部文件夹中
      

    $ cp -a .git / hooks .githooks

  2. 将.githooks添加到存储库
      

    $ git add .githooks

         

    $ git commit -m'添加了.githooks目录'

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

在存储库的每个本地副本中,您将需要

  1. 将本地存储库的配置更改为使用.githooks而不是.git / hooks
      

    $ git config --local core.hooksPath .githooks

  2. 这时,本地存储库已准备好开始制作.git / gitHeadInfo文件,但目前尚未这样做。要么
    1. 提交
    2. 手动执行gitinfo2钩子之一(毕竟是脚本)。例如。,
        

      $ .githooks / post-commit