To automatically do things with git
, use hooks
You want to do \thing{}
with every git commit? That's what a hook is for. For example, the post-commit
hook:
After the entire commit process is completed, the post-commit hook runs. It doesn’t take any parameters, but you can easily get the last commit by running git log -1 HEAD
. Generally, this script is used for notification or something similar.
from the GIT SCM website / manual
Adding a date with every git commit
This is the straightforward part. You could use this hook to achieve the first part of your two goals quite simply:
- use
git diff-tree --no-commit-id --name-only -r HEAD
to get the files changed in the current revision (see this explanation on SO)
- iterate over those files in the language of your choice and use the tool of your choice to insert the date, for example with
sed
or awk
If you do this locally, it will leave you with changed unstaged files on your working copy; ie git status
will show changed files. You may want to avoid that by using an additional repo as below.
Adding a version with each git tag
This is slightly trickier.
You could work around the fact that git tag
does not trigger any hooks by using an intermediate git repo - either locally or remote - and git push
to that repo each time you tag. You could then use a post-update
hook on that server to rewrite based on the latest tag.
For example:
- Get the latest tag / version name:
git describe --tags $(git rev-list --tags --max-count=1)
- Format the files using
sed
(or awk
or whichever tool you choose)
- Upload to CTAN from that directory
Can't I do this with another hook?
It is possible to detect if a ref has a tag by using git describe
, for example:
# In a working git repo
$ git tag -a "test1" -m "test tag"
$ git describe --exact-match HEAD
test1
$ git describe --exact-match HEAD^1
fatal: no tag exactly matches 'f8a1d888151809ccfbb3460bbcf310541967e292'
128
However, using git tag
will not trigger a commit
hook.