James Mead by James Mead

Building a Git repository of Ruby method definitions

Follows on from my earlier article: Tracing the Git history of a Ruby method (11 Mar 2014)

As Chris mentioned in the notes from last week, I’ve come up with an idea for a useful way to represent the history of Ruby method definitions.

I’ve hacked together a script to walk through all the commits in a repository extracting all the method definitions using the Ruby parser code I already had. The script builds a new “shadow” repository with a commit for each commit in the original repository, but constructing a “file” for each method definition.

Thus the script turns the method definition for Foo::Bar#baz into the “file” Foo/Bar/#baz.rb and the one for Foo.bar into Foo/.bar.rb. I know this sounds crazy, but by doing this it means that a number of Git tools just work and give interesting results.

I’ve run the script against the Mocha repository so you can see what I mean. If you click the “Show Diff Stats” button on this commit page then you can see which methods have been added, modified & changed. You can even see methods that have been moved and/or renamed!

Renamed methods in GitHub commit file list

You can also see the history of an individual method on GitHub or the patch version using git log --patch on your local machine. This is actually the same functionality as method_log itself already provides, but it solves the speed problem that I mentioned in my earlier article.

Git log showing changes to method implementation

I think it would be trivial to run the script against a repository and then add an after-commit hook to update the “shadow” repository after each commit to keep it up-to-date. It would also be possible to link the commits in the “shadow” repository to those in the original repository.

Anyway, hopefully that’s some food for thought. I’d love to hear what you think.

If you have any feedback on this article, please get in touch!

Historical comments can be found here.