Merging files with Bazaar Source Control
Posted by Kash Farooq on September 28, 2009
This blog post is a more complex example of renaming files with Bazaar Source Control.
Here’s the example I’m going to demonstrate:
- Two people create a personal branch from a shared repository.
- Person A adds a method to MyTest.cs.
- Person B also adds a method to MyTest.cs, but also moves it to a new namespace folder called Tests, and, why not, Person B also renames the class and file.
- Person A commits and pushes to the shared repository.
- Person B then tries to push to the shared repository.
The above would really make SVN scream and give up. [I would be interested to hear how Git would handle the above situation, please send me your comments.]
Setting up
Let’s start with directory structure that will hold a shared repository and two directories representing two developers:
Directory of C:\Dev\bazaar_branches 28/09/2009 21:00 <DIR> . 28/09/2009 21:00 <DIR> .. 28/09/2009 20:58 <DIR> PersonA 28/09/2009 20:59 <DIR> PersonB 28/09/2009 21:00 <DIR> Shared
Initialize Shared as a Bazaar repository:
cd Shared bzr init
This results in the message:
Created a standalone tree (format: pack-0.92)
Now put some sample code to the Shared directory, add it, then commit it:
bzr add bzr commit
Which gives:
Committing to: C:/Dev/bazaar_branches/Shared/ added BazaarRenames added BazaarRenames/BazaarRename.csproj added BazaarRenames/UnitTests added BazaarRenames/Properties/AssemblyInfo.cs added BazaarRenames/UnitTests/MyTest.cs Committed revision 1.
Now PersonA and PersonB can make local branches of the shared repository:
cd ..\PersonA C:\Dev\bazaar_branches\PersonA>bzr branch ..\Shared MyBranch cd. ..\PersonB C:\Dev\bazaar_branches\PersonB>bzr branch ..\Shared MyBranch
Two developers update the same file
PersonA will now add a method to MyTest, commit it and then push it back to the Shared repository. I’ll call it PersonATest so we can see it clearly at the end of this blog post.
C:\Dev\bzr_branches\PersonA\MyBranch>bzr commit Committing to: C:/Dev/bzr_branches/PersonA/MyBranch/ modified BazaarRenames/UnitTests/MyTest.cs Committed revision 2. C:\Dev\bzr_branches\PersonA\MyBranch>bzr push ..\..\Shared All changes applied successfully. Pushed up to revision 2.
PersonB will also change this same class.
PersonB adds a new folder called TestCases, moves the test class to this directory, renames the test file and then adds a new method. I’ll call the new method PersonBHas_RenamedTheClass_MovedIt_AndAddedATest so, again, we can see it clearly at the end of this blog post:
C:\Dev\bzr_branches\PersonB\MyBranch>bzr add --no-recurse adding BazaarRenames/TestCases C:\Dev\bzr_branches\PersonB\MyBranch>bzr mv --auto BazaarRenames/UnitTests/MyTest.cs => BazaarRenames/TestCases/SystemTests.cs C:\Dev\bzr_branches\PersonB\MyBranch>bzr commit Committing to: C:/Dev/bzr_branches/PersonB/MyBranch/ added BazaarRenames/TestCases renamed BazaarRenames/UnitTests/MyTest.cs => BazaarRenames/TestCases/SystemTests.cs Committed revision 2.
Merging the two sets of changes
Now PersonB tries to push this back to Shared:
C:\Dev\bzr_branches\PersonB\MyBranch>bzr push ..\..\Shared
This results in the following message:
bzr: ERROR: These branches have diverged. See "bzr help diverged-branches" for more information.
Bazaar knows that PersonB cannot just push the changes back. Let’s pull the changes from Shared to see what has happened:
C:\Dev\bzr_branches\PersonB\MyBranch>bzr pull
This results in the following message:
Using saved parent location: C:/Dev/bzr_branches/Shared/ bzr: ERROR: These branches have diverged. Use the missing command to see how. Use the merge command to reconcile them.
Bazaar is informing you that PersonB cannot just pull the changes – a merge is needed. So, let’s run the merge command:
C:\Dev\bzr_branches\PersonB\MyBranch>bzr merge
This results in the following message:
Merging from remembered parent location C:/Dev/bzr_branches/Shared/ M BazaarRenames/TestCases/SystemTests.cs All changes applied successfully.
[/sourcecode]
That’s it – Bazaar has not only kept track of the fact that PersonB renamed and moved the file, it has also done the merge – PersonA’s changes have been merged into the new location and for the file.
In my example, I didn’t get any conflicts. [In another blog post I will look at dealing with conflicts]
With my code samples the resultant test file in the TestCases folder looks like this:
[TestFixture] public class SystemTests { [Test] public void PersonATest() { //Here is PersonA's test Assert.That(1, Is.EqualTo(1)); } [Test] public void SomeOtherCode() { // } [Test] public void PersonBHas_RenamedTheClass_MovedIt_AndAddedATest() { Console.WriteLine("Person B changed"); } }
Our final step is for PersonB to commit the merge results and push them back to the Shared repository:
C:\Dev\bzr_branches\PersonB\MyBranch>bzr commit Committing to: C:/Dev/bzr_branches/PersonB/MyBranch/ modified BazaarRenames/TestCases/SystemTests.cs Committed revision 3. C:\Dev\bzr_branches\PersonB\MyBranch>bzr push ..\..\Shared All changes applied successfully. Pushed up to revision 3.
Sorry, the comment form is closed at this time.