Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git hook for whenever HEAD changes

Tags:

git

githooks

My aim is to keep an (untracked) file up to date with the current HEAD commit hash, branch and some other details. I have a working git post-checkout hook that updates the file, but I want the hook to run whenever HEAD changes, whether that is by commit, merge or whatever else might be possible.

What is the easiest way to ensure that my hook runs whenever HEAD changes?

Details

The file that I am generating is a javascript file which is part of a statically served website. We have no dependency on server side scripting, so we would like to avoid such a dependency here.

The script that should be run is the following:

#!/bin/sh

FILE=js/git-status.js

echo "// This file is autogenerated by a post-checkout hook. Your changes here WILL be lost." > $FILE

echo "GIT_BRANCH = '$(git rev-parse --abbrev-ref HEAD)';" >> $FILE
echo "GIT_COMMIT_HASH = '$(git rev-parse HEAD)';" >> $FILE
echo "GIT_COMMIT_TIME = '$(git log -1 -s --format=%ci HEAD)';"  >> $FILE

echo "Updated $FILE"

While there may be another approach I could take, I would still like an answer to the original question.

like image 879
BudgieInWA Avatar asked Nov 22 '14 03:11

BudgieInWA


People also ask

How do you force a pre-commit hook?

pre-commit will now run on every commit. Every time you clone a project using pre-commit running pre-commit install should always be the first thing you do. If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> .

What is a pre-commit hook?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.


1 Answers

It seems like it may not be possible (though please prove me wrong), so I have settled with this good-enough approach: I trigger the script from a number of hooks, which means that it will be run most of the time.

These are the hooks that I am using:

  • post-checkout
  • post-commit
  • post-merge
  • post-rebase
like image 120
BudgieInWA Avatar answered Oct 18 '22 03:10

BudgieInWA