2

I'm using git for writing some LaTeX packages. To be able to make use of LaTeX release features, a string of the following format needs to be included in each package:

\ProvidesPackage{MyPackageName}[yyyy/mm/dd VersionNumber SomeText]

I often forget to update the date and version number before I upload the files to CTAN and was wondering if git can be used to automate this.

I am looking for a method to:

  1. automatically insert the current date into the source code upon every commit for only the files changed in this commit

  2. automatically insert the current date and version number into all files when I tag a version (the tag name is the same as the version number)

1

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:

  1. 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)
  2. 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:

  1. Get the latest tag / version name: git describe --tags $(git rev-list --tags --max-count=1)
  2. Format the files using sed (or awk or whichever tool you choose)
  3. 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.

| improve this answer | |
  • Fantastic answer with very good explanation! Thanks a lot! – aloneprism Oct 22 '18 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.