/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to HACKING

  • Committer: Martin Pool
  • Date: 2006-10-06 02:04:17 UTC
  • mfrom: (1908.10.1 bench_usecases.merge2)
  • mto: This revision was merged to the branch mainline in revision 2068.
  • Revision ID: mbp@sourcefrog.net-20061006020417-4949ca86f4417a4d
merge additional fix from cfbolz

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
.. contents::
6
6
 
7
7
(The current version of this document is available in the file ``HACKING``
8
 
in the source tree, or at http://bazaar-ng.org/hacking.html)
 
8
in the source tree, or at http://doc.bazaar-vcs.org/current/hacking.html)
9
9
 
10
10
Overall
11
11
=======
38
38
  pipelines.
39
39
 
40
40
  Recommended values are 
41
 
    0- OK, 
42
 
    1- Conflicts in merge-like operations, or changes are present in
 
41
    0. OK, 
 
42
    1. Conflicts in merge-like operations, or changes are present in
43
43
       diff-like operations. 
44
 
    2- Unrepresentable diff changes (i.e. binary files that we cannot show 
 
44
    2. Unrepresentable diff changes (i.e. binary files that we cannot show 
45
45
       a diff of).
46
 
    3- An error or exception has occurred.
 
46
    3. An error or exception has occurred.
47
47
 
48
48
Evolving interfaces
49
49
-------------------
194
194
    may not catch every case but it's still useful sometimes.
195
195
 
196
196
 
 
197
Factories
 
198
---------
 
199
 
 
200
In some places we have variables which point to callables that construct
 
201
new instances.  That is to say, they can be used a lot like class objects,
 
202
but they shouldn't be *named* like classes:
 
203
 
 
204
> I think that things named FooBar should create instances of FooBar when
 
205
> called. Its plain confusing for them to do otherwise. When we have
 
206
> something that is going to be used as a class - that is, checked for via
 
207
> isinstance or other such idioms, them I would call it foo_class, so that
 
208
> it is clear that a callable is not sufficient. If it is only used as a
 
209
> factory, then yes, foo_factory is what I would use.
 
210
 
 
211
 
 
212
Lazy Imports
 
213
------------
 
214
 
 
215
To make startup time faster, we use the ``bzrlib.lazy_import`` module to
 
216
delay importing modules until they are actually used. ``lazy_import`` uses
 
217
the same syntax as regular python imports. So to import a few modules in a
 
218
lazy fashion do::
 
219
 
 
220
  from bzrlib.lazy_import import lazy_import
 
221
  lazy_import(globals(), """
 
222
  import os
 
223
  import subprocess
 
224
  import sys
 
225
  import time
 
226
 
 
227
  from bzrlib import (
 
228
     errors,
 
229
     transport,
 
230
     foo as bar,
 
231
     )
 
232
  import bzrlib.transport
 
233
  import bzrlib.xml5
 
234
  """)
 
235
 
 
236
At this point, all of these exist as a ``ImportReplacer`` object, ready to
 
237
be imported once a member is accessed.
 
238
 
 
239
 
 
240
Modules versus Members
 
241
~~~~~~~~~~~~~~~~~~~~~~
 
242
 
 
243
While it is possible for ``lazy_import()`` to import members of a module
 
244
wehn using the ``from module import member`` syntax, it is recommended to
 
245
only use that syntax to load sub modules ``from module import submodule``.
 
246
This is because variables and classes can frequently be used without
 
247
needing a sub-member for example::
 
248
 
 
249
  lazy_import(globals(), """
 
250
  from module import MyClass
 
251
  """)
 
252
 
 
253
  def test(x):
 
254
      return isinstance(x, MyClass)
 
255
 
 
256
This will incorrectly fail, because ``MyClass`` is a ``ImportReplacer``
 
257
object, rather than the real class.
 
258
 
 
259
 
 
260
Passing to other variables
 
261
~~~~~~~~~~~~~~~~~~~~~~~~~~
 
262
 
 
263
It also is incorrect to assign ``ImportReplacer`` objects to other variables.
 
264
Because the replacer only knows about the original name, it is unable to
 
265
replace other variables. The ``ImportReplacer`` class will raise an
 
266
``IllegalUseOfScopeReplacer`` exception if it can figure out that this
 
267
happened. But it requires accessing a member more than once from the new
 
268
variable, so some bugs are not detected right away.
 
269
 
 
270
 
197
271
Writing output
198
272
==============
199
273
 
245
319
option, then you should be writing a UI test.  If you are both adding UI
246
320
functionality and library functionality, you will want to write tests for 
247
321
both the UI and the core behaviours.  We call UI tests 'blackbox' tests
248
 
and they are found in bzrlib/tests/blackbox/*.py. 
 
322
and they are found in ``bzrlib/tests/blackbox/*.py``. 
249
323
 
250
324
When writing blackbox tests please honour the following conventions:
251
325
 
267
341
    command changes it name or signature. Ideally only the tests for a
268
342
    given command are affected when a given command is changed.
269
343
 
 
344
Doctests
 
345
--------
 
346
 
 
347
We make selective use of doctests__.  In general they should provide 
 
348
*examples* within the API documentation which can incidentally be tested.  We 
 
349
don't try to test every important case using doctests -- regular Python
 
350
tests are generally a better solution.
 
351
 
 
352
Most of these are in ``bzrlib/doc/api``.  More additions are welcome.
 
353
 
 
354
  __ http://docs.python.org/lib/module-doctest.html
 
355
 
 
356
 
270
357
Running tests
271
358
=============
272
359
Currently, bzr selftest is used to invoke tests.
275
362
 
276
363
  ./bzr selftest -v blackbox
277
364
 
 
365
To skip a particular test (or set of tests), you need to use a negative
 
366
match, like so::
 
367
 
 
368
  ./bzr selftest '^(?!.*blackbox)'  
 
369
 
278
370
 
279
371
Errors and exceptions
280
372
=====================
294
386
    indexes into the branch's revision history.
295
387
 
296
388
 
 
389
Transport
 
390
=========
 
391
 
 
392
The ``Transport`` layer handles access to local or remote directories.
 
393
Each Transport object acts like a logical connection to a particular
 
394
directory, and it allows various operations on files within it.  You can
 
395
*clone* a transport to get a new Transport connected to a subdirectory or
 
396
parent directory.
 
397
 
 
398
Transports are not used for access to the working tree.  At present
 
399
working trees are always local and they are accessed through the regular
 
400
Python file io mechanisms.
 
401
 
 
402
filenames vs URLs
 
403
-----------------
 
404
 
 
405
Transports work in URLs.  Take note that URLs are by definition only
 
406
ASCII - the decision of how to encode a Unicode string into a URL must be
 
407
taken at a higher level, typically in the Store.  (Note that Stores also
 
408
escape filenames which cannot be safely stored on all filesystems, but
 
409
this is a different level.)
 
410
 
 
411
The main reason for this is that it's not possible to safely roundtrip a
 
412
URL into Unicode and then back into the same URL.  The URL standard
 
413
gives a way to represent non-ASCII bytes in ASCII (as %-escapes), but
 
414
doesn't say how those bytes represent non-ASCII characters.  (They're not
 
415
guaranteed to be UTF-8 -- that is common but doesn't happen everywhere.)
 
416
 
 
417
For example if the user enters the url ``http://example/%e0`` there's no
 
418
way to tell whether that character represents "latin small letter a with
 
419
grave" in iso-8859-1, or "latin small letter r with acute" in iso-8859-2
 
420
or malformed UTF-8.  So we can't convert their URL to Unicode reliably.
 
421
 
 
422
Equally problematic if we're given a url-like string containing non-ascii
 
423
characters (such as the accented a) we can't be sure how to convert that
 
424
to the correct URL, because we don't know what encoding the server expects
 
425
for those characters.  (Although this is not totally reliable we might still
 
426
accept these and assume they should be put into UTF-8.)
 
427
 
 
428
A similar edge case is that the url ``http://foo/sweet%2Fsour`` contains
 
429
one directory component whose name is "sweet/sour".  The escaped slash is
 
430
not a directory separator.  If we try to convert URLs to regular Unicode
 
431
paths this information will be lost.
 
432
 
 
433
This implies that Transports must natively deal with URLs; for simplicity
 
434
they *only* deal with URLs and conversion of other strings to URLs is done
 
435
elsewhere.  Information they return, such as from ``list_dir``, is also in
 
436
the form of URL components.
 
437
 
 
438
 
 
439
Unicode and Encoding Support
 
440
============================
 
441
 
 
442
This section discusses various techniques that Bazaar uses to handle
 
443
characters that are outside the ASCII set.
 
444
 
 
445
``Command.outf``
 
446
----------------
 
447
 
 
448
When a ``Command`` object is created, it is given a member variable
 
449
accessible by ``self.outf``.  This is a file-like object, which is bound to
 
450
``sys.stdout``, and should be used to write information to the screen,
 
451
rather than directly writing to ``sys.stdout`` or calling ``print``.
 
452
This file has the ability to translate Unicode objects into the correct
 
453
representation, based on the console encoding.  Also, the class attribute
 
454
``encoding_type`` will effect how unprintable characters will be
 
455
handled.  This parameter can take one of 3 values:
 
456
 
 
457
  replace
 
458
    Unprintable characters will be represented with a suitable replacement
 
459
    marker (typically '?'), and no exception will be raised. This is for
 
460
    any command which generates text for the user to review, rather than
 
461
    for automated processing.
 
462
    For example: ``bzr log`` should not fail if one of the entries has text
 
463
    that cannot be displayed.
 
464
  
 
465
  strict
 
466
    Attempting to print and unprintable character will cause a UnicodeError.
 
467
    This is for commands that are intended more as scripting support, rather
 
468
    than plain user review.
 
469
    For exampl: ``bzr ls`` is designed to be used with shell scripting. One
 
470
    use would be ``bzr ls --null --unknows | xargs -0 rm``.  If ``bzr``
 
471
    printed a filename with a '?', the wrong file could be deleted. (At the
 
472
    very least, the correct file would not be deleted). An error is used to
 
473
    indicate that the requested action could not be performed.
 
474
  
 
475
  exact
 
476
    Do not attempt to automatically convert Unicode strings. This is used
 
477
    for commands that must handle conversion themselves.
 
478
    For example: ``bzr diff`` needs to translate Unicode paths, but should
 
479
    not change the exact text of the contents of the files.
 
480
 
 
481
 
 
482
``bzrlib.urlutils.unescape_for_display``
 
483
----------------------------------------
 
484
 
 
485
Because Transports work in URLs (as defined earlier), printing the raw URL
 
486
to the user is usually less than optimal. Characters outside the standard
 
487
set are printed as escapes, rather than the real character, and local
 
488
paths would be printed as ``file://`` urls. The function
 
489
``unescape_for_display`` attempts to unescape a URL, such that anything
 
490
that cannot be printed in the current encoding stays an escaped URL, but
 
491
valid characters are generated where possible.
 
492
 
 
493
 
297
494
Merge/review process
298
495
====================
299
496
 
337
534
so, please reply and say so.)
338
535
 
339
536
 
340
 
:: vim:tw=74:ai
 
537
Making installers for OS Windows
 
538
================================
 
539
To build a win32 installer, see the instructions on the wiki page:
 
540
http://bazaar-vcs.org/BzrWin32Installer
 
541
 
 
542
 
 
543
:: vim: ft=rst tw=74 ai