bluprince13
bluprince13

Reputation: 5001

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).

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. This works well on my computer, but fails when I push the repo to Gitlab.

It doesn't cause an error, but gives the following warning, which basically means that the git hook never executed.

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? 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.
  2. I push the commit to 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.
  4. The latex document is built using Gitlab CI, with the gitinfo package helping to pull the git version info from gitHeadInfo.gin.
  5. The pdf is deployed to 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.

Contents of the git hook:

#!/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

Upvotes: 9

Views: 441

Answers (2)

mikemtnbikes
mikemtnbikes

Reputation: 582

Here's a solution that works for me. It does require you to execute two short commands once within any copy of the 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. You can choose a different folder name than .githooks, of course. Specifically, within the top folder of your repo use

    $ cp -a .git/hooks .githooks

  2. Add .githooks to the repo

    $ git add .githooks

    $ git commit -m 'added .githooks directory'

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

    $ 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. Either
    1. Make a commit
    2. Execute one of the gitinfo2 hooks (which are scripts after all) manually. E.g.,

      $ .githooks/post-commit

Upvotes: 4

VonC
VonC

Reputation: 1327784

client side git hooks don't execute on the server - but why not?

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:

  • either to activate some kind of server-side hook, which is for now only available at GitHub, with GitHub actions.
  • 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.

Upvotes: 7

Related Questions