bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
1 |
======================= |
2 |
Integrating with Bazaar |
|
3 |
======================= |
|
4 |
||
|
5225.2.5
by Martin Pool
Introduction to integration |
5 |
This document provides an overview of the bzrlib api at a higher level |
6 |
than the docstrings. It is intended to be |
|
7 |
useful to someone developing either a plugin or some other piece of software |
|
8 |
that integrates with bzr. If you want to know about a topic that's not |
|
9 |
covered here, just ask us. |
|
|
5225.2.4
by Martin Pool
ReST format fixes |
10 |
|
11 |
||
|
5225.2.6
by Martin Pool
mention bzrlib.initialize and get_command |
12 |
Starting with bzrlib |
13 |
==================== |
|
14 |
||
15 |
Before doing anything else with bzrlib, you should run `bzrlib.initialize` |
|
16 |
which sets up some global state. |
|
17 |
||
18 |
||
19 |
Running bzr commands |
|
20 |
==================== |
|
21 |
||
22 |
To run command-line commands in-process:: |
|
23 |
||
24 |
from bzrlib.commands import get_command |
|
25 |
||
26 |
cmd = get_command('version')
|
|
27 |
cmd.run([]) |
|
28 |
||
29 |
This will send output through the current UIFactory; you can redirect this |
|
30 |
elsewhere through the parameters to `bzrlib.initialize`. |
|
31 |
||
|
5225.2.4
by Martin Pool
ReST format fixes |
32 |
|
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
33 |
Manipulating the Working Tree |
34 |
============================= |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
35 |
Most objects in Bazaar are in files, named after the class they contain. |
36 |
To manipulate the Working Tree we need a valid WorkingTree object, which |
|
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
37 |
is loaded from the workingtree.py file, eg:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
38 |
|
39 |
from bzrlib import workingtree |
|
40 |
wt = workingtree.WorkingTree.open('/home/jebw/bzrtest')
|
|
41 |
||
42 |
||
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
43 |
This gives us a WorkingTree object, which has various methods spread over |
44 |
itself, and its parent classes MutableTree and Tree - its worth having a |
|
45 |
look through these three files (workingtree.py, mutabletree.py and tree.py) |
|
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
46 |
to see which methods are available. |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
47 |
|
|
3250.4.6
by Martin Albisetti
Added changes sent by Aaron Bently |
48 |
Compare trees |
|
5225.2.4
by Martin Pool
ReST format fixes |
49 |
------------- |
50 |
||
|
3250.4.6
by Martin Albisetti
Added changes sent by Aaron Bently |
51 |
There are two methods for comparing trees: ``changes_from`` and |
52 |
``iter_changes``. ``iter_changes`` is more regular and precise, but it is |
|
53 |
somewhat harder to work with. See the API documentation for more details. |
|
54 |
||
55 |
``changes_from`` creates a Delta object showing changes:: |
|
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
56 |
|
57 |
from bzrlib import delta |
|
58 |
changes = wt.changes_from(wt.basis_tree()) |
|
59 |
||
|
3250.4.6
by Martin Albisetti
Added changes sent by Aaron Bently |
60 |
This gives us a Delta object, which has several lists of files for each type of |
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
61 |
change, eg changes.added is a list of added files, changes.removed is list |
62 |
of removed files, changes.modified is a list of modified files. The contents |
|
|
3250.4.6
by Martin Albisetti
Added changes sent by Aaron Bently |
63 |
of the lists aren't just filenames, but include other information as well. |
|
3344.1.2
by Martin Pool
Fix ReST syntax in integration guide |
64 |
To grab just the filename we want the first value, eg:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
65 |
|
66 |
print("list of newly added files")
|
|
67 |
for filename in changes.added: |
|
68 |
print("%s has been added" % filename[0])
|
|
69 |
||
70 |
||
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
71 |
The exception to this is changes.renamed, where the list returned for each |
72 |
renamed files contains both the old and new names -- one or both may interest |
|
73 |
you, depending on what you're doing. |
|
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
74 |
|
75 |
For example:: |
|
76 |
||
77 |
print("list of renamed files")
|
|
78 |
for filename in changes.renamed: |
|
79 |
print("%s has been renamed to %s" % (filename[0], filename[1]))
|
|
80 |
||
81 |
||
82 |
Adding Files |
|
|
5225.2.4
by Martin Pool
ReST format fixes |
83 |
------------ |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
84 |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
85 |
If you want to add files the same way ``bzr add`` does, you can use |
86 |
MutableTree.smart_add. By default, this is recursive. Paths can either be |
|
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
87 |
absolute or relative to the workingtree:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
88 |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
89 |
wt.smart_add(['dir1/filea.txt', 'fileb.txt', |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
90 |
'/home/jebw/bzrtesttree/filec.txt']) |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
91 |
|
92 |
||
93 |
For more precise control over which files to add, use MutableTree.add:: |
|
94 |
||
95 |
wt.add(['dir1/filea.txt', 'fileb.txt', '/home/jebw/bzrtesttree/filec.txt']) |
|
96 |
||
97 |
||
98 |
Removing Files |
|
|
5225.2.4
by Martin Pool
ReST format fixes |
99 |
-------------- |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
100 |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
101 |
You can remove multiple files at once. The file paths need to be relative |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
102 |
to the workingtree:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
103 |
|
104 |
wt.remove(['filea.txt', 'fileb.txt', 'dir1']) |
|
105 |
||
106 |
||
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
107 |
By default, the files are not deleted, just removed from the inventory. |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
108 |
To delete them from the filesystem as well:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
109 |
|
110 |
wt.remove(['filea.txt', 'fileb.txt', 'dir1'], keep_files=False) |
|
111 |
||
112 |
||
113 |
Renaming a File |
|
|
5225.2.4
by Martin Pool
ReST format fixes |
114 |
--------------- |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
115 |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
116 |
You can rename one file to a different name using WorkingTree.rename_one. |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
117 |
You just provide the old and new names, eg:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
118 |
|
119 |
wt.rename_one('oldfile.txt','newfile.txt')
|
|
120 |
||
121 |
||
122 |
Moving Files |
|
|
5225.2.4
by Martin Pool
ReST format fixes |
123 |
------------ |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
124 |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
125 |
You can move multiple files from one directory into another using |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
126 |
WorkingTree.move:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
127 |
|
128 |
wt.move(['olddir/file.txt'], 'newdir') |
|
129 |
||
130 |
||
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
131 |
More complicated renames/moves can be done with transform.TreeTransform, |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
132 |
which is outside the scope of this document. |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
133 |
|
134 |
||
135 |
Committing Changes |
|
|
5225.2.4
by Martin Pool
ReST format fixes |
136 |
------------------ |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
137 |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
138 |
To commit _all_ the changes to our working tree we can just call the |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
139 |
WorkingTree's commit method, giving it a commit message, eg:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
140 |
|
141 |
wt.commit('this is my commit message')
|
|
142 |
||
143 |
||
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
144 |
To commit only certain files, we need to provide a list of filenames which we |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
145 |
want committing, eg:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
146 |
|
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
147 |
wt.commit(message='this is my commit message', specific_files=['fileA.txt', |
148 |
'dir2/fileB.txt', 'fileD.txt']) |
|
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
149 |
|
150 |
||
151 |
Generating a Log for a File |
|
152 |
=========================== |
|
153 |
||
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
154 |
Generating a log is, in itself, simple. Grab a branch (see below) and pass |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
155 |
it to show_log together with a log formatter, eg:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
156 |
|
157 |
from bzrlib import log |
|
158 |
from bzrlib import branch |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
159 |
|
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
160 |
b = branch.Branch.open('/path/to/bazaar/branch')
|
161 |
lf = log.LongLogFormatter(to_file=sys.stdout) |
|
162 |
log.show_log(b, lf) |
|
163 |
||
164 |
||
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
165 |
Three log formatters are included with bzrlib: LongLogFormatter, |
166 |
ShortLogFormatter and LineLogFormatter. These provide long, short and |
|
167 |
single-line log output formats. It's also possible to write your own in |
|
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
168 |
very little code. |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
169 |
|
170 |
Annotating a File |
|
171 |
================= |
|
172 |
||
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
173 |
To annotate a file, we want to walk every line of a file, retrieving the |
174 |
revision which last modified/created that line and then retrieving the |
|
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
175 |
information for that revision. |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
176 |
|
177 |
First we get an annotation iterator for the file we are interested in:: |
|
178 |
||
179 |
tree, relpath = workingtree.WorkingTree.open_containing('/path/to/file.txt')
|
|
180 |
fileid = tree.path2id(relpath) |
|
181 |
annotation = list(tree.annotate_iter(fileid)) |
|
182 |
||
183 |
||
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
184 |
To avoid repeatedly retrieving the same revisions we grab all revisions |
185 |
associated with the file at once and build up a map of id to revision |
|
186 |
information. We also build an map of revision numbers, again indexed |
|
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
187 |
by the revision id:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
188 |
|
189 |
revision_ids = set(revision_id for revision_id, text in annotation) |
|
190 |
revisions = tree.branch.repository.get_revisions(revision_ids) |
|
191 |
revision_map = dict(izip(revision_ids, revisions)) |
|
192 |
revno_map = tree.branch.get_revision_id_to_revno_map() |
|
193 |
||
194 |
||
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
195 |
Finally, we use our annotation iterator to walk the lines of the file, |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
196 |
displaying the information from our revision maps as we go:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
197 |
|
198 |
for revision_id, text in annotation : |
|
199 |
rev = revision_map[revision_id] |
|
200 |
revno = revno_map[revision_id] |
|
201 |
revno_string = '.'.join(str(i) for i in revno) |
|
202 |
print "%s, %s: %s" % (revno_string, rev.committer, text) |
|
203 |
||
204 |
||
205 |
Working with branches |
|
206 |
===================== |
|
207 |
||
208 |
To work with a branch you need a branch object, created from your branch:: |
|
209 |
||
210 |
from bzrlib import branch |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
211 |
|
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
212 |
b = branch.Branch.open('/home/jebw/bzrtest')
|
213 |
||
214 |
||
215 |
Branching from an existing branch |
|
|
5225.2.4
by Martin Pool
ReST format fixes |
216 |
--------------------------------- |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
217 |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
218 |
To branch you create a branch object representing the branch you are |
219 |
branching from, and supply a path/url to the new branch location. |
|
220 |
The following code clones the bzr.dev branch (the latest copy of the Bazaar |
|
221 |
source code) - be warned it has to download 60meg so takes a while to run |
|
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
222 |
with no feedback:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
223 |
|
224 |
from bzrlib import branch |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
225 |
|
|
4675.2.2
by Robert Collins
Replace bazaar-vcs.org/bzr/ references with launchpad hosting urls in developer docs. |
226 |
b = branch.Branch.open('http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev')
|
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
227 |
nb = b.bzrdir.sprout('/tmp/newBzrBranch').open_branch()
|
228 |
||
229 |
||
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
230 |
This provides no feedback, since Bazaar automatically uses the 'silent' UI. |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
231 |
|
232 |
||
233 |
Pushing and pulling branches |
|
|
5225.2.4
by Martin Pool
ReST format fixes |
234 |
---------------------------- |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
235 |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
236 |
To push a branch you need to open the source and destination branches, then |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
237 |
just call push with the other branch as a parameter:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
238 |
|
239 |
from bzrlib import branch |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
240 |
|
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
241 |
b1 = branch.Branch.open('file:///home/user/mybranch')
|
|
4675.2.2
by Robert Collins
Replace bazaar-vcs.org/bzr/ references with launchpad hosting urls in developer docs. |
242 |
b2 = branch.Branch.open('http://bazaar.launchpad.net/~bzr-pqm/bzr/bzr.dev')
|
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
243 |
b1.push(b2) |
244 |
||
245 |
||
246 |
Pulling is much the same:: |
|
247 |
||
248 |
b1.pull(b2) |
|
249 |
||
250 |
||
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
251 |
If you have a working tree, as well as a branch, you should use |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
252 |
WorkingTree.pull, not Branch.pull. |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
253 |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
254 |
This won't handle conflicts automatically though, so any conflicts will be |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
255 |
left in the working tree for the user to resolve. |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
256 |
|
257 |
||
258 |
Checkout from an existing branch |
|
259 |
================================ |
|
260 |
||
261 |
This performs a Lightweight checkout from an existing Branch:: |
|
262 |
||
263 |
from bzrlib import bzrdir |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
264 |
|
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
265 |
accelerator_tree, source = bzrdir.BzrDir.open_tree_or_branch('http:URL')
|
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
266 |
source.create_checkout('/tmp/newBzrCheckout', None, True, accelerator_tree)
|
267 |
||
268 |
||
269 |
To make a heavyweight checkout, change the last line to:: |
|
270 |
||
271 |
source.create_checkout('/tmp/newBzrCheckout', None, False, accelerator_tree
|
|
272 |
||
|
4634.39.32
by Ian Clatworthy
proper Contents panel in bzr-developers.chm |
273 |
|
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
274 |
History Operations |
275 |
================== |
|
276 |
||
277 |
Finding the last revision number or id |
|
|
4634.39.32
by Ian Clatworthy
proper Contents panel in bzr-developers.chm |
278 |
-------------------------------------- |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
279 |
|
280 |
To get the last revision number and id of a branch use:: |
|
281 |
||
282 |
revision_number, revision_id = branch.last_revision_info() |
|
283 |
||
284 |
||
285 |
If all you care about is the revision_id there is also the |
|
286 |
method:: |
|
287 |
||
288 |
revision_id = branch.last_revision() |
|
289 |
||
290 |
||
291 |
Getting the list of revision ids that make up a branch |
|
|
4634.39.32
by Ian Clatworthy
proper Contents panel in bzr-developers.chm |
292 |
------------------------------------------------------ |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
293 |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
294 |
IMPORTANT: This should be avoided wherever possible, as it scales with the |
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
295 |
length of history:: |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
296 |
|
297 |
revisions = branch.revision_history() |
|
298 |
||
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
299 |
now revisions[0] is the revision id of the first commit, and revs[-1] is the |
300 |
revision id of the most recent. Note that if all you want is the last |
|
301 |
revision then you should use branch.last_revision() as described above, as |
|
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
302 |
it is vastly more efficient. |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
303 |
|
304 |
||
305 |
Getting a Revision object from a revision id |
|
|
4634.39.32
by Ian Clatworthy
proper Contents panel in bzr-developers.chm |
306 |
-------------------------------------------- |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
307 |
|
308 |
The Revision object has attributes like "message" to get the information |
|
309 |
about the revision:: |
|
310 |
||
311 |
repo = branch.repository |
|
312 |
revision = repo.get_revision(rev_id) |
|
313 |
||
314 |
||
315 |
Accessing the files from a revision |
|
|
4634.39.32
by Ian Clatworthy
proper Contents panel in bzr-developers.chm |
316 |
----------------------------------- |
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
317 |
|
318 |
To get the file contents and tree shape for a specific revision you need |
|
319 |
a RevisionTree. These are supplied by the repository for a specific |
|
320 |
revision id:: |
|
321 |
||
322 |
revtree = repo.revision_tree(rev_id) |
|
323 |
||
|
3250.4.6
by Martin Albisetti
Added changes sent by Aaron Bently |
324 |
RevisionTrees, like all trees, can be compared as described in "Comparing |
325 |
Trees" above. |
|
|
3250.4.1
by Martin Albisetti
Added integration guide for developers |
326 |
|
|
3250.4.5
by Martin Albisetti
Removed trailing whitespaces |
327 |
The most common way to list files in a tree is ``Tree.iter_entries()``. |
328 |
The simplest way to get file content is ``Tree.get_file()``. The best way |
|
329 |
to retrieve file content for large numbers of files `Tree.iter_files_bytes()`` |
|
|
3250.4.4
by Martin Albisetti
Shortened to under 80 characters per line, and removed all wiki-specific information |
330 |