Saturday, August 20, 2011

git notes

Now git looks even more powerful in my eyes. git-notes gives you the ability to attach some information to the commits. When I came upon this when searching blindly on Google, I thought, "Wow, this is damn cool. Exactly what I'm looking for!"

For the details of this command, see here on Pro Git or in the git manual. This post is about how I used it.

As it always does, git gives the command lots of flexibility. See it for yourself. But what I needed is simple. I don't care about all those feature. I just want "git notes add" and "git notes show", which will help me attach information to the HEAD and later retrieve it.

This is my use case. I'm converting conary repositories (which stores the conary source packages) into git repositories. And I want incremental conversion, i.e. when the conary repo gets new commits, I want to add them on top of the existing git repo, without generating it all once again.

The problem is, there is no way to tell the revisions of the conary packages from the git revisions (neither the other way around). The history in the git repo is identical with that in the conary repo. The commit messages are the same and the commit body are the same. But there is no way to tell which conary revision is which git revision.

Which means, I don't know the progress of the conversion, so I don't know where to resume when new commits come into the conary repo.

I thought about storing the progress in an external file, but that's fragile. What would the git repo was changed by someone else than my script? I thought about storing the conary revision in each of the git commit messages. But that would break my goal of keeping the history untouched.

Then blindly and without much hope, I searched for "git store extra info in commit" on google. The first result is some mailing list discussion. And someone mentioned "git notes". Voilà!

How I used it is actually simple. When one pass of conversion is done, I store the revision of each package that's been converted in a git note. And the next time, I just fetch the note and retrieve the revisions :-)

And I'm grateful that git doesn't complain when I store a 52KB note :)

The code of my script is available at github.