mercredi 25 septembre 2013

displaying Gerrit review history in git log

Over the last few weeks, several people complained I should share my tip'n tricks with more people. They are apparently worth a post.  I am giving it a try there to show you how to make available in your git cli the Gerrit review informations. I will start with some context then spurt magical commands to let you make it happen on your own computer.

In the beginning of 2012, the Wikimedia Foundation switched from Subversion to Git. The main reason for the switch was to prevent committers from inserting bad code in the central repository. Thus lowering the stress when deploying said code on the production site.  The only way to do it is to elect a gatekeeper between patch authors and the reference repository used for deployment. In Linux, that is more or less the role of Linus Torvalds. At Wikimedia we selected Gerrit which is used at Google to accept patches for their Android platform.

The Gerrit workflow is pretty simple: authors send their patches to Gerrit, he and others comment or amend the patch, rinse and repeat until there is no more to say. After a few iterations, the community ends up with a robust patch which is worth entering the reference repository. A few people are allowed to approve the change and have it actually merged.

The history that led to the final commit is available (example for a patch I wrote: https://gerrit.wikimedia.org/r/75569) and is very valuable whenever a bug appear: the comments can give you an idea of the design choices and list out every single person that get involved in the patch. If the original author is not available, you can most probably reach out to the person that review the code. When you are facing an issue in production, the more people to ask the merrier.

It is too cumbersome to have to open the browser in the Gerrit web interface. Aging, I rely more and more on terminals and command lines. How would I get the commit history from the command line without having to parse some Gerrit json? Luckily, there is a way to get all the reviewing history saved locally and thus available offline.

Internally, Gerrit relies a lot upon git, its internal configuration for a given repository is even available (under refs/meta/config). The review history for a repository is thus stored in Git as well and it lives under refs/notes.

You can thus fetch all the review history:
git fetch -v origin refs/notes/*:refs/notes/*
Then configure your repository to always show the notes in git log:
git config --local notes.displayRef refs/notes/review
End result:

$ git log -n1 1dad0218
commit 1dad021840bf936b0e130ddda3459d11f0ad8421
Author: Antoine Musso 
Date:   Wed Jul 24 12:08:52 2013 +0200

    SpecialPrefixindex formatting methods are now protected
    
    namespacePrefixForm() and showPrefixChunk() accepted additional
    arguments to slightly alter the form such as hiding redirects.  The
    argument has been removed in favor of class property just like
    stripprefix introduced earlier and the method have been made protected
    since there is no point in calling them out of the special page scope.
    
    Change-Id: I55728fd2634f8a935a033052dcce3c7247cb1aa3

Notes (review):
    Verified+2: jenkins-bot
    Code-Review+2: Matmarex 
    Submitted-by: jenkins-bot
    Submitted-at: Fri, 30 Aug 2013 19:41:21 +0000
    Reviewed-on: https://gerrit.wikimedia.org/r/75569
    Project: mediawiki/core
    Branch: refs/heads/master


On this patchset, we can see that Matmarex has accepted the change :-) You can prevent notes from displaying whenever you like by passing --no-notes to the git log command. Note: you will have to fetch the notes from time to time since they are not fetched by default. An alias would be useful:
git config --global alias.fetchreviews \ 'fetch -v gerrit refs/notes/review:refs/notes/review'
You can then refresh notes by nvoking git fetchreviews.

Summary:

* Get notes: git fetch -v origin refs/notes/review:refs/notes/review
* Configure git to always show them: git config --local notes.displayRef refs/notes/review