Eekhoorn
Eekhoorn

Reputation: 926

git post-commit hook not executed

I'm trying to implement a way to include git commit data into a LaTex document using the method described here.

The hook looks like this:

$ cat post-commit 
#!/bin/sh

cd ../..

git log -1 --format="format:\
                \\gdef\\GITAbrHash{%h}\
                \\gdef\\GITHash{%H}\
                \\gdef\\GITAuthorDate{%ad}\
                \\gdef\\GITAuthorName{%an}" >> git.tex

When I do chmod a+x post-commit and then ./post-commit, the file is generated appropriately. However, when I actually make a commit, the hook is not executed. What could be the problem?

Upvotes: 2

Views: 413

Answers (2)

Eekhoorn
Eekhoorn

Reputation: 926

The problem was that I didn't need to do cd ../.. because the code is executed in the root directory of the repository anyway. The other mistake was >> git.tex, which of course should read > git.tex.

Upvotes: 0

eckes
eckes

Reputation: 67177

I guess the problem is that you cd ../.. in order to navigate to your sources.

Try to replace the cd ../.. with a pushd ../... After calling git log, restore the directory with popd


Apart of this: I doubt that a post-commit hook is what you want here: if you did a successful commit, git.tex will be changed afterwards and you have a modified file.

A pre-commit hook would be a better choice: right before the commit is made, the hook gets called, modifies git.tex and then adds it to the commit.

Another option would be to have a smudge and clean filter for your git.tex file (http://git-scm.com/book/en/Customizing-Git-Git-Attributes#Keyword-Expansion). Every time you're going to do something like a release, just do a

git add git.tex

This will invoke the clean filter which generates the content of git.tex. This content will be sent to the repo. The smudge filter will be run when the file gets checked out. It's duty is to make the file empty, so it's just

echo '' > git.tex

Upvotes: 3

Related Questions