GIT smudge filter on branch switch: How to get log from "new" branch?

305 Views Asked by At

I am using a smudge filter to expand keywords (e.g. Commit Hash, Author, Date). The script is written in python and triggers "git log" in a subprocess to retrieve the information. Everything works fine if I delete a file and check it out again.

But when I am switching branches, the information expanded into the files is wrong. I figured out that the smudge script runs before the HEAD switches to the branch I want to check out, which makes sense to me. So in that case "git log" will give me the information of the branch I am coming from and not the branch I am switching to.

How can I know which branch is to be checked out in a smudge script? Or how do I retrieve information from the branch I am switching to?

2

There are 2 best solutions below

1
bk2204 On

Neither the commit hash nor the ref being checked out are available to smudge and clean filters. It could possibly be passed in the new filter.<driver>.process form as an attribute, but nobody has implemented that in Git yet.

If you want to implement just the commit hash, you can specify the ident attribute in the .gitattributes file in your repository, like so:

*.c ident

and then you can write $Id$ in your code and it will be expanded automatically. That's the only thing that's possible in this case.

1
jthill On

The information you're after is available in the post-checkout hook, but the expansions you're doing are typical of deployment work, for which git archive | tar Cx is more appropriate and allows selective keyword expansion and more via the export-subst and export-ignore attributes. The problem with making Git ~just a little more friendly~ for deployment tasks is, it never ends. For development work, Git simply doesn't have the limitations that prompted the facility; as you've noticed everything you need is available from git log, since you've got the actual history right there anyway, and you're making a stale duplicate for which the best you can hope is it's not out of date.