Mercurial_ The Definitive Guide - Bryan O'Sullivan [109]
$ hg extdiff
--- a.a6ba002523c0/myfile 2009-05-05 06:44:37.769474821 +0000
+++ /tmp/extdiffXbMA0x/a/myfile 2009-05-05 06:44:37.677474721 +0000
@@ -1 +1,2 @@
The first line.
+The second line.
The result won’t be exactly the same as with the built-in hg diff variations, because the output of diff varies from one system to another, even when passed the same options.
As the “making snapshot” lines of output above imply, the extdiff command works by creating two snapshots of your source tree. The first snapshot is of the source revision; the second, of the target revision or working directory. The extdiff command generates these snapshots in a temporary directory, passes the name of each directory to an external diff viewer, then deletes the temporary directory. For efficiency, it only snapshots the directories and files that have changed between the two revisions.
Snapshot directory names have the same base name as your repository. If your repository path is /quux/bar/foo, then foo will be the name of each snapshot directory. Each snapshot directory name has its changeset ID appended, if appropriate. If a snapshot is of revision a631aca1083f, the directory will be named foo.a631aca1083f. A snapshot of the working directory won’t have a changeset ID appended, so it would just be foo in this example. To see what this looks like in practice, look again at the extdiff example above. Notice that the diff has the snapshot directory names embedded in its header.
The extdiff command accepts two important options. The hg -p option lets you choose a program to view differences with, instead of diff. With the hg -o option, you can change the options that extdiff passes to the program (by default, these options are -Npru, which only make sense if you’re running diff). In other respects, the extdiff command acts similarly to the built-in hg diff command: you use the same option names, syntax, and arguments to specify the revisions you want, the files you want, and so on.
As an example, here’s how to run the normal system diff command, getting it to generate context diffs (using the -c option) instead of unified diffs, and five lines of context instead of the default three (passing 5 as the argument to the -C option).
$ hg extdiff -o -NprcC5
*** a.a6ba002523c0/myfile Tue May 5 06:44:37 2009
--- /tmp/extdiffXbMA0x/a/myfile Tue May 5 06:44:37 2009
***************
*** 1 ****
--- 1,2 ----
The first line.
+ The second line.
Launching a visual diff tool is just as easy. Here’s how to launch the kdiff3 viewer.
hg extdiff -p kdiff3 -o
If your diff viewing command can’t deal with directories, you can easily work around this with a little scripting. For an example of such scripting in action with the mq extension and the interdiff command, see Viewing the History of a Patch.
Defining Command Aliases
It can be cumbersome to remember the options to both the extdiff command and the diff viewer you want to use, so the extdiff extension lets you define new commands that will invoke your diff viewer with exactly the right options.
All you need to do is edit your ~/.hgrc, and add a section named extdiff. Inside this section, you can define multiple commands. Here’s how to add a kdiff3 command. Once you’ve defined this, you can type hg kdiff3 and the extdiff extension will run kdiff3 for you.
[extdiff]
cmd.kdiff3 =
If you leave the right-hand side of the definition empty, as above, the extdiff extension uses the name of the command you defined as the name of the external program to run. But these names don’t have to be the same. Here, we define a command named hg wibble, which runs kdiff3.
[extdiff]
cmd.wibble = kdiff3
You can also specify the default options that you want to invoke your diff viewing program with. The prefix to use is opts., followed by the name of the command to which the options apply. This example defines a command hg vimdiff that runs the