簡體   English   中英

在服務器端復制 git 客戶端鈎子

[英]Duplicating git client hooks on the server side

我已經在.git/hooks中定義了一個提交后.git/hooks ,我也想在服務器端(在我的例子中是Gitlab.com )執行它。

背景:我在 LaTeX 項目中使用 gitinfo2 和 post-commit 鈎子來引用有關 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 鈎子不會在服務器上執行 - 但為什么不呢? 在這種情況下,我希望鈎子在客戶端和服務器上都執行。

所以,基本上,我希望事件的順序是這樣的:

  1. 我提交了 .tex 文件。
  2. 我將提交推送到 Gitlab。
  3. 一旦它在 Gitlab 中,就會執行 git 鈎子,從而在.git文件夾中創建一個名為gitHeadInfo.gin的文件。
  4. Latex 文檔是使用 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 鈎子不在服務器上執行 - 但為什么不呢?

通常,您正在推送到一個裸倉庫(一個沒有工作樹的倉庫,您不能直接進行任何提交)
因此, 服務器端提交更多的是關於執行策略而不是創建新的提交。

如果您確實需要在服務器端創建新內容(尤其是您無法直接控制的內容,例如 GitLab.com),您需要:

  • 要么激活某種服務器端掛鈎,該掛鈎目前僅在 GitHub 上可用,並帶有GitHub 操作
  • 或者為GitLab 網絡鈎子設置一個監聽器:該網絡鈎子會調用(在每個推送事件上)你的監聽器,它可以反過來獲取最新的歷史記錄,做你需要的任何修改,創建新的提交並推回。

這是一個對我有用的解決方案。 它確實需要您在 repo 的任何副本中執行一次兩個短命令。

在您的存儲庫的任何副本中,執行以下操作:

  1. 將您的 .git/hooks 文件夾和/或您想要使用的任何鈎子腳本復制到 .githooks。 當然,您可以選擇與 .githooks 不同的文件夾名稱。 具體來說,在您的回購使用的頂部文件夾中

    $ 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 文件,但還沒有這樣做。 任何一個
    1. 提交
    2. 手動執行 gitinfo2 鈎子之一(畢竟是腳本)。 例如,

      $ .githooks/提交后

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM