1
1
=======================
2
Integrating with Bazaar
2
Integrating with Breezy
3
3
=======================
5
This page should hopefully become a quick guide to integrating other
6
(Python-based) software with Bazaar.
5
This document provides some general observations on integrating with
6
Breezy 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 breezy within the ``bzr`` program (for instance as a bzr
21
plugin), breezy's global state is already available for use.
26
To use breezy outside of ``bzr`` some global state needs to be setup.
27
breezy 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
``breezy.initialize``.
32
This returns a context manager within which breezy functions will work
33
correctly. See the pydoc for ``breezy.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 = breezy.initialize()
43
library_state.__enter__()
48
library_state.__exit__(None, None, None)
54
To run command-line commands in-process::
56
from breezy.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 `breezy.initialize`.
8
65
Manipulating the Working Tree
9
66
=============================
10
Most objects in Bazaar are in files, named after the class they contain.
67
Most objects in Breezy are in files, named after the class they contain.
11
68
To manipulate the Working Tree we need a valid WorkingTree object, which
12
69
is loaded from the workingtree.py file, eg::
14
from bzrlib import workingtree
71
from breezy import workingtree
15
72
wt = workingtree.WorkingTree.open('/home/jebw/bzrtest')
18
75
This gives us a WorkingTree object, which has various methods spread over
19
itself, and its parent classes MutableTree and Tree - its worth having a
76
itself, and its parent classes MutableTree and Tree - it's worth having a
20
77
look through these three files (workingtree.py, mutabletree.py and tree.py)
21
78
to see which methods are available.
25
83
There are two methods for comparing trees: ``changes_from`` and
26
84
``iter_changes``. ``iter_changes`` is more regular and precise, but it is
27
85
somewhat harder to work with. See the API documentation for more details.
29
87
``changes_from`` creates a Delta object showing changes::
31
from bzrlib import delta
89
from breezy import delta
32
90
changes = wt.changes_from(wt.basis_tree())
34
92
This gives us a Delta object, which has several lists of files for each type of
128
186
Generating a log is, in itself, simple. Grab a branch (see below) and pass
129
187
it to show_log together with a log formatter, eg::
131
from bzrlib import log
132
from bzrlib import branch
189
from breezy import log
190
from breezy import branch
134
192
b = branch.Branch.open('/path/to/bazaar/branch')
135
193
lf = log.LongLogFormatter(to_file=sys.stdout)
136
194
log.show_log(b, lf)
139
Three log formatters are included with bzrlib: LongLogFormatter,
197
Three log formatters are included with breezy: LongLogFormatter,
140
198
ShortLogFormatter and LineLogFormatter. These provide long, short and
141
199
single-line log output formats. It's also possible to write your own in
142
200
very little code.
182
240
To work with a branch you need a branch object, created from your branch::
184
from bzrlib import branch
242
from breezy import branch
186
244
b = branch.Branch.open('/home/jebw/bzrtest')
189
247
Branching from an existing branch
190
=================================
248
---------------------------------
192
250
To branch you create a branch object representing the branch you are
193
251
branching from, and supply a path/url to the new branch location.
194
The following code clones the bzr.dev branch (the latest copy of the Bazaar
252
The following code clones the bzr.dev branch (the latest copy of the Breezy
195
253
source code) - be warned it has to download 60meg so takes a while to run
196
254
with no feedback::
198
from bzrlib import branch
256
from breezy import branch
200
258
b = branch.Branch.open('http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev')
201
nb = b.bzrdir.sprout('/tmp/newBzrBranch').open_branch()
204
This provides no feedback, since Bazaar automatically uses the 'silent' UI.
259
nb = b.controldir.sprout('/tmp/newBzrBranch').open_branch()
262
This provides no feedback, since Breezy automatically uses the 'silent' UI.
207
265
Pushing and pulling branches
208
============================
266
----------------------------
210
268
To push a branch you need to open the source and destination branches, then
211
269
just call push with the other branch as a parameter::
213
from bzrlib import branch
271
from breezy import branch
215
273
b1 = branch.Branch.open('file:///home/user/mybranch')
216
274
b2 = branch.Branch.open('http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev')