Hacking Django, how Bazaar
A short while ago, I went on a mission to find a better way to hack Django. I needed a way to keep my patches organized and updated. So, in this article I discuss my experiences with a tool I found to help me achieve these goals…
Enter Bazaar, one of the several distributed version control systems that have been getting some hype recently. With so many to choose from, why did I choose Bazaar? Well, maybe it was because of the cool name, or maybe it was the pretty website, or maybe just because it’s written in the language I love — Python. If you are familiar with Subversion, then you will feel right at home with Bazaar. This is because Bazaar uses most of the same commands as Subversion (e.g. svn ci = bzr ci, svn st = bzr st, etc.).
Following this guide for hacking an open source project using bzr, I created a django directory to stash my django hacks. After using svn to check out the latest Django code to a directory named upstream, and turning upstream into a bzr repository too, it was time to start the real work. To re-iterate in command line speak, here is what I did:
Update: The steps below have been updated to reflect changes with bzr commands (versions >=0.15rc2).
mkdir django
cd django
bzr init-repo . # use "bzr init-repo --trees ." here instead if using a version <0.15rc2
svn co http://code.djangoproject.com/svn/django/trunk/ upstream
cd upstream
bzr init
echo ".svn" > .bzrignore # do not store svn metadata in our bzr repo
bzr add
bzr ci -m 'Initial import of Django.'
Usually, when you want to add a new feature to some code, you create a branch. The purpose of these, so called, feature branches is to keep the features separated. Trying to create multiple features in the same branch would just be a big mess. Creating branches in a Subversion repository is easy enough, you just svn cp the code to a new location. It’s the keeping your branch in sync with the trunk that’s not as easy.
With Subversion, you have to keep track of the revisions that have already been merged to your branch and make sure to only merge the changes that haven’t already been merged. Not so with Bazaar; Bazaar keeps track of this for you. A simple bzr merge will pull in upstream changes that have not been merged yet.
So, when I wanted to start working on a patch for ticket #2473, I created a branch:
bzr branch upstream in-and-empty-list-2473
After cd‘ing into my new branch and committing my changes, I created a diff to attach to the ticket with the command:
bzr diff -r branch:../upstream
So far, working with Bazaar has been a breeze. Without it, my Django holiday hacking wouldn’t have been nearly as fun.
Gary, thanks a lot for posting this. I’ve been struggling similarly with a couple projects for which I have my own tweaks and patches, Django being one of them. This may be the thing that finally gets me to try distributed source control.
Comment by Paul — January 11, 2007 @ 8:02 pm
Thank you for introducing me to Bazaar. I will look into it further. Is there a GUI for it? Particularly for OS X?
Comment by Noah — January 11, 2007 @ 11:17 pm
What do you then when the django trunk has changed? How do you merge the changes into your branch? Did I miss something?
Comment by Philipp Keller — January 12, 2007 @ 7:25 am
Great stuff, but even better — the bzr-svn plugin lets you dodge svn entirely.
Comment by Jeff Waugh — January 12, 2007 @ 7:26 am
@Noah: Check out Olive (gtk based) at http://bazaar-vcs.org/Olive
Comment by vezult — January 12, 2007 @ 10:30 am
@Philipp:
First svn update upstream:
cd upstream
svn up
Then add/remove files to/from bzr repo that were added/removed in the svn update and commit changes to bzr repo:
bzr add <files_to_add>
bzr ci
Now merge and commit the upstream changes to your branches:
cd in-and-empty-list-2473
bzr merge
bzr ci
Comment by Gary — January 12, 2007 @ 11:02 am
@Jeff: I haven’t tried the bzr-svn plugin yet, but it looks like it would make things even easier. Thanks!
Comment by Gary — January 12, 2007 @ 11:06 am
Using bzr to work on a svn project seems like a bad idea. I would recommend using SVK, which will create a full local copy of the project’s svn tree that you can branch and merge to with ease. It’s smart enough to track merges so that you don’t keep merging the same patch each time. It can also “push” to a patch instead of the upstream repository, so you can mail your changes to the developers if you don’t have write access. There are also features like darcs-style interactive commits (but with a nicer interface), smart merging, views, and so on.
Take a look:
http://search.cpan.org/~clkao/SVK-v2.0.0/
Comment by Jonathan Rockway — January 12, 2007 @ 11:07 am
Jonathan: the bzr-svn plugin works in a similar way.
Comment by Andrew — January 13, 2007 @ 1:48 am