Chapter 13
Advanced uses of Mercurial Queues

While it’s easy to pick up straightforward uses of Mercurial Queues, use of a little discipline and some of MQ’s less frequently used capabilities makes it possible to work in complicated development environments.

In this chapter, I will use as an example a technique I have used to manage the development of an Infiniband device driver for the Linux kernel. The driver in question is large (at least as drivers go), with 25,000 lines of code spread across 35 source files. It is maintained by a small team of developers.

While much of the material in this chapter is specific to Linux, the same principles apply to any code base for which you’re not the primary owner, and upon which you need to do a lot of development.

13.1 The problem of many targets

The Linux kernel changes rapidly, and has never been internally stable; developers frequently make drastic changes between releases. This means that a version of the driver that works well with a particular released version of the kernel will not even compile correctly against, typically, any other version.

To maintain a driver, we have to keep a number of distinct versions of Linux in mind.

13.1.1 Tempting approaches that don’t work well

There are two “standard” ways to maintain a piece of software that has to target many different environments.

The first is to maintain a number of branches, each intended for a single target. The trouble with this approach is that you must maintain iron discipline in the flow of changes between repositories. A new feature or bug fix must start life in a “pristine” repository, then percolate out to every backport repository. Backport changes are more limited in the branches they should propagate to; a backport change that is applied to a branch where it doesn’t belong will probably stop the driver from compiling.

The second is to maintain a single source tree filled with conditional statements that turn chunks of code on or off depending on the intended target. Because these “ifdefs” are not allowed in the Linux kernel tree, a manual or automatic process must be followed to strip them out and yield a clean tree. A code base maintained in this fashion rapidly becomes a rat’s nest of conditional blocks that are difficult to understand and maintain.

Neither of these approaches is well suited to a situation where you don’t “own” the canonical copy of a source tree. In the case of a Linux driver that is distributed with the standard kernel, Linus’s tree contains the copy of the code that will be treated by the world as canonical. The upstream version of “my” driver can be modified by people I don’t know, without me even finding out about it until after the changes show up in Linus’s tree.

These approaches have the added weakness of making it difficult to generate well-formed patches to submit upstream.

In principle, Mercurial Queues seems like a good candidate to manage a development scenario such as the above. While this is indeed the case, MQ contains a few added features that make the job more pleasant.

13.2 Conditionally applying patches with guards

Perhaps the best way to maintain sanity with so many targets is to be able to choose specific patches to apply for a given situation. MQ provides a feature called “guards” (which originates with quilt’s guards command) that does just this. To start off, let’s create a simple repository for experimenting in.

1  $ hg qinit
2  $ hg qnew hello.patch
3  $ echo hello > hello
4  $ hg add hello
5  $ hg qrefresh
6  $ hg qnew goodbye.patch
7  $ echo goodbye > goodbye
8  $ hg add goodbye
9  $ hg qrefresh

This gives us a tiny repository that contains two patches that don’t have any dependencies on each other, because they touch different files.

The idea behind conditional application is that you can “tag” a patch with a guard, which is simply a text string of your choosing, then tell MQ to select specific guards to use when applying patches. MQ will then either apply, or skip over, a guarded patch, depending on the guards that you have selected.

A patch can have an arbitrary number of guards; each one is positive (“apply this patch if this guard is selected”) or negative (“skip this patch if this guard is selected”). A patch with no guards is always applied.

13.3 Controlling the guards on a patch

The hg qguard” command lets you determine which guards should apply to a patch, or display the guards that are already in effect. Without any arguments, it displays the guards on the current topmost patch.

1  $ hg qguard
2  goodbye.patch: unguarded

To set a positive guard on a patch, prefix the name of the guard with a “+”.

1  $ hg qguard +foo
2  $ hg qguard
3  goodbye.patch: +foo

To set a negative guard on a patch, prefix the name of the guard with a “-”.

1  $ hg qguard hello.patch -quux
2  $ hg qguard hello.patch
3  hello.patch: -quux
Note: The hg qguard” command sets the guards on a patch; it doesn’t modify them. What this means is that if you run hg qguard +a +b” on a patch, then hg qguard +c” on the same patch, the only guard that will be set on it afterwards is +c.

Mercurial stores guards in the series file; the form in which they are stored is easy both to understand and to edit by hand. (In other words, you don’t have to use the hg qguard” command if you don’t want to; it’s okay to simply edit the series file.)

