Skip to content

First steps using bzrlib

I am working on a project at work that requires controlling some sort of VCS programatically, preferably with python. Immediately I first tried to use subversion since that is what I have used for the last few years as my (only) VCS of choice but I came to a roadblock pretty quickly, I couldn’t get pysvn to compile on my Mac and the precomiled binaries where missing from the site. So next up I thought I would try bazaar, it’s backed by Canonical and heck - it’s written in python. Seemed like the prefect fit.

Until it came to try using it in my program.

You see, I work best with examples in front of me and when working with a new builtin module in python I almost always skip to the example at the end and then read backwards though the docs. But unfortunly there is no such examples in the API docs, so I was on my own reading the docs with nowhere to start. Googling around didn’t turn up anything so I turned to #bzr on irc.freenode.net for some advice on how to use it, so here is a sample bit that I got from the IRC channel and what I figured out on myself.
In the following example I open up my local branch, commit to my local branch, merge some changes from a remote branch, and then commit them into my branch. If anyone sees any errors here please to post in the comments! I am still learning this and wanted to get it out to the rest of the coumunity so someone that needs a head start can get one.

#!/usr/bin/env python

import bzrlib.workingtree
import bzrlib.branch

workingCopy = bzrlib.workingtree.WorkingTree.open("workingfolder/project") #open up my local branch
remoteBranch = bzrlib.branch.Branch.open("http://example.com/project") #open up the remote branch over HTTP

workingCopy.commit("Commit some changes") #commit some changes I made earlier

workingCopy.merge_from_branch(remoteBranch) #now merge some changes from the remote branch
workingCopy.commit('Merge from remote branch') #now commit those changes to my local branch

Now what I don’t know about with this example is what happens when there is a conflict during the merge, guess I will have to play around some more - but that will be for a latter post.

{ 1 } Comments

  1. Ryan Parrish | June 19, 2008 at 1:23 pm | Permalink

    Christian Robottom Reis leaves the following comment….

    http://bazaar-vcs.org/Integrating_with_Bazaar

    which combined with this API reference:

    http://python.net/crew/mwh/bzrlibapi

    made a whole lot of difference in my learning bzrlib. It was amazing –
    from zero to working program in 2h!

    —–

    Thanks Christian! It looks like there are some very good examples there (much better than mine), looks I missed the creation of that wiki page by just a couple weeks.

    Sorry the commenting system was a little messed up earlier.

Post a Comment

Your email is never published nor shared. Required fields are marked *