Git smudge filter

git

I am trying to setup a filter so that on checkout it will inject some git related information to my file and on commit it will remove this information. I have verified that when I commit the file it is properly cleaned. Also if I delete the file and then do a checkout the file is properly smudged.

My problem is the following. If I make a change to the file and I commit the change, the smudge filter does not run. Is this the expected behavior? Is there a way to have the smudge filter run on commit? (perhaps by having a commit followed immediately by a checkout?)

Best Answer

A clean/smudge filter is definitely not the place for reliably adding commit based information into a work tree file. The reason - as you've discovered - is that the file won't be touched in operations that don't have to write a new version of the file into the work tree. You can merge or fast-forward many commits without the filter being applied if the given file hasn't been touched by those commits.

You would probably be better off with a template file that can be transformed to an ignored real file with embedded commit information by a build step which uses something like git rev-parse HEAD or git describe to retrieve the current commit information.