1  $ cat .hg/patches/series
2  hello.patch #-quux
3  goodbye.patch #+foo

13.4 Selecting the guards to use

The hg qselect” command determines which guards are active at a given time. The effect of this is to determine which patches MQ will apply the next time you run hg qpush”. It has no other effect; in particular, it doesn’t do anything to patches that are already applied.

With no arguments, the hg qselect” command lists the guards currently in effect, one per line of output. Each argument is treated as the name of a guard to apply.

1  $ hg qpop -a
2  Patch queue now empty
3  $ hg qselect
4  no active guards
5  $ hg qselect foo
6  number of unguarded, unapplied patches has changed from 1 to 2
7  $ hg qselect
8  foo

In case you’re interested, the currently selected guards are stored in the guards file.

1  $ cat .hg/patches/guards
2  foo

We can see the effect the selected guards have when we run hg qpush”.

1  $ hg qpush -a
2  applying hello.patch
3  applying goodbye.patch
4  Now at: goodbye.patch

A guard cannot start with a “+” or “-” character. The name of a guard must not contain white space, but most othter characters are acceptable. If you try to use a guard with an invalid name, MQ will complain:

1  $ hg qselect +foo
2  abort: guard '+foo' starts with invalid character: '+'

Changing the selected guards changes the patches that are applied.

1  $ hg qselect quux
2  number of guarded, applied patches has changed from 0 to 2
3  $ hg qpop -a
4  Patch queue now empty
5  $ hg qpush -a
6  patch series already fully applied

You can see in the example below that negative guards take precedence over positive guards.

1  $ hg qselect foo bar
2  number of unguarded, unapplied patches has changed from 0 to 2
3  $ hg qpop -a
4  no patches applied
5  $ hg qpush -a
6  applying hello.patch
7  applying goodbye.patch
8  Now at: goodbye.patch

13.5 MQ’s rules for applying patches

The rules that MQ uses when deciding whether to apply a patch are as follows.

13.6 Trimming the work environment

In working on the device driver I mentioned earlier, I don’t apply the patches to a normal Linux kernel tree. Instead, I use a repository that contains only a snapshot of the source files and headers that are relevant to Infiniband development. This repository is 1% the size of a kernel repository, so it’s easier to work with.

I then choose a “base” version on top of which the patches are applied. This is a snapshot of the Linux kernel tree as of a revision of my choosing. When I take the snapshot, I record the changeset ID from the kernel repository in the commit message. Since the snapshot preserves the “shape” and content of the relevant parts of the kernel tree, I can apply my patches on top of either my tiny repository or a normal kernel tree.

Normally, the base tree atop which the patches apply should be a snapshot of a very recent upstream tree. This best facilitates the development of patches that can easily be submitted upstream with few or no modifications.

13.7 Dividing up the series file

I categorise the patches in the series file into a number of logical groups. Each section of like patches begins with a block of comments that describes the purpose of the patches that follow.

The sequence of patch groups that I maintain follows. The ordering of these groups is important; I’ll describe why after I introduce the groups.

Now to return to the reasons for ordering groups of patches in this way. We would like the lowest patches in the stack to be as stable as possible, so that we will not need to rework higher patches due to changes in context. Putting patches that will never be changed first in the series file serves this purpose.

We would also like the patches that we know we’ll need to modify to be applied on top of a source tree that resembles the upstream tree as closely as possible. This is why we keep accepted patches around for a while.

The “backport” and “do not ship” patches float at the end of the series file. The backport patches must be applied on top of all other patches, and the “do not ship” patches might as well stay out of harm’s way.

13.8 Maintaining the patch series

In my work, I use a number of guards to control which patches are to be applied.

This variety of guards gives me considerable flexibility in qdetermining what kind of source tree I want to end up with. For most situations, the selection of appropriate guards is automated during the build process, but I can manually tune the guards to use for less common circumstances.

13.8.1 The art of writing backport patches

Using MQ, writing a backport patch is a simple process. All such a patch has to do is modify a piece of code that uses a kernel feature not present in the older version of the kernel, so that the driver continues to work correctly under that older version.

