Mercurial's format source extensions

2018-12-03

Last Friday, Mozilla pushed a massive patch to their Firefox repository. This patch enforce their new coding style and automatically formatting of their source-code using clang.

This formatting implies a large amount of change spread all across the code base. This can result in a lot of conflict with all the other branches and in-progress code made before the formatting.

To address this issue, Mozilla has been using Mercurial's format-source extensions. This extension tracks the formatting that has been applied and the file they have been applied to. Mercurial can then leverage this information during merge to smooth the formatting out of the equation, focusing on the conflict from the actual edition.

How does it works?

To perform its duty, the format-source extension tracks formatting operation within the change history. To start formatting, one using the hg format-source commands, specifying the tools used for formatting and the affected files. The command then formats the target files and record the operation. Later, when merges happen, the extensions checks if formatting is different on each side of the merge. When one side of the merge has a more recent formatting, all content involved in the automated merging process will be pre-formatted using the appropriate tools. This formatting of all items (including the merge base) remove the formatting changes from the equation. The tool can then focus on solving conflicts on actual code changes, and provide readable conflict to users if they occur.

What next?

More projects are now considering using format-source and we are improving the extension to support their more diverse use-case. Once the extension has matured a solid support of a wide enough set of use case, we will work to getting the feature integrated in the main version of Mercurial.

The initial implementation of this extension was made by Octobus, last year, with a Mozilla funding. Thanks to Sylvestre Ledru for making it happen.