File Merge: How did we get ourselves into this mess

[article]

and Joe can simply declare the merge to be resolved without actually doing anything.  This decision remains subject to Joe's judgment.

However, most of the time it will be necessary for the merge to actually happen.  In these cases, Joe has the following options: 

  • Attempt to automerge  
  • Use a visual merge tool  
  • Redo one set of changes by hand

Each of these will be explained further in the sections below.

Attempt to automerge
As I mentioned above, a surprising number of cases can be easily handled automatically.  Most source control tools include the ability to attempt an automatic merge.  The algorithm uses all three of the involved versions of the file and attempts to safely produce a merged version.

Best Practice: Only use "automerge on get"

It is widely accepted that SCM tools should only attempt automerge on the "get" of a file. In other words, when Joe realizes that he must merge in the changes Jane made between version 4 and version 5, he will tell his SCM client application to "get" version 5 and attempt to automatically merge it into his working file.  CVS, Subversion and Vault all function in this manner.

Unfortunately, SourceSafe attempts to "automerge on checkin". This is just a really bad idea. When Joe tries to checkin his changes, SourceSafe attempts the automerge.  If it believes that it has succeeded, then his changes are checked in and version 6 was created.  However, it is possible that Joe never examined version 6, or even compiled it.  The repository now contains a file which has never existed in the working folder of any developer on earth.  Its contents have never been seen by human eyes, and it has never been run through a compiler.  Automerge is safe, but it's not   that safe.

It is much better to "automerge on get". This way, the developer can (and should) examine the file after the automerge has happened. This simple change makes it easier to trust automerge.  Instead of trying to do the developer's job, automerge simply becomes a tool which the developer can use to get his job done faster.

The reason that automerge is so safe in practice is that the algorithm is extremely conservative.  Automerge will refuse to produce a merged version if Joe's changes and Jane's changes appear to be in conflict.  In the most obvious case, if Joe and Jane both modified the same line, automerge will detect this "conflict" and refuse to proceed.  In other cases, automerge may fail with conflicts if two changes are too close to each other.

Use a visual merge tool
In cases where automerge cannot automatically resolve conflicts, we can use a visual merge tool to make the job easier.  These tools provide a visual display which shows all three files and highlights exactly what has changed.  This makes it much easier for the developer to perform the merge, since she can zero in on the conflicts very quickly.

There are several excellent visual merge tools available, including Guiffy and  Araxis Merge .  The following screen dump is from "SourceGear DiffMerge", the visual merge tool which is included with Vault.  (Please note sometimes I have to reduce the size of screen dumps to make them fit.  In those cases, you can click on the image to see it at full resolution).

This picture is typical of other three-way visual merge applications.  The left pane shows Jane's version of the file.  The right pane shows Joe's version.  The center pane shows the original file, the common ancestor from which they both started to make changes.  As you

About the author

TechWell Contributor's picture TechWell Contributor

The opinions and positions expressed within these guest posts are those of the author alone and do not represent those of the TechWell Community Sites. Guest authors represent that they have the right to distribute this content and that such content is not violating the legal rights of others. If you would like to contribute content to a TechWell Community Site, email editors@techwell.com.

AgileConnection is one of the growing communities of the TechWell network.

Featuring fresh, insightful stories, TechWell.com is the place to go for what is happening in software development and delivery.  Join the conversation now!