/brz/remove-bazaar

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