Merging and branching in Subversion 1.5

Upcoming release features automatic merge tracking -- finally!

1 2 3 4 Page 4
Page 4 of 4

Merge conflicts

There are of course times when even the most automated of processes gets stuck, and Subversion merges are no exception. When you merge changes in from another branch, there is always the possibility that someone else has modified a section of code that you have also been working on. In this case, Subversion wisely lets you decide what version is correct. This is known as a merge conflict.

Merge conflicts are handled more smoothly in Subversion 1.5 than in previous versions, making it easier to fix minor conflicts on the fly, rather than having to abandon the merge process. When a conflict occurs, Subversion proposes a number of options, including visualising the differences via the diff command, opening the conflicting file in a text editor, or postponing the correction (and thus the merge) to a later date. In Listing 11 we use the d option to view the differences in the two versions.

Listing 11. What's the difference?

> svn merge
Select: (p)ostpone, (d)iff, (e)dit, (h)elp for more options : d

--- C:\DOCUME~1\john\LOCALS~1\Temp/tmp  Mon Dec 31 14:38:32 2007
+++ tax-calculator/src/com/jpt/taxcalculator/domain/.svn/tmp/tempfile
.9.tmp  Mon Dec 31 14:38:32 2007
@@ -1,8 +1,12 @@
 package com.jpt.taxcalculator.domain;

+<<<<<<< .working
 /**
  * A Tax rate.
+ * More stuff.
  */
+=======
+>>>>>>> .merge-right.r23
 public class TaxRate {

     private double minimumRevenue;
Select: (p)ostpone, (d)iff, (e)dit, (r)esolved, (h)elp for more options :

Once you've either visualised the modifications using the (d)iff option, or edited the conflicting file via the (e)dit option, a new option appears: (r)esolved. When you choose this option, Subversion will consider the conflict fixed, and move on to other things. Other convenient short-cuts are also available for more quick fixes, including (m)ine ("accept my version of file") and (t)heirs ("accept their version of file").

The svn resolved method has also been improved to make resolving conflicting files easier.Subversion 1.5 has added the --accept option, which makes it easy to resolve a conflicting file and specify the file you wish to keep without resorting to file copying or renaming. The --accept option takes intuitive values such as mine (keep my file), theirs (use the other version of the file) or base (use the base version of this file in the local working copy). This is especially useful for binary files, as illustrated here.

Listing 12. --accept makes it easier to resolve merge conflicts

> svn resolved src/web/images/logo.gif --accept=mine

Using Subversion 1.5 in Eclipse

Of course, not everyone likes using the command line. The new version of Subversion also comes well integrated with Eclipse, including a new merge feature built on top of the Subclipse plugin. See the Resources section to install this new plugin.

Once you've installed the plugin, you can access the new merge client via the Team->CollabNet Merge menu. This opens a wizard-like interface that guides you through your merge in a fairly intuitive way. You can either opt for the standard merge interface, which keeps things simple, or the advanced interface, which gives you access to all the command-line options. This is a big improvement on the previous Subclipse merge menu, letting you perform common merges with virtually nothing but the source branch of your merge. The merge wizard is shown in Figure 3.

The new Subclipse merge wizard
Figure 3. The new Subclipse merge wizard (click to enlarge)

This wizard guides you through the merge process. If there are any conflicting files, Eclipse proposes a number of options. You can choose to resolve the conflict directly or edit the file either manually or using a graphical conflict resolution editor, as shown in Figure 4.

The graphical conflict resolution editor
Figure 4. The graphical conflict resolution editor (click to enlarge)

Not surprisingly, the History view (Team->History) now lets you visualise information about previous merges. You need to activate the option "Include merged revisions" to display merge history, as shown in Figure 5.

Selecting 'Include merged revisions' in the editor
Figure 5. Activating merge history (click to enlarge)

Once you have activated merge history, you can also view details of a particular merge by selecting "Show Merged Revisions" in the contextual menu, as shown in Figure 6.

The Show Merge Revisions screen
Figure 6. The Show Merge Revisions screen (click to enlarge)

In conclusion

Subversion is a popular version control system that promises to overcome one of its main drawbacks with its upcoming release, Subversion 1.5. In this article you've seen how Subversion 1.5 makes it easier to handle branching and merging in your development projects. With Subversion 1.5, you can automatically keep track of the various merges that happen in your project. Merging changes from one branch to another is both smoother and more reliable. This in turn makes it easier to use branches and merges efficiently in your development process.

It's important to note that Subversion 1.5 is still in the final stages of development at the time of writing -- there are still a few wrinkles to be ironed out, and using it in production in its current form is not recommended. The final release date for Subversion 1.5 has yet to be announced.

Also note that merge tracking isn't the only major new feature added to Subversion 1.5. The new version also adds change lists and sparse directories to Subversion's feature set. Merge tracking is likely to be the major selling point for Subversion 1.5, however.

See the Resources section to learn more about Subversion and other open source version control systems.

John Ferguson Smart has been involved in the IT industry since 1991, and in Java enterprise development since 1999. His specialties are Java EE architecture and development and IT project management, including offshore project management. He has a wide experience in open source Java technologies. He has worked on many large-scale JEE projects for government and business in both hemispheres, involving international and offshore teams, and also writes technical articles in the JEE field. His technical blog can be found at http://www.jroller.com/page/wakaleo.

Learn more about this topic

1 2 3 4 Page 4
Page 4 of 4