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?
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.
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> .
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With