A useful goal when writing a good backport patch is to make your code look as if it was written for the older version of the kernel you’re targeting. The less obtrusive the patch, the easier it will be to understand and maintain. If you’re writing a collection of backport patches to avoid the “rat’s nest” effect of lots of #ifdefs (hunks of source code that are only used conditionally) in your code, don’t introduce version-dependent #ifdefs into the patches. Instead, write several patches, each of which makes unconditional changes, and control their application using guards.

There are two reasons to divide backport patches into a distinct group, away from the “regular” patches whose effects they modify. The first is that intermingling the two makes it more difficult to use a tool like the patchbomb extension to automate the process of submitting the patches to an upstream maintainer. The second is that a backport patch could perturb the context in which a subsequent regular patch is applied, making it impossible to apply the regular patch cleanly without the earlier backport patch already being applied.

13.9 Useful tips for developing with MQ

13.9.1 Organising patches in directories

If you’re working on a substantial project with MQ, it’s not difficult to accumulate a large number of patches. For example, I have one patch repository that contains over 250 patches.

If you can group these patches into separate logical categories, you can if you like store them in different directories; MQ has no problems with patch names that contain path separators.

13.9.2 Viewing the history of a patch

If you’re developing a set of patches over a long time, it’s a good idea to maintain them in a repository, as discussed in section 12.11. If you do so, you’ll quickly discover that using the hg diff” command to look at the history of changes to a patch is unworkable. This is in part because you’re looking at the second derivative of the real code (a diff of a diff), but also because MQ adds noise to the process by modifying time stamps and directory names when it updates a patch.

However, you can use the extdiff extension, which is bundled with Mercurial, to turn a diff of two versions of a patch into something readable. To do this, you will need a third-party package called patchutils [Wau]. This provides a command named interdiff, which shows the differences between two diffs as a diff. Used on two versions of the same diff, it generates a diff that represents the diff from the first to the second version.

You can enable the extdiff extension in the usual way, by adding a line to the [extensions] section of your hgrc.

1  [extensions]
2  extdiff =

The interdiff command expects to be passed the names of two files, but the extdiff extension passes the program it runs a pair of directories, each of which can contain an arbitrary number of files. We thus need a small program that will run interdiff on each pair of files in these two directories. This program is available as hg-interdiff in the examples directory of the source code repository that accompanies this book.

1  #!/usr/bin/env python
2  #
3  # Adapter for using interdiff with mercurial's extdiff extension.
4  #
5  # Copyright 2006 Bryan O'Sullivan <bos@serpentine.com>
6  #
7  # This software may be used and distributed according to the terms of
8  # the GNU General Public License, incorporated herein by reference.
9  
10  import os, sys
11  
12  def walk(base):
13      # yield all non-directories below the base path.
14      for root, dirs, files in os.walk(base):
15          for f in files:
16              path = os.path.join(root, f)
17              yield path[len(base)+1:], path
18      else:
19          if os.path.isfile(base):
20              yield '', base
21  
22  # create list of unique file names under both directories.
23  files = dict(walk(sys.argv[1]))
24  files.update(walk(sys.argv[2]))
25  files = files.keys()
26  files.sort()
27  
28  def name(base, f):
29      if f:
30          path = os.path.join(base, f)
31      else:
32          path = base
33      # interdiff requires two files; use /dev/null if one is missing.
34      if os.path.exists(path):
35          return path
36      return '/dev/null'
37  
38  ret = 0
39  
40  for f in files:
41      if os.system('interdiff "%s" "%s"' % (name(sys.argv[1], f),
42                                            name(sys.argv[2], f))):
43          ret = 1
44  
45  sys.exit(ret)

With the hg-interdiff program in your shell’s search path, you can run it as follows, from inside an MQ patch directory:

1  hg extdiff -p hg-interdiff -r A:B my-change.patch

Since you’ll probably want to use this long-winded command a lot, you can get hgext to make it available as a normal Mercurial command, again by editing your hgrc.

1  [extdiff]
2  cmd.interdiff = hg-interdiff

This directs hgext to make an interdiff command available, so you can now shorten the previous invocation of hg extdiff” to something a little more wieldy.

1  hg interdiff -r A:B my-change.patch
Note: The interdiff command works well only if the underlying files against which versions of a patch are generated remain the same. If you create a patch, modify the underlying files, and then regenerate the patch, interdiff may not produce useful output.

The extdiff extension is useful for more than merely improving the presentation of MQ patches. To read more about it, go to section 14.2.