简体   繁体   English

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

[英]Duplicating git client hooks on the server side

I've defined a post-commit hook in .git/hooks that I want to also execute on the server side ( Gitlab.com in my case).我已经在.git/hooks中定义了一个提交后.git/hooks ,我也想在服务器端(在我的例子中是Gitlab.com )执行它。

Background: I'm using gitinfo2 along with a post-commit hook in a LaTeX project to quote info about the latest git tag within the pdf.背景:我在 LaTeX 项目中使用 gitinfo2 和 post-commit 钩子来引用有关 pdf 中最新 git 标签的信息。 This works well on my computer, but fails when I push the repo to Gitlab.这在我的计算机上运行良好,但是当我将 repo 推送到 Gitlab 时失败了。

It doesn't cause an error, but gives the following warning, which basically means that the git hook never executed.它不会导致错误,但会给出以下警告,这基本上意味着 git hook 从未执行过。

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

From what I've read online so far, client side git hooks don't execute on the server - but why not?从我目前在线阅读的内容来看,客户端 git 钩子不会在服务器上执行 - 但为什么不呢? In a situation like this, I would want the hook to execute on both the client and the server.在这种情况下,我希望钩子在客户端和服务器上都执行。

So, basically, I want the sequence of events to be like this:所以,基本上,我希望事件的顺序是这样的:

  1. I make a commit of the .tex file.我提交了 .tex 文件。
  2. I push the commit to Gitlab.我将提交推送到 Gitlab。
  3. Once it's at Gitlab, the git hook is executed leading to a file called gitHeadInfo.gin being created in the .git folder.一旦它在 Gitlab 中,就会执行 git 钩子,从而在.git文件夹中创建一个名为gitHeadInfo.gin的文件。
  4. The latex document is built using Gitlab CI, with the gitinfo package helping to pull the git version info from gitHeadInfo.gin . Latex 文档是使用 Gitlab CI 构建的,其中gitinfo包有助于从gitHeadInfo.gin提取 git 版本信息。
  5. The pdf is deployed to Gitlab Pages . pdf 被部署到Gitlab Pages

I've got everything working except step 3. So, my current workaround is to build the pdf on my computer as well and commit it rather than relying on Gitlab CI.除了第 3 步之外,我已经完成了所有工作。因此,我目前的解决方法是也在我的计算机上构建 pdf 并提交它,而不是依赖 Gitlab CI。

Contents of the git hook: 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?客户端 git 钩子不在服务器上执行 - 但为什么不呢?

Generally, you are pushing to a bare repo (a repo without working tree, where you cannot do any commit directly)通常,您正在推送到一个裸仓库(一个没有工作树的仓库,您不能直接进行任何提交)
So server-side commits are more about enforcing policies than creating new commits.因此, 服务器端提交更多的是关于执行策略而不是创建新的提交。

If you really needed new content to be created on the server side (especially one you have no direct control, like GitLab.com), you would need:如果您确实需要在服务器端创建新内容(尤其是您无法直接控制的内容,例如 GitLab.com),您需要:

  • either to activate some kind of server-side hook, which is for now only available at GitHub, with GitHub actions .要么激活某种服务器端挂钩,该挂钩目前仅在 GitHub 上可用,并带有GitHub 操作
  • or setup a listener to a GitLab webhook : that webhook would call (on each push events ) your listener which could in turn get the latest history, do any modification you need, create new commit and push back.或者为GitLab 网络钩子设置一个监听器:该网络钩子会调用(在每个推送事件上)你的监听器,它可以反过来获取最新的历史记录,做你需要的任何修改,创建新的提交并推回。

Here's a solution that works for me.这是一个对我有用的解决方案。 It does require you to execute two short commands once within any copy of the repo.它确实需要您在 repo 的任何副本中执行一次两个短命令。

Within any copy of your repo, do the following:在您的存储库的任何副本中,执行以下操作:

  1. Copy your .git/hooks folder and/or any hook scripts you want to use to .githooks.将您的 .git/hooks 文件夹和/或您想要使用的任何钩子脚本复制到 .githooks。 You can choose a different folder name than .githooks, of course.当然,您可以选择与 .githooks 不同的文件夹名称。 Specifically, within the top folder of your repo use具体来说,在您的回购使用的顶部文件夹中

    $ cp -a .git/hooks .githooks $ cp -a .git/hooks .githooks

  2. Add .githooks to the repo将 .githooks 添加到 repo

    $ git add .githooks $ git add .githooks

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

Note you only have to do these first two steps once in any of the repos, not in every local copy.请注意,您只需在任何存储库中执行前两个步骤一次,而不是在每个本地副本中。

Within each local copy of a repo you will need to在存储库的每个本地副本中,您需要

  1. Change the local repo's configuration to use .githooks instead of .git/hooks更改本地 repo 的配置以使用 .githooks 而不是 .git/hooks

    $ git config --local core.hooksPath .githooks $ git config --local core.hooksPath .githooks

  2. At this point the local repo is poised to start making the .git/gitHeadInfo file, but won't have done so yet.此时本地存储库准备开始制作 .git/gitHeadInfo 文件,但还没有这样做。 Either任何一个
    1. Make a commit提交
    2. Execute one of the gitinfo2 hooks (which are scripts after all) manually.手动执行 gitinfo2 钩子之一(毕竟是脚本)。 Eg,例如,

      $ .githooks/post-commit $ .githooks/提交后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM