pvillega’s posterous

pvillega’s posterous

Pere Villega  //  Born in Barcelona, living in Dublin, and tagged as geek since youth. Developer in the path to becoming a software architect. I swear this is not a proper blog :)

Feb 16 / 6:14am

Bazaar

Bazaar is a distributed revision control system sponsored by Canonical Ltd., designed to make it easier for anyone to contribute to free and open source software projects. Bazaar commands are quite similar to those found in CVS or SVN. In contrast to purely distributed version control systems which don't use a central server, Bazaar supports working with or without a central server and it is possible to use both methods at the same time with the same project. Bazaar has support for working with some other revision control systems. This allows users to branch from another system (such as Subversion), make local changes and commit them into a Bazaar branch, and then later merge them back into the other system. Bazaar has basic support for Subversion with the bzr-svn plugin. There is also beginnings of support for both Mercurial and Git. I had an introduction to Bazaar at FOSDEM 2009, and it looked promising. The first think that made me pay special attention to the presentation was the fact it was created by the guys of Canonical, the creators of Ubuntu, a great reference. Next, they said you can use a central server and a distributed approach. And that it can interact transparently with other repositories like Subversion. And that it has binaries for all Linux, Windows and MacOS. So many wonderful things together that I had to test it. And here I am! That said, Bazaar is not perfect: is slower than Git and Mercurial and being (yet) less popular its support in IDEs and other development-related applications is not too good. But it's improving and seems a really good choice as SCM. The website Launchpad provides a free hosting service for projects managed with Bazaar.

Why Bazaar?

So you are not sure Bazaar is a good idea? I know it's hard to change, but maybe reading some "propaganda" of Bazaar will help igniting that dormant hype inside you. Of course the site is not objective, but some articles are quite interesting:

Download and Install

There are packages for all the major distros and operating systems (yes, even Windows!), so installing Bazaar is quite simple. In Ubuntu 8.10 you just need to run:

  sudo apt-get install bzr

Using Bazaar

Once installed, first thing you should do is read this tutorial (don't worry, I will summarize the main points here). Then identify yourself so all your commits can be related to you. Remember Bazaar is distributed so proper identification is important.

  bzr whoami "Your name "

bzr whoami #this verifies it worked

Now, to build your first repository create a folder with some files in it. From that folder run:

   bzr init

Although it seems nothing happened this command initialized the Bazaar repository. If you check you will see a hidden folder called .bzr that contains the structure. This is the only folder with Bazaar-related data, so you can remove you files from version control deleting that folder, and it avoids the mess of .svn folders. Now run:

  bzr add

bzr commit -m "Initial import"

The first command will add to the version control any files not added yet. Second command will do a commit into your main branch. Remember this is a distributed repository, the commit is local. With these commands you can start working in your project. Once you have done some changes in your project, you might need to check the differences. Use:

  bzr status

to see the modified files. Use this command:

  bzr diff

to list an unified diff of all the files. And you can see the commit history using:

  bzr log

Other common commands supported by Bazaar are:

  bzr branch   #clones an external branch for your personal use

bzr merge #merge changes with your branch

bzr send #creates a patch

bzr uncommit #rolls-back last commit

As you can see up to know, the syntax is clear and has lot in common with well-know SCM systems (and you can adapt it). The official site has some tutorials on how to use Bazaar with different workflows: for personal development, sharing with peers, centralized or distributed. Use these guides to see how to properly use Bazaar.

Setting a server

A Bazaar central repository is nothing else than a branch that has been designed as central repository. It's usually initialized in a specific way as it only needs to store story, no working copies of files. My choice has been to set up an SFTP server to restrict who can access it. The steps are:

  • Install the SFTP components and make SFTP a valid shell
     #as root

apt-get install ssh openssh-server

echo '/usr/lib/sftp-server' >> /etc/shells

  • Create a user for the sftp
     #as root

sudo useradd --create-home --home-dir /home/bzr --shell /usr/lib/sftp-server bzr

You are done. Easy, isn't it? If you added extra security to your SSH configuration maybe you need to allow the bzr user to connect through SSH, but your Bazaar server is completely functional. Once created you can interact with the branch with the following commands:

  bzr commit -m "message"                #local commit

bzr push --create-prefix sftp://bzr@server:port/~/test #pushes the project to the server and creates folder if needed

bzr pull sftp://bzr@server:port/~/test #update changes from the repo

bzr co sftp://bzr@server:port/~/test #checkout the project

I have to warn you this setup is really slow. Problem is that the smart servers are not secure (no user restriction) or I couldn't set them up properly (SSH server).

Daily operations

Once we have the repository in the server, we need to start working. This section shows a flow of common operations that can be done with Bazaar. Bazaar has two ways of making a "working copy." The most general way is making a branch of another branch, I will show that later. For the trunk, I find it useful to have an in sync copy of the server. Bazaar calls this a checkout, and it works similarly to a Subversion checkout. Any commits made on the working copy automatically get copied to the published branch on the server.

   bzr checkout sftp://bzr@mysourceserver.com/var/bzr/projectfoo/trunk/ ~/Code/projectfoo/trunk

When you want to start working on a new feature, make a branch off the trunk and give it a descriptive name. This name is local and private to your machine so you don't have to worry about conflicts or a specific naming convention.

   bzr branch ~/Code/projectfoo/trunk ~/Code/projectfoo/featureA

To add new data you can use:

   bzr add                          #adds all new files

bzr add File.txt #adds specific files

bzr mkdir DIR #creates and adds a folder

bzr mv f1.txt docs/f2.txt #moves and/or renames a file

Bazaar will automatically mark as deleted missing files at the moment of a commit. You commit your changes locally using:

   bzr commit -m 'message'

As time passes probably the trunk was updated but those changes didn't affect your branch. To update your branch:

  cd ~/Code/projectfoo/trunk

#check if we need to update

bzr status

#update if needed

bzr update

#trunk is updated, now move to your branch

cd ../featureA

#update branch from trunk folder

bzr pull ../trunk

Once you finish work with your branch, you can merge these changes into trunk. First make sure trunk is up to date, and then:

   cd ~/Code/projectfoo/trunk

#merge from you branch folder

bzr merge ../featureA

Now you can push the changes to the server:

   bzr push sftp://bzr@mysourceserver.com/var/bzr/projectfoo/events/

Next time bzr push will suffice as bzr stores the target address. This process is quite similar to the standard SVN process (although with local commits that allow you to work without connection to the server). You can spice it a bit adding branches from workmates. To do that just branch their code (using a path reference to their code folder), work with it and merge it into your own branch. The way to do it is equivalent to considering your local branch as a trunk. Now you have unleashed the power of distributed vcs!

Miscellaneous Topics

Bazaar capabilities go beyond what's mentioned here. Functionalities include views over the repository, smart servers and hooks. You can get more information here.

Plugins

One of the characteristics of Bazaar is how easy it can be extended using plugins. Some interesting plugins are:

Integration with Trac

There's a plugin to integrate Bazaar and Trac, called Trac-bzr. Sadly the plugin is still in a beta status and it fails quite a lot, so I won't recommend it for a serious integration until updated.

Migrate to Bazaar

Canonical wants you to use Bazaar (so do I), and for that reason provides a really extensive list of tools you can use for that. You can use it to import your favourite project and see how it works. And remember that Bazaar allows you to interact with other SCM, like Subversion, so you can use both at the same time until you decide.

Loading mentions Retweet

Filed under // bazaar scm

Comments (0)