Sharing your development environment across branches
If you’re working on a project that bootstraps a development environment (using virtualenv or similar) it can be painful to bootstrap the environment for every bug/feature branch that you work on. There are lots of ways you can avoid this (local cache etc.), but if you’re using bzr to manage your branches, a single working directory for all your branches can solve the problem for you. Most bzr users will be familiar with this, but people new to bzr might benefit.
The one-time setup is pretty straight forward – create a repository with a local copy of trunk, and a branch of trunk for your new feature/bug:
$ cd dev/ $ bzr init-repo myproject $ cd myproject $ bzr branch lp:myproject trunk $ bzr branch trunk myfeaturex
From now on, you can simply `cd trunk && bzr pull` when you want to update your local trunk [1]. Now we’ll create a light-weight checkout and bootstrap our environment there. I tend to call this directory ‘current_work’ but whatever works for you. Also, most of the projects I’m working on are using fabric for setting up the virtualenv and other tidbits, but replace that with your own bootstrap command:
$ bzr checkout --lightweight myfeaturex current_work $ cd current_work && fab bootstrap
Assuming everything went well, you now have your development environment setup and are ready to work on myfeaturex. Make changes, commit etc., push to launchpad – it’ll all be for your myfeaturex branch. But what if half-way through, you need to switch and work on an urgent bug? Easy: commit (or shelve) any changes you’ve got for your current branch, then:
$ cd .. $ bzr branch trunk criticalbugx $ cd current_work $ bzr switch ../criticalbugx [2]
Edit 2011-05-23: See Ricardo’s comment – the above 4 lines can be replaced by his two.
That’s it – your current_work directory now reflects the criticalbugx branch – and is already bootstrapped [3], you can work away, commit push etc., and then switch back to your original branch with:
$ bzr switch ../myfeaturex
If you’re working on large branches, once you’ve got the above workflow, you may want to try next keeping large features reviewable with bzr pipelines.
[1] It’s a good idea to never do any work in your trunk tree… always work in a branch of trunk.
[2] The ../ is not actually required, but when you’ve lots of branches with long names, it means you can tab-complete the branch name.
[3] Of course, if one of your branches changes any virtualenv requirements or similar, you’ll need to re-bootstrap, but that isn’t so often in practise. Or sometimes a new version of a dependency is released and you won’t get it until you rebootstrap.
Just a small shortcut to the workflow. Instead of having to do
cd ..
bzr branch trunk criticalbugx
cd current_work
bzr switch ../criticalbugx
you can all of this with
bzr switch trunk
bzr switch -b criticalbugx
This will switch to trunk first, so that the next step behaves correctly. The -b argument to switch will create a new branch and switch to it. The branch will be created as a child of the currently active branch (that’s why we first switch to trunk).
Thanks for the post!
Ricardo Kirkner
May 23, 2011 at 4:01 pm
Nice – thanks Ricardo… I’ll update the post.
Michael
May 23, 2011 at 5:45 pm
[…] Sharing your development environment across branches (micknelson.wordpress.com) […]
How to solve: fatal: remote origin already exists « Computer And You
June 7, 2011 at 10:34 pm
[…] you’ve already got a lightweight checkout of your project code, create a new branch for part-1 of your feature and switch to […]
Keeping large features reviewable with bzr pipelines « Something-driven development
June 24, 2011 at 4:21 pm
[…] Sharing your development environment across branches (micknelson.wordpress.com) […]
How to solve: fatal: remote origin already exists | Computer And You ,Computer And You
February 17, 2013 at 8:54 pm