Mercurial_ The Definitive Guide - Bryan O'Sullivan [29]
If both changesets have modified a file, invoke an external merge program to choose the new contents for the merged file. This may require input from the user.
If one changeset has modified a file and the other has renamed or copied the file, make sure that the changes follow the new name of the file.
There are more details—merging has plenty of corner cases—but these are the most common choices that are involved in a merge. As you can see, most cases are completely automatic, and indeed most merges finish automatically, without requiring your input to resolve any conflicts.
When you’re thinking about what happens when you commit after a merge, once again the working directory is “the changeset I’m about to commit.” After the hg merge command completes, the working directory has two parents; these will become the parents of the new changeset.
Mercurial lets you perform multiple merges, but you must commit the results of each individual merge as you go. This is necessary because Mercurial only tracks two parents for both revisions and the working directory. While it would be technically feasible to merge multiple changesets at once, Mercurial avoids this for simplicity. With multi-way merges, the risks of user confusion, nasty conflict resolution, and making a terrible mess of a merge would grow intolerable.
Merging and Renames
A surprising number of revision control systems pay little or no attention to a file’s name over time. For instance, it used to be common that if a file got renamed on one side of a merge, the changes from the other side would be silently dropped.
Mercurial records metadata when you tell it to perform a rename or copy. It uses this metadata to do the right thing during a merge. For instance, if I rename a file, and you edit it without renaming it, when we merge our work the file will be renamed and have your edits applied.
Other Interesting Design Features
In the sections above, I’ve tried to highlight some of the most important aspects of Mercurial’s design, to illustrate that it pays careful attention to reliability and performance. However, the attention to detail doesn’t stop there. There are a number of other aspects of Mercurial’s construction that I personally find interesting. I’ll detail a few of them here, separate from the “big ticket” items above, so that if you’re interested, you can gain a better idea of the amount of thinking that goes into a well-designed system.
Clever Compression
When appropriate, Mercurial will store both snapshots and deltas in compressed form. It does this by always trying to compress a snapshot or delta, but only storing the compressed version if it’s smaller than the uncompressed version.
This means that Mercurial does “the right thing” when storing a file whose native form is compressed, such as a zip archive or a JPEG image. When these types of files are compressed a second time, the resulting file is usually bigger than the once-compressed form, and so Mercurial will store the plain zip or JPEG.
Deltas between revisions of a compressed file are usually larger than snapshots of the file, and Mercurial again does “the right thing” in these cases. It finds that such a delta exceeds the threshold at which it should store a complete snapshot of the file, so it stores the snapshot, again saving space compared to a naive delta-only approach.
Network recompression
When storing revisions on disk, Mercurial uses the “deflate” compression algorithm (the same one used by the popular zip archive format), which balances good speed with a respectable compression ratio. However, when transmitting revision data over a network connection, Mercurial uncompresses the compressed revision data.
If the connection is over HTTP, Mercurial recompresses the entire stream of data using a compression algorithm that gives a better compression ratio (the Burrows-Wheeler algorithm from the widely used bzip2 compression package). This