Kash Farooq's software development blog

.NET Developer

Conflicts in Bazaar Source Control

Posted by Kash Farooq on October 5, 2009

In merging files with Bazaar Source Control I made sure I didn’t get any conflicts by adding the two methods strategically! Of course, in real life we often will get conflicts. This blog post will demonstrate how to deal with them.

Here’s the example I’m going to demonstrate:

  1. Two people create a personal branch from a shared repository.
  2. Person A adds changes MyMethod in SystemUnderTest.cs.
  3. Person B also changes MyMethod in a conflicting way.
  4. Person A commits and pushes to the shared repository.
  5. Person B then tries to push to the shared repository.

This will result in a text conflict.

Setting up

PersonA and PersonB make local branches of the shared repository:

C:\Dev\bazaar_branches\PersonA>bzr branch \myserver\shared_bzr_repo MyBranch
cd. ..\PersonB
C:\Dev\bazaar_branches\PersonB>bzr branch \myserver\shared_bzr_repo MyBranch

First, one developer updates a method…

The method that both developers will change:

public void MyMethod() {
  smtpClient.Send(new MailMessage {Subject = "Hello!"});

First, PersonA changes the method:

public void MyMethod() {
  smtpClient.Send(new MailMessage {Subject = "Person A says hi!"});

Now PersonA commits and pushes back to the shared location:

C:\Dev\PersonA\Shared>bzr status

C:\Dev\PersonA\Shared>bzr commit
Committing to: C:/Dev/PersonA/Shared/
modified SystemUnderTest.cs
Committed revision 2.

C:\Dev\PersonA\Shared>bzr push \myserver\shared_bzr_repo
All changes applied successfully.
Pushed up to revision 2.

Introducing a conflict

PersonB changes the same method:

public void MyMethod() {
  smtpClient.Send(new MailMessage {Subject = "Person B says hello there!"});

Now, after commiting and then attempting to push back to the shared repository, the following message is given:

C:\Dev\PersonB\Shared>bzr push \myserver\shared_bzr_repo
bzr: ERROR: These branches have diverged.  See "bzr help diverged-branches" for more information.

Let’s try to pull the changes down:

C:\Dev\PersonB\Shared>bzr pull
bzr: ERROR: These branches have diverged. Use the missing command to see how.
Use the merge command to reconcile them.

Okay, let’s do a merge:

C:\Dev\PersonB\Shared>bzr merge
Merging from remembered parent location //myserver/shared_bzr_repo
 M  SystemUnderTest.cs
Text conflict in SystemUnderTest.cs
1 conflicts encountered.

Examining the conflict

Let’s look at the list of conflicts:

C:\Dev\PersonB\Shared>bzr conflicts
Text conflict in SystemUnderTest.cs

So, we have a text conflict. If we look at the directory, we can see the following files:

C:\Dev\PersonB\Shared>dir /b

Some definitions:

  1. The .BASE file: This is the original file without PersonA or PersonB’s (“Hello!”)
  2. The OTHER file: This contains PersonA’s changes (“Person A says hi!”)
  3. The THIS file: This contains PersonB’s changes (“Person B says hello there!”)
  4. The .cs file: See below.

Opening SystemUnderTest.cs shows us the location of the conflicts and the differences between PersonA and PersonB’s changes:

public void MethodUnderTest() {
<<<<<<< TREE
  smtpClient.Send(new MailMessage {Subject = "Hello there"});
  smtpClient.Send(new MailMessage {Subject = "Hi"});

You may recognise the above format, along with the .OTHER and .THIS files, from other source control systems. Basically, this means you can use a number of tools to deal with the conflict.

Dealing with conflict

At this point you have a manual step. You can:

  1. Option 1: Edit SystemUnderTest.cs by hand.
  2. Option 2: Fire up a merge tool to help you deal with the conflicts.

Whichever method you choose, after you have completed this task, you need to tell Bazaar and then you can commit and push back to the shared repository:

bzr resolve SystemUnderTest.cs
bzr commit
bzr push

Sorry, the comment form is closed at this time.

%d bloggers like this: