/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 bzrlib/branch.py

  • Committer: Ian Clatworthy
  • Date: 2009-01-22 13:58:18 UTC
  • mto: (3951.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 3952.
  • Revision ID: ian.clatworthy@canonical.com-20090122135818-twftjodatp3cm7xm
review feedback from jam

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
import sys
 
19
 
 
20
from bzrlib.lazy_import import lazy_import
 
21
lazy_import(globals(), """
 
22
from itertools import chain
 
23
from bzrlib import (
 
24
        bzrdir,
 
25
        cache_utf8,
 
26
        config as _mod_config,
 
27
        debug,
 
28
        errors,
 
29
        lockdir,
 
30
        lockable_files,
 
31
        repository,
 
32
        revision as _mod_revision,
 
33
        transport,
 
34
        tsort,
 
35
        ui,
 
36
        urlutils,
 
37
        )
 
38
from bzrlib.config import BranchConfig
 
39
from bzrlib.repofmt.pack_repo import RepositoryFormatKnitPack5RichRoot
 
40
from bzrlib.tag import (
 
41
    BasicTags,
 
42
    DisabledTags,
 
43
    )
 
44
""")
 
45
 
 
46
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
47
from bzrlib.hooks import Hooks
 
48
from bzrlib.symbol_versioning import (
 
49
    deprecated_in,
 
50
    deprecated_method,
 
51
    )
 
52
from bzrlib.trace import mutter, mutter_callsite, note, is_quiet
 
53
 
 
54
 
 
55
BZR_BRANCH_FORMAT_4 = "Bazaar-NG branch, format 0.0.4\n"
 
56
BZR_BRANCH_FORMAT_5 = "Bazaar-NG branch, format 5\n"
 
57
BZR_BRANCH_FORMAT_6 = "Bazaar Branch Format 6 (bzr 0.15)\n"
 
58
 
 
59
 
 
60
# TODO: Maybe include checks for common corruption of newlines, etc?
 
61
 
 
62
# TODO: Some operations like log might retrieve the same revisions
 
63
# repeatedly to calculate deltas.  We could perhaps have a weakref
 
64
# cache in memory to make this faster.  In general anything can be
 
65
# cached in memory between lock and unlock operations. .. nb thats
 
66
# what the transaction identity map provides
 
67
 
 
68
 
 
69
######################################################################
 
70
# branch objects
 
71
 
 
72
class Branch(object):
 
73
    """Branch holding a history of revisions.
 
74
 
 
75
    base
 
76
        Base directory/url of the branch.
 
77
 
 
78
    hooks: An instance of BranchHooks.
 
79
    """
 
80
    # this is really an instance variable - FIXME move it there
 
81
    # - RBC 20060112
 
82
    base = None
 
83
 
 
84
    # override this to set the strategy for storing tags
 
85
    def _make_tags(self):
 
86
        return DisabledTags(self)
 
87
 
 
88
    def __init__(self, *ignored, **ignored_too):
 
89
        self.tags = self._make_tags()
 
90
        self._revision_history_cache = None
 
91
        self._revision_id_to_revno_cache = None
 
92
        self._partial_revision_id_to_revno_cache = {}
 
93
        self._last_revision_info_cache = None
 
94
        self._open_hook()
 
95
        hooks = Branch.hooks['open']
 
96
        for hook in hooks:
 
97
            hook(self)
 
98
 
 
99
    def _open_hook(self):
 
100
        """Called by init to allow simpler extension of the base class."""
 
101
 
 
102
    def break_lock(self):
 
103
        """Break a lock if one is present from another instance.
 
104
 
 
105
        Uses the ui factory to ask for confirmation if the lock may be from
 
106
        an active process.
 
107
 
 
108
        This will probe the repository for its lock as well.
 
109
        """
 
110
        self.control_files.break_lock()
 
111
        self.repository.break_lock()
 
112
        master = self.get_master_branch()
 
113
        if master is not None:
 
114
            master.break_lock()
 
115
 
 
116
    @staticmethod
 
117
    def open(base, _unsupported=False, possible_transports=None):
 
118
        """Open the branch rooted at base.
 
119
 
 
120
        For instance, if the branch is at URL/.bzr/branch,
 
121
        Branch.open(URL) -> a Branch instance.
 
122
        """
 
123
        control = bzrdir.BzrDir.open(base, _unsupported,
 
124
                                     possible_transports=possible_transports)
 
125
        return control.open_branch(_unsupported)
 
126
 
 
127
    @staticmethod
 
128
    def open_from_transport(transport, _unsupported=False):
 
129
        """Open the branch rooted at transport"""
 
130
        control = bzrdir.BzrDir.open_from_transport(transport, _unsupported)
 
131
        return control.open_branch(_unsupported)
 
132
 
 
133
    @staticmethod
 
134
    def open_containing(url, possible_transports=None):
 
135
        """Open an existing branch which contains url.
 
136
        
 
137
        This probes for a branch at url, and searches upwards from there.
 
138
 
 
139
        Basically we keep looking up until we find the control directory or
 
140
        run into the root.  If there isn't one, raises NotBranchError.
 
141
        If there is one and it is either an unrecognised format or an unsupported 
 
142
        format, UnknownFormatError or UnsupportedFormatError are raised.
 
143
        If there is one, it is returned, along with the unused portion of url.
 
144
        """
 
145
        control, relpath = bzrdir.BzrDir.open_containing(url,
 
146
                                                         possible_transports)
 
147
        return control.open_branch(), relpath
 
148
 
 
149
    def get_config(self):
 
150
        return BranchConfig(self)
 
151
 
 
152
    def _get_nick(self, local=False, possible_transports=None):
 
153
        config = self.get_config()
 
154
        # explicit overrides master, but don't look for master if local is True
 
155
        if not local and not config.has_explicit_nickname():
 
156
            try:
 
157
                master = self.get_master_branch(possible_transports)
 
158
                if master is not None:
 
159
                    # return the master branch value
 
160
                    return master.nick
 
161
            except errors.BzrError, e:
 
162
                # Silently fall back to local implicit nick if the master is
 
163
                # unavailable
 
164
                mutter("Could not connect to bound branch, "
 
165
                    "falling back to local nick.\n " + str(e))
 
166
        return config.get_nickname()
 
167
 
 
168
    def _set_nick(self, nick):
 
169
        self.get_config().set_user_option('nickname', nick, warn_masked=True)
 
170
 
 
171
    nick = property(_get_nick, _set_nick)
 
172
 
 
173
    def is_locked(self):
 
174
        raise NotImplementedError(self.is_locked)
 
175
 
 
176
    def lock_write(self):
 
177
        raise NotImplementedError(self.lock_write)
 
178
 
 
179
    def lock_read(self):
 
180
        raise NotImplementedError(self.lock_read)
 
181
 
 
182
    def unlock(self):
 
183
        raise NotImplementedError(self.unlock)
 
184
 
 
185
    def peek_lock_mode(self):
 
186
        """Return lock mode for the Branch: 'r', 'w' or None"""
 
187
        raise NotImplementedError(self.peek_lock_mode)
 
188
 
 
189
    def get_physical_lock_status(self):
 
190
        raise NotImplementedError(self.get_physical_lock_status)
 
191
 
 
192
    @needs_read_lock
 
193
    def dotted_revno_to_revision_id(self, revno, _cache_reverse=False):
 
194
        """Return the revision_id for a dotted revno.
 
195
 
 
196
        :param revno: a tuple like (1,) or (1,1,2)
 
197
        :param _cache_reverse: a private parameter enabling storage
 
198
           of the reverse mapping in a top level cache. (This should
 
199
           only be done in selective circumstances as we want to
 
200
           avoid having the mapping cached multiple times.)
 
201
        :return: the revision_id
 
202
        :raises errors.NoSuchRevision: if the revno doesn't exist
 
203
        """
 
204
        rev_id = self._do_dotted_revno_to_revision_id(revno)
 
205
        if _cache_reverse:
 
206
            self._partial_revision_id_to_revno_cache[rev_id] = revno
 
207
        return rev_id
 
208
 
 
209
    def _do_dotted_revno_to_revision_id(self, revno):
 
210
        """Worker function for dotted_revno_to_revision_id.
 
211
 
 
212
        Subclasses should override this if they wish to
 
213
        provide a more efficient implementation.
 
214
        """
 
215
        if len(revno) == 1:
 
216
            return self.get_rev_id(revno[0])
 
217
        revision_id_to_revno = self.get_revision_id_to_revno_map()
 
218
        revision_ids = [revision_id for revision_id, this_revno
 
219
                        in revision_id_to_revno.iteritems()
 
220
                        if revno == this_revno]
 
221
        if len(revision_ids) == 1:
 
222
            return revision_ids[0]
 
223
        else:
 
224
            revno_str = '.'.join(map(str, revno))
 
225
            raise errors.NoSuchRevision(self, revno_str)
 
226
 
 
227
    @needs_read_lock
 
228
    def revision_id_to_dotted_revno(self, revision_id):
 
229
        """Given a revision id, return its dotted revno.
 
230
        
 
231
        :return: a tuple like (1,) or (400,1,3).
 
232
        """
 
233
        return self._do_revision_id_to_dotted_revno(revision_id)
 
234
 
 
235
    def _do_revision_id_to_dotted_revno(self, revision_id):
 
236
        """Worker function for revision_id_to_revno."""
 
237
        # Try the caches if they are loaded
 
238
        result = self._partial_revision_id_to_revno_cache.get(revision_id)
 
239
        if result is not None:
 
240
            return result
 
241
        if self._revision_id_to_revno_cache:
 
242
            result = self._revision_id_to_revno_cache.get(revision_id)
 
243
            if result is None:
 
244
                raise errors.NoSuchRevision(self, revision_id)
 
245
        # Try the mainline as it's optimised
 
246
        try:
 
247
            revno = self.revision_id_to_revno(revision_id)
 
248
            return (revno,)
 
249
        except errors.NoSuchRevision:
 
250
            # We need to load and use the full revno map after all
 
251
            result = self.get_revision_id_to_revno_map().get(revision_id)
 
252
            if result is None:
 
253
                raise errors.NoSuchRevision(self, revision_id)
 
254
        return result
 
255
 
 
256
    def get_revision_id_to_revno_map(self):
 
257
        """Return the revision_id => dotted revno map.
 
258
 
 
259
        This will be regenerated on demand, but will be cached.
 
260
 
 
261
        :return: A dictionary mapping revision_id => dotted revno.
 
262
            This dictionary should not be modified by the caller.
 
263
        """
 
264
        if self._revision_id_to_revno_cache is not None:
 
265
            mapping = self._revision_id_to_revno_cache
 
266
        else:
 
267
            mapping = self._gen_revno_map()
 
268
            self._cache_revision_id_to_revno(mapping)
 
269
        # TODO: jam 20070417 Since this is being cached, should we be returning
 
270
        #       a copy?
 
271
        # I would rather not, and instead just declare that users should not
 
272
        # modify the return value.
 
273
        return mapping
 
274
 
 
275
    def _gen_revno_map(self):
 
276
        """Create a new mapping from revision ids to dotted revnos.
 
277
 
 
278
        Dotted revnos are generated based on the current tip in the revision
 
279
        history.
 
280
        This is the worker function for get_revision_id_to_revno_map, which
 
281
        just caches the return value.
 
282
 
 
283
        :return: A dictionary mapping revision_id => dotted revno.
 
284
        """
 
285
        last_revision = self.last_revision()
 
286
        revision_graph = repository._old_get_graph(self.repository,
 
287
            last_revision)
 
288
        merge_sorted_revisions = tsort.merge_sort(
 
289
            revision_graph,
 
290
            last_revision,
 
291
            None,
 
292
            generate_revno=True)
 
293
        revision_id_to_revno = dict((rev_id, revno)
 
294
                                    for seq_num, rev_id, depth, revno, end_of_merge
 
295
                                     in merge_sorted_revisions)
 
296
        return revision_id_to_revno
 
297
 
 
298
    def leave_lock_in_place(self):
 
299
        """Tell this branch object not to release the physical lock when this
 
300
        object is unlocked.
 
301
        
 
302
        If lock_write doesn't return a token, then this method is not supported.
 
303
        """
 
304
        self.control_files.leave_in_place()
 
305
 
 
306
    def dont_leave_lock_in_place(self):
 
307
        """Tell this branch object to release the physical lock when this
 
308
        object is unlocked, even if it didn't originally acquire it.
 
309
 
 
310
        If lock_write doesn't return a token, then this method is not supported.
 
311
        """
 
312
        self.control_files.dont_leave_in_place()
 
313
 
 
314
    def bind(self, other):
 
315
        """Bind the local branch the other branch.
 
316
 
 
317
        :param other: The branch to bind to
 
318
        :type other: Branch
 
319
        """
 
320
        raise errors.UpgradeRequired(self.base)
 
321
 
 
322
    @needs_write_lock
 
323
    def fetch(self, from_branch, last_revision=None, pb=None):
 
324
        """Copy revisions from from_branch into this branch.
 
325
 
 
326
        :param from_branch: Where to copy from.
 
327
        :param last_revision: What revision to stop at (None for at the end
 
328
                              of the branch.
 
329
        :param pb: An optional progress bar to use.
 
330
 
 
331
        Returns the copied revision count and the failed revisions in a tuple:
 
332
        (copied, failures).
 
333
        """
 
334
        if self.base == from_branch.base:
 
335
            return (0, [])
 
336
        if pb is None:
 
337
            nested_pb = ui.ui_factory.nested_progress_bar()
 
338
            pb = nested_pb
 
339
        else:
 
340
            nested_pb = None
 
341
 
 
342
        from_branch.lock_read()
 
343
        try:
 
344
            if last_revision is None:
 
345
                pb.update('get source history')
 
346
                last_revision = from_branch.last_revision()
 
347
                last_revision = _mod_revision.ensure_null(last_revision)
 
348
            return self.repository.fetch(from_branch.repository,
 
349
                                         revision_id=last_revision,
 
350
                                         pb=nested_pb)
 
351
        finally:
 
352
            if nested_pb is not None:
 
353
                nested_pb.finished()
 
354
            from_branch.unlock()
 
355
 
 
356
    def get_bound_location(self):
 
357
        """Return the URL of the branch we are bound to.
 
358
 
 
359
        Older format branches cannot bind, please be sure to use a metadir
 
360
        branch.
 
361
        """
 
362
        return None
 
363
    
 
364
    def get_old_bound_location(self):
 
365
        """Return the URL of the branch we used to be bound to
 
366
        """
 
367
        raise errors.UpgradeRequired(self.base)
 
368
 
 
369
    def get_commit_builder(self, parents, config=None, timestamp=None, 
 
370
                           timezone=None, committer=None, revprops=None, 
 
371
                           revision_id=None):
 
372
        """Obtain a CommitBuilder for this branch.
 
373
        
 
374
        :param parents: Revision ids of the parents of the new revision.
 
375
        :param config: Optional configuration to use.
 
376
        :param timestamp: Optional timestamp recorded for commit.
 
377
        :param timezone: Optional timezone for timestamp.
 
378
        :param committer: Optional committer to set for commit.
 
379
        :param revprops: Optional dictionary of revision properties.
 
380
        :param revision_id: Optional revision id.
 
381
        """
 
382
 
 
383
        if config is None:
 
384
            config = self.get_config()
 
385
        
 
386
        return self.repository.get_commit_builder(self, parents, config,
 
387
            timestamp, timezone, committer, revprops, revision_id)
 
388
 
 
389
    def get_master_branch(self, possible_transports=None):
 
390
        """Return the branch we are bound to.
 
391
        
 
392
        :return: Either a Branch, or None
 
393
        """
 
394
        return None
 
395
 
 
396
    def get_revision_delta(self, revno):
 
397
        """Return the delta for one revision.
 
398
 
 
399
        The delta is relative to its mainline predecessor, or the
 
400
        empty tree for revision 1.
 
401
        """
 
402
        rh = self.revision_history()
 
403
        if not (1 <= revno <= len(rh)):
 
404
            raise errors.InvalidRevisionNumber(revno)
 
405
        return self.repository.get_revision_delta(rh[revno-1])
 
406
 
 
407
    def get_stacked_on_url(self):
 
408
        """Get the URL this branch is stacked against.
 
409
 
 
410
        :raises NotStacked: If the branch is not stacked.
 
411
        :raises UnstackableBranchFormat: If the branch does not support
 
412
            stacking.
 
413
        """
 
414
        raise NotImplementedError(self.get_stacked_on_url)
 
415
 
 
416
    def print_file(self, file, revision_id):
 
417
        """Print `file` to stdout."""
 
418
        raise NotImplementedError(self.print_file)
 
419
 
 
420
    def set_revision_history(self, rev_history):
 
421
        raise NotImplementedError(self.set_revision_history)
 
422
 
 
423
    def set_stacked_on_url(self, url):
 
424
        """Set the URL this branch is stacked against.
 
425
 
 
426
        :raises UnstackableBranchFormat: If the branch does not support
 
427
            stacking.
 
428
        :raises UnstackableRepositoryFormat: If the repository does not support
 
429
            stacking.
 
430
        """
 
431
        raise NotImplementedError(self.set_stacked_on_url)
 
432
 
 
433
    def _cache_revision_history(self, rev_history):
 
434
        """Set the cached revision history to rev_history.
 
435
 
 
436
        The revision_history method will use this cache to avoid regenerating
 
437
        the revision history.
 
438
 
 
439
        This API is semi-public; it only for use by subclasses, all other code
 
440
        should consider it to be private.
 
441
        """
 
442
        self._revision_history_cache = rev_history
 
443
 
 
444
    def _cache_revision_id_to_revno(self, revision_id_to_revno):
 
445
        """Set the cached revision_id => revno map to revision_id_to_revno.
 
446
 
 
447
        This API is semi-public; it only for use by subclasses, all other code
 
448
        should consider it to be private.
 
449
        """
 
450
        self._revision_id_to_revno_cache = revision_id_to_revno
 
451
 
 
452
    def _clear_cached_state(self):
 
453
        """Clear any cached data on this branch, e.g. cached revision history.
 
454
 
 
455
        This means the next call to revision_history will need to call
 
456
        _gen_revision_history.
 
457
 
 
458
        This API is semi-public; it only for use by subclasses, all other code
 
459
        should consider it to be private.
 
460
        """
 
461
        self._revision_history_cache = None
 
462
        self._revision_id_to_revno_cache = None
 
463
        self._last_revision_info_cache = None
 
464
 
 
465
    def _gen_revision_history(self):
 
466
        """Return sequence of revision hashes on to this branch.
 
467
        
 
468
        Unlike revision_history, this method always regenerates or rereads the
 
469
        revision history, i.e. it does not cache the result, so repeated calls
 
470
        may be expensive.
 
471
 
 
472
        Concrete subclasses should override this instead of revision_history so
 
473
        that subclasses do not need to deal with caching logic.
 
474
        
 
475
        This API is semi-public; it only for use by subclasses, all other code
 
476
        should consider it to be private.
 
477
        """
 
478
        raise NotImplementedError(self._gen_revision_history)
 
479
 
 
480
    @needs_read_lock
 
481
    def revision_history(self):
 
482
        """Return sequence of revision ids on this branch.
 
483
        
 
484
        This method will cache the revision history for as long as it is safe to
 
485
        do so.
 
486
        """
 
487
        if 'evil' in debug.debug_flags:
 
488
            mutter_callsite(3, "revision_history scales with history.")
 
489
        if self._revision_history_cache is not None:
 
490
            history = self._revision_history_cache
 
491
        else:
 
492
            history = self._gen_revision_history()
 
493
            self._cache_revision_history(history)
 
494
        return list(history)
 
495
 
 
496
    def revno(self):
 
497
        """Return current revision number for this branch.
 
498
 
 
499
        That is equivalent to the number of revisions committed to
 
500
        this branch.
 
501
        """
 
502
        return self.last_revision_info()[0]
 
503
 
 
504
    def unbind(self):
 
505
        """Older format branches cannot bind or unbind."""
 
506
        raise errors.UpgradeRequired(self.base)
 
507
 
 
508
    def set_append_revisions_only(self, enabled):
 
509
        """Older format branches are never restricted to append-only"""
 
510
        raise errors.UpgradeRequired(self.base)
 
511
 
 
512
    def last_revision(self):
 
513
        """Return last revision id, or NULL_REVISION."""
 
514
        return self.last_revision_info()[1]
 
515
 
 
516
    @needs_read_lock
 
517
    def last_revision_info(self):
 
518
        """Return information about the last revision.
 
519
 
 
520
        :return: A tuple (revno, revision_id).
 
521
        """
 
522
        if self._last_revision_info_cache is None:
 
523
            self._last_revision_info_cache = self._last_revision_info()
 
524
        return self._last_revision_info_cache
 
525
 
 
526
    def _last_revision_info(self):
 
527
        rh = self.revision_history()
 
528
        revno = len(rh)
 
529
        if revno:
 
530
            return (revno, rh[-1])
 
531
        else:
 
532
            return (0, _mod_revision.NULL_REVISION)
 
533
 
 
534
    @deprecated_method(deprecated_in((1, 6, 0)))
 
535
    def missing_revisions(self, other, stop_revision=None):
 
536
        """Return a list of new revisions that would perfectly fit.
 
537
        
 
538
        If self and other have not diverged, return a list of the revisions
 
539
        present in other, but missing from self.
 
540
        """
 
541
        self_history = self.revision_history()
 
542
        self_len = len(self_history)
 
543
        other_history = other.revision_history()
 
544
        other_len = len(other_history)
 
545
        common_index = min(self_len, other_len) -1
 
546
        if common_index >= 0 and \
 
547
            self_history[common_index] != other_history[common_index]:
 
548
            raise errors.DivergedBranches(self, other)
 
549
 
 
550
        if stop_revision is None:
 
551
            stop_revision = other_len
 
552
        else:
 
553
            if stop_revision > other_len:
 
554
                raise errors.NoSuchRevision(self, stop_revision)
 
555
        return other_history[self_len:stop_revision]
 
556
 
 
557
    @needs_write_lock
 
558
    def update_revisions(self, other, stop_revision=None, overwrite=False,
 
559
                         graph=None):
 
560
        """Pull in new perfect-fit revisions.
 
561
 
 
562
        :param other: Another Branch to pull from
 
563
        :param stop_revision: Updated until the given revision
 
564
        :param overwrite: Always set the branch pointer, rather than checking
 
565
            to see if it is a proper descendant.
 
566
        :param graph: A Graph object that can be used to query history
 
567
            information. This can be None.
 
568
        :return: None
 
569
        """
 
570
        other.lock_read()
 
571
        try:
 
572
            other_revno, other_last_revision = other.last_revision_info()
 
573
            stop_revno = None # unknown
 
574
            if stop_revision is None:
 
575
                stop_revision = other_last_revision
 
576
                if _mod_revision.is_null(stop_revision):
 
577
                    # if there are no commits, we're done.
 
578
                    return
 
579
                stop_revno = other_revno
 
580
 
 
581
            # what's the current last revision, before we fetch [and change it
 
582
            # possibly]
 
583
            last_rev = _mod_revision.ensure_null(self.last_revision())
 
584
            # we fetch here so that we don't process data twice in the common
 
585
            # case of having something to pull, and so that the check for 
 
586
            # already merged can operate on the just fetched graph, which will
 
587
            # be cached in memory.
 
588
            self.fetch(other, stop_revision)
 
589
            # Check to see if one is an ancestor of the other
 
590
            if not overwrite:
 
591
                if graph is None:
 
592
                    graph = self.repository.get_graph()
 
593
                if self._check_if_descendant_or_diverged(
 
594
                        stop_revision, last_rev, graph, other):
 
595
                    # stop_revision is a descendant of last_rev, but we aren't
 
596
                    # overwriting, so we're done.
 
597
                    return
 
598
            if stop_revno is None:
 
599
                if graph is None:
 
600
                    graph = self.repository.get_graph()
 
601
                this_revno, this_last_revision = self.last_revision_info()
 
602
                stop_revno = graph.find_distance_to_null(stop_revision,
 
603
                                [(other_last_revision, other_revno),
 
604
                                 (this_last_revision, this_revno)])
 
605
            self.set_last_revision_info(stop_revno, stop_revision)
 
606
        finally:
 
607
            other.unlock()
 
608
 
 
609
    def revision_id_to_revno(self, revision_id):
 
610
        """Given a revision id, return its revno"""
 
611
        if _mod_revision.is_null(revision_id):
 
612
            return 0
 
613
        history = self.revision_history()
 
614
        try:
 
615
            return history.index(revision_id) + 1
 
616
        except ValueError:
 
617
            raise errors.NoSuchRevision(self, revision_id)
 
618
 
 
619
    def get_rev_id(self, revno, history=None):
 
620
        """Find the revision id of the specified revno."""
 
621
        if revno == 0:
 
622
            return _mod_revision.NULL_REVISION
 
623
        if history is None:
 
624
            history = self.revision_history()
 
625
        if revno <= 0 or revno > len(history):
 
626
            raise errors.NoSuchRevision(self, revno)
 
627
        return history[revno - 1]
 
628
 
 
629
    def pull(self, source, overwrite=False, stop_revision=None,
 
630
             possible_transports=None, _override_hook_target=None):
 
631
        """Mirror source into this branch.
 
632
 
 
633
        This branch is considered to be 'local', having low latency.
 
634
 
 
635
        :returns: PullResult instance
 
636
        """
 
637
        raise NotImplementedError(self.pull)
 
638
 
 
639
    def push(self, target, overwrite=False, stop_revision=None):
 
640
        """Mirror this branch into target.
 
641
 
 
642
        This branch is considered to be 'local', having low latency.
 
643
        """
 
644
        raise NotImplementedError(self.push)
 
645
 
 
646
    def basis_tree(self):
 
647
        """Return `Tree` object for last revision."""
 
648
        return self.repository.revision_tree(self.last_revision())
 
649
 
 
650
    def get_parent(self):
 
651
        """Return the parent location of the branch.
 
652
 
 
653
        This is the default location for push/pull/missing.  The usual
 
654
        pattern is that the user can override it by specifying a
 
655
        location.
 
656
        """
 
657
        raise NotImplementedError(self.get_parent)
 
658
 
 
659
    def _set_config_location(self, name, url, config=None,
 
660
                             make_relative=False):
 
661
        if config is None:
 
662
            config = self.get_config()
 
663
        if url is None:
 
664
            url = ''
 
665
        elif make_relative:
 
666
            url = urlutils.relative_url(self.base, url)
 
667
        config.set_user_option(name, url, warn_masked=True)
 
668
 
 
669
    def _get_config_location(self, name, config=None):
 
670
        if config is None:
 
671
            config = self.get_config()
 
672
        location = config.get_user_option(name)
 
673
        if location == '':
 
674
            location = None
 
675
        return location
 
676
 
 
677
    def get_submit_branch(self):
 
678
        """Return the submit location of the branch.
 
679
 
 
680
        This is the default location for bundle.  The usual
 
681
        pattern is that the user can override it by specifying a
 
682
        location.
 
683
        """
 
684
        return self.get_config().get_user_option('submit_branch')
 
685
 
 
686
    def set_submit_branch(self, location):
 
687
        """Return the submit location of the branch.
 
688
 
 
689
        This is the default location for bundle.  The usual
 
690
        pattern is that the user can override it by specifying a
 
691
        location.
 
692
        """
 
693
        self.get_config().set_user_option('submit_branch', location,
 
694
            warn_masked=True)
 
695
 
 
696
    def get_public_branch(self):
 
697
        """Return the public location of the branch.
 
698
 
 
699
        This is is used by merge directives.
 
700
        """
 
701
        return self._get_config_location('public_branch')
 
702
 
 
703
    def set_public_branch(self, location):
 
704
        """Return the submit location of the branch.
 
705
 
 
706
        This is the default location for bundle.  The usual
 
707
        pattern is that the user can override it by specifying a
 
708
        location.
 
709
        """
 
710
        self._set_config_location('public_branch', location)
 
711
 
 
712
    def get_push_location(self):
 
713
        """Return the None or the location to push this branch to."""
 
714
        push_loc = self.get_config().get_user_option('push_location')
 
715
        return push_loc
 
716
 
 
717
    def set_push_location(self, location):
 
718
        """Set a new push location for this branch."""
 
719
        raise NotImplementedError(self.set_push_location)
 
720
 
 
721
    def set_parent(self, url):
 
722
        raise NotImplementedError(self.set_parent)
 
723
 
 
724
    @needs_write_lock
 
725
    def update(self):
 
726
        """Synchronise this branch with the master branch if any. 
 
727
 
 
728
        :return: None or the last_revision pivoted out during the update.
 
729
        """
 
730
        return None
 
731
 
 
732
    def check_revno(self, revno):
 
733
        """\
 
734
        Check whether a revno corresponds to any revision.
 
735
        Zero (the NULL revision) is considered valid.
 
736
        """
 
737
        if revno != 0:
 
738
            self.check_real_revno(revno)
 
739
            
 
740
    def check_real_revno(self, revno):
 
741
        """\
 
742
        Check whether a revno corresponds to a real revision.
 
743
        Zero (the NULL revision) is considered invalid
 
744
        """
 
745
        if revno < 1 or revno > self.revno():
 
746
            raise errors.InvalidRevisionNumber(revno)
 
747
 
 
748
    @needs_read_lock
 
749
    def clone(self, to_bzrdir, revision_id=None):
 
750
        """Clone this branch into to_bzrdir preserving all semantic values.
 
751
        
 
752
        revision_id: if not None, the revision history in the new branch will
 
753
                     be truncated to end with revision_id.
 
754
        """
 
755
        result = to_bzrdir.create_branch()
 
756
        self.copy_content_into(result, revision_id=revision_id)
 
757
        return  result
 
758
 
 
759
    @needs_read_lock
 
760
    def sprout(self, to_bzrdir, revision_id=None):
 
761
        """Create a new line of development from the branch, into to_bzrdir.
 
762
 
 
763
        to_bzrdir controls the branch format.
 
764
 
 
765
        revision_id: if not None, the revision history in the new branch will
 
766
                     be truncated to end with revision_id.
 
767
        """
 
768
        result = to_bzrdir.create_branch()
 
769
        self.copy_content_into(result, revision_id=revision_id)
 
770
        result.set_parent(self.bzrdir.root_transport.base)
 
771
        return result
 
772
 
 
773
    def _synchronize_history(self, destination, revision_id):
 
774
        """Synchronize last revision and revision history between branches.
 
775
 
 
776
        This version is most efficient when the destination is also a
 
777
        BzrBranch6, but works for BzrBranch5, as long as the destination's
 
778
        repository contains all the lefthand ancestors of the intended
 
779
        last_revision.  If not, set_last_revision_info will fail.
 
780
 
 
781
        :param destination: The branch to copy the history into
 
782
        :param revision_id: The revision-id to truncate history at.  May
 
783
          be None to copy complete history.
 
784
        """
 
785
        source_revno, source_revision_id = self.last_revision_info()
 
786
        if revision_id is None:
 
787
            revno, revision_id = source_revno, source_revision_id
 
788
        elif source_revision_id == revision_id:
 
789
            # we know the revno without needing to walk all of history
 
790
            revno = source_revno
 
791
        else:
 
792
            # To figure out the revno for a random revision, we need to build
 
793
            # the revision history, and count its length.
 
794
            # We don't care about the order, just how long it is.
 
795
            # Alternatively, we could start at the current location, and count
 
796
            # backwards. But there is no guarantee that we will find it since
 
797
            # it may be a merged revision.
 
798
            revno = len(list(self.repository.iter_reverse_revision_history(
 
799
                                                                revision_id)))
 
800
        destination.set_last_revision_info(revno, revision_id)
 
801
    
 
802
    @needs_read_lock
 
803
    def copy_content_into(self, destination, revision_id=None):
 
804
        """Copy the content of self into destination.
 
805
 
 
806
        revision_id: if not None, the revision history in the new branch will
 
807
                     be truncated to end with revision_id.
 
808
        """
 
809
        self._synchronize_history(destination, revision_id)
 
810
        try:
 
811
            parent = self.get_parent()
 
812
        except errors.InaccessibleParent, e:
 
813
            mutter('parent was not accessible to copy: %s', e)
 
814
        else:
 
815
            if parent:
 
816
                destination.set_parent(parent)
 
817
        self.tags.merge_to(destination.tags)
 
818
 
 
819
    @needs_read_lock
 
820
    def check(self):
 
821
        """Check consistency of the branch.
 
822
 
 
823
        In particular this checks that revisions given in the revision-history
 
824
        do actually match up in the revision graph, and that they're all 
 
825
        present in the repository.
 
826
        
 
827
        Callers will typically also want to check the repository.
 
828
 
 
829
        :return: A BranchCheckResult.
 
830
        """
 
831
        mainline_parent_id = None
 
832
        last_revno, last_revision_id = self.last_revision_info()
 
833
        real_rev_history = list(self.repository.iter_reverse_revision_history(
 
834
                                last_revision_id))
 
835
        real_rev_history.reverse()
 
836
        if len(real_rev_history) != last_revno:
 
837
            raise errors.BzrCheckError('revno does not match len(mainline)'
 
838
                ' %s != %s' % (last_revno, len(real_rev_history)))
 
839
        # TODO: We should probably also check that real_rev_history actually
 
840
        #       matches self.revision_history()
 
841
        for revision_id in real_rev_history:
 
842
            try:
 
843
                revision = self.repository.get_revision(revision_id)
 
844
            except errors.NoSuchRevision, e:
 
845
                raise errors.BzrCheckError("mainline revision {%s} not in repository"
 
846
                            % revision_id)
 
847
            # In general the first entry on the revision history has no parents.
 
848
            # But it's not illegal for it to have parents listed; this can happen
 
849
            # in imports from Arch when the parents weren't reachable.
 
850
            if mainline_parent_id is not None:
 
851
                if mainline_parent_id not in revision.parent_ids:
 
852
                    raise errors.BzrCheckError("previous revision {%s} not listed among "
 
853
                                        "parents of {%s}"
 
854
                                        % (mainline_parent_id, revision_id))
 
855
            mainline_parent_id = revision_id
 
856
        return BranchCheckResult(self)
 
857
 
 
858
    def _get_checkout_format(self):
 
859
        """Return the most suitable metadir for a checkout of this branch.
 
860
        Weaves are used if this branch's repository uses weaves.
 
861
        """
 
862
        if isinstance(self.bzrdir, bzrdir.BzrDirPreSplitOut):
 
863
            from bzrlib.repofmt import weaverepo
 
864
            format = bzrdir.BzrDirMetaFormat1()
 
865
            format.repository_format = weaverepo.RepositoryFormat7()
 
866
        else:
 
867
            format = self.repository.bzrdir.checkout_metadir()
 
868
            format.set_branch_format(self._format)
 
869
        return format
 
870
 
 
871
    def create_checkout(self, to_location, revision_id=None,
 
872
                        lightweight=False, accelerator_tree=None,
 
873
                        hardlink=False):
 
874
        """Create a checkout of a branch.
 
875
        
 
876
        :param to_location: The url to produce the checkout at
 
877
        :param revision_id: The revision to check out
 
878
        :param lightweight: If True, produce a lightweight checkout, otherwise,
 
879
        produce a bound branch (heavyweight checkout)
 
880
        :param accelerator_tree: A tree which can be used for retrieving file
 
881
            contents more quickly than the revision tree, i.e. a workingtree.
 
882
            The revision tree will be used for cases where accelerator_tree's
 
883
            content is different.
 
884
        :param hardlink: If true, hard-link files from accelerator_tree,
 
885
            where possible.
 
886
        :return: The tree of the created checkout
 
887
        """
 
888
        t = transport.get_transport(to_location)
 
889
        t.ensure_base()
 
890
        if lightweight:
 
891
            format = self._get_checkout_format()
 
892
            checkout = format.initialize_on_transport(t)
 
893
            from_branch = BranchReferenceFormat().initialize(checkout, self)
 
894
        else:
 
895
            format = self._get_checkout_format()
 
896
            checkout_branch = bzrdir.BzrDir.create_branch_convenience(
 
897
                to_location, force_new_tree=False, format=format)
 
898
            checkout = checkout_branch.bzrdir
 
899
            checkout_branch.bind(self)
 
900
            # pull up to the specified revision_id to set the initial 
 
901
            # branch tip correctly, and seed it with history.
 
902
            checkout_branch.pull(self, stop_revision=revision_id)
 
903
            from_branch=None
 
904
        tree = checkout.create_workingtree(revision_id,
 
905
                                           from_branch=from_branch,
 
906
                                           accelerator_tree=accelerator_tree,
 
907
                                           hardlink=hardlink)
 
908
        basis_tree = tree.basis_tree()
 
909
        basis_tree.lock_read()
 
910
        try:
 
911
            for path, file_id in basis_tree.iter_references():
 
912
                reference_parent = self.reference_parent(file_id, path)
 
913
                reference_parent.create_checkout(tree.abspath(path),
 
914
                    basis_tree.get_reference_revision(file_id, path),
 
915
                    lightweight)
 
916
        finally:
 
917
            basis_tree.unlock()
 
918
        return tree
 
919
 
 
920
    @needs_write_lock
 
921
    def reconcile(self, thorough=True):
 
922
        """Make sure the data stored in this branch is consistent."""
 
923
        from bzrlib.reconcile import BranchReconciler
 
924
        reconciler = BranchReconciler(self, thorough=thorough)
 
925
        reconciler.reconcile()
 
926
        return reconciler
 
927
 
 
928
    def reference_parent(self, file_id, path):
 
929
        """Return the parent branch for a tree-reference file_id
 
930
        :param file_id: The file_id of the tree reference
 
931
        :param path: The path of the file_id in the tree
 
932
        :return: A branch associated with the file_id
 
933
        """
 
934
        # FIXME should provide multiple branches, based on config
 
935
        return Branch.open(self.bzrdir.root_transport.clone(path).base)
 
936
 
 
937
    def supports_tags(self):
 
938
        return self._format.supports_tags()
 
939
 
 
940
    def _check_if_descendant_or_diverged(self, revision_a, revision_b, graph,
 
941
                                         other_branch):
 
942
        """Ensure that revision_b is a descendant of revision_a.
 
943
 
 
944
        This is a helper function for update_revisions.
 
945
        
 
946
        :raises: DivergedBranches if revision_b has diverged from revision_a.
 
947
        :returns: True if revision_b is a descendant of revision_a.
 
948
        """
 
949
        relation = self._revision_relations(revision_a, revision_b, graph)
 
950
        if relation == 'b_descends_from_a':
 
951
            return True
 
952
        elif relation == 'diverged':
 
953
            raise errors.DivergedBranches(self, other_branch)
 
954
        elif relation == 'a_descends_from_b':
 
955
            return False
 
956
        else:
 
957
            raise AssertionError("invalid relation: %r" % (relation,))
 
958
 
 
959
    def _revision_relations(self, revision_a, revision_b, graph):
 
960
        """Determine the relationship between two revisions.
 
961
        
 
962
        :returns: One of: 'a_descends_from_b', 'b_descends_from_a', 'diverged'
 
963
        """
 
964
        heads = graph.heads([revision_a, revision_b])
 
965
        if heads == set([revision_b]):
 
966
            return 'b_descends_from_a'
 
967
        elif heads == set([revision_a, revision_b]):
 
968
            # These branches have diverged
 
969
            return 'diverged'
 
970
        elif heads == set([revision_a]):
 
971
            return 'a_descends_from_b'
 
972
        else:
 
973
            raise AssertionError("invalid heads: %r" % (heads,))
 
974
 
 
975
 
 
976
class BranchFormat(object):
 
977
    """An encapsulation of the initialization and open routines for a format.
 
978
 
 
979
    Formats provide three things:
 
980
     * An initialization routine,
 
981
     * a format string,
 
982
     * an open routine.
 
983
 
 
984
    Formats are placed in an dict by their format string for reference 
 
985
    during branch opening. Its not required that these be instances, they
 
986
    can be classes themselves with class methods - it simply depends on 
 
987
    whether state is needed for a given format or not.
 
988
 
 
989
    Once a format is deprecated, just deprecate the initialize and open
 
990
    methods on the format class. Do not deprecate the object, as the 
 
991
    object will be created every time regardless.
 
992
    """
 
993
 
 
994
    _default_format = None
 
995
    """The default format used for new branches."""
 
996
 
 
997
    _formats = {}
 
998
    """The known formats."""
 
999
 
 
1000
    def __eq__(self, other):
 
1001
        return self.__class__ is other.__class__
 
1002
 
 
1003
    def __ne__(self, other):
 
1004
        return not (self == other)
 
1005
 
 
1006
    @classmethod
 
1007
    def find_format(klass, a_bzrdir):
 
1008
        """Return the format for the branch object in a_bzrdir."""
 
1009
        try:
 
1010
            transport = a_bzrdir.get_branch_transport(None)
 
1011
            format_string = transport.get("format").read()
 
1012
            return klass._formats[format_string]
 
1013
        except errors.NoSuchFile:
 
1014
            raise errors.NotBranchError(path=transport.base)
 
1015
        except KeyError:
 
1016
            raise errors.UnknownFormatError(format=format_string, kind='branch')
 
1017
 
 
1018
    @classmethod
 
1019
    def get_default_format(klass):
 
1020
        """Return the current default format."""
 
1021
        return klass._default_format
 
1022
 
 
1023
    def get_reference(self, a_bzrdir):
 
1024
        """Get the target reference of the branch in a_bzrdir.
 
1025
 
 
1026
        format probing must have been completed before calling
 
1027
        this method - it is assumed that the format of the branch
 
1028
        in a_bzrdir is correct.
 
1029
 
 
1030
        :param a_bzrdir: The bzrdir to get the branch data from.
 
1031
        :return: None if the branch is not a reference branch.
 
1032
        """
 
1033
        return None
 
1034
 
 
1035
    @classmethod
 
1036
    def set_reference(self, a_bzrdir, to_branch):
 
1037
        """Set the target reference of the branch in a_bzrdir.
 
1038
 
 
1039
        format probing must have been completed before calling
 
1040
        this method - it is assumed that the format of the branch
 
1041
        in a_bzrdir is correct.
 
1042
 
 
1043
        :param a_bzrdir: The bzrdir to set the branch reference for.
 
1044
        :param to_branch: branch that the checkout is to reference
 
1045
        """
 
1046
        raise NotImplementedError(self.set_reference)
 
1047
 
 
1048
    def get_format_string(self):
 
1049
        """Return the ASCII format string that identifies this format."""
 
1050
        raise NotImplementedError(self.get_format_string)
 
1051
 
 
1052
    def get_format_description(self):
 
1053
        """Return the short format description for this format."""
 
1054
        raise NotImplementedError(self.get_format_description)
 
1055
 
 
1056
    def _initialize_helper(self, a_bzrdir, utf8_files, lock_type='metadir',
 
1057
                           set_format=True):
 
1058
        """Initialize a branch in a bzrdir, with specified files
 
1059
 
 
1060
        :param a_bzrdir: The bzrdir to initialize the branch in
 
1061
        :param utf8_files: The files to create as a list of
 
1062
            (filename, content) tuples
 
1063
        :param set_format: If True, set the format with
 
1064
            self.get_format_string.  (BzrBranch4 has its format set
 
1065
            elsewhere)
 
1066
        :return: a branch in this format
 
1067
        """
 
1068
        mutter('creating branch %r in %s', self, a_bzrdir.transport.base)
 
1069
        branch_transport = a_bzrdir.get_branch_transport(self)
 
1070
        lock_map = {
 
1071
            'metadir': ('lock', lockdir.LockDir),
 
1072
            'branch4': ('branch-lock', lockable_files.TransportLock),
 
1073
        }
 
1074
        lock_name, lock_class = lock_map[lock_type]
 
1075
        control_files = lockable_files.LockableFiles(branch_transport,
 
1076
            lock_name, lock_class)
 
1077
        control_files.create_lock()
 
1078
        control_files.lock_write()
 
1079
        if set_format:
 
1080
            utf8_files += [('format', self.get_format_string())]
 
1081
        try:
 
1082
            for (filename, content) in utf8_files:
 
1083
                branch_transport.put_bytes(
 
1084
                    filename, content,
 
1085
                    mode=a_bzrdir._get_file_mode())
 
1086
        finally:
 
1087
            control_files.unlock()
 
1088
        return self.open(a_bzrdir, _found=True)
 
1089
 
 
1090
    def initialize(self, a_bzrdir):
 
1091
        """Create a branch of this format in a_bzrdir."""
 
1092
        raise NotImplementedError(self.initialize)
 
1093
 
 
1094
    def is_supported(self):
 
1095
        """Is this format supported?
 
1096
 
 
1097
        Supported formats can be initialized and opened.
 
1098
        Unsupported formats may not support initialization or committing or 
 
1099
        some other features depending on the reason for not being supported.
 
1100
        """
 
1101
        return True
 
1102
 
 
1103
    def open(self, a_bzrdir, _found=False):
 
1104
        """Return the branch object for a_bzrdir
 
1105
 
 
1106
        _found is a private parameter, do not use it. It is used to indicate
 
1107
               if format probing has already be done.
 
1108
        """
 
1109
        raise NotImplementedError(self.open)
 
1110
 
 
1111
    @classmethod
 
1112
    def register_format(klass, format):
 
1113
        klass._formats[format.get_format_string()] = format
 
1114
 
 
1115
    @classmethod
 
1116
    def set_default_format(klass, format):
 
1117
        klass._default_format = format
 
1118
 
 
1119
    def supports_stacking(self):
 
1120
        """True if this format records a stacked-on branch."""
 
1121
        return False
 
1122
 
 
1123
    @classmethod
 
1124
    def unregister_format(klass, format):
 
1125
        del klass._formats[format.get_format_string()]
 
1126
 
 
1127
    def __str__(self):
 
1128
        return self.get_format_string().rstrip()
 
1129
 
 
1130
    def supports_tags(self):
 
1131
        """True if this format supports tags stored in the branch"""
 
1132
        return False  # by default
 
1133
 
 
1134
 
 
1135
class BranchHooks(Hooks):
 
1136
    """A dictionary mapping hook name to a list of callables for branch hooks.
 
1137
    
 
1138
    e.g. ['set_rh'] Is the list of items to be called when the
 
1139
    set_revision_history function is invoked.
 
1140
    """
 
1141
 
 
1142
    def __init__(self):
 
1143
        """Create the default hooks.
 
1144
 
 
1145
        These are all empty initially, because by default nothing should get
 
1146
        notified.
 
1147
        """
 
1148
        Hooks.__init__(self)
 
1149
        # Introduced in 0.15:
 
1150
        # invoked whenever the revision history has been set
 
1151
        # with set_revision_history. The api signature is
 
1152
        # (branch, revision_history), and the branch will
 
1153
        # be write-locked.
 
1154
        self['set_rh'] = []
 
1155
        # Invoked after a branch is opened. The api signature is (branch).
 
1156
        self['open'] = []
 
1157
        # invoked after a push operation completes.
 
1158
        # the api signature is
 
1159
        # (push_result)
 
1160
        # containing the members
 
1161
        # (source, local, master, old_revno, old_revid, new_revno, new_revid)
 
1162
        # where local is the local target branch or None, master is the target 
 
1163
        # master branch, and the rest should be self explanatory. The source
 
1164
        # is read locked and the target branches write locked. Source will
 
1165
        # be the local low-latency branch.
 
1166
        self['post_push'] = []
 
1167
        # invoked after a pull operation completes.
 
1168
        # the api signature is
 
1169
        # (pull_result)
 
1170
        # containing the members
 
1171
        # (source, local, master, old_revno, old_revid, new_revno, new_revid)
 
1172
        # where local is the local branch or None, master is the target 
 
1173
        # master branch, and the rest should be self explanatory. The source
 
1174
        # is read locked and the target branches write locked. The local
 
1175
        # branch is the low-latency branch.
 
1176
        self['post_pull'] = []
 
1177
        # invoked before a commit operation takes place.
 
1178
        # the api signature is
 
1179
        # (local, master, old_revno, old_revid, future_revno, future_revid,
 
1180
        #  tree_delta, future_tree).
 
1181
        # old_revid is NULL_REVISION for the first commit to a branch
 
1182
        # tree_delta is a TreeDelta object describing changes from the basis
 
1183
        # revision, hooks MUST NOT modify this delta
 
1184
        # future_tree is an in-memory tree obtained from
 
1185
        # CommitBuilder.revision_tree() and hooks MUST NOT modify this tree
 
1186
        self['pre_commit'] = []
 
1187
        # invoked after a commit operation completes.
 
1188
        # the api signature is 
 
1189
        # (local, master, old_revno, old_revid, new_revno, new_revid)
 
1190
        # old_revid is NULL_REVISION for the first commit to a branch.
 
1191
        self['post_commit'] = []
 
1192
        # invoked after a uncommit operation completes.
 
1193
        # the api signature is
 
1194
        # (local, master, old_revno, old_revid, new_revno, new_revid) where
 
1195
        # local is the local branch or None, master is the target branch,
 
1196
        # and an empty branch recieves new_revno of 0, new_revid of None.
 
1197
        self['post_uncommit'] = []
 
1198
        # Introduced in 1.6
 
1199
        # Invoked before the tip of a branch changes.
 
1200
        # the api signature is
 
1201
        # (params) where params is a ChangeBranchTipParams with the members
 
1202
        # (branch, old_revno, new_revno, old_revid, new_revid)
 
1203
        self['pre_change_branch_tip'] = []
 
1204
        # Introduced in 1.4
 
1205
        # Invoked after the tip of a branch changes.
 
1206
        # the api signature is
 
1207
        # (params) where params is a ChangeBranchTipParams with the members
 
1208
        # (branch, old_revno, new_revno, old_revid, new_revid)
 
1209
        self['post_change_branch_tip'] = []
 
1210
        # Introduced in 1.9
 
1211
        # Invoked when a stacked branch activates its fallback locations and
 
1212
        # allows the transformation of the url of said location.
 
1213
        # the api signature is
 
1214
        # (branch, url) where branch is the branch having its fallback
 
1215
        # location activated and url is the url for the fallback location.
 
1216
        # The hook should return a url.
 
1217
        self['transform_fallback_location'] = []
 
1218
 
 
1219
 
 
1220
# install the default hooks into the Branch class.
 
1221
Branch.hooks = BranchHooks()
 
1222
 
 
1223
 
 
1224
class ChangeBranchTipParams(object):
 
1225
    """Object holding parameters passed to *_change_branch_tip hooks.
 
1226
 
 
1227
    There are 5 fields that hooks may wish to access:
 
1228
 
 
1229
    :ivar branch: the branch being changed
 
1230
    :ivar old_revno: revision number before the change
 
1231
    :ivar new_revno: revision number after the change
 
1232
    :ivar old_revid: revision id before the change
 
1233
    :ivar new_revid: revision id after the change
 
1234
 
 
1235
    The revid fields are strings. The revno fields are integers.
 
1236
    """
 
1237
 
 
1238
    def __init__(self, branch, old_revno, new_revno, old_revid, new_revid):
 
1239
        """Create a group of ChangeBranchTip parameters.
 
1240
 
 
1241
        :param branch: The branch being changed.
 
1242
        :param old_revno: Revision number before the change.
 
1243
        :param new_revno: Revision number after the change.
 
1244
        :param old_revid: Tip revision id before the change.
 
1245
        :param new_revid: Tip revision id after the change.
 
1246
        """
 
1247
        self.branch = branch
 
1248
        self.old_revno = old_revno
 
1249
        self.new_revno = new_revno
 
1250
        self.old_revid = old_revid
 
1251
        self.new_revid = new_revid
 
1252
 
 
1253
    def __eq__(self, other):
 
1254
        return self.__dict__ == other.__dict__
 
1255
    
 
1256
    def __repr__(self):
 
1257
        return "<%s of %s from (%s, %s) to (%s, %s)>" % (
 
1258
            self.__class__.__name__, self.branch, 
 
1259
            self.old_revno, self.old_revid, self.new_revno, self.new_revid)
 
1260
 
 
1261
 
 
1262
class BzrBranchFormat4(BranchFormat):
 
1263
    """Bzr branch format 4.
 
1264
 
 
1265
    This format has:
 
1266
     - a revision-history file.
 
1267
     - a branch-lock lock file [ to be shared with the bzrdir ]
 
1268
    """
 
1269
 
 
1270
    def get_format_description(self):
 
1271
        """See BranchFormat.get_format_description()."""
 
1272
        return "Branch format 4"
 
1273
 
 
1274
    def initialize(self, a_bzrdir):
 
1275
        """Create a branch of this format in a_bzrdir."""
 
1276
        utf8_files = [('revision-history', ''),
 
1277
                      ('branch-name', ''),
 
1278
                      ]
 
1279
        return self._initialize_helper(a_bzrdir, utf8_files,
 
1280
                                       lock_type='branch4', set_format=False)
 
1281
 
 
1282
    def __init__(self):
 
1283
        super(BzrBranchFormat4, self).__init__()
 
1284
        self._matchingbzrdir = bzrdir.BzrDirFormat6()
 
1285
 
 
1286
    def open(self, a_bzrdir, _found=False):
 
1287
        """Return the branch object for a_bzrdir
 
1288
 
 
1289
        _found is a private parameter, do not use it. It is used to indicate
 
1290
               if format probing has already be done.
 
1291
        """
 
1292
        if not _found:
 
1293
            # we are being called directly and must probe.
 
1294
            raise NotImplementedError
 
1295
        return BzrBranch(_format=self,
 
1296
                         _control_files=a_bzrdir._control_files,
 
1297
                         a_bzrdir=a_bzrdir,
 
1298
                         _repository=a_bzrdir.open_repository())
 
1299
 
 
1300
    def __str__(self):
 
1301
        return "Bazaar-NG branch format 4"
 
1302
 
 
1303
 
 
1304
class BranchFormatMetadir(BranchFormat):
 
1305
    """Common logic for meta-dir based branch formats."""
 
1306
 
 
1307
    def _branch_class(self):
 
1308
        """What class to instantiate on open calls."""
 
1309
        raise NotImplementedError(self._branch_class)
 
1310
 
 
1311
    def open(self, a_bzrdir, _found=False):
 
1312
        """Return the branch object for a_bzrdir.
 
1313
 
 
1314
        _found is a private parameter, do not use it. It is used to indicate
 
1315
               if format probing has already be done.
 
1316
        """
 
1317
        if not _found:
 
1318
            format = BranchFormat.find_format(a_bzrdir)
 
1319
            if format.__class__ != self.__class__:
 
1320
                raise AssertionError("wrong format %r found for %r" %
 
1321
                    (format, self))
 
1322
        try:
 
1323
            transport = a_bzrdir.get_branch_transport(None)
 
1324
            control_files = lockable_files.LockableFiles(transport, 'lock',
 
1325
                                                         lockdir.LockDir)
 
1326
            return self._branch_class()(_format=self,
 
1327
                              _control_files=control_files,
 
1328
                              a_bzrdir=a_bzrdir,
 
1329
                              _repository=a_bzrdir.find_repository())
 
1330
        except errors.NoSuchFile:
 
1331
            raise errors.NotBranchError(path=transport.base)
 
1332
 
 
1333
    def __init__(self):
 
1334
        super(BranchFormatMetadir, self).__init__()
 
1335
        self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
 
1336
        self._matchingbzrdir.set_branch_format(self)
 
1337
 
 
1338
    def supports_tags(self):
 
1339
        return True
 
1340
 
 
1341
 
 
1342
class BzrBranchFormat5(BranchFormatMetadir):
 
1343
    """Bzr branch format 5.
 
1344
 
 
1345
    This format has:
 
1346
     - a revision-history file.
 
1347
     - a format string
 
1348
     - a lock dir guarding the branch itself
 
1349
     - all of this stored in a branch/ subdirectory
 
1350
     - works with shared repositories.
 
1351
 
 
1352
    This format is new in bzr 0.8.
 
1353
    """
 
1354
 
 
1355
    def _branch_class(self):
 
1356
        return BzrBranch5
 
1357
 
 
1358
    def get_format_string(self):
 
1359
        """See BranchFormat.get_format_string()."""
 
1360
        return "Bazaar-NG branch format 5\n"
 
1361
 
 
1362
    def get_format_description(self):
 
1363
        """See BranchFormat.get_format_description()."""
 
1364
        return "Branch format 5"
 
1365
        
 
1366
    def initialize(self, a_bzrdir):
 
1367
        """Create a branch of this format in a_bzrdir."""
 
1368
        utf8_files = [('revision-history', ''),
 
1369
                      ('branch-name', ''),
 
1370
                      ]
 
1371
        return self._initialize_helper(a_bzrdir, utf8_files)
 
1372
 
 
1373
    def supports_tags(self):
 
1374
        return False
 
1375
 
 
1376
 
 
1377
class BzrBranchFormat6(BranchFormatMetadir):
 
1378
    """Branch format with last-revision and tags.
 
1379
 
 
1380
    Unlike previous formats, this has no explicit revision history. Instead,
 
1381
    this just stores the last-revision, and the left-hand history leading
 
1382
    up to there is the history.
 
1383
 
 
1384
    This format was introduced in bzr 0.15
 
1385
    and became the default in 0.91.
 
1386
    """
 
1387
 
 
1388
    def _branch_class(self):
 
1389
        return BzrBranch6
 
1390
 
 
1391
    def get_format_string(self):
 
1392
        """See BranchFormat.get_format_string()."""
 
1393
        return "Bazaar Branch Format 6 (bzr 0.15)\n"
 
1394
 
 
1395
    def get_format_description(self):
 
1396
        """See BranchFormat.get_format_description()."""
 
1397
        return "Branch format 6"
 
1398
 
 
1399
    def initialize(self, a_bzrdir):
 
1400
        """Create a branch of this format in a_bzrdir."""
 
1401
        utf8_files = [('last-revision', '0 null:\n'),
 
1402
                      ('branch.conf', ''),
 
1403
                      ('tags', ''),
 
1404
                      ]
 
1405
        return self._initialize_helper(a_bzrdir, utf8_files)
 
1406
 
 
1407
 
 
1408
class BzrBranchFormat7(BranchFormatMetadir):
 
1409
    """Branch format with last-revision, tags, and a stacked location pointer.
 
1410
 
 
1411
    The stacked location pointer is passed down to the repository and requires
 
1412
    a repository format with supports_external_lookups = True.
 
1413
 
 
1414
    This format was introduced in bzr 1.6.
 
1415
    """
 
1416
 
 
1417
    def _branch_class(self):
 
1418
        return BzrBranch7
 
1419
 
 
1420
    def get_format_string(self):
 
1421
        """See BranchFormat.get_format_string()."""
 
1422
        return "Bazaar Branch Format 7 (needs bzr 1.6)\n"
 
1423
 
 
1424
    def get_format_description(self):
 
1425
        """See BranchFormat.get_format_description()."""
 
1426
        return "Branch format 7"
 
1427
 
 
1428
    def initialize(self, a_bzrdir):
 
1429
        """Create a branch of this format in a_bzrdir."""
 
1430
        utf8_files = [('last-revision', '0 null:\n'),
 
1431
                      ('branch.conf', ''),
 
1432
                      ('tags', ''),
 
1433
                      ]
 
1434
        return self._initialize_helper(a_bzrdir, utf8_files)
 
1435
 
 
1436
    def __init__(self):
 
1437
        super(BzrBranchFormat7, self).__init__()
 
1438
        self._matchingbzrdir.repository_format = \
 
1439
            RepositoryFormatKnitPack5RichRoot()
 
1440
 
 
1441
    def supports_stacking(self):
 
1442
        return True
 
1443
 
 
1444
 
 
1445
class BranchReferenceFormat(BranchFormat):
 
1446
    """Bzr branch reference format.
 
1447
 
 
1448
    Branch references are used in implementing checkouts, they
 
1449
    act as an alias to the real branch which is at some other url.
 
1450
 
 
1451
    This format has:
 
1452
     - A location file
 
1453
     - a format string
 
1454
    """
 
1455
 
 
1456
    def get_format_string(self):
 
1457
        """See BranchFormat.get_format_string()."""
 
1458
        return "Bazaar-NG Branch Reference Format 1\n"
 
1459
 
 
1460
    def get_format_description(self):
 
1461
        """See BranchFormat.get_format_description()."""
 
1462
        return "Checkout reference format 1"
 
1463
 
 
1464
    def get_reference(self, a_bzrdir):
 
1465
        """See BranchFormat.get_reference()."""
 
1466
        transport = a_bzrdir.get_branch_transport(None)
 
1467
        return transport.get('location').read()
 
1468
 
 
1469
    def set_reference(self, a_bzrdir, to_branch):
 
1470
        """See BranchFormat.set_reference()."""
 
1471
        transport = a_bzrdir.get_branch_transport(None)
 
1472
        location = transport.put_bytes('location', to_branch.base)
 
1473
 
 
1474
    def initialize(self, a_bzrdir, target_branch=None):
 
1475
        """Create a branch of this format in a_bzrdir."""
 
1476
        if target_branch is None:
 
1477
            # this format does not implement branch itself, thus the implicit
 
1478
            # creation contract must see it as uninitializable
 
1479
            raise errors.UninitializableFormat(self)
 
1480
        mutter('creating branch reference in %s', a_bzrdir.transport.base)
 
1481
        branch_transport = a_bzrdir.get_branch_transport(self)
 
1482
        branch_transport.put_bytes('location',
 
1483
            target_branch.bzrdir.root_transport.base)
 
1484
        branch_transport.put_bytes('format', self.get_format_string())
 
1485
        return self.open(
 
1486
            a_bzrdir, _found=True,
 
1487
            possible_transports=[target_branch.bzrdir.root_transport])
 
1488
 
 
1489
    def __init__(self):
 
1490
        super(BranchReferenceFormat, self).__init__()
 
1491
        self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
 
1492
        self._matchingbzrdir.set_branch_format(self)
 
1493
 
 
1494
    def _make_reference_clone_function(format, a_branch):
 
1495
        """Create a clone() routine for a branch dynamically."""
 
1496
        def clone(to_bzrdir, revision_id=None):
 
1497
            """See Branch.clone()."""
 
1498
            return format.initialize(to_bzrdir, a_branch)
 
1499
            # cannot obey revision_id limits when cloning a reference ...
 
1500
            # FIXME RBC 20060210 either nuke revision_id for clone, or
 
1501
            # emit some sort of warning/error to the caller ?!
 
1502
        return clone
 
1503
 
 
1504
    def open(self, a_bzrdir, _found=False, location=None,
 
1505
             possible_transports=None):
 
1506
        """Return the branch that the branch reference in a_bzrdir points at.
 
1507
 
 
1508
        _found is a private parameter, do not use it. It is used to indicate
 
1509
               if format probing has already be done.
 
1510
        """
 
1511
        if not _found:
 
1512
            format = BranchFormat.find_format(a_bzrdir)
 
1513
            if format.__class__ != self.__class__:
 
1514
                raise AssertionError("wrong format %r found for %r" %
 
1515
                    (format, self))
 
1516
        if location is None:
 
1517
            location = self.get_reference(a_bzrdir)
 
1518
        real_bzrdir = bzrdir.BzrDir.open(
 
1519
            location, possible_transports=possible_transports)
 
1520
        result = real_bzrdir.open_branch()
 
1521
        # this changes the behaviour of result.clone to create a new reference
 
1522
        # rather than a copy of the content of the branch.
 
1523
        # I did not use a proxy object because that needs much more extensive
 
1524
        # testing, and we are only changing one behaviour at the moment.
 
1525
        # If we decide to alter more behaviours - i.e. the implicit nickname
 
1526
        # then this should be refactored to introduce a tested proxy branch
 
1527
        # and a subclass of that for use in overriding clone() and ....
 
1528
        # - RBC 20060210
 
1529
        result.clone = self._make_reference_clone_function(result)
 
1530
        return result
 
1531
 
 
1532
 
 
1533
# formats which have no format string are not discoverable
 
1534
# and not independently creatable, so are not registered.
 
1535
__format5 = BzrBranchFormat5()
 
1536
__format6 = BzrBranchFormat6()
 
1537
__format7 = BzrBranchFormat7()
 
1538
BranchFormat.register_format(__format5)
 
1539
BranchFormat.register_format(BranchReferenceFormat())
 
1540
BranchFormat.register_format(__format6)
 
1541
BranchFormat.register_format(__format7)
 
1542
BranchFormat.set_default_format(__format6)
 
1543
_legacy_formats = [BzrBranchFormat4(),
 
1544
                   ]
 
1545
 
 
1546
class BzrBranch(Branch):
 
1547
    """A branch stored in the actual filesystem.
 
1548
 
 
1549
    Note that it's "local" in the context of the filesystem; it doesn't
 
1550
    really matter if it's on an nfs/smb/afs/coda/... share, as long as
 
1551
    it's writable, and can be accessed via the normal filesystem API.
 
1552
 
 
1553
    :ivar _transport: Transport for file operations on this branch's 
 
1554
        control files, typically pointing to the .bzr/branch directory.
 
1555
    :ivar repository: Repository for this branch.
 
1556
    :ivar base: The url of the base directory for this branch; the one 
 
1557
        containing the .bzr directory.
 
1558
    """
 
1559
    
 
1560
    def __init__(self, _format=None,
 
1561
                 _control_files=None, a_bzrdir=None, _repository=None):
 
1562
        """Create new branch object at a particular location."""
 
1563
        if a_bzrdir is None:
 
1564
            raise ValueError('a_bzrdir must be supplied')
 
1565
        else:
 
1566
            self.bzrdir = a_bzrdir
 
1567
        self._base = self.bzrdir.transport.clone('..').base
 
1568
        # XXX: We should be able to just do
 
1569
        #   self.base = self.bzrdir.root_transport.base
 
1570
        # but this does not quite work yet -- mbp 20080522
 
1571
        self._format = _format
 
1572
        if _control_files is None:
 
1573
            raise ValueError('BzrBranch _control_files is None')
 
1574
        self.control_files = _control_files
 
1575
        self._transport = _control_files._transport
 
1576
        self.repository = _repository
 
1577
        Branch.__init__(self)
 
1578
 
 
1579
    def __str__(self):
 
1580
        return '%s(%r)' % (self.__class__.__name__, self.base)
 
1581
 
 
1582
    __repr__ = __str__
 
1583
 
 
1584
    def _get_base(self):
 
1585
        """Returns the directory containing the control directory."""
 
1586
        return self._base
 
1587
 
 
1588
    base = property(_get_base, doc="The URL for the root of this branch.")
 
1589
 
 
1590
    def is_locked(self):
 
1591
        return self.control_files.is_locked()
 
1592
 
 
1593
    def lock_write(self, token=None):
 
1594
        repo_token = self.repository.lock_write()
 
1595
        try:
 
1596
            token = self.control_files.lock_write(token=token)
 
1597
        except:
 
1598
            self.repository.unlock()
 
1599
            raise
 
1600
        return token
 
1601
 
 
1602
    def lock_read(self):
 
1603
        self.repository.lock_read()
 
1604
        try:
 
1605
            self.control_files.lock_read()
 
1606
        except:
 
1607
            self.repository.unlock()
 
1608
            raise
 
1609
 
 
1610
    def unlock(self):
 
1611
        # TODO: test for failed two phase locks. This is known broken.
 
1612
        try:
 
1613
            self.control_files.unlock()
 
1614
        finally:
 
1615
            self.repository.unlock()
 
1616
        if not self.control_files.is_locked():
 
1617
            # we just released the lock
 
1618
            self._clear_cached_state()
 
1619
        
 
1620
    def peek_lock_mode(self):
 
1621
        if self.control_files._lock_count == 0:
 
1622
            return None
 
1623
        else:
 
1624
            return self.control_files._lock_mode
 
1625
 
 
1626
    def get_physical_lock_status(self):
 
1627
        return self.control_files.get_physical_lock_status()
 
1628
 
 
1629
    @needs_read_lock
 
1630
    def print_file(self, file, revision_id):
 
1631
        """See Branch.print_file."""
 
1632
        return self.repository.print_file(file, revision_id)
 
1633
 
 
1634
    def _write_revision_history(self, history):
 
1635
        """Factored out of set_revision_history.
 
1636
 
 
1637
        This performs the actual writing to disk.
 
1638
        It is intended to be called by BzrBranch5.set_revision_history."""
 
1639
        self._transport.put_bytes(
 
1640
            'revision-history', '\n'.join(history),
 
1641
            mode=self.bzrdir._get_file_mode())
 
1642
 
 
1643
    @needs_write_lock
 
1644
    def set_revision_history(self, rev_history):
 
1645
        """See Branch.set_revision_history."""
 
1646
        if 'evil' in debug.debug_flags:
 
1647
            mutter_callsite(3, "set_revision_history scales with history.")
 
1648
        check_not_reserved_id = _mod_revision.check_not_reserved_id
 
1649
        for rev_id in rev_history:
 
1650
            check_not_reserved_id(rev_id)
 
1651
        if Branch.hooks['post_change_branch_tip']:
 
1652
            # Don't calculate the last_revision_info() if there are no hooks
 
1653
            # that will use it.
 
1654
            old_revno, old_revid = self.last_revision_info()
 
1655
        if len(rev_history) == 0:
 
1656
            revid = _mod_revision.NULL_REVISION
 
1657
        else:
 
1658
            revid = rev_history[-1]
 
1659
        self._run_pre_change_branch_tip_hooks(len(rev_history), revid)
 
1660
        self._write_revision_history(rev_history)
 
1661
        self._clear_cached_state()
 
1662
        self._cache_revision_history(rev_history)
 
1663
        for hook in Branch.hooks['set_rh']:
 
1664
            hook(self, rev_history)
 
1665
        if Branch.hooks['post_change_branch_tip']:
 
1666
            self._run_post_change_branch_tip_hooks(old_revno, old_revid)
 
1667
 
 
1668
    def _synchronize_history(self, destination, revision_id):
 
1669
        """Synchronize last revision and revision history between branches.
 
1670
 
 
1671
        This version is most efficient when the destination is also a
 
1672
        BzrBranch5, but works for BzrBranch6 as long as the revision
 
1673
        history is the true lefthand parent history, and all of the revisions
 
1674
        are in the destination's repository.  If not, set_revision_history
 
1675
        will fail.
 
1676
 
 
1677
        :param destination: The branch to copy the history into
 
1678
        :param revision_id: The revision-id to truncate history at.  May
 
1679
          be None to copy complete history.
 
1680
        """
 
1681
        if not isinstance(destination._format, BzrBranchFormat5):
 
1682
            super(BzrBranch, self)._synchronize_history(
 
1683
                destination, revision_id)
 
1684
            return
 
1685
        if revision_id == _mod_revision.NULL_REVISION:
 
1686
            new_history = []
 
1687
        else:
 
1688
            new_history = self.revision_history()
 
1689
        if revision_id is not None and new_history != []:
 
1690
            try:
 
1691
                new_history = new_history[:new_history.index(revision_id) + 1]
 
1692
            except ValueError:
 
1693
                rev = self.repository.get_revision(revision_id)
 
1694
                new_history = rev.get_history(self.repository)[1:]
 
1695
        destination.set_revision_history(new_history)
 
1696
 
 
1697
    def _run_pre_change_branch_tip_hooks(self, new_revno, new_revid):
 
1698
        """Run the pre_change_branch_tip hooks."""
 
1699
        hooks = Branch.hooks['pre_change_branch_tip']
 
1700
        if not hooks:
 
1701
            return
 
1702
        old_revno, old_revid = self.last_revision_info()
 
1703
        params = ChangeBranchTipParams(
 
1704
            self, old_revno, new_revno, old_revid, new_revid)
 
1705
        for hook in hooks:
 
1706
            try:
 
1707
                hook(params)
 
1708
            except errors.TipChangeRejected:
 
1709
                raise
 
1710
            except Exception:
 
1711
                exc_info = sys.exc_info()
 
1712
                hook_name = Branch.hooks.get_hook_name(hook)
 
1713
                raise errors.HookFailed(
 
1714
                    'pre_change_branch_tip', hook_name, exc_info)
 
1715
 
 
1716
    def _run_post_change_branch_tip_hooks(self, old_revno, old_revid):
 
1717
        """Run the post_change_branch_tip hooks."""
 
1718
        hooks = Branch.hooks['post_change_branch_tip']
 
1719
        if not hooks:
 
1720
            return
 
1721
        new_revno, new_revid = self.last_revision_info()
 
1722
        params = ChangeBranchTipParams(
 
1723
            self, old_revno, new_revno, old_revid, new_revid)
 
1724
        for hook in hooks:
 
1725
            hook(params)
 
1726
 
 
1727
    @needs_write_lock
 
1728
    def set_last_revision_info(self, revno, revision_id):
 
1729
        """Set the last revision of this branch.
 
1730
 
 
1731
        The caller is responsible for checking that the revno is correct
 
1732
        for this revision id.
 
1733
 
 
1734
        It may be possible to set the branch last revision to an id not
 
1735
        present in the repository.  However, branches can also be 
 
1736
        configured to check constraints on history, in which case this may not
 
1737
        be permitted.
 
1738
        """
 
1739
        revision_id = _mod_revision.ensure_null(revision_id)
 
1740
        # this old format stores the full history, but this api doesn't
 
1741
        # provide it, so we must generate, and might as well check it's
 
1742
        # correct
 
1743
        history = self._lefthand_history(revision_id)
 
1744
        if len(history) != revno:
 
1745
            raise AssertionError('%d != %d' % (len(history), revno))
 
1746
        self.set_revision_history(history)
 
1747
 
 
1748
    def _gen_revision_history(self):
 
1749
        history = self._transport.get_bytes('revision-history').split('\n')
 
1750
        if history[-1:] == ['']:
 
1751
            # There shouldn't be a trailing newline, but just in case.
 
1752
            history.pop()
 
1753
        return history
 
1754
 
 
1755
    def _lefthand_history(self, revision_id, last_rev=None,
 
1756
                          other_branch=None):
 
1757
        if 'evil' in debug.debug_flags:
 
1758
            mutter_callsite(4, "_lefthand_history scales with history.")
 
1759
        # stop_revision must be a descendant of last_revision
 
1760
        graph = self.repository.get_graph()
 
1761
        if last_rev is not None:
 
1762
            if not graph.is_ancestor(last_rev, revision_id):
 
1763
                # our previous tip is not merged into stop_revision
 
1764
                raise errors.DivergedBranches(self, other_branch)
 
1765
        # make a new revision history from the graph
 
1766
        parents_map = graph.get_parent_map([revision_id])
 
1767
        if revision_id not in parents_map:
 
1768
            raise errors.NoSuchRevision(self, revision_id)
 
1769
        current_rev_id = revision_id
 
1770
        new_history = []
 
1771
        check_not_reserved_id = _mod_revision.check_not_reserved_id
 
1772
        # Do not include ghosts or graph origin in revision_history
 
1773
        while (current_rev_id in parents_map and
 
1774
               len(parents_map[current_rev_id]) > 0):
 
1775
            check_not_reserved_id(current_rev_id)
 
1776
            new_history.append(current_rev_id)
 
1777
            current_rev_id = parents_map[current_rev_id][0]
 
1778
            parents_map = graph.get_parent_map([current_rev_id])
 
1779
        new_history.reverse()
 
1780
        return new_history
 
1781
 
 
1782
    @needs_write_lock
 
1783
    def generate_revision_history(self, revision_id, last_rev=None,
 
1784
        other_branch=None):
 
1785
        """Create a new revision history that will finish with revision_id.
 
1786
 
 
1787
        :param revision_id: the new tip to use.
 
1788
        :param last_rev: The previous last_revision. If not None, then this
 
1789
            must be a ancestory of revision_id, or DivergedBranches is raised.
 
1790
        :param other_branch: The other branch that DivergedBranches should
 
1791
            raise with respect to.
 
1792
        """
 
1793
        self.set_revision_history(self._lefthand_history(revision_id,
 
1794
            last_rev, other_branch))
 
1795
 
 
1796
    def basis_tree(self):
 
1797
        """See Branch.basis_tree."""
 
1798
        return self.repository.revision_tree(self.last_revision())
 
1799
 
 
1800
    @needs_write_lock
 
1801
    def pull(self, source, overwrite=False, stop_revision=None,
 
1802
             _hook_master=None, run_hooks=True, possible_transports=None,
 
1803
             _override_hook_target=None):
 
1804
        """See Branch.pull.
 
1805
 
 
1806
        :param _hook_master: Private parameter - set the branch to 
 
1807
            be supplied as the master to pull hooks.
 
1808
        :param run_hooks: Private parameter - if false, this branch
 
1809
            is being called because it's the master of the primary branch,
 
1810
            so it should not run its hooks.
 
1811
        :param _override_hook_target: Private parameter - set the branch to be
 
1812
            supplied as the target_branch to pull hooks.
 
1813
        """
 
1814
        result = PullResult()
 
1815
        result.source_branch = source
 
1816
        if _override_hook_target is None:
 
1817
            result.target_branch = self
 
1818
        else:
 
1819
            result.target_branch = _override_hook_target
 
1820
        source.lock_read()
 
1821
        try:
 
1822
            # We assume that during 'pull' the local repository is closer than
 
1823
            # the remote one.
 
1824
            graph = self.repository.get_graph(source.repository)
 
1825
            result.old_revno, result.old_revid = self.last_revision_info()
 
1826
            self.update_revisions(source, stop_revision, overwrite=overwrite,
 
1827
                                  graph=graph)
 
1828
            result.tag_conflicts = source.tags.merge_to(self.tags, overwrite)
 
1829
            result.new_revno, result.new_revid = self.last_revision_info()
 
1830
            if _hook_master:
 
1831
                result.master_branch = _hook_master
 
1832
                result.local_branch = result.target_branch
 
1833
            else:
 
1834
                result.master_branch = result.target_branch
 
1835
                result.local_branch = None
 
1836
            if run_hooks:
 
1837
                for hook in Branch.hooks['post_pull']:
 
1838
                    hook(result)
 
1839
        finally:
 
1840
            source.unlock()
 
1841
        return result
 
1842
 
 
1843
    def _get_parent_location(self):
 
1844
        _locs = ['parent', 'pull', 'x-pull']
 
1845
        for l in _locs:
 
1846
            try:
 
1847
                return self._transport.get_bytes(l).strip('\n')
 
1848
            except errors.NoSuchFile:
 
1849
                pass
 
1850
        return None
 
1851
 
 
1852
    @needs_read_lock
 
1853
    def push(self, target, overwrite=False, stop_revision=None,
 
1854
             _override_hook_source_branch=None):
 
1855
        """See Branch.push.
 
1856
 
 
1857
        This is the basic concrete implementation of push()
 
1858
 
 
1859
        :param _override_hook_source_branch: If specified, run
 
1860
        the hooks passing this Branch as the source, rather than self.  
 
1861
        This is for use of RemoteBranch, where push is delegated to the
 
1862
        underlying vfs-based Branch. 
 
1863
        """
 
1864
        # TODO: Public option to disable running hooks - should be trivial but
 
1865
        # needs tests.
 
1866
        return _run_with_write_locked_target(
 
1867
            target, self._push_with_bound_branches, target, overwrite,
 
1868
            stop_revision,
 
1869
            _override_hook_source_branch=_override_hook_source_branch)
 
1870
 
 
1871
    def _push_with_bound_branches(self, target, overwrite,
 
1872
            stop_revision,
 
1873
            _override_hook_source_branch=None):
 
1874
        """Push from self into target, and into target's master if any.
 
1875
        
 
1876
        This is on the base BzrBranch class even though it doesn't support 
 
1877
        bound branches because the *target* might be bound.
 
1878
        """
 
1879
        def _run_hooks():
 
1880
            if _override_hook_source_branch:
 
1881
                result.source_branch = _override_hook_source_branch
 
1882
            for hook in Branch.hooks['post_push']:
 
1883
                hook(result)
 
1884
 
 
1885
        bound_location = target.get_bound_location()
 
1886
        if bound_location and target.base != bound_location:
 
1887
            # there is a master branch.
 
1888
            #
 
1889
            # XXX: Why the second check?  Is it even supported for a branch to
 
1890
            # be bound to itself? -- mbp 20070507
 
1891
            master_branch = target.get_master_branch()
 
1892
            master_branch.lock_write()
 
1893
            try:
 
1894
                # push into the master from this branch.
 
1895
                self._basic_push(master_branch, overwrite, stop_revision)
 
1896
                # and push into the target branch from this. Note that we push from
 
1897
                # this branch again, because its considered the highest bandwidth
 
1898
                # repository.
 
1899
                result = self._basic_push(target, overwrite, stop_revision)
 
1900
                result.master_branch = master_branch
 
1901
                result.local_branch = target
 
1902
                _run_hooks()
 
1903
                return result
 
1904
            finally:
 
1905
                master_branch.unlock()
 
1906
        else:
 
1907
            # no master branch
 
1908
            result = self._basic_push(target, overwrite, stop_revision)
 
1909
            # TODO: Why set master_branch and local_branch if there's no
 
1910
            # binding?  Maybe cleaner to just leave them unset? -- mbp
 
1911
            # 20070504
 
1912
            result.master_branch = target
 
1913
            result.local_branch = None
 
1914
            _run_hooks()
 
1915
            return result
 
1916
 
 
1917
    def _basic_push(self, target, overwrite, stop_revision):
 
1918
        """Basic implementation of push without bound branches or hooks.
 
1919
 
 
1920
        Must be called with self read locked and target write locked.
 
1921
        """
 
1922
        result = PushResult()
 
1923
        result.source_branch = self
 
1924
        result.target_branch = target
 
1925
        result.old_revno, result.old_revid = target.last_revision_info()
 
1926
        if result.old_revid != self.last_revision():
 
1927
            # We assume that during 'push' this repository is closer than
 
1928
            # the target.
 
1929
            graph = self.repository.get_graph(target.repository)
 
1930
            target.update_revisions(self, stop_revision, overwrite=overwrite,
 
1931
                                    graph=graph)
 
1932
        if self._push_should_merge_tags():
 
1933
            result.tag_conflicts = self.tags.merge_to(target.tags, overwrite)
 
1934
        result.new_revno, result.new_revid = target.last_revision_info()
 
1935
        return result
 
1936
 
 
1937
    def _push_should_merge_tags(self):
 
1938
        """Should _basic_push merge this branch's tags into the target?
 
1939
        
 
1940
        The default implementation returns False if this branch has no tags,
 
1941
        and True the rest of the time.  Subclasses may override this.
 
1942
        """
 
1943
        return self.tags.supports_tags() and self.tags.get_tag_dict()
 
1944
 
 
1945
    def get_parent(self):
 
1946
        """See Branch.get_parent."""
 
1947
        parent = self._get_parent_location()
 
1948
        if parent is None:
 
1949
            return parent
 
1950
        # This is an old-format absolute path to a local branch
 
1951
        # turn it into a url
 
1952
        if parent.startswith('/'):
 
1953
            parent = urlutils.local_path_to_url(parent.decode('utf8'))
 
1954
        try:
 
1955
            return urlutils.join(self.base[:-1], parent)
 
1956
        except errors.InvalidURLJoin, e:
 
1957
            raise errors.InaccessibleParent(parent, self.base)
 
1958
 
 
1959
    def get_stacked_on_url(self):
 
1960
        raise errors.UnstackableBranchFormat(self._format, self.base)
 
1961
 
 
1962
    def set_push_location(self, location):
 
1963
        """See Branch.set_push_location."""
 
1964
        self.get_config().set_user_option(
 
1965
            'push_location', location,
 
1966
            store=_mod_config.STORE_LOCATION_NORECURSE)
 
1967
 
 
1968
    @needs_write_lock
 
1969
    def set_parent(self, url):
 
1970
        """See Branch.set_parent."""
 
1971
        # TODO: Maybe delete old location files?
 
1972
        # URLs should never be unicode, even on the local fs,
 
1973
        # FIXUP this and get_parent in a future branch format bump:
 
1974
        # read and rewrite the file. RBC 20060125
 
1975
        if url is not None:
 
1976
            if isinstance(url, unicode):
 
1977
                try:
 
1978
                    url = url.encode('ascii')
 
1979
                except UnicodeEncodeError:
 
1980
                    raise errors.InvalidURL(url,
 
1981
                        "Urls must be 7-bit ascii, "
 
1982
                        "use bzrlib.urlutils.escape")
 
1983
            url = urlutils.relative_url(self.base, url)
 
1984
        self._set_parent_location(url)
 
1985
 
 
1986
    def _set_parent_location(self, url):
 
1987
        if url is None:
 
1988
            self._transport.delete('parent')
 
1989
        else:
 
1990
            self._transport.put_bytes('parent', url + '\n',
 
1991
                mode=self.bzrdir._get_file_mode())
 
1992
 
 
1993
    def set_stacked_on_url(self, url):
 
1994
        raise errors.UnstackableBranchFormat(self._format, self.base)
 
1995
 
 
1996
 
 
1997
class BzrBranch5(BzrBranch):
 
1998
    """A format 5 branch. This supports new features over plain branches.
 
1999
 
 
2000
    It has support for a master_branch which is the data for bound branches.
 
2001
    """
 
2002
 
 
2003
    @needs_write_lock
 
2004
    def pull(self, source, overwrite=False, stop_revision=None,
 
2005
             run_hooks=True, possible_transports=None,
 
2006
             _override_hook_target=None):
 
2007
        """Pull from source into self, updating my master if any.
 
2008
        
 
2009
        :param run_hooks: Private parameter - if false, this branch
 
2010
            is being called because it's the master of the primary branch,
 
2011
            so it should not run its hooks.
 
2012
        """
 
2013
        bound_location = self.get_bound_location()
 
2014
        master_branch = None
 
2015
        if bound_location and source.base != bound_location:
 
2016
            # not pulling from master, so we need to update master.
 
2017
            master_branch = self.get_master_branch(possible_transports)
 
2018
            master_branch.lock_write()
 
2019
        try:
 
2020
            if master_branch:
 
2021
                # pull from source into master.
 
2022
                master_branch.pull(source, overwrite, stop_revision,
 
2023
                    run_hooks=False)
 
2024
            return super(BzrBranch5, self).pull(source, overwrite,
 
2025
                stop_revision, _hook_master=master_branch,
 
2026
                run_hooks=run_hooks,
 
2027
                _override_hook_target=_override_hook_target)
 
2028
        finally:
 
2029
            if master_branch:
 
2030
                master_branch.unlock()
 
2031
 
 
2032
    def get_bound_location(self):
 
2033
        try:
 
2034
            return self._transport.get_bytes('bound')[:-1]
 
2035
        except errors.NoSuchFile:
 
2036
            return None
 
2037
 
 
2038
    @needs_read_lock
 
2039
    def get_master_branch(self, possible_transports=None):
 
2040
        """Return the branch we are bound to.
 
2041
        
 
2042
        :return: Either a Branch, or None
 
2043
 
 
2044
        This could memoise the branch, but if thats done
 
2045
        it must be revalidated on each new lock.
 
2046
        So for now we just don't memoise it.
 
2047
        # RBC 20060304 review this decision.
 
2048
        """
 
2049
        bound_loc = self.get_bound_location()
 
2050
        if not bound_loc:
 
2051
            return None
 
2052
        try:
 
2053
            return Branch.open(bound_loc,
 
2054
                               possible_transports=possible_transports)
 
2055
        except (errors.NotBranchError, errors.ConnectionError), e:
 
2056
            raise errors.BoundBranchConnectionFailure(
 
2057
                    self, bound_loc, e)
 
2058
 
 
2059
    @needs_write_lock
 
2060
    def set_bound_location(self, location):
 
2061
        """Set the target where this branch is bound to.
 
2062
 
 
2063
        :param location: URL to the target branch
 
2064
        """
 
2065
        if location:
 
2066
            self._transport.put_bytes('bound', location+'\n',
 
2067
                mode=self.bzrdir._get_file_mode())
 
2068
        else:
 
2069
            try:
 
2070
                self._transport.delete('bound')
 
2071
            except errors.NoSuchFile:
 
2072
                return False
 
2073
            return True
 
2074
 
 
2075
    @needs_write_lock
 
2076
    def bind(self, other):
 
2077
        """Bind this branch to the branch other.
 
2078
 
 
2079
        This does not push or pull data between the branches, though it does
 
2080
        check for divergence to raise an error when the branches are not
 
2081
        either the same, or one a prefix of the other. That behaviour may not
 
2082
        be useful, so that check may be removed in future.
 
2083
        
 
2084
        :param other: The branch to bind to
 
2085
        :type other: Branch
 
2086
        """
 
2087
        # TODO: jam 20051230 Consider checking if the target is bound
 
2088
        #       It is debatable whether you should be able to bind to
 
2089
        #       a branch which is itself bound.
 
2090
        #       Committing is obviously forbidden,
 
2091
        #       but binding itself may not be.
 
2092
        #       Since we *have* to check at commit time, we don't
 
2093
        #       *need* to check here
 
2094
 
 
2095
        # we want to raise diverged if:
 
2096
        # last_rev is not in the other_last_rev history, AND
 
2097
        # other_last_rev is not in our history, and do it without pulling
 
2098
        # history around
 
2099
        self.set_bound_location(other.base)
 
2100
 
 
2101
    @needs_write_lock
 
2102
    def unbind(self):
 
2103
        """If bound, unbind"""
 
2104
        return self.set_bound_location(None)
 
2105
 
 
2106
    @needs_write_lock
 
2107
    def update(self, possible_transports=None):
 
2108
        """Synchronise this branch with the master branch if any. 
 
2109
 
 
2110
        :return: None or the last_revision that was pivoted out during the
 
2111
                 update.
 
2112
        """
 
2113
        master = self.get_master_branch(possible_transports)
 
2114
        if master is not None:
 
2115
            old_tip = _mod_revision.ensure_null(self.last_revision())
 
2116
            self.pull(master, overwrite=True)
 
2117
            if self.repository.get_graph().is_ancestor(old_tip,
 
2118
                _mod_revision.ensure_null(self.last_revision())):
 
2119
                return None
 
2120
            return old_tip
 
2121
        return None
 
2122
 
 
2123
 
 
2124
class BzrBranch7(BzrBranch5):
 
2125
    """A branch with support for a fallback repository."""
 
2126
 
 
2127
    def _get_fallback_repository(self, url):
 
2128
        """Get the repository we fallback to at url."""
 
2129
        url = urlutils.join(self.base, url)
 
2130
        a_bzrdir = bzrdir.BzrDir.open(url,
 
2131
                                      possible_transports=[self._transport])
 
2132
        return a_bzrdir.open_branch().repository
 
2133
 
 
2134
    def _activate_fallback_location(self, url):
 
2135
        """Activate the branch/repository from url as a fallback repository."""
 
2136
        self.repository.add_fallback_repository(
 
2137
            self._get_fallback_repository(url))
 
2138
 
 
2139
    def _open_hook(self):
 
2140
        try:
 
2141
            url = self.get_stacked_on_url()
 
2142
        except (errors.UnstackableRepositoryFormat, errors.NotStacked,
 
2143
            errors.UnstackableBranchFormat):
 
2144
            pass
 
2145
        else:
 
2146
            for hook in Branch.hooks['transform_fallback_location']:
 
2147
                url = hook(self, url)
 
2148
                if url is None:
 
2149
                    hook_name = Branch.hooks.get_hook_name(hook)
 
2150
                    raise AssertionError(
 
2151
                        "'transform_fallback_location' hook %s returned "
 
2152
                        "None, not a URL." % hook_name)
 
2153
            self._activate_fallback_location(url)
 
2154
 
 
2155
    def _check_stackable_repo(self):
 
2156
        if not self.repository._format.supports_external_lookups:
 
2157
            raise errors.UnstackableRepositoryFormat(self.repository._format,
 
2158
                self.repository.base)
 
2159
 
 
2160
    def __init__(self, *args, **kwargs):
 
2161
        super(BzrBranch7, self).__init__(*args, **kwargs)
 
2162
        self._last_revision_info_cache = None
 
2163
        self._partial_revision_history_cache = []
 
2164
 
 
2165
    def _clear_cached_state(self):
 
2166
        super(BzrBranch7, self)._clear_cached_state()
 
2167
        self._last_revision_info_cache = None
 
2168
        self._partial_revision_history_cache = []
 
2169
 
 
2170
    def _last_revision_info(self):
 
2171
        revision_string = self._transport.get_bytes('last-revision')
 
2172
        revno, revision_id = revision_string.rstrip('\n').split(' ', 1)
 
2173
        revision_id = cache_utf8.get_cached_utf8(revision_id)
 
2174
        revno = int(revno)
 
2175
        return revno, revision_id
 
2176
 
 
2177
    def _write_last_revision_info(self, revno, revision_id):
 
2178
        """Simply write out the revision id, with no checks.
 
2179
 
 
2180
        Use set_last_revision_info to perform this safely.
 
2181
 
 
2182
        Does not update the revision_history cache.
 
2183
        Intended to be called by set_last_revision_info and
 
2184
        _write_revision_history.
 
2185
        """
 
2186
        revision_id = _mod_revision.ensure_null(revision_id)
 
2187
        out_string = '%d %s\n' % (revno, revision_id)
 
2188
        self._transport.put_bytes('last-revision', out_string,
 
2189
            mode=self.bzrdir._get_file_mode())
 
2190
 
 
2191
    @needs_write_lock
 
2192
    def set_last_revision_info(self, revno, revision_id):
 
2193
        revision_id = _mod_revision.ensure_null(revision_id)
 
2194
        old_revno, old_revid = self.last_revision_info()
 
2195
        if self._get_append_revisions_only():
 
2196
            self._check_history_violation(revision_id)
 
2197
        self._run_pre_change_branch_tip_hooks(revno, revision_id)
 
2198
        self._write_last_revision_info(revno, revision_id)
 
2199
        self._clear_cached_state()
 
2200
        self._last_revision_info_cache = revno, revision_id
 
2201
        self._run_post_change_branch_tip_hooks(old_revno, old_revid)
 
2202
 
 
2203
    def _synchronize_history(self, destination, revision_id):
 
2204
        """Synchronize last revision and revision history between branches.
 
2205
        
 
2206
        :see: Branch._synchronize_history
 
2207
        """
 
2208
        # XXX: The base Branch has a fast implementation of this method based
 
2209
        # on set_last_revision_info, but BzrBranch/BzrBranch5 have a slower one
 
2210
        # that uses set_revision_history.  This class inherits from BzrBranch5,
 
2211
        # but wants the fast implementation, so it calls
 
2212
        # Branch._synchronize_history directly.
 
2213
        Branch._synchronize_history(self, destination, revision_id)
 
2214
 
 
2215
    def _check_history_violation(self, revision_id):
 
2216
        last_revision = _mod_revision.ensure_null(self.last_revision())
 
2217
        if _mod_revision.is_null(last_revision):
 
2218
            return
 
2219
        if last_revision not in self._lefthand_history(revision_id):
 
2220
            raise errors.AppendRevisionsOnlyViolation(self.base)
 
2221
 
 
2222
    def _gen_revision_history(self):
 
2223
        """Generate the revision history from last revision
 
2224
        """
 
2225
        last_revno, last_revision = self.last_revision_info()
 
2226
        self._extend_partial_history(stop_index=last_revno-1)
 
2227
        return list(reversed(self._partial_revision_history_cache))
 
2228
 
 
2229
    def _extend_partial_history(self, stop_index=None, stop_revision=None):
 
2230
        """Extend the partial history to include a given index
 
2231
 
 
2232
        If a stop_index is supplied, stop when that index has been reached.
 
2233
        If a stop_revision is supplied, stop when that revision is
 
2234
        encountered.  Otherwise, stop when the beginning of history is
 
2235
        reached.
 
2236
 
 
2237
        :param stop_index: The index which should be present.  When it is
 
2238
            present, history extension will stop.
 
2239
        :param revision_id: The revision id which should be present.  When
 
2240
            it is encountered, history extension will stop.
 
2241
        """
 
2242
        repo = self.repository
 
2243
        if len(self._partial_revision_history_cache) == 0:
 
2244
            iterator = repo.iter_reverse_revision_history(self.last_revision())
 
2245
        else:
 
2246
            start_revision = self._partial_revision_history_cache[-1]
 
2247
            iterator = repo.iter_reverse_revision_history(start_revision)
 
2248
            #skip the last revision in the list
 
2249
            next_revision = iterator.next()
 
2250
        for revision_id in iterator:
 
2251
            self._partial_revision_history_cache.append(revision_id)
 
2252
            if (stop_index is not None and
 
2253
                len(self._partial_revision_history_cache) > stop_index):
 
2254
                break
 
2255
            if revision_id == stop_revision:
 
2256
                break
 
2257
 
 
2258
    def _write_revision_history(self, history):
 
2259
        """Factored out of set_revision_history.
 
2260
 
 
2261
        This performs the actual writing to disk, with format-specific checks.
 
2262
        It is intended to be called by BzrBranch5.set_revision_history.
 
2263
        """
 
2264
        if len(history) == 0:
 
2265
            last_revision = 'null:'
 
2266
        else:
 
2267
            if history != self._lefthand_history(history[-1]):
 
2268
                raise errors.NotLefthandHistory(history)
 
2269
            last_revision = history[-1]
 
2270
        if self._get_append_revisions_only():
 
2271
            self._check_history_violation(last_revision)
 
2272
        self._write_last_revision_info(len(history), last_revision)
 
2273
 
 
2274
    @needs_write_lock
 
2275
    def _set_parent_location(self, url):
 
2276
        """Set the parent branch"""
 
2277
        self._set_config_location('parent_location', url, make_relative=True)
 
2278
 
 
2279
    @needs_read_lock
 
2280
    def _get_parent_location(self):
 
2281
        """Set the parent branch"""
 
2282
        return self._get_config_location('parent_location')
 
2283
 
 
2284
    def set_push_location(self, location):
 
2285
        """See Branch.set_push_location."""
 
2286
        self._set_config_location('push_location', location)
 
2287
 
 
2288
    def set_bound_location(self, location):
 
2289
        """See Branch.set_push_location."""
 
2290
        result = None
 
2291
        config = self.get_config()
 
2292
        if location is None:
 
2293
            if config.get_user_option('bound') != 'True':
 
2294
                return False
 
2295
            else:
 
2296
                config.set_user_option('bound', 'False', warn_masked=True)
 
2297
                return True
 
2298
        else:
 
2299
            self._set_config_location('bound_location', location,
 
2300
                                      config=config)
 
2301
            config.set_user_option('bound', 'True', warn_masked=True)
 
2302
        return True
 
2303
 
 
2304
    def _get_bound_location(self, bound):
 
2305
        """Return the bound location in the config file.
 
2306
 
 
2307
        Return None if the bound parameter does not match"""
 
2308
        config = self.get_config()
 
2309
        config_bound = (config.get_user_option('bound') == 'True')
 
2310
        if config_bound != bound:
 
2311
            return None
 
2312
        return self._get_config_location('bound_location', config=config)
 
2313
 
 
2314
    def get_bound_location(self):
 
2315
        """See Branch.set_push_location."""
 
2316
        return self._get_bound_location(True)
 
2317
 
 
2318
    def get_old_bound_location(self):
 
2319
        """See Branch.get_old_bound_location"""
 
2320
        return self._get_bound_location(False)
 
2321
 
 
2322
    def get_stacked_on_url(self):
 
2323
        # you can always ask for the URL; but you might not be able to use it
 
2324
        # if the repo can't support stacking.
 
2325
        ## self._check_stackable_repo()
 
2326
        stacked_url = self._get_config_location('stacked_on_location')
 
2327
        if stacked_url is None:
 
2328
            raise errors.NotStacked(self)
 
2329
        return stacked_url
 
2330
 
 
2331
    def set_append_revisions_only(self, enabled):
 
2332
        if enabled:
 
2333
            value = 'True'
 
2334
        else:
 
2335
            value = 'False'
 
2336
        self.get_config().set_user_option('append_revisions_only', value,
 
2337
            warn_masked=True)
 
2338
 
 
2339
    def set_stacked_on_url(self, url):
 
2340
        self._check_stackable_repo()
 
2341
        if not url:
 
2342
            try:
 
2343
                old_url = self.get_stacked_on_url()
 
2344
            except (errors.NotStacked, errors.UnstackableBranchFormat,
 
2345
                errors.UnstackableRepositoryFormat):
 
2346
                return
 
2347
            url = ''
 
2348
            # repositories don't offer an interface to remove fallback
 
2349
            # repositories today; take the conceptually simpler option and just
 
2350
            # reopen it.
 
2351
            self.repository = self.bzrdir.find_repository()
 
2352
            # for every revision reference the branch has, ensure it is pulled
 
2353
            # in.
 
2354
            source_repository = self._get_fallback_repository(old_url)
 
2355
            for revision_id in chain([self.last_revision()],
 
2356
                self.tags.get_reverse_tag_dict()):
 
2357
                self.repository.fetch(source_repository, revision_id,
 
2358
                    find_ghosts=True)
 
2359
        else:
 
2360
            self._activate_fallback_location(url)
 
2361
        # write this out after the repository is stacked to avoid setting a
 
2362
        # stacked config that doesn't work.
 
2363
        self._set_config_location('stacked_on_location', url)
 
2364
 
 
2365
    def _get_append_revisions_only(self):
 
2366
        value = self.get_config().get_user_option('append_revisions_only')
 
2367
        return value == 'True'
 
2368
 
 
2369
    def _make_tags(self):
 
2370
        return BasicTags(self)
 
2371
 
 
2372
    @needs_write_lock
 
2373
    def generate_revision_history(self, revision_id, last_rev=None,
 
2374
                                  other_branch=None):
 
2375
        """See BzrBranch5.generate_revision_history"""
 
2376
        history = self._lefthand_history(revision_id, last_rev, other_branch)
 
2377
        revno = len(history)
 
2378
        self.set_last_revision_info(revno, revision_id)
 
2379
 
 
2380
    @needs_read_lock
 
2381
    def get_rev_id(self, revno, history=None):
 
2382
        """Find the revision id of the specified revno."""
 
2383
        if revno == 0:
 
2384
            return _mod_revision.NULL_REVISION
 
2385
 
 
2386
        last_revno, last_revision_id = self.last_revision_info()
 
2387
        if revno <= 0 or revno > last_revno:
 
2388
            raise errors.NoSuchRevision(self, revno)
 
2389
 
 
2390
        if history is not None:
 
2391
            return history[revno - 1]
 
2392
 
 
2393
        index = last_revno - revno
 
2394
        if len(self._partial_revision_history_cache) <= index:
 
2395
            self._extend_partial_history(stop_index=index)
 
2396
        if len(self._partial_revision_history_cache) > index:
 
2397
            return self._partial_revision_history_cache[index]
 
2398
        else:
 
2399
            raise errors.NoSuchRevision(self, revno)
 
2400
 
 
2401
    @needs_read_lock
 
2402
    def revision_id_to_revno(self, revision_id):
 
2403
        """Given a revision id, return its revno"""
 
2404
        if _mod_revision.is_null(revision_id):
 
2405
            return 0
 
2406
        try:
 
2407
            index = self._partial_revision_history_cache.index(revision_id)
 
2408
        except ValueError:
 
2409
            self._extend_partial_history(stop_revision=revision_id)
 
2410
            index = len(self._partial_revision_history_cache) - 1
 
2411
            if self._partial_revision_history_cache[index] != revision_id:
 
2412
                raise errors.NoSuchRevision(self, revision_id)
 
2413
        return self.revno() - index
 
2414
 
 
2415
 
 
2416
class BzrBranch6(BzrBranch7):
 
2417
    """See BzrBranchFormat6 for the capabilities of this branch.
 
2418
 
 
2419
    This subclass of BzrBranch7 disables the new features BzrBranch7 added,
 
2420
    i.e. stacking.
 
2421
    """
 
2422
 
 
2423
    def get_stacked_on_url(self):
 
2424
        raise errors.UnstackableBranchFormat(self._format, self.base)
 
2425
 
 
2426
    def set_stacked_on_url(self, url):
 
2427
        raise errors.UnstackableBranchFormat(self._format, self.base)
 
2428
 
 
2429
 
 
2430
######################################################################
 
2431
# results of operations
 
2432
 
 
2433
 
 
2434
class _Result(object):
 
2435
 
 
2436
    def _show_tag_conficts(self, to_file):
 
2437
        if not getattr(self, 'tag_conflicts', None):
 
2438
            return
 
2439
        to_file.write('Conflicting tags:\n')
 
2440
        for name, value1, value2 in self.tag_conflicts:
 
2441
            to_file.write('    %s\n' % (name, ))
 
2442
 
 
2443
 
 
2444
class PullResult(_Result):
 
2445
    """Result of a Branch.pull operation.
 
2446
 
 
2447
    :ivar old_revno: Revision number before pull.
 
2448
    :ivar new_revno: Revision number after pull.
 
2449
    :ivar old_revid: Tip revision id before pull.
 
2450
    :ivar new_revid: Tip revision id after pull.
 
2451
    :ivar source_branch: Source (local) branch object.
 
2452
    :ivar master_branch: Master branch of the target, or the target if no
 
2453
        Master
 
2454
    :ivar local_branch: target branch if there is a Master, else None
 
2455
    :ivar target_branch: Target/destination branch object.
 
2456
    :ivar tag_conflicts: A list of tag conflicts, see BasicTags.merge_to
 
2457
    """
 
2458
 
 
2459
    def __int__(self):
 
2460
        # DEPRECATED: pull used to return the change in revno
 
2461
        return self.new_revno - self.old_revno
 
2462
 
 
2463
    def report(self, to_file):
 
2464
        if not is_quiet():
 
2465
            if self.old_revid == self.new_revid:
 
2466
                to_file.write('No revisions to pull.\n')
 
2467
            else:
 
2468
                to_file.write('Now on revision %d.\n' % self.new_revno)
 
2469
        self._show_tag_conficts(to_file)
 
2470
 
 
2471
 
 
2472
class PushResult(_Result):
 
2473
    """Result of a Branch.push operation.
 
2474
 
 
2475
    :ivar old_revno: Revision number before push.
 
2476
    :ivar new_revno: Revision number after push.
 
2477
    :ivar old_revid: Tip revision id before push.
 
2478
    :ivar new_revid: Tip revision id after push.
 
2479
    :ivar source_branch: Source branch object.
 
2480
    :ivar master_branch: Master branch of the target, or None.
 
2481
    :ivar target_branch: Target/destination branch object.
 
2482
    """
 
2483
 
 
2484
    def __int__(self):
 
2485
        # DEPRECATED: push used to return the change in revno
 
2486
        return self.new_revno - self.old_revno
 
2487
 
 
2488
    def report(self, to_file):
 
2489
        """Write a human-readable description of the result."""
 
2490
        if self.old_revid == self.new_revid:
 
2491
            to_file.write('No new revisions to push.\n')
 
2492
        else:
 
2493
            to_file.write('Pushed up to revision %d.\n' % self.new_revno)
 
2494
        self._show_tag_conficts(to_file)
 
2495
 
 
2496
 
 
2497
class BranchCheckResult(object):
 
2498
    """Results of checking branch consistency.
 
2499
 
 
2500
    :see: Branch.check
 
2501
    """
 
2502
 
 
2503
    def __init__(self, branch):
 
2504
        self.branch = branch
 
2505
 
 
2506
    def report_results(self, verbose):
 
2507
        """Report the check results via trace.note.
 
2508
        
 
2509
        :param verbose: Requests more detailed display of what was checked,
 
2510
            if any.
 
2511
        """
 
2512
        note('checked branch %s format %s',
 
2513
             self.branch.base,
 
2514
             self.branch._format)
 
2515
 
 
2516
 
 
2517
class Converter5to6(object):
 
2518
    """Perform an in-place upgrade of format 5 to format 6"""
 
2519
 
 
2520
    def convert(self, branch):
 
2521
        # Data for 5 and 6 can peacefully coexist.
 
2522
        format = BzrBranchFormat6()
 
2523
        new_branch = format.open(branch.bzrdir, _found=True)
 
2524
 
 
2525
        # Copy source data into target
 
2526
        new_branch._write_last_revision_info(*branch.last_revision_info())
 
2527
        new_branch.set_parent(branch.get_parent())
 
2528
        new_branch.set_bound_location(branch.get_bound_location())
 
2529
        new_branch.set_push_location(branch.get_push_location())
 
2530
 
 
2531
        # New branch has no tags by default
 
2532
        new_branch.tags._set_tag_dict({})
 
2533
 
 
2534
        # Copying done; now update target format
 
2535
        new_branch._transport.put_bytes('format',
 
2536
            format.get_format_string(),
 
2537
            mode=new_branch.bzrdir._get_file_mode())
 
2538
 
 
2539
        # Clean up old files
 
2540
        new_branch._transport.delete('revision-history')
 
2541
        try:
 
2542
            branch.set_parent(None)
 
2543
        except errors.NoSuchFile:
 
2544
            pass
 
2545
        branch.set_bound_location(None)
 
2546
 
 
2547
 
 
2548
class Converter6to7(object):
 
2549
    """Perform an in-place upgrade of format 6 to format 7"""
 
2550
 
 
2551
    def convert(self, branch):
 
2552
        format = BzrBranchFormat7()
 
2553
        branch._set_config_location('stacked_on_location', '')
 
2554
        # update target format
 
2555
        branch._transport.put_bytes('format', format.get_format_string())
 
2556
 
 
2557
 
 
2558
 
 
2559
def _run_with_write_locked_target(target, callable, *args, **kwargs):
 
2560
    """Run ``callable(*args, **kwargs)``, write-locking target for the
 
2561
    duration.
 
2562
 
 
2563
    _run_with_write_locked_target will attempt to release the lock it acquires.
 
2564
 
 
2565
    If an exception is raised by callable, then that exception *will* be
 
2566
    propagated, even if the unlock attempt raises its own error.  Thus
 
2567
    _run_with_write_locked_target should be preferred to simply doing::
 
2568
 
 
2569
        target.lock_write()
 
2570
        try:
 
2571
            return callable(*args, **kwargs)
 
2572
        finally:
 
2573
            target.unlock()
 
2574
    
 
2575
    """
 
2576
    # This is very similar to bzrlib.decorators.needs_write_lock.  Perhaps they
 
2577
    # should share code?
 
2578
    target.lock_write()
 
2579
    try:
 
2580
        result = callable(*args, **kwargs)
 
2581
    except:
 
2582
        exc_info = sys.exc_info()
 
2583
        try:
 
2584
            target.unlock()
 
2585
        finally:
 
2586
            raise exc_info[0], exc_info[1], exc_info[2]
 
2587
    else:
 
2588
        target.unlock()
 
2589
        return result