Search code examples
gitgithookspre-commit

Can a git pre-commit hook add a file to the repo?


I'm keeping a text file of my git log in my working directory, and I have a script that updates it after a commit. This is fine, but the effect of this is that the version that is inside the repo is always one commit behind.

Is it possible to write a pre-commit hook that would call a script and add a file to the repo, in effect saving the text version of the log with the current commit info inside it?

I'm new to git hooks and have no idea whatsoever how this would work, if it's even possible. I appreciate the help!


Solution

  • Short answer: no. The log contains the SHA1 of the commit, which depends on the contents of the commit. If you change the log, you'll change the commit, and it'll still be invalid.

    I wonder why you're trying to do this, really. Within the repository, git log is just as good as cat saved-git-log. If you want this as a changelog, for released versions, simply create it as part of your build/deploy process, for example something like this:

    tarname=my-project-$(git describe HEAD).tar
    git log > changelog.txt
    git archive --format=tar HEAD > $tarname
    tar -Af $tarname changelot.txt
    gzip $tarname