2
2
Integrating with Bazaar
3
3
=======================
5
This document provides some general observations on integrating with
6
Bazaar and some recipes for typical tasks. It is intended to be useful to
7
someone developing either a plugin or some other piece of software that
8
integrates with bzr. If you want to know about a topic that's not covered
20
When using bzrlib within the ``bzr`` program (for instance as a bzr
21
plugin), bzrlib's global state is already available for use.
26
To use bzrlib outside of ``bzr`` some global state needs to be setup.
27
bzrlib needs ways to handle user input, passwords, a place to emit
28
progress bars, logging setup appropriately for your program. The easiest
29
way to set all this up in the same fashion ``bzr`` does is to call
30
``bzrlib.initialize``.
32
This returns a context manager within which bzrlib functions will work
33
correctly. See the pydoc for ``bzrlib.initialize`` for more information.
34
(You can get away without entering the context manager, because the setup
35
work happens directly from ``initialize``.)
37
In Python 2.4 the ``with`` keyword is not supported and
38
so you need to use the context manager manually::
40
# This sets up your ~/.bzr.log, ui factory and so on and so forth. It is
41
# not safe to use as a doctest.
42
library_state = bzrlib.initialize()
43
library_state.__enter__()
48
library_state.__exit__(None, None, None)
54
To run command-line commands in-process::
56
from bzrlib.commands import get_command
58
cmd = get_command('version')
61
This will send output through the current UIFactory; you can redirect this
62
elsewhere through the parameters to `bzrlib.initialize`.
5
This page should hopefully become a quick guide to integrating other
6
(Python-based) software with Bazaar.
65
8
Manipulating the Working Tree
66
9
=============================
75
18
This gives us a WorkingTree object, which has various methods spread over
76
itself, and its parent classes MutableTree and Tree - it's worth having a
19
itself, and its parent classes MutableTree and Tree - its worth having a
77
20
look through these three files (workingtree.py, mutabletree.py and tree.py)
78
21
to see which methods are available.
83
25
There are two methods for comparing trees: ``changes_from`` and
84
26
``iter_changes``. ``iter_changes`` is more regular and precise, but it is
85
27
somewhat harder to work with. See the API documentation for more details.
247
189
Branching from an existing branch
248
---------------------------------
190
=================================
250
192
To branch you create a branch object representing the branch you are
251
193
branching from, and supply a path/url to the new branch location.
256
198
from bzrlib import branch
258
b = branch.Branch.open('http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev')
200
b = branch.Branch.open('http://bazaar-vcs.org/bzr/bzr.dev')
259
201
nb = b.bzrdir.sprout('/tmp/newBzrBranch').open_branch()
265
207
Pushing and pulling branches
266
----------------------------
208
============================
268
210
To push a branch you need to open the source and destination branches, then
269
211
just call push with the other branch as a parameter::
271
213
from bzrlib import branch
273
215
b1 = branch.Branch.open('file:///home/user/mybranch')
274
b2 = branch.Branch.open('http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev')
216
b2 = branch.Branch.open('http://bazaar-vcs.org/bzr/bzr.dev')
303
245
source.create_checkout('/tmp/newBzrCheckout', None, False, accelerator_tree
306
248
History Operations
307
249
==================
309
251
Finding the last revision number or id
310
--------------------------------------
252
======================================
312
254
To get the last revision number and id of a branch use::
323
265
Getting the list of revision ids that make up a branch
324
------------------------------------------------------
266
======================================================
326
268
IMPORTANT: This should be avoided wherever possible, as it scales with the
327
269
length of history::
337
279
Getting a Revision object from a revision id
338
--------------------------------------------
280
============================================
340
282
The Revision object has attributes like "message" to get the information
341
283
about the revision::
347
289
Accessing the files from a revision
348
-----------------------------------
290
===================================
350
292
To get the file contents and tree shape for a specific revision you need
351
293
a RevisionTree. These are supplied by the repository for a specific