Reputation: 230
I have a git repository including about a hundred .cpp and .h files. Each file has a short header including the license data. I would like to extend this part with a few lines of metadata to show the date when the specific file has been created and modified last time.
Are there good practices to manage this automatically without conflicts with git?
Upvotes: 1
Views: 194
Reputation: 94511
Use git filters. Git Book has an example that does exactly that. See https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes, starting at "Another interesting example gets $Date$ keyword expansion, RCS style.".
There are two filters, one to filter data before adding it to commit (clean filter) and one to checkout the file from commit to the worktree (smudge filter). Get the scripts and configure the filters:
$ git config filter.dater.clean clean_date
$ git config filter.dater.smudge expand_date
Upvotes: 1
Reputation: 311508
This can be done with a git hook.
Suppose all your files follow a template. E.g.:
/**
* Some license information
*
* Last update: Thu, Jun 28, 2018 8:19:47 PM
*/
// Code comes here...
Create an executable file .git/hooks/pre-commit
with a simple script to replace the "Last update:" line:
#!/bin/sh
d=$(date)
git diff --cached --name-only --diff-filter=ACM | \
xargs sed -i "s/\* Last update:.*/* Last update: $d/"
Upvotes: 0