1
# Copyright (C) 2005-2011 Canonical Ltd
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from cStringIO import StringIO
20
from bzrlib.lazy_import import lazy_import
21
lazy_import(globals(), """
27
config as _mod_config,
36
revision as _mod_revision,
48
from bzrlib.decorators import (
53
from bzrlib.hooks import Hooks
54
from bzrlib.inter import InterObject
55
from bzrlib.lock import _RelockDebugMixin, LogicalLockResult
56
from bzrlib import registry
57
from bzrlib.symbol_versioning import (
61
from bzrlib.trace import mutter, mutter_callsite, note, is_quiet
64
class Branch(controldir.ControlComponent):
65
"""Branch holding a history of revisions.
68
Base directory/url of the branch; using control_url and
69
control_transport is more standardized.
70
:ivar hooks: An instance of BranchHooks.
71
:ivar _master_branch_cache: cached result of get_master_branch, see
74
# this is really an instance variable - FIXME move it there
79
def control_transport(self):
80
return self._transport
83
def user_transport(self):
84
return self.bzrdir.user_transport
86
def __init__(self, *ignored, **ignored_too):
87
self.tags = self._format.make_tags(self)
88
self._revision_history_cache = None
89
self._revision_id_to_revno_cache = None
90
self._partial_revision_id_to_revno_cache = {}
91
self._partial_revision_history_cache = []
92
self._tags_bytes = None
93
self._last_revision_info_cache = None
94
self._master_branch_cache = None
95
self._merge_sorted_revisions_cache = None
97
hooks = Branch.hooks['open']
101
def _open_hook(self):
102
"""Called by init to allow simpler extension of the base class."""
104
def _activate_fallback_location(self, url):
105
"""Activate the branch/repository from url as a fallback repository."""
106
for existing_fallback_repo in self.repository._fallback_repositories:
107
if existing_fallback_repo.user_url == url:
108
# This fallback is already configured. This probably only
109
# happens because BzrDir.sprout is a horrible mess. To avoid
110
# confusing _unstack we don't add this a second time.
111
mutter('duplicate activation of fallback %r on %r', url, self)
113
repo = self._get_fallback_repository(url)
114
if repo.has_same_location(self.repository):
115
raise errors.UnstackableLocationError(self.user_url, url)
116
self.repository.add_fallback_repository(repo)
118
def break_lock(self):
119
"""Break a lock if one is present from another instance.
121
Uses the ui factory to ask for confirmation if the lock may be from
124
This will probe the repository for its lock as well.
126
self.control_files.break_lock()
127
self.repository.break_lock()
128
master = self.get_master_branch()
129
if master is not None:
132
def _check_stackable_repo(self):
133
if not self.repository._format.supports_external_lookups:
134
raise errors.UnstackableRepositoryFormat(self.repository._format,
135
self.repository.base)
137
def _extend_partial_history(self, stop_index=None, stop_revision=None):
138
"""Extend the partial history to include a given index
140
If a stop_index is supplied, stop when that index has been reached.
141
If a stop_revision is supplied, stop when that revision is
142
encountered. Otherwise, stop when the beginning of history is
145
:param stop_index: The index which should be present. When it is
146
present, history extension will stop.
147
:param stop_revision: The revision id which should be present. When
148
it is encountered, history extension will stop.
150
if len(self._partial_revision_history_cache) == 0:
151
self._partial_revision_history_cache = [self.last_revision()]
152
repository._iter_for_revno(
153
self.repository, self._partial_revision_history_cache,
154
stop_index=stop_index, stop_revision=stop_revision)
155
if self._partial_revision_history_cache[-1] == _mod_revision.NULL_REVISION:
156
self._partial_revision_history_cache.pop()
158
def _get_check_refs(self):
159
"""Get the references needed for check().
163
revid = self.last_revision()
164
return [('revision-existence', revid), ('lefthand-distance', revid)]
167
def open(base, _unsupported=False, possible_transports=None):
168
"""Open the branch rooted at base.
170
For instance, if the branch is at URL/.bzr/branch,
171
Branch.open(URL) -> a Branch instance.
173
control = bzrdir.BzrDir.open(base, _unsupported,
174
possible_transports=possible_transports)
175
return control.open_branch(unsupported=_unsupported)
178
def open_from_transport(transport, name=None, _unsupported=False):
179
"""Open the branch rooted at transport"""
180
control = bzrdir.BzrDir.open_from_transport(transport, _unsupported)
181
return control.open_branch(name=name, unsupported=_unsupported)
184
def open_containing(url, possible_transports=None):
185
"""Open an existing branch which contains url.
187
This probes for a branch at url, and searches upwards from there.
189
Basically we keep looking up until we find the control directory or
190
run into the root. If there isn't one, raises NotBranchError.
191
If there is one and it is either an unrecognised format or an unsupported
192
format, UnknownFormatError or UnsupportedFormatError are raised.
193
If there is one, it is returned, along with the unused portion of url.
195
control, relpath = bzrdir.BzrDir.open_containing(url,
197
return control.open_branch(), relpath
199
def _push_should_merge_tags(self):
200
"""Should _basic_push merge this branch's tags into the target?
202
The default implementation returns False if this branch has no tags,
203
and True the rest of the time. Subclasses may override this.
205
return self.supports_tags() and self.tags.get_tag_dict()
207
def get_config(self):
208
"""Get a bzrlib.config.BranchConfig for this Branch.
210
This can then be used to get and set configuration options for the
213
:return: A bzrlib.config.BranchConfig.
215
return _mod_config.BranchConfig(self)
217
def _get_config(self):
218
"""Get the concrete config for just the config in this branch.
220
This is not intended for client use; see Branch.get_config for the
225
:return: An object supporting get_option and set_option.
227
raise NotImplementedError(self._get_config)
229
def _get_fallback_repository(self, url):
230
"""Get the repository we fallback to at url."""
231
url = urlutils.join(self.base, url)
232
a_branch = Branch.open(url,
233
possible_transports=[self.bzrdir.root_transport])
234
return a_branch.repository
237
def _get_tags_bytes(self):
238
"""Get the bytes of a serialised tags dict.
240
Note that not all branches support tags, nor do all use the same tags
241
logic: this method is specific to BasicTags. Other tag implementations
242
may use the same method name and behave differently, safely, because
243
of the double-dispatch via
244
format.make_tags->tags_instance->get_tags_dict.
246
:return: The bytes of the tags file.
247
:seealso: Branch._set_tags_bytes.
249
if self._tags_bytes is None:
250
self._tags_bytes = self._transport.get_bytes('tags')
251
return self._tags_bytes
253
def _get_nick(self, local=False, possible_transports=None):
254
config = self.get_config()
255
# explicit overrides master, but don't look for master if local is True
256
if not local and not config.has_explicit_nickname():
258
master = self.get_master_branch(possible_transports)
259
if master and self.user_url == master.user_url:
260
raise errors.RecursiveBind(self.user_url)
261
if master is not None:
262
# return the master branch value
264
except errors.RecursiveBind, e:
266
except errors.BzrError, e:
267
# Silently fall back to local implicit nick if the master is
269
mutter("Could not connect to bound branch, "
270
"falling back to local nick.\n " + str(e))
271
return config.get_nickname()
273
def _set_nick(self, nick):
274
self.get_config().set_user_option('nickname', nick, warn_masked=True)
276
nick = property(_get_nick, _set_nick)
279
raise NotImplementedError(self.is_locked)
281
def _lefthand_history(self, revision_id, last_rev=None,
283
if 'evil' in debug.debug_flags:
284
mutter_callsite(4, "_lefthand_history scales with history.")
285
# stop_revision must be a descendant of last_revision
286
graph = self.repository.get_graph()
287
if last_rev is not None:
288
if not graph.is_ancestor(last_rev, revision_id):
289
# our previous tip is not merged into stop_revision
290
raise errors.DivergedBranches(self, other_branch)
291
# make a new revision history from the graph
292
parents_map = graph.get_parent_map([revision_id])
293
if revision_id not in parents_map:
294
raise errors.NoSuchRevision(self, revision_id)
295
current_rev_id = revision_id
297
check_not_reserved_id = _mod_revision.check_not_reserved_id
298
# Do not include ghosts or graph origin in revision_history
299
while (current_rev_id in parents_map and
300
len(parents_map[current_rev_id]) > 0):
301
check_not_reserved_id(current_rev_id)
302
new_history.append(current_rev_id)
303
current_rev_id = parents_map[current_rev_id][0]
304
parents_map = graph.get_parent_map([current_rev_id])
305
new_history.reverse()
308
def lock_write(self, token=None):
309
"""Lock the branch for write operations.
311
:param token: A token to permit reacquiring a previously held and
313
:return: A BranchWriteLockResult.
315
raise NotImplementedError(self.lock_write)
318
"""Lock the branch for read operations.
320
:return: A bzrlib.lock.LogicalLockResult.
322
raise NotImplementedError(self.lock_read)
325
raise NotImplementedError(self.unlock)
327
def peek_lock_mode(self):
328
"""Return lock mode for the Branch: 'r', 'w' or None"""
329
raise NotImplementedError(self.peek_lock_mode)
331
def get_physical_lock_status(self):
332
raise NotImplementedError(self.get_physical_lock_status)
335
def dotted_revno_to_revision_id(self, revno, _cache_reverse=False):
336
"""Return the revision_id for a dotted revno.
338
:param revno: a tuple like (1,) or (1,1,2)
339
:param _cache_reverse: a private parameter enabling storage
340
of the reverse mapping in a top level cache. (This should
341
only be done in selective circumstances as we want to
342
avoid having the mapping cached multiple times.)
343
:return: the revision_id
344
:raises errors.NoSuchRevision: if the revno doesn't exist
346
rev_id = self._do_dotted_revno_to_revision_id(revno)
348
self._partial_revision_id_to_revno_cache[rev_id] = revno
351
def _do_dotted_revno_to_revision_id(self, revno):
352
"""Worker function for dotted_revno_to_revision_id.
354
Subclasses should override this if they wish to
355
provide a more efficient implementation.
358
return self.get_rev_id(revno[0])
359
revision_id_to_revno = self.get_revision_id_to_revno_map()
360
revision_ids = [revision_id for revision_id, this_revno
361
in revision_id_to_revno.iteritems()
362
if revno == this_revno]
363
if len(revision_ids) == 1:
364
return revision_ids[0]
366
revno_str = '.'.join(map(str, revno))
367
raise errors.NoSuchRevision(self, revno_str)
370
def revision_id_to_dotted_revno(self, revision_id):
371
"""Given a revision id, return its dotted revno.
373
:return: a tuple like (1,) or (400,1,3).
375
return self._do_revision_id_to_dotted_revno(revision_id)
377
def _do_revision_id_to_dotted_revno(self, revision_id):
378
"""Worker function for revision_id_to_revno."""
379
# Try the caches if they are loaded
380
result = self._partial_revision_id_to_revno_cache.get(revision_id)
381
if result is not None:
383
if self._revision_id_to_revno_cache:
384
result = self._revision_id_to_revno_cache.get(revision_id)
386
raise errors.NoSuchRevision(self, revision_id)
387
# Try the mainline as it's optimised
389
revno = self.revision_id_to_revno(revision_id)
391
except errors.NoSuchRevision:
392
# We need to load and use the full revno map after all
393
result = self.get_revision_id_to_revno_map().get(revision_id)
395
raise errors.NoSuchRevision(self, revision_id)
399
def get_revision_id_to_revno_map(self):
400
"""Return the revision_id => dotted revno map.
402
This will be regenerated on demand, but will be cached.
404
:return: A dictionary mapping revision_id => dotted revno.
405
This dictionary should not be modified by the caller.
407
if self._revision_id_to_revno_cache is not None:
408
mapping = self._revision_id_to_revno_cache
410
mapping = self._gen_revno_map()
411
self._cache_revision_id_to_revno(mapping)
412
# TODO: jam 20070417 Since this is being cached, should we be returning
414
# I would rather not, and instead just declare that users should not
415
# modify the return value.
418
def _gen_revno_map(self):
419
"""Create a new mapping from revision ids to dotted revnos.
421
Dotted revnos are generated based on the current tip in the revision
423
This is the worker function for get_revision_id_to_revno_map, which
424
just caches the return value.
426
:return: A dictionary mapping revision_id => dotted revno.
428
revision_id_to_revno = dict((rev_id, revno)
429
for rev_id, depth, revno, end_of_merge
430
in self.iter_merge_sorted_revisions())
431
return revision_id_to_revno
434
def iter_merge_sorted_revisions(self, start_revision_id=None,
435
stop_revision_id=None, stop_rule='exclude', direction='reverse'):
436
"""Walk the revisions for a branch in merge sorted order.
438
Merge sorted order is the output from a merge-aware,
439
topological sort, i.e. all parents come before their
440
children going forward; the opposite for reverse.
442
:param start_revision_id: the revision_id to begin walking from.
443
If None, the branch tip is used.
444
:param stop_revision_id: the revision_id to terminate the walk
445
after. If None, the rest of history is included.
446
:param stop_rule: if stop_revision_id is not None, the precise rule
447
to use for termination:
449
* 'exclude' - leave the stop revision out of the result (default)
450
* 'include' - the stop revision is the last item in the result
451
* 'with-merges' - include the stop revision and all of its
452
merged revisions in the result
453
* 'with-merges-without-common-ancestry' - filter out revisions
454
that are in both ancestries
455
:param direction: either 'reverse' or 'forward':
457
* reverse means return the start_revision_id first, i.e.
458
start at the most recent revision and go backwards in history
459
* forward returns tuples in the opposite order to reverse.
460
Note in particular that forward does *not* do any intelligent
461
ordering w.r.t. depth as some clients of this API may like.
462
(If required, that ought to be done at higher layers.)
464
:return: an iterator over (revision_id, depth, revno, end_of_merge)
467
* revision_id: the unique id of the revision
468
* depth: How many levels of merging deep this node has been
470
* revno_sequence: This field provides a sequence of
471
revision numbers for all revisions. The format is:
472
(REVNO, BRANCHNUM, BRANCHREVNO). BRANCHNUM is the number of the
473
branch that the revno is on. From left to right the REVNO numbers
474
are the sequence numbers within that branch of the revision.
475
* end_of_merge: When True the next node (earlier in history) is
476
part of a different merge.
478
# Note: depth and revno values are in the context of the branch so
479
# we need the full graph to get stable numbers, regardless of the
481
if self._merge_sorted_revisions_cache is None:
482
last_revision = self.last_revision()
483
known_graph = self.repository.get_known_graph_ancestry(
485
self._merge_sorted_revisions_cache = known_graph.merge_sort(
487
filtered = self._filter_merge_sorted_revisions(
488
self._merge_sorted_revisions_cache, start_revision_id,
489
stop_revision_id, stop_rule)
490
# Make sure we don't return revisions that are not part of the
491
# start_revision_id ancestry.
492
filtered = self._filter_start_non_ancestors(filtered)
493
if direction == 'reverse':
495
if direction == 'forward':
496
return reversed(list(filtered))
498
raise ValueError('invalid direction %r' % direction)
500
def _filter_merge_sorted_revisions(self, merge_sorted_revisions,
501
start_revision_id, stop_revision_id, stop_rule):
502
"""Iterate over an inclusive range of sorted revisions."""
503
rev_iter = iter(merge_sorted_revisions)
504
if start_revision_id is not None:
505
for node in rev_iter:
507
if rev_id != start_revision_id:
510
# The decision to include the start or not
511
# depends on the stop_rule if a stop is provided
512
# so pop this node back into the iterator
513
rev_iter = itertools.chain(iter([node]), rev_iter)
515
if stop_revision_id is None:
517
for node in rev_iter:
519
yield (rev_id, node.merge_depth, node.revno,
521
elif stop_rule == 'exclude':
522
for node in rev_iter:
524
if rev_id == stop_revision_id:
526
yield (rev_id, node.merge_depth, node.revno,
528
elif stop_rule == 'include':
529
for node in rev_iter:
531
yield (rev_id, node.merge_depth, node.revno,
533
if rev_id == stop_revision_id:
535
elif stop_rule == 'with-merges-without-common-ancestry':
536
# We want to exclude all revisions that are already part of the
537
# stop_revision_id ancestry.
538
graph = self.repository.get_graph()
539
ancestors = graph.find_unique_ancestors(start_revision_id,
541
for node in rev_iter:
543
if rev_id not in ancestors:
545
yield (rev_id, node.merge_depth, node.revno,
547
elif stop_rule == 'with-merges':
548
stop_rev = self.repository.get_revision(stop_revision_id)
549
if stop_rev.parent_ids:
550
left_parent = stop_rev.parent_ids[0]
552
left_parent = _mod_revision.NULL_REVISION
553
# left_parent is the actual revision we want to stop logging at,
554
# since we want to show the merged revisions after the stop_rev too
555
reached_stop_revision_id = False
556
revision_id_whitelist = []
557
for node in rev_iter:
559
if rev_id == left_parent:
560
# reached the left parent after the stop_revision
562
if (not reached_stop_revision_id or
563
rev_id in revision_id_whitelist):
564
yield (rev_id, node.merge_depth, node.revno,
566
if reached_stop_revision_id or rev_id == stop_revision_id:
567
# only do the merged revs of rev_id from now on
568
rev = self.repository.get_revision(rev_id)
570
reached_stop_revision_id = True
571
revision_id_whitelist.extend(rev.parent_ids)
573
raise ValueError('invalid stop_rule %r' % stop_rule)
575
def _filter_start_non_ancestors(self, rev_iter):
576
# If we started from a dotted revno, we want to consider it as a tip
577
# and don't want to yield revisions that are not part of its
578
# ancestry. Given the order guaranteed by the merge sort, we will see
579
# uninteresting descendants of the first parent of our tip before the
581
first = rev_iter.next()
582
(rev_id, merge_depth, revno, end_of_merge) = first
585
# We start at a mainline revision so by definition, all others
586
# revisions in rev_iter are ancestors
587
for node in rev_iter:
592
pmap = self.repository.get_parent_map([rev_id])
593
parents = pmap.get(rev_id, [])
595
whitelist.update(parents)
597
# If there is no parents, there is nothing of interest left
599
# FIXME: It's hard to test this scenario here as this code is never
600
# called in that case. -- vila 20100322
603
for (rev_id, merge_depth, revno, end_of_merge) in rev_iter:
605
if rev_id in whitelist:
606
pmap = self.repository.get_parent_map([rev_id])
607
parents = pmap.get(rev_id, [])
608
whitelist.remove(rev_id)
609
whitelist.update(parents)
611
# We've reached the mainline, there is nothing left to
615
# A revision that is not part of the ancestry of our
618
yield (rev_id, merge_depth, revno, end_of_merge)
620
def leave_lock_in_place(self):
621
"""Tell this branch object not to release the physical lock when this
624
If lock_write doesn't return a token, then this method is not supported.
626
self.control_files.leave_in_place()
628
def dont_leave_lock_in_place(self):
629
"""Tell this branch object to release the physical lock when this
630
object is unlocked, even if it didn't originally acquire it.
632
If lock_write doesn't return a token, then this method is not supported.
634
self.control_files.dont_leave_in_place()
636
def bind(self, other):
637
"""Bind the local branch the other branch.
639
:param other: The branch to bind to
642
raise errors.UpgradeRequired(self.user_url)
644
def get_append_revisions_only(self):
645
"""Whether it is only possible to append revisions to the history.
647
if not self._format.supports_set_append_revisions_only():
649
return self.get_config(
650
).get_user_option_as_bool('append_revisions_only')
652
def set_append_revisions_only(self, enabled):
653
if not self._format.supports_set_append_revisions_only():
654
raise errors.UpgradeRequired(self.user_url)
659
self.get_config().set_user_option('append_revisions_only', value,
662
def set_reference_info(self, file_id, tree_path, branch_location):
663
"""Set the branch location to use for a tree reference."""
664
raise errors.UnsupportedOperation(self.set_reference_info, self)
666
def get_reference_info(self, file_id):
667
"""Get the tree_path and branch_location for a tree reference."""
668
raise errors.UnsupportedOperation(self.get_reference_info, self)
671
def fetch(self, from_branch, last_revision=None, limit=None):
672
"""Copy revisions from from_branch into this branch.
674
:param from_branch: Where to copy from.
675
:param last_revision: What revision to stop at (None for at the end
677
:param limit: Optional rough limit of revisions to fetch
680
return InterBranch.get(from_branch, self).fetch(last_revision, limit=limit)
682
def get_bound_location(self):
683
"""Return the URL of the branch we are bound to.
685
Older format branches cannot bind, please be sure to use a metadir
690
def get_old_bound_location(self):
691
"""Return the URL of the branch we used to be bound to
693
raise errors.UpgradeRequired(self.user_url)
695
def get_commit_builder(self, parents, config=None, timestamp=None,
696
timezone=None, committer=None, revprops=None,
697
revision_id=None, lossy=False):
698
"""Obtain a CommitBuilder for this branch.
700
:param parents: Revision ids of the parents of the new revision.
701
:param config: Optional configuration to use.
702
:param timestamp: Optional timestamp recorded for commit.
703
:param timezone: Optional timezone for timestamp.
704
:param committer: Optional committer to set for commit.
705
:param revprops: Optional dictionary of revision properties.
706
:param revision_id: Optional revision id.
707
:param lossy: Whether to discard data that can not be natively
708
represented, when pushing to a foreign VCS
712
config = self.get_config()
714
return self.repository.get_commit_builder(self, parents, config,
715
timestamp, timezone, committer, revprops, revision_id,
718
def get_master_branch(self, possible_transports=None):
719
"""Return the branch we are bound to.
721
:return: Either a Branch, or None
725
def get_revision_delta(self, revno):
726
"""Return the delta for one revision.
728
The delta is relative to its mainline predecessor, or the
729
empty tree for revision 1.
731
rh = self.revision_history()
732
if not (1 <= revno <= len(rh)):
733
raise errors.InvalidRevisionNumber(revno)
734
return self.repository.get_revision_delta(rh[revno-1])
736
def get_stacked_on_url(self):
737
"""Get the URL this branch is stacked against.
739
:raises NotStacked: If the branch is not stacked.
740
:raises UnstackableBranchFormat: If the branch does not support
743
raise NotImplementedError(self.get_stacked_on_url)
745
def print_file(self, file, revision_id):
746
"""Print `file` to stdout."""
747
raise NotImplementedError(self.print_file)
749
@deprecated_method(deprecated_in((2, 4, 0)))
750
def set_revision_history(self, rev_history):
751
"""See Branch.set_revision_history."""
752
self._set_revision_history(rev_history)
755
def _set_revision_history(self, rev_history):
756
if len(rev_history) == 0:
757
revid = _mod_revision.NULL_REVISION
759
revid = rev_history[-1]
760
if rev_history != self._lefthand_history(revid):
761
raise errors.NotLefthandHistory(rev_history)
762
self.set_last_revision_info(len(rev_history), revid)
763
self._cache_revision_history(rev_history)
764
for hook in Branch.hooks['set_rh']:
765
hook(self, rev_history)
768
def set_last_revision_info(self, revno, revision_id):
769
"""Set the last revision of this branch.
771
The caller is responsible for checking that the revno is correct
772
for this revision id.
774
It may be possible to set the branch last revision to an id not
775
present in the repository. However, branches can also be
776
configured to check constraints on history, in which case this may not
779
raise NotImplementedError(self.set_last_revision_info)
782
def generate_revision_history(self, revision_id, last_rev=None,
784
"""See Branch.generate_revision_history"""
785
graph = self.repository.get_graph()
786
(last_revno, last_revid) = self.last_revision_info()
787
known_revision_ids = [
788
(last_revid, last_revno),
789
(_mod_revision.NULL_REVISION, 0),
791
if last_rev is not None:
792
if not graph.is_ancestor(last_rev, revision_id):
793
# our previous tip is not merged into stop_revision
794
raise errors.DivergedBranches(self, other_branch)
795
revno = graph.find_distance_to_null(revision_id, known_revision_ids)
796
self.set_last_revision_info(revno, revision_id)
799
def set_parent(self, url):
800
"""See Branch.set_parent."""
801
# TODO: Maybe delete old location files?
802
# URLs should never be unicode, even on the local fs,
803
# FIXUP this and get_parent in a future branch format bump:
804
# read and rewrite the file. RBC 20060125
806
if isinstance(url, unicode):
808
url = url.encode('ascii')
809
except UnicodeEncodeError:
810
raise errors.InvalidURL(url,
811
"Urls must be 7-bit ascii, "
812
"use bzrlib.urlutils.escape")
813
url = urlutils.relative_url(self.base, url)
814
self._set_parent_location(url)
817
def set_stacked_on_url(self, url):
818
"""Set the URL this branch is stacked against.
820
:raises UnstackableBranchFormat: If the branch does not support
822
:raises UnstackableRepositoryFormat: If the repository does not support
825
if not self._format.supports_stacking():
826
raise errors.UnstackableBranchFormat(self._format, self.user_url)
827
# XXX: Changing from one fallback repository to another does not check
828
# that all the data you need is present in the new fallback.
829
# Possibly it should.
830
self._check_stackable_repo()
833
old_url = self.get_stacked_on_url()
834
except (errors.NotStacked, errors.UnstackableBranchFormat,
835
errors.UnstackableRepositoryFormat):
839
self._activate_fallback_location(url)
840
# write this out after the repository is stacked to avoid setting a
841
# stacked config that doesn't work.
842
self._set_config_location('stacked_on_location', url)
845
"""Change a branch to be unstacked, copying data as needed.
847
Don't call this directly, use set_stacked_on_url(None).
849
pb = ui.ui_factory.nested_progress_bar()
851
pb.update("Unstacking")
852
# The basic approach here is to fetch the tip of the branch,
853
# including all available ghosts, from the existing stacked
854
# repository into a new repository object without the fallbacks.
856
# XXX: See <https://launchpad.net/bugs/397286> - this may not be
857
# correct for CHKMap repostiories
858
old_repository = self.repository
859
if len(old_repository._fallback_repositories) != 1:
860
raise AssertionError("can't cope with fallback repositories "
861
"of %r (fallbacks: %r)" % (old_repository,
862
old_repository._fallback_repositories))
863
# Open the new repository object.
864
# Repositories don't offer an interface to remove fallback
865
# repositories today; take the conceptually simpler option and just
866
# reopen it. We reopen it starting from the URL so that we
867
# get a separate connection for RemoteRepositories and can
868
# stream from one of them to the other. This does mean doing
869
# separate SSH connection setup, but unstacking is not a
870
# common operation so it's tolerable.
871
new_bzrdir = bzrdir.BzrDir.open(self.bzrdir.root_transport.base)
872
new_repository = new_bzrdir.find_repository()
873
if new_repository._fallback_repositories:
874
raise AssertionError("didn't expect %r to have "
875
"fallback_repositories"
876
% (self.repository,))
877
# Replace self.repository with the new repository.
878
# Do our best to transfer the lock state (i.e. lock-tokens and
879
# lock count) of self.repository to the new repository.
880
lock_token = old_repository.lock_write().repository_token
881
self.repository = new_repository
882
if isinstance(self, remote.RemoteBranch):
883
# Remote branches can have a second reference to the old
884
# repository that need to be replaced.
885
if self._real_branch is not None:
886
self._real_branch.repository = new_repository
887
self.repository.lock_write(token=lock_token)
888
if lock_token is not None:
889
old_repository.leave_lock_in_place()
890
old_repository.unlock()
891
if lock_token is not None:
892
# XXX: self.repository.leave_lock_in_place() before this
893
# function will not be preserved. Fortunately that doesn't
894
# affect the current default format (2a), and would be a
895
# corner-case anyway.
896
# - Andrew Bennetts, 2010/06/30
897
self.repository.dont_leave_lock_in_place()
901
old_repository.unlock()
902
except errors.LockNotHeld:
905
if old_lock_count == 0:
906
raise AssertionError(
907
'old_repository should have been locked at least once.')
908
for i in range(old_lock_count-1):
909
self.repository.lock_write()
910
# Fetch from the old repository into the new.
911
old_repository.lock_read()
913
# XXX: If you unstack a branch while it has a working tree
914
# with a pending merge, the pending-merged revisions will no
915
# longer be present. You can (probably) revert and remerge.
917
tags_to_fetch = set(self.tags.get_reverse_tag_dict())
918
except errors.TagsNotSupported:
919
tags_to_fetch = set()
920
fetch_spec = _mod_graph.NotInOtherForRevs(self.repository,
921
old_repository, required_ids=[self.last_revision()],
922
if_present_ids=tags_to_fetch, find_ghosts=True).execute()
923
self.repository.fetch(old_repository, fetch_spec=fetch_spec)
925
old_repository.unlock()
929
def _set_tags_bytes(self, bytes):
930
"""Mirror method for _get_tags_bytes.
932
:seealso: Branch._get_tags_bytes.
934
op = cleanup.OperationWithCleanups(self._set_tags_bytes_locked)
935
op.add_cleanup(self.lock_write().unlock)
936
return op.run_simple(bytes)
938
def _set_tags_bytes_locked(self, bytes):
939
self._tags_bytes = bytes
940
return self._transport.put_bytes('tags', bytes)
942
def _cache_revision_history(self, rev_history):
943
"""Set the cached revision history to rev_history.
945
The revision_history method will use this cache to avoid regenerating
946
the revision history.
948
This API is semi-public; it only for use by subclasses, all other code
949
should consider it to be private.
951
self._revision_history_cache = rev_history
953
def _cache_revision_id_to_revno(self, revision_id_to_revno):
954
"""Set the cached revision_id => revno map to revision_id_to_revno.
956
This API is semi-public; it only for use by subclasses, all other code
957
should consider it to be private.
959
self._revision_id_to_revno_cache = revision_id_to_revno
961
def _clear_cached_state(self):
962
"""Clear any cached data on this branch, e.g. cached revision history.
964
This means the next call to revision_history will need to call
965
_gen_revision_history.
967
This API is semi-public; it only for use by subclasses, all other code
968
should consider it to be private.
970
self._revision_history_cache = None
971
self._revision_id_to_revno_cache = None
972
self._last_revision_info_cache = None
973
self._master_branch_cache = None
974
self._merge_sorted_revisions_cache = None
975
self._partial_revision_history_cache = []
976
self._partial_revision_id_to_revno_cache = {}
977
self._tags_bytes = None
979
def _gen_revision_history(self):
980
"""Return sequence of revision hashes on to this branch.
982
Unlike revision_history, this method always regenerates or rereads the
983
revision history, i.e. it does not cache the result, so repeated calls
986
Concrete subclasses should override this instead of revision_history so
987
that subclasses do not need to deal with caching logic.
989
This API is semi-public; it only for use by subclasses, all other code
990
should consider it to be private.
992
raise NotImplementedError(self._gen_revision_history)
995
def revision_history(self):
996
"""Return sequence of revision ids on this branch.
998
This method will cache the revision history for as long as it is safe to
1001
if 'evil' in debug.debug_flags:
1002
mutter_callsite(3, "revision_history scales with history.")
1003
if self._revision_history_cache is not None:
1004
history = self._revision_history_cache
1006
history = self._gen_revision_history()
1007
self._cache_revision_history(history)
1008
return list(history)
1011
"""Return current revision number for this branch.
1013
That is equivalent to the number of revisions committed to
1016
return self.last_revision_info()[0]
1019
"""Older format branches cannot bind or unbind."""
1020
raise errors.UpgradeRequired(self.user_url)
1022
def last_revision(self):
1023
"""Return last revision id, or NULL_REVISION."""
1024
return self.last_revision_info()[1]
1027
def last_revision_info(self):
1028
"""Return information about the last revision.
1030
:return: A tuple (revno, revision_id).
1032
if self._last_revision_info_cache is None:
1033
self._last_revision_info_cache = self._read_last_revision_info()
1034
return self._last_revision_info_cache
1036
def _read_last_revision_info(self):
1037
raise NotImplementedError(self._read_last_revision_info)
1039
@deprecated_method(deprecated_in((2, 4, 0)))
1040
def import_last_revision_info(self, source_repo, revno, revid):
1041
"""Set the last revision info, importing from another repo if necessary.
1043
:param source_repo: Source repository to optionally fetch from
1044
:param revno: Revision number of the new tip
1045
:param revid: Revision id of the new tip
1047
if not self.repository.has_same_location(source_repo):
1048
self.repository.fetch(source_repo, revision_id=revid)
1049
self.set_last_revision_info(revno, revid)
1051
def import_last_revision_info_and_tags(self, source, revno, revid,
1053
"""Set the last revision info, importing from another repo if necessary.
1055
This is used by the bound branch code to upload a revision to
1056
the master branch first before updating the tip of the local branch.
1057
Revisions referenced by source's tags are also transferred.
1059
:param source: Source branch to optionally fetch from
1060
:param revno: Revision number of the new tip
1061
:param revid: Revision id of the new tip
1062
:param lossy: Whether to discard metadata that can not be
1063
natively represented
1064
:return: Tuple with the new revision number and revision id
1065
(should only be different from the arguments when lossy=True)
1067
if not self.repository.has_same_location(source.repository):
1068
self.fetch(source, revid)
1069
self.set_last_revision_info(revno, revid)
1070
return (revno, revid)
1072
def revision_id_to_revno(self, revision_id):
1073
"""Given a revision id, return its revno"""
1074
if _mod_revision.is_null(revision_id):
1076
history = self.revision_history()
1078
return history.index(revision_id) + 1
1080
raise errors.NoSuchRevision(self, revision_id)
1083
def get_rev_id(self, revno, history=None):
1084
"""Find the revision id of the specified revno."""
1086
return _mod_revision.NULL_REVISION
1087
last_revno, last_revid = self.last_revision_info()
1088
if revno == last_revno:
1090
if revno <= 0 or revno > last_revno:
1091
raise errors.NoSuchRevision(self, revno)
1092
distance_from_last = last_revno - revno
1093
if len(self._partial_revision_history_cache) <= distance_from_last:
1094
self._extend_partial_history(distance_from_last)
1095
return self._partial_revision_history_cache[distance_from_last]
1097
def pull(self, source, overwrite=False, stop_revision=None,
1098
possible_transports=None, *args, **kwargs):
1099
"""Mirror source into this branch.
1101
This branch is considered to be 'local', having low latency.
1103
:returns: PullResult instance
1105
return InterBranch.get(source, self).pull(overwrite=overwrite,
1106
stop_revision=stop_revision,
1107
possible_transports=possible_transports, *args, **kwargs)
1109
def push(self, target, overwrite=False, stop_revision=None, lossy=False,
1111
"""Mirror this branch into target.
1113
This branch is considered to be 'local', having low latency.
1115
return InterBranch.get(self, target).push(overwrite, stop_revision,
1116
lossy, *args, **kwargs)
1118
def basis_tree(self):
1119
"""Return `Tree` object for last revision."""
1120
return self.repository.revision_tree(self.last_revision())
1122
def get_parent(self):
1123
"""Return the parent location of the branch.
1125
This is the default location for pull/missing. The usual
1126
pattern is that the user can override it by specifying a
1129
parent = self._get_parent_location()
1132
# This is an old-format absolute path to a local branch
1133
# turn it into a url
1134
if parent.startswith('/'):
1135
parent = urlutils.local_path_to_url(parent.decode('utf8'))
1137
return urlutils.join(self.base[:-1], parent)
1138
except errors.InvalidURLJoin, e:
1139
raise errors.InaccessibleParent(parent, self.user_url)
1141
def _get_parent_location(self):
1142
raise NotImplementedError(self._get_parent_location)
1144
def _set_config_location(self, name, url, config=None,
1145
make_relative=False):
1147
config = self.get_config()
1151
url = urlutils.relative_url(self.base, url)
1152
config.set_user_option(name, url, warn_masked=True)
1154
def _get_config_location(self, name, config=None):
1156
config = self.get_config()
1157
location = config.get_user_option(name)
1162
def get_child_submit_format(self):
1163
"""Return the preferred format of submissions to this branch."""
1164
return self.get_config().get_user_option("child_submit_format")
1166
def get_submit_branch(self):
1167
"""Return the submit location of the branch.
1169
This is the default location for bundle. The usual
1170
pattern is that the user can override it by specifying a
1173
return self.get_config().get_user_option('submit_branch')
1175
def set_submit_branch(self, location):
1176
"""Return the submit location of the branch.
1178
This is the default location for bundle. The usual
1179
pattern is that the user can override it by specifying a
1182
self.get_config().set_user_option('submit_branch', location,
1185
def get_public_branch(self):
1186
"""Return the public location of the branch.
1188
This is used by merge directives.
1190
return self._get_config_location('public_branch')
1192
def set_public_branch(self, location):
1193
"""Return the submit location of the branch.
1195
This is the default location for bundle. The usual
1196
pattern is that the user can override it by specifying a
1199
self._set_config_location('public_branch', location)
1201
def get_push_location(self):
1202
"""Return the None or the location to push this branch to."""
1203
push_loc = self.get_config().get_user_option('push_location')
1206
def set_push_location(self, location):
1207
"""Set a new push location for this branch."""
1208
raise NotImplementedError(self.set_push_location)
1210
def _run_post_change_branch_tip_hooks(self, old_revno, old_revid):
1211
"""Run the post_change_branch_tip hooks."""
1212
hooks = Branch.hooks['post_change_branch_tip']
1215
new_revno, new_revid = self.last_revision_info()
1216
params = ChangeBranchTipParams(
1217
self, old_revno, new_revno, old_revid, new_revid)
1221
def _run_pre_change_branch_tip_hooks(self, new_revno, new_revid):
1222
"""Run the pre_change_branch_tip hooks."""
1223
hooks = Branch.hooks['pre_change_branch_tip']
1226
old_revno, old_revid = self.last_revision_info()
1227
params = ChangeBranchTipParams(
1228
self, old_revno, new_revno, old_revid, new_revid)
1234
"""Synchronise this branch with the master branch if any.
1236
:return: None or the last_revision pivoted out during the update.
1240
def check_revno(self, revno):
1242
Check whether a revno corresponds to any revision.
1243
Zero (the NULL revision) is considered valid.
1246
self.check_real_revno(revno)
1248
def check_real_revno(self, revno):
1250
Check whether a revno corresponds to a real revision.
1251
Zero (the NULL revision) is considered invalid
1253
if revno < 1 or revno > self.revno():
1254
raise errors.InvalidRevisionNumber(revno)
1257
def clone(self, to_bzrdir, revision_id=None, repository_policy=None):
1258
"""Clone this branch into to_bzrdir preserving all semantic values.
1260
Most API users will want 'create_clone_on_transport', which creates a
1261
new bzrdir and branch on the fly.
1263
revision_id: if not None, the revision history in the new branch will
1264
be truncated to end with revision_id.
1266
result = to_bzrdir.create_branch()
1269
if repository_policy is not None:
1270
repository_policy.configure_branch(result)
1271
self.copy_content_into(result, revision_id=revision_id)
1277
def sprout(self, to_bzrdir, revision_id=None, repository_policy=None,
1279
"""Create a new line of development from the branch, into to_bzrdir.
1281
to_bzrdir controls the branch format.
1283
revision_id: if not None, the revision history in the new branch will
1284
be truncated to end with revision_id.
1286
if (repository_policy is not None and
1287
repository_policy.requires_stacking()):
1288
to_bzrdir._format.require_stacking(_skip_repo=True)
1289
result = to_bzrdir.create_branch(repository=repository)
1292
if repository_policy is not None:
1293
repository_policy.configure_branch(result)
1294
self.copy_content_into(result, revision_id=revision_id)
1295
master_url = self.get_bound_location()
1296
if master_url is None:
1297
result.set_parent(self.bzrdir.root_transport.base)
1299
result.set_parent(master_url)
1304
def _synchronize_history(self, destination, revision_id):
1305
"""Synchronize last revision and revision history between branches.
1307
This version is most efficient when the destination is also a
1308
BzrBranch6, but works for BzrBranch5, as long as the destination's
1309
repository contains all the lefthand ancestors of the intended
1310
last_revision. If not, set_last_revision_info will fail.
1312
:param destination: The branch to copy the history into
1313
:param revision_id: The revision-id to truncate history at. May
1314
be None to copy complete history.
1316
source_revno, source_revision_id = self.last_revision_info()
1317
if revision_id is None:
1318
revno, revision_id = source_revno, source_revision_id
1320
graph = self.repository.get_graph()
1322
revno = graph.find_distance_to_null(revision_id,
1323
[(source_revision_id, source_revno)])
1324
except errors.GhostRevisionsHaveNoRevno:
1325
# Default to 1, if we can't find anything else
1327
destination.set_last_revision_info(revno, revision_id)
1329
def copy_content_into(self, destination, revision_id=None):
1330
"""Copy the content of self into destination.
1332
revision_id: if not None, the revision history in the new branch will
1333
be truncated to end with revision_id.
1335
return InterBranch.get(self, destination).copy_content_into(
1336
revision_id=revision_id)
1338
def update_references(self, target):
1339
if not getattr(self._format, 'supports_reference_locations', False):
1341
reference_dict = self._get_all_reference_info()
1342
if len(reference_dict) == 0:
1344
old_base = self.base
1345
new_base = target.base
1346
target_reference_dict = target._get_all_reference_info()
1347
for file_id, (tree_path, branch_location) in (
1348
reference_dict.items()):
1349
branch_location = urlutils.rebase_url(branch_location,
1351
target_reference_dict.setdefault(
1352
file_id, (tree_path, branch_location))
1353
target._set_all_reference_info(target_reference_dict)
1356
def check(self, refs):
1357
"""Check consistency of the branch.
1359
In particular this checks that revisions given in the revision-history
1360
do actually match up in the revision graph, and that they're all
1361
present in the repository.
1363
Callers will typically also want to check the repository.
1365
:param refs: Calculated refs for this branch as specified by
1366
branch._get_check_refs()
1367
:return: A BranchCheckResult.
1369
result = BranchCheckResult(self)
1370
last_revno, last_revision_id = self.last_revision_info()
1371
actual_revno = refs[('lefthand-distance', last_revision_id)]
1372
if actual_revno != last_revno:
1373
result.errors.append(errors.BzrCheckError(
1374
'revno does not match len(mainline) %s != %s' % (
1375
last_revno, actual_revno)))
1376
# TODO: We should probably also check that self.revision_history
1377
# matches the repository for older branch formats.
1378
# If looking for the code that cross-checks repository parents against
1379
# the iter_reverse_revision_history output, that is now a repository
1383
def _get_checkout_format(self, lightweight=False):
1384
"""Return the most suitable metadir for a checkout of this branch.
1385
Weaves are used if this branch's repository uses weaves.
1387
format = self.repository.bzrdir.checkout_metadir()
1388
format.set_branch_format(self._format)
1391
def create_clone_on_transport(self, to_transport, revision_id=None,
1392
stacked_on=None, create_prefix=False, use_existing_dir=False,
1394
"""Create a clone of this branch and its bzrdir.
1396
:param to_transport: The transport to clone onto.
1397
:param revision_id: The revision id to use as tip in the new branch.
1398
If None the tip is obtained from this branch.
1399
:param stacked_on: An optional URL to stack the clone on.
1400
:param create_prefix: Create any missing directories leading up to
1402
:param use_existing_dir: Use an existing directory if one exists.
1404
# XXX: Fix the bzrdir API to allow getting the branch back from the
1405
# clone call. Or something. 20090224 RBC/spiv.
1406
# XXX: Should this perhaps clone colocated branches as well,
1407
# rather than just the default branch? 20100319 JRV
1408
if revision_id is None:
1409
revision_id = self.last_revision()
1410
dir_to = self.bzrdir.clone_on_transport(to_transport,
1411
revision_id=revision_id, stacked_on=stacked_on,
1412
create_prefix=create_prefix, use_existing_dir=use_existing_dir,
1414
return dir_to.open_branch()
1416
def create_checkout(self, to_location, revision_id=None,
1417
lightweight=False, accelerator_tree=None,
1419
"""Create a checkout of a branch.
1421
:param to_location: The url to produce the checkout at
1422
:param revision_id: The revision to check out
1423
:param lightweight: If True, produce a lightweight checkout, otherwise,
1424
produce a bound branch (heavyweight checkout)
1425
:param accelerator_tree: A tree which can be used for retrieving file
1426
contents more quickly than the revision tree, i.e. a workingtree.
1427
The revision tree will be used for cases where accelerator_tree's
1428
content is different.
1429
:param hardlink: If true, hard-link files from accelerator_tree,
1431
:return: The tree of the created checkout
1433
t = transport.get_transport(to_location)
1435
format = self._get_checkout_format(lightweight=lightweight)
1437
checkout = format.initialize_on_transport(t)
1438
from_branch = BranchReferenceFormat().initialize(checkout,
1441
checkout_branch = bzrdir.BzrDir.create_branch_convenience(
1442
to_location, force_new_tree=False, format=format)
1443
checkout = checkout_branch.bzrdir
1444
checkout_branch.bind(self)
1445
# pull up to the specified revision_id to set the initial
1446
# branch tip correctly, and seed it with history.
1447
checkout_branch.pull(self, stop_revision=revision_id)
1449
tree = checkout.create_workingtree(revision_id,
1450
from_branch=from_branch,
1451
accelerator_tree=accelerator_tree,
1453
basis_tree = tree.basis_tree()
1454
basis_tree.lock_read()
1456
for path, file_id in basis_tree.iter_references():
1457
reference_parent = self.reference_parent(file_id, path)
1458
reference_parent.create_checkout(tree.abspath(path),
1459
basis_tree.get_reference_revision(file_id, path),
1466
def reconcile(self, thorough=True):
1467
"""Make sure the data stored in this branch is consistent."""
1468
from bzrlib.reconcile import BranchReconciler
1469
reconciler = BranchReconciler(self, thorough=thorough)
1470
reconciler.reconcile()
1473
def reference_parent(self, file_id, path, possible_transports=None):
1474
"""Return the parent branch for a tree-reference file_id
1476
:param file_id: The file_id of the tree reference
1477
:param path: The path of the file_id in the tree
1478
:return: A branch associated with the file_id
1480
# FIXME should provide multiple branches, based on config
1481
return Branch.open(self.bzrdir.root_transport.clone(path).base,
1482
possible_transports=possible_transports)
1484
def supports_tags(self):
1485
return self._format.supports_tags()
1487
def automatic_tag_name(self, revision_id):
1488
"""Try to automatically find the tag name for a revision.
1490
:param revision_id: Revision id of the revision.
1491
:return: A tag name or None if no tag name could be determined.
1493
for hook in Branch.hooks['automatic_tag_name']:
1494
ret = hook(self, revision_id)
1499
def _check_if_descendant_or_diverged(self, revision_a, revision_b, graph,
1501
"""Ensure that revision_b is a descendant of revision_a.
1503
This is a helper function for update_revisions.
1505
:raises: DivergedBranches if revision_b has diverged from revision_a.
1506
:returns: True if revision_b is a descendant of revision_a.
1508
relation = self._revision_relations(revision_a, revision_b, graph)
1509
if relation == 'b_descends_from_a':
1511
elif relation == 'diverged':
1512
raise errors.DivergedBranches(self, other_branch)
1513
elif relation == 'a_descends_from_b':
1516
raise AssertionError("invalid relation: %r" % (relation,))
1518
def _revision_relations(self, revision_a, revision_b, graph):
1519
"""Determine the relationship between two revisions.
1521
:returns: One of: 'a_descends_from_b', 'b_descends_from_a', 'diverged'
1523
heads = graph.heads([revision_a, revision_b])
1524
if heads == set([revision_b]):
1525
return 'b_descends_from_a'
1526
elif heads == set([revision_a, revision_b]):
1527
# These branches have diverged
1529
elif heads == set([revision_a]):
1530
return 'a_descends_from_b'
1532
raise AssertionError("invalid heads: %r" % (heads,))
1534
def heads_to_fetch(self):
1535
"""Return the heads that must and that should be fetched to copy this
1536
branch into another repo.
1538
:returns: a 2-tuple of (must_fetch, if_present_fetch). must_fetch is a
1539
set of heads that must be fetched. if_present_fetch is a set of
1540
heads that must be fetched if present, but no error is necessary if
1541
they are not present.
1543
# For bzr native formats must_fetch is just the tip, and if_present_fetch
1545
must_fetch = set([self.last_revision()])
1546
if_present_fetch = set()
1547
c = self.get_config()
1548
include_tags = c.get_user_option_as_bool('branch.fetch_tags',
1552
if_present_fetch = set(self.tags.get_reverse_tag_dict())
1553
except errors.TagsNotSupported:
1555
must_fetch.discard(_mod_revision.NULL_REVISION)
1556
if_present_fetch.discard(_mod_revision.NULL_REVISION)
1557
return must_fetch, if_present_fetch
1560
class BranchFormat(controldir.ControlComponentFormat):
1561
"""An encapsulation of the initialization and open routines for a format.
1563
Formats provide three things:
1564
* An initialization routine,
1568
Formats are placed in an dict by their format string for reference
1569
during branch opening. It's not required that these be instances, they
1570
can be classes themselves with class methods - it simply depends on
1571
whether state is needed for a given format or not.
1573
Once a format is deprecated, just deprecate the initialize and open
1574
methods on the format class. Do not deprecate the object, as the
1575
object will be created every time regardless.
1578
def __eq__(self, other):
1579
return self.__class__ is other.__class__
1581
def __ne__(self, other):
1582
return not (self == other)
1585
def find_format(klass, a_bzrdir, name=None):
1586
"""Return the format for the branch object in a_bzrdir."""
1588
transport = a_bzrdir.get_branch_transport(None, name=name)
1589
format_string = transport.get_bytes("format")
1590
return format_registry.get(format_string)
1591
except errors.NoSuchFile:
1592
raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
1594
raise errors.UnknownFormatError(format=format_string, kind='branch')
1597
@deprecated_method(deprecated_in((2, 4, 0)))
1598
def get_default_format(klass):
1599
"""Return the current default format."""
1600
return format_registry.get_default()
1603
@deprecated_method(deprecated_in((2, 4, 0)))
1604
def get_formats(klass):
1605
"""Get all the known formats.
1607
Warning: This triggers a load of all lazy registered formats: do not
1608
use except when that is desireed.
1610
return format_registry._get_all()
1612
def get_reference(self, a_bzrdir, name=None):
1613
"""Get the target reference of the branch in a_bzrdir.
1615
format probing must have been completed before calling
1616
this method - it is assumed that the format of the branch
1617
in a_bzrdir is correct.
1619
:param a_bzrdir: The bzrdir to get the branch data from.
1620
:param name: Name of the colocated branch to fetch
1621
:return: None if the branch is not a reference branch.
1626
def set_reference(self, a_bzrdir, name, to_branch):
1627
"""Set the target reference of the branch in a_bzrdir.
1629
format probing must have been completed before calling
1630
this method - it is assumed that the format of the branch
1631
in a_bzrdir is correct.
1633
:param a_bzrdir: The bzrdir to set the branch reference for.
1634
:param name: Name of colocated branch to set, None for default
1635
:param to_branch: branch that the checkout is to reference
1637
raise NotImplementedError(self.set_reference)
1639
def get_format_string(self):
1640
"""Return the ASCII format string that identifies this format."""
1641
raise NotImplementedError(self.get_format_string)
1643
def get_format_description(self):
1644
"""Return the short format description for this format."""
1645
raise NotImplementedError(self.get_format_description)
1647
def _run_post_branch_init_hooks(self, a_bzrdir, name, branch):
1648
hooks = Branch.hooks['post_branch_init']
1651
params = BranchInitHookParams(self, a_bzrdir, name, branch)
1655
def initialize(self, a_bzrdir, name=None, repository=None,
1656
append_revisions_only=None):
1657
"""Create a branch of this format in a_bzrdir.
1659
:param name: Name of the colocated branch to create.
1661
raise NotImplementedError(self.initialize)
1663
def is_supported(self):
1664
"""Is this format supported?
1666
Supported formats can be initialized and opened.
1667
Unsupported formats may not support initialization or committing or
1668
some other features depending on the reason for not being supported.
1672
def make_tags(self, branch):
1673
"""Create a tags object for branch.
1675
This method is on BranchFormat, because BranchFormats are reflected
1676
over the wire via network_name(), whereas full Branch instances require
1677
multiple VFS method calls to operate at all.
1679
The default implementation returns a disabled-tags instance.
1681
Note that it is normal for branch to be a RemoteBranch when using tags
1684
return _mod_tag.DisabledTags(branch)
1686
def network_name(self):
1687
"""A simple byte string uniquely identifying this format for RPC calls.
1689
MetaDir branch formats use their disk format string to identify the
1690
repository over the wire. All in one formats such as bzr < 0.8, and
1691
foreign formats like svn/git and hg should use some marker which is
1692
unique and immutable.
1694
raise NotImplementedError(self.network_name)
1696
def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
1697
found_repository=None):
1698
"""Return the branch object for a_bzrdir
1700
:param a_bzrdir: A BzrDir that contains a branch.
1701
:param name: Name of colocated branch to open
1702
:param _found: a private parameter, do not use it. It is used to
1703
indicate if format probing has already be done.
1704
:param ignore_fallbacks: when set, no fallback branches will be opened
1705
(if there are any). Default is to open fallbacks.
1707
raise NotImplementedError(self.open)
1710
@deprecated_method(deprecated_in((2, 4, 0)))
1711
def register_format(klass, format):
1712
"""Register a metadir format.
1714
See MetaDirBranchFormatFactory for the ability to register a format
1715
without loading the code the format needs until it is actually used.
1717
format_registry.register(format)
1720
@deprecated_method(deprecated_in((2, 4, 0)))
1721
def set_default_format(klass, format):
1722
format_registry.set_default(format)
1724
def supports_set_append_revisions_only(self):
1725
"""True if this format supports set_append_revisions_only."""
1728
def supports_stacking(self):
1729
"""True if this format records a stacked-on branch."""
1732
def supports_leaving_lock(self):
1733
"""True if this format supports leaving locks in place."""
1734
return False # by default
1737
@deprecated_method(deprecated_in((2, 4, 0)))
1738
def unregister_format(klass, format):
1739
format_registry.remove(format)
1742
return self.get_format_description().rstrip()
1744
def supports_tags(self):
1745
"""True if this format supports tags stored in the branch"""
1746
return False # by default
1748
def tags_are_versioned(self):
1749
"""Whether the tag container for this branch versions tags."""
1752
def supports_tags_referencing_ghosts(self):
1753
"""True if tags can reference ghost revisions."""
1757
class MetaDirBranchFormatFactory(registry._LazyObjectGetter):
1758
"""A factory for a BranchFormat object, permitting simple lazy registration.
1760
While none of the built in BranchFormats are lazy registered yet,
1761
bzrlib.tests.test_branch.TestMetaDirBranchFormatFactory demonstrates how to
1762
use it, and the bzr-loom plugin uses it as well (see
1763
bzrlib.plugins.loom.formats).
1766
def __init__(self, format_string, module_name, member_name):
1767
"""Create a MetaDirBranchFormatFactory.
1769
:param format_string: The format string the format has.
1770
:param module_name: Module to load the format class from.
1771
:param member_name: Attribute name within the module for the format class.
1773
registry._LazyObjectGetter.__init__(self, module_name, member_name)
1774
self._format_string = format_string
1776
def get_format_string(self):
1777
"""See BranchFormat.get_format_string."""
1778
return self._format_string
1781
"""Used for network_format_registry support."""
1782
return self.get_obj()()
1785
class BranchHooks(Hooks):
1786
"""A dictionary mapping hook name to a list of callables for branch hooks.
1788
e.g. ['set_rh'] Is the list of items to be called when the
1789
set_revision_history function is invoked.
1793
"""Create the default hooks.
1795
These are all empty initially, because by default nothing should get
1798
Hooks.__init__(self, "bzrlib.branch", "Branch.hooks")
1799
self.add_hook('set_rh',
1800
"Invoked whenever the revision history has been set via "
1801
"set_revision_history. The api signature is (branch, "
1802
"revision_history), and the branch will be write-locked. "
1803
"The set_rh hook can be expensive for bzr to trigger, a better "
1804
"hook to use is Branch.post_change_branch_tip.", (0, 15))
1805
self.add_hook('open',
1806
"Called with the Branch object that has been opened after a "
1807
"branch is opened.", (1, 8))
1808
self.add_hook('post_push',
1809
"Called after a push operation completes. post_push is called "
1810
"with a bzrlib.branch.BranchPushResult object and only runs in the "
1811
"bzr client.", (0, 15))
1812
self.add_hook('post_pull',
1813
"Called after a pull operation completes. post_pull is called "
1814
"with a bzrlib.branch.PullResult object and only runs in the "
1815
"bzr client.", (0, 15))
1816
self.add_hook('pre_commit',
1817
"Called after a commit is calculated but before it is "
1818
"completed. pre_commit is called with (local, master, old_revno, "
1819
"old_revid, future_revno, future_revid, tree_delta, future_tree"
1820
"). old_revid is NULL_REVISION for the first commit to a branch, "
1821
"tree_delta is a TreeDelta object describing changes from the "
1822
"basis revision. hooks MUST NOT modify this delta. "
1823
" future_tree is an in-memory tree obtained from "
1824
"CommitBuilder.revision_tree() and hooks MUST NOT modify this "
1826
self.add_hook('post_commit',
1827
"Called in the bzr client after a commit has completed. "
1828
"post_commit is called with (local, master, old_revno, old_revid, "
1829
"new_revno, new_revid). old_revid is NULL_REVISION for the first "
1830
"commit to a branch.", (0, 15))
1831
self.add_hook('post_uncommit',
1832
"Called in the bzr client after an uncommit completes. "
1833
"post_uncommit is called with (local, master, old_revno, "
1834
"old_revid, new_revno, new_revid) where local is the local branch "
1835
"or None, master is the target branch, and an empty branch "
1836
"receives new_revno of 0, new_revid of None.", (0, 15))
1837
self.add_hook('pre_change_branch_tip',
1838
"Called in bzr client and server before a change to the tip of a "
1839
"branch is made. pre_change_branch_tip is called with a "
1840
"bzrlib.branch.ChangeBranchTipParams. Note that push, pull, "
1841
"commit, uncommit will all trigger this hook.", (1, 6))
1842
self.add_hook('post_change_branch_tip',
1843
"Called in bzr client and server after a change to the tip of a "
1844
"branch is made. post_change_branch_tip is called with a "
1845
"bzrlib.branch.ChangeBranchTipParams. Note that push, pull, "
1846
"commit, uncommit will all trigger this hook.", (1, 4))
1847
self.add_hook('transform_fallback_location',
1848
"Called when a stacked branch is activating its fallback "
1849
"locations. transform_fallback_location is called with (branch, "
1850
"url), and should return a new url. Returning the same url "
1851
"allows it to be used as-is, returning a different one can be "
1852
"used to cause the branch to stack on a closer copy of that "
1853
"fallback_location. Note that the branch cannot have history "
1854
"accessing methods called on it during this hook because the "
1855
"fallback locations have not been activated. When there are "
1856
"multiple hooks installed for transform_fallback_location, "
1857
"all are called with the url returned from the previous hook."
1858
"The order is however undefined.", (1, 9))
1859
self.add_hook('automatic_tag_name',
1860
"Called to determine an automatic tag name for a revision. "
1861
"automatic_tag_name is called with (branch, revision_id) and "
1862
"should return a tag name or None if no tag name could be "
1863
"determined. The first non-None tag name returned will be used.",
1865
self.add_hook('post_branch_init',
1866
"Called after new branch initialization completes. "
1867
"post_branch_init is called with a "
1868
"bzrlib.branch.BranchInitHookParams. "
1869
"Note that init, branch and checkout (both heavyweight and "
1870
"lightweight) will all trigger this hook.", (2, 2))
1871
self.add_hook('post_switch',
1872
"Called after a checkout switches branch. "
1873
"post_switch is called with a "
1874
"bzrlib.branch.SwitchHookParams.", (2, 2))
1878
# install the default hooks into the Branch class.
1879
Branch.hooks = BranchHooks()
1882
class ChangeBranchTipParams(object):
1883
"""Object holding parameters passed to `*_change_branch_tip` hooks.
1885
There are 5 fields that hooks may wish to access:
1887
:ivar branch: the branch being changed
1888
:ivar old_revno: revision number before the change
1889
:ivar new_revno: revision number after the change
1890
:ivar old_revid: revision id before the change
1891
:ivar new_revid: revision id after the change
1893
The revid fields are strings. The revno fields are integers.
1896
def __init__(self, branch, old_revno, new_revno, old_revid, new_revid):
1897
"""Create a group of ChangeBranchTip parameters.
1899
:param branch: The branch being changed.
1900
:param old_revno: Revision number before the change.
1901
:param new_revno: Revision number after the change.
1902
:param old_revid: Tip revision id before the change.
1903
:param new_revid: Tip revision id after the change.
1905
self.branch = branch
1906
self.old_revno = old_revno
1907
self.new_revno = new_revno
1908
self.old_revid = old_revid
1909
self.new_revid = new_revid
1911
def __eq__(self, other):
1912
return self.__dict__ == other.__dict__
1915
return "<%s of %s from (%s, %s) to (%s, %s)>" % (
1916
self.__class__.__name__, self.branch,
1917
self.old_revno, self.old_revid, self.new_revno, self.new_revid)
1920
class BranchInitHookParams(object):
1921
"""Object holding parameters passed to `*_branch_init` hooks.
1923
There are 4 fields that hooks may wish to access:
1925
:ivar format: the branch format
1926
:ivar bzrdir: the BzrDir where the branch will be/has been initialized
1927
:ivar name: name of colocated branch, if any (or None)
1928
:ivar branch: the branch created
1930
Note that for lightweight checkouts, the bzrdir and format fields refer to
1931
the checkout, hence they are different from the corresponding fields in
1932
branch, which refer to the original branch.
1935
def __init__(self, format, a_bzrdir, name, branch):
1936
"""Create a group of BranchInitHook parameters.
1938
:param format: the branch format
1939
:param a_bzrdir: the BzrDir where the branch will be/has been
1941
:param name: name of colocated branch, if any (or None)
1942
:param branch: the branch created
1944
Note that for lightweight checkouts, the bzrdir and format fields refer
1945
to the checkout, hence they are different from the corresponding fields
1946
in branch, which refer to the original branch.
1948
self.format = format
1949
self.bzrdir = a_bzrdir
1951
self.branch = branch
1953
def __eq__(self, other):
1954
return self.__dict__ == other.__dict__
1957
return "<%s of %s>" % (self.__class__.__name__, self.branch)
1960
class SwitchHookParams(object):
1961
"""Object holding parameters passed to `*_switch` hooks.
1963
There are 4 fields that hooks may wish to access:
1965
:ivar control_dir: BzrDir of the checkout to change
1966
:ivar to_branch: branch that the checkout is to reference
1967
:ivar force: skip the check for local commits in a heavy checkout
1968
:ivar revision_id: revision ID to switch to (or None)
1971
def __init__(self, control_dir, to_branch, force, revision_id):
1972
"""Create a group of SwitchHook parameters.
1974
:param control_dir: BzrDir of the checkout to change
1975
:param to_branch: branch that the checkout is to reference
1976
:param force: skip the check for local commits in a heavy checkout
1977
:param revision_id: revision ID to switch to (or None)
1979
self.control_dir = control_dir
1980
self.to_branch = to_branch
1982
self.revision_id = revision_id
1984
def __eq__(self, other):
1985
return self.__dict__ == other.__dict__
1988
return "<%s for %s to (%s, %s)>" % (self.__class__.__name__,
1989
self.control_dir, self.to_branch,
1993
class BranchFormatMetadir(BranchFormat):
1994
"""Common logic for meta-dir based branch formats."""
1996
def _branch_class(self):
1997
"""What class to instantiate on open calls."""
1998
raise NotImplementedError(self._branch_class)
2000
def _get_initial_config(self, append_revisions_only=None):
2001
if append_revisions_only:
2002
return "append_revisions_only = True\n"
2004
# Avoid writing anything if append_revisions_only is disabled,
2005
# as that is the default.
2008
def _initialize_helper(self, a_bzrdir, utf8_files, name=None,
2010
"""Initialize a branch in a bzrdir, with specified files
2012
:param a_bzrdir: The bzrdir to initialize the branch in
2013
:param utf8_files: The files to create as a list of
2014
(filename, content) tuples
2015
:param name: Name of colocated branch to create, if any
2016
:return: a branch in this format
2018
mutter('creating branch %r in %s', self, a_bzrdir.user_url)
2019
branch_transport = a_bzrdir.get_branch_transport(self, name=name)
2020
control_files = lockable_files.LockableFiles(branch_transport,
2021
'lock', lockdir.LockDir)
2022
control_files.create_lock()
2023
control_files.lock_write()
2025
utf8_files += [('format', self.get_format_string())]
2026
for (filename, content) in utf8_files:
2027
branch_transport.put_bytes(
2029
mode=a_bzrdir._get_file_mode())
2031
control_files.unlock()
2032
branch = self.open(a_bzrdir, name, _found=True,
2033
found_repository=repository)
2034
self._run_post_branch_init_hooks(a_bzrdir, name, branch)
2037
def network_name(self):
2038
"""A simple byte string uniquely identifying this format for RPC calls.
2040
Metadir branch formats use their format string.
2042
return self.get_format_string()
2044
def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
2045
found_repository=None):
2046
"""See BranchFormat.open()."""
2048
format = BranchFormat.find_format(a_bzrdir, name=name)
2049
if format.__class__ != self.__class__:
2050
raise AssertionError("wrong format %r found for %r" %
2052
transport = a_bzrdir.get_branch_transport(None, name=name)
2054
control_files = lockable_files.LockableFiles(transport, 'lock',
2056
if found_repository is None:
2057
found_repository = a_bzrdir.find_repository()
2058
return self._branch_class()(_format=self,
2059
_control_files=control_files,
2062
_repository=found_repository,
2063
ignore_fallbacks=ignore_fallbacks)
2064
except errors.NoSuchFile:
2065
raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
2068
super(BranchFormatMetadir, self).__init__()
2069
self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
2070
self._matchingbzrdir.set_branch_format(self)
2072
def supports_tags(self):
2075
def supports_leaving_lock(self):
2079
class BzrBranchFormat5(BranchFormatMetadir):
2080
"""Bzr branch format 5.
2083
- a revision-history file.
2085
- a lock dir guarding the branch itself
2086
- all of this stored in a branch/ subdirectory
2087
- works with shared repositories.
2089
This format is new in bzr 0.8.
2092
def _branch_class(self):
2095
def get_format_string(self):
2096
"""See BranchFormat.get_format_string()."""
2097
return "Bazaar-NG branch format 5\n"
2099
def get_format_description(self):
2100
"""See BranchFormat.get_format_description()."""
2101
return "Branch format 5"
2103
def initialize(self, a_bzrdir, name=None, repository=None,
2104
append_revisions_only=None):
2105
"""Create a branch of this format in a_bzrdir."""
2106
if append_revisions_only:
2107
raise errors.UpgradeRequired(a_bzrdir.user_url)
2108
utf8_files = [('revision-history', ''),
2109
('branch-name', ''),
2111
return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2113
def supports_tags(self):
2117
class BzrBranchFormat6(BranchFormatMetadir):
2118
"""Branch format with last-revision and tags.
2120
Unlike previous formats, this has no explicit revision history. Instead,
2121
this just stores the last-revision, and the left-hand history leading
2122
up to there is the history.
2124
This format was introduced in bzr 0.15
2125
and became the default in 0.91.
2128
def _branch_class(self):
2131
def get_format_string(self):
2132
"""See BranchFormat.get_format_string()."""
2133
return "Bazaar Branch Format 6 (bzr 0.15)\n"
2135
def get_format_description(self):
2136
"""See BranchFormat.get_format_description()."""
2137
return "Branch format 6"
2139
def initialize(self, a_bzrdir, name=None, repository=None,
2140
append_revisions_only=None):
2141
"""Create a branch of this format in a_bzrdir."""
2142
utf8_files = [('last-revision', '0 null:\n'),
2144
self._get_initial_config(append_revisions_only)),
2147
return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2149
def make_tags(self, branch):
2150
"""See bzrlib.branch.BranchFormat.make_tags()."""
2151
return _mod_tag.BasicTags(branch)
2153
def supports_set_append_revisions_only(self):
2157
class BzrBranchFormat8(BranchFormatMetadir):
2158
"""Metadir format supporting storing locations of subtree branches."""
2160
def _branch_class(self):
2163
def get_format_string(self):
2164
"""See BranchFormat.get_format_string()."""
2165
return "Bazaar Branch Format 8 (needs bzr 1.15)\n"
2167
def get_format_description(self):
2168
"""See BranchFormat.get_format_description()."""
2169
return "Branch format 8"
2171
def initialize(self, a_bzrdir, name=None, repository=None,
2172
append_revisions_only=None):
2173
"""Create a branch of this format in a_bzrdir."""
2174
utf8_files = [('last-revision', '0 null:\n'),
2176
self._get_initial_config(append_revisions_only)),
2180
return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2182
def make_tags(self, branch):
2183
"""See bzrlib.branch.BranchFormat.make_tags()."""
2184
return _mod_tag.BasicTags(branch)
2186
def supports_set_append_revisions_only(self):
2189
def supports_stacking(self):
2192
supports_reference_locations = True
2195
class BzrBranchFormat7(BranchFormatMetadir):
2196
"""Branch format with last-revision, tags, and a stacked location pointer.
2198
The stacked location pointer is passed down to the repository and requires
2199
a repository format with supports_external_lookups = True.
2201
This format was introduced in bzr 1.6.
2204
def initialize(self, a_bzrdir, name=None, repository=None,
2205
append_revisions_only=None):
2206
"""Create a branch of this format in a_bzrdir."""
2207
utf8_files = [('last-revision', '0 null:\n'),
2209
self._get_initial_config(append_revisions_only)),
2212
return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2214
def _branch_class(self):
2217
def get_format_string(self):
2218
"""See BranchFormat.get_format_string()."""
2219
return "Bazaar Branch Format 7 (needs bzr 1.6)\n"
2221
def get_format_description(self):
2222
"""See BranchFormat.get_format_description()."""
2223
return "Branch format 7"
2225
def supports_set_append_revisions_only(self):
2228
def supports_stacking(self):
2231
def make_tags(self, branch):
2232
"""See bzrlib.branch.BranchFormat.make_tags()."""
2233
return _mod_tag.BasicTags(branch)
2235
supports_reference_locations = False
2238
class BranchReferenceFormat(BranchFormat):
2239
"""Bzr branch reference format.
2241
Branch references are used in implementing checkouts, they
2242
act as an alias to the real branch which is at some other url.
2249
def get_format_string(self):
2250
"""See BranchFormat.get_format_string()."""
2251
return "Bazaar-NG Branch Reference Format 1\n"
2253
def get_format_description(self):
2254
"""See BranchFormat.get_format_description()."""
2255
return "Checkout reference format 1"
2257
def get_reference(self, a_bzrdir, name=None):
2258
"""See BranchFormat.get_reference()."""
2259
transport = a_bzrdir.get_branch_transport(None, name=name)
2260
return transport.get_bytes('location')
2262
def set_reference(self, a_bzrdir, name, to_branch):
2263
"""See BranchFormat.set_reference()."""
2264
transport = a_bzrdir.get_branch_transport(None, name=name)
2265
location = transport.put_bytes('location', to_branch.base)
2267
def initialize(self, a_bzrdir, name=None, target_branch=None,
2268
repository=None, append_revisions_only=None):
2269
"""Create a branch of this format in a_bzrdir."""
2270
if target_branch is None:
2271
# this format does not implement branch itself, thus the implicit
2272
# creation contract must see it as uninitializable
2273
raise errors.UninitializableFormat(self)
2274
mutter('creating branch reference in %s', a_bzrdir.user_url)
2275
if a_bzrdir._format.fixed_components:
2276
raise errors.IncompatibleFormat(self, a_bzrdir._format)
2277
branch_transport = a_bzrdir.get_branch_transport(self, name=name)
2278
branch_transport.put_bytes('location',
2279
target_branch.bzrdir.user_url)
2280
branch_transport.put_bytes('format', self.get_format_string())
2282
a_bzrdir, name, _found=True,
2283
possible_transports=[target_branch.bzrdir.root_transport])
2284
self._run_post_branch_init_hooks(a_bzrdir, name, branch)
2288
super(BranchReferenceFormat, self).__init__()
2289
self._matchingbzrdir = bzrdir.BzrDirMetaFormat1()
2290
self._matchingbzrdir.set_branch_format(self)
2292
def _make_reference_clone_function(format, a_branch):
2293
"""Create a clone() routine for a branch dynamically."""
2294
def clone(to_bzrdir, revision_id=None,
2295
repository_policy=None):
2296
"""See Branch.clone()."""
2297
return format.initialize(to_bzrdir, target_branch=a_branch)
2298
# cannot obey revision_id limits when cloning a reference ...
2299
# FIXME RBC 20060210 either nuke revision_id for clone, or
2300
# emit some sort of warning/error to the caller ?!
2303
def open(self, a_bzrdir, name=None, _found=False, location=None,
2304
possible_transports=None, ignore_fallbacks=False,
2305
found_repository=None):
2306
"""Return the branch that the branch reference in a_bzrdir points at.
2308
:param a_bzrdir: A BzrDir that contains a branch.
2309
:param name: Name of colocated branch to open, if any
2310
:param _found: a private parameter, do not use it. It is used to
2311
indicate if format probing has already be done.
2312
:param ignore_fallbacks: when set, no fallback branches will be opened
2313
(if there are any). Default is to open fallbacks.
2314
:param location: The location of the referenced branch. If
2315
unspecified, this will be determined from the branch reference in
2317
:param possible_transports: An optional reusable transports list.
2320
format = BranchFormat.find_format(a_bzrdir, name=name)
2321
if format.__class__ != self.__class__:
2322
raise AssertionError("wrong format %r found for %r" %
2324
if location is None:
2325
location = self.get_reference(a_bzrdir, name)
2326
real_bzrdir = bzrdir.BzrDir.open(
2327
location, possible_transports=possible_transports)
2328
result = real_bzrdir.open_branch(name=name,
2329
ignore_fallbacks=ignore_fallbacks)
2330
# this changes the behaviour of result.clone to create a new reference
2331
# rather than a copy of the content of the branch.
2332
# I did not use a proxy object because that needs much more extensive
2333
# testing, and we are only changing one behaviour at the moment.
2334
# If we decide to alter more behaviours - i.e. the implicit nickname
2335
# then this should be refactored to introduce a tested proxy branch
2336
# and a subclass of that for use in overriding clone() and ....
2338
result.clone = self._make_reference_clone_function(result)
2342
class BranchFormatRegistry(controldir.ControlComponentFormatRegistry):
2343
"""Branch format registry."""
2345
def __init__(self, other_registry=None):
2346
super(BranchFormatRegistry, self).__init__(other_registry)
2347
self._default_format = None
2349
def set_default(self, format):
2350
self._default_format = format
2352
def get_default(self):
2353
return self._default_format
2356
network_format_registry = registry.FormatRegistry()
2357
"""Registry of formats indexed by their network name.
2359
The network name for a branch format is an identifier that can be used when
2360
referring to formats with smart server operations. See
2361
BranchFormat.network_name() for more detail.
2364
format_registry = BranchFormatRegistry(network_format_registry)
2367
# formats which have no format string are not discoverable
2368
# and not independently creatable, so are not registered.
2369
__format5 = BzrBranchFormat5()
2370
__format6 = BzrBranchFormat6()
2371
__format7 = BzrBranchFormat7()
2372
__format8 = BzrBranchFormat8()
2373
format_registry.register(__format5)
2374
format_registry.register(BranchReferenceFormat())
2375
format_registry.register(__format6)
2376
format_registry.register(__format7)
2377
format_registry.register(__format8)
2378
format_registry.set_default(__format7)
2381
class BranchWriteLockResult(LogicalLockResult):
2382
"""The result of write locking a branch.
2384
:ivar branch_token: The token obtained from the underlying branch lock, or
2386
:ivar unlock: A callable which will unlock the lock.
2389
def __init__(self, unlock, branch_token):
2390
LogicalLockResult.__init__(self, unlock)
2391
self.branch_token = branch_token
2394
return "BranchWriteLockResult(%s, %s)" % (self.branch_token,
2398
class BzrBranch(Branch, _RelockDebugMixin):
2399
"""A branch stored in the actual filesystem.
2401
Note that it's "local" in the context of the filesystem; it doesn't
2402
really matter if it's on an nfs/smb/afs/coda/... share, as long as
2403
it's writable, and can be accessed via the normal filesystem API.
2405
:ivar _transport: Transport for file operations on this branch's
2406
control files, typically pointing to the .bzr/branch directory.
2407
:ivar repository: Repository for this branch.
2408
:ivar base: The url of the base directory for this branch; the one
2409
containing the .bzr directory.
2410
:ivar name: Optional colocated branch name as it exists in the control
2414
def __init__(self, _format=None,
2415
_control_files=None, a_bzrdir=None, name=None,
2416
_repository=None, ignore_fallbacks=False):
2417
"""Create new branch object at a particular location."""
2418
if a_bzrdir is None:
2419
raise ValueError('a_bzrdir must be supplied')
2421
self.bzrdir = a_bzrdir
2422
self._base = self.bzrdir.transport.clone('..').base
2424
# XXX: We should be able to just do
2425
# self.base = self.bzrdir.root_transport.base
2426
# but this does not quite work yet -- mbp 20080522
2427
self._format = _format
2428
if _control_files is None:
2429
raise ValueError('BzrBranch _control_files is None')
2430
self.control_files = _control_files
2431
self._transport = _control_files._transport
2432
self.repository = _repository
2433
Branch.__init__(self)
2436
if self.name is None:
2437
return '%s(%s)' % (self.__class__.__name__, self.user_url)
2439
return '%s(%s,%s)' % (self.__class__.__name__, self.user_url,
2444
def _get_base(self):
2445
"""Returns the directory containing the control directory."""
2448
base = property(_get_base, doc="The URL for the root of this branch.")
2450
def _get_config(self):
2451
return _mod_config.TransportConfig(self._transport, 'branch.conf')
2453
def is_locked(self):
2454
return self.control_files.is_locked()
2456
def lock_write(self, token=None):
2457
"""Lock the branch for write operations.
2459
:param token: A token to permit reacquiring a previously held and
2461
:return: A BranchWriteLockResult.
2463
if not self.is_locked():
2464
self._note_lock('w')
2465
# All-in-one needs to always unlock/lock.
2466
repo_control = getattr(self.repository, 'control_files', None)
2467
if self.control_files == repo_control or not self.is_locked():
2468
self.repository._warn_if_deprecated(self)
2469
self.repository.lock_write()
2474
return BranchWriteLockResult(self.unlock,
2475
self.control_files.lock_write(token=token))
2478
self.repository.unlock()
2481
def lock_read(self):
2482
"""Lock the branch for read operations.
2484
:return: A bzrlib.lock.LogicalLockResult.
2486
if not self.is_locked():
2487
self._note_lock('r')
2488
# All-in-one needs to always unlock/lock.
2489
repo_control = getattr(self.repository, 'control_files', None)
2490
if self.control_files == repo_control or not self.is_locked():
2491
self.repository._warn_if_deprecated(self)
2492
self.repository.lock_read()
2497
self.control_files.lock_read()
2498
return LogicalLockResult(self.unlock)
2501
self.repository.unlock()
2504
@only_raises(errors.LockNotHeld, errors.LockBroken)
2507
self.control_files.unlock()
2509
# All-in-one needs to always unlock/lock.
2510
repo_control = getattr(self.repository, 'control_files', None)
2511
if (self.control_files == repo_control or
2512
not self.control_files.is_locked()):
2513
self.repository.unlock()
2514
if not self.control_files.is_locked():
2515
# we just released the lock
2516
self._clear_cached_state()
2518
def peek_lock_mode(self):
2519
if self.control_files._lock_count == 0:
2522
return self.control_files._lock_mode
2524
def get_physical_lock_status(self):
2525
return self.control_files.get_physical_lock_status()
2528
def print_file(self, file, revision_id):
2529
"""See Branch.print_file."""
2530
return self.repository.print_file(file, revision_id)
2533
def set_last_revision_info(self, revno, revision_id):
2534
if not revision_id or not isinstance(revision_id, basestring):
2535
raise errors.InvalidRevisionId(revision_id=revision_id, branch=self)
2536
revision_id = _mod_revision.ensure_null(revision_id)
2537
old_revno, old_revid = self.last_revision_info()
2538
if self.get_append_revisions_only():
2539
self._check_history_violation(revision_id)
2540
self._run_pre_change_branch_tip_hooks(revno, revision_id)
2541
self._write_last_revision_info(revno, revision_id)
2542
self._clear_cached_state()
2543
self._last_revision_info_cache = revno, revision_id
2544
self._run_post_change_branch_tip_hooks(old_revno, old_revid)
2546
def basis_tree(self):
2547
"""See Branch.basis_tree."""
2548
return self.repository.revision_tree(self.last_revision())
2550
def _get_parent_location(self):
2551
_locs = ['parent', 'pull', 'x-pull']
2554
return self._transport.get_bytes(l).strip('\n')
2555
except errors.NoSuchFile:
2559
def get_stacked_on_url(self):
2560
raise errors.UnstackableBranchFormat(self._format, self.user_url)
2562
def set_push_location(self, location):
2563
"""See Branch.set_push_location."""
2564
self.get_config().set_user_option(
2565
'push_location', location,
2566
store=_mod_config.STORE_LOCATION_NORECURSE)
2568
def _set_parent_location(self, url):
2570
self._transport.delete('parent')
2572
self._transport.put_bytes('parent', url + '\n',
2573
mode=self.bzrdir._get_file_mode())
2577
"""If bound, unbind"""
2578
return self.set_bound_location(None)
2581
def bind(self, other):
2582
"""Bind this branch to the branch other.
2584
This does not push or pull data between the branches, though it does
2585
check for divergence to raise an error when the branches are not
2586
either the same, or one a prefix of the other. That behaviour may not
2587
be useful, so that check may be removed in future.
2589
:param other: The branch to bind to
2592
# TODO: jam 20051230 Consider checking if the target is bound
2593
# It is debatable whether you should be able to bind to
2594
# a branch which is itself bound.
2595
# Committing is obviously forbidden,
2596
# but binding itself may not be.
2597
# Since we *have* to check at commit time, we don't
2598
# *need* to check here
2600
# we want to raise diverged if:
2601
# last_rev is not in the other_last_rev history, AND
2602
# other_last_rev is not in our history, and do it without pulling
2604
self.set_bound_location(other.base)
2606
def get_bound_location(self):
2608
return self._transport.get_bytes('bound')[:-1]
2609
except errors.NoSuchFile:
2613
def get_master_branch(self, possible_transports=None):
2614
"""Return the branch we are bound to.
2616
:return: Either a Branch, or None
2618
if self._master_branch_cache is None:
2619
self._master_branch_cache = self._get_master_branch(
2620
possible_transports)
2621
return self._master_branch_cache
2623
def _get_master_branch(self, possible_transports):
2624
bound_loc = self.get_bound_location()
2628
return Branch.open(bound_loc,
2629
possible_transports=possible_transports)
2630
except (errors.NotBranchError, errors.ConnectionError), e:
2631
raise errors.BoundBranchConnectionFailure(
2635
def set_bound_location(self, location):
2636
"""Set the target where this branch is bound to.
2638
:param location: URL to the target branch
2640
self._master_branch_cache = None
2642
self._transport.put_bytes('bound', location+'\n',
2643
mode=self.bzrdir._get_file_mode())
2646
self._transport.delete('bound')
2647
except errors.NoSuchFile:
2652
def update(self, possible_transports=None):
2653
"""Synchronise this branch with the master branch if any.
2655
:return: None or the last_revision that was pivoted out during the
2658
master = self.get_master_branch(possible_transports)
2659
if master is not None:
2660
old_tip = _mod_revision.ensure_null(self.last_revision())
2661
self.pull(master, overwrite=True)
2662
if self.repository.get_graph().is_ancestor(old_tip,
2663
_mod_revision.ensure_null(self.last_revision())):
2668
def _read_last_revision_info(self):
2669
revision_string = self._transport.get_bytes('last-revision')
2670
revno, revision_id = revision_string.rstrip('\n').split(' ', 1)
2671
revision_id = cache_utf8.get_cached_utf8(revision_id)
2673
return revno, revision_id
2675
def _write_last_revision_info(self, revno, revision_id):
2676
"""Simply write out the revision id, with no checks.
2678
Use set_last_revision_info to perform this safely.
2680
Does not update the revision_history cache.
2682
revision_id = _mod_revision.ensure_null(revision_id)
2683
out_string = '%d %s\n' % (revno, revision_id)
2684
self._transport.put_bytes('last-revision', out_string,
2685
mode=self.bzrdir._get_file_mode())
2688
class FullHistoryBzrBranch(BzrBranch):
2689
"""Bzr branch which contains the full revision history."""
2692
def set_last_revision_info(self, revno, revision_id):
2693
if not revision_id or not isinstance(revision_id, basestring):
2694
raise errors.InvalidRevisionId(revision_id=revision_id, branch=self)
2695
revision_id = _mod_revision.ensure_null(revision_id)
2696
# this old format stores the full history, but this api doesn't
2697
# provide it, so we must generate, and might as well check it's
2699
history = self._lefthand_history(revision_id)
2700
if len(history) != revno:
2701
raise AssertionError('%d != %d' % (len(history), revno))
2702
self._set_revision_history(history)
2704
def _read_last_revision_info(self):
2705
rh = self.revision_history()
2708
return (revno, rh[-1])
2710
return (0, _mod_revision.NULL_REVISION)
2712
@deprecated_method(deprecated_in((2, 4, 0)))
2714
def set_revision_history(self, rev_history):
2715
"""See Branch.set_revision_history."""
2716
self._set_revision_history(rev_history)
2718
def _set_revision_history(self, rev_history):
2719
if 'evil' in debug.debug_flags:
2720
mutter_callsite(3, "set_revision_history scales with history.")
2721
check_not_reserved_id = _mod_revision.check_not_reserved_id
2722
for rev_id in rev_history:
2723
check_not_reserved_id(rev_id)
2724
if Branch.hooks['post_change_branch_tip']:
2725
# Don't calculate the last_revision_info() if there are no hooks
2727
old_revno, old_revid = self.last_revision_info()
2728
if len(rev_history) == 0:
2729
revid = _mod_revision.NULL_REVISION
2731
revid = rev_history[-1]
2732
self._run_pre_change_branch_tip_hooks(len(rev_history), revid)
2733
self._write_revision_history(rev_history)
2734
self._clear_cached_state()
2735
self._cache_revision_history(rev_history)
2736
for hook in Branch.hooks['set_rh']:
2737
hook(self, rev_history)
2738
if Branch.hooks['post_change_branch_tip']:
2739
self._run_post_change_branch_tip_hooks(old_revno, old_revid)
2741
def _write_revision_history(self, history):
2742
"""Factored out of set_revision_history.
2744
This performs the actual writing to disk.
2745
It is intended to be called by set_revision_history."""
2746
self._transport.put_bytes(
2747
'revision-history', '\n'.join(history),
2748
mode=self.bzrdir._get_file_mode())
2750
def _gen_revision_history(self):
2751
history = self._transport.get_bytes('revision-history').split('\n')
2752
if history[-1:] == ['']:
2753
# There shouldn't be a trailing newline, but just in case.
2757
def _synchronize_history(self, destination, revision_id):
2758
if not isinstance(destination, FullHistoryBzrBranch):
2759
super(BzrBranch, self)._synchronize_history(
2760
destination, revision_id)
2762
if revision_id == _mod_revision.NULL_REVISION:
2765
new_history = self.revision_history()
2766
if revision_id is not None and new_history != []:
2768
new_history = new_history[:new_history.index(revision_id) + 1]
2770
rev = self.repository.get_revision(revision_id)
2771
new_history = rev.get_history(self.repository)[1:]
2772
destination._set_revision_history(new_history)
2775
def generate_revision_history(self, revision_id, last_rev=None,
2777
"""Create a new revision history that will finish with revision_id.
2779
:param revision_id: the new tip to use.
2780
:param last_rev: The previous last_revision. If not None, then this
2781
must be a ancestory of revision_id, or DivergedBranches is raised.
2782
:param other_branch: The other branch that DivergedBranches should
2783
raise with respect to.
2785
self._set_revision_history(self._lefthand_history(revision_id,
2786
last_rev, other_branch))
2789
class BzrBranch5(FullHistoryBzrBranch):
2790
"""A format 5 branch. This supports new features over plain branches.
2792
It has support for a master_branch which is the data for bound branches.
2796
class BzrBranch8(BzrBranch):
2797
"""A branch that stores tree-reference locations."""
2799
def _open_hook(self):
2800
if self._ignore_fallbacks:
2803
url = self.get_stacked_on_url()
2804
except (errors.UnstackableRepositoryFormat, errors.NotStacked,
2805
errors.UnstackableBranchFormat):
2808
for hook in Branch.hooks['transform_fallback_location']:
2809
url = hook(self, url)
2811
hook_name = Branch.hooks.get_hook_name(hook)
2812
raise AssertionError(
2813
"'transform_fallback_location' hook %s returned "
2814
"None, not a URL." % hook_name)
2815
self._activate_fallback_location(url)
2817
def __init__(self, *args, **kwargs):
2818
self._ignore_fallbacks = kwargs.get('ignore_fallbacks', False)
2819
super(BzrBranch8, self).__init__(*args, **kwargs)
2820
self._last_revision_info_cache = None
2821
self._reference_info = None
2823
def _clear_cached_state(self):
2824
super(BzrBranch8, self)._clear_cached_state()
2825
self._last_revision_info_cache = None
2826
self._reference_info = None
2828
def _check_history_violation(self, revision_id):
2829
current_revid = self.last_revision()
2830
last_revision = _mod_revision.ensure_null(current_revid)
2831
if _mod_revision.is_null(last_revision):
2833
graph = self.repository.get_graph()
2834
for lh_ancestor in graph.iter_lefthand_ancestry(revision_id):
2835
if lh_ancestor == current_revid:
2837
raise errors.AppendRevisionsOnlyViolation(self.user_url)
2839
def _gen_revision_history(self):
2840
"""Generate the revision history from last revision
2842
last_revno, last_revision = self.last_revision_info()
2843
self._extend_partial_history(stop_index=last_revno-1)
2844
return list(reversed(self._partial_revision_history_cache))
2847
def _set_parent_location(self, url):
2848
"""Set the parent branch"""
2849
self._set_config_location('parent_location', url, make_relative=True)
2852
def _get_parent_location(self):
2853
"""Set the parent branch"""
2854
return self._get_config_location('parent_location')
2857
def _set_all_reference_info(self, info_dict):
2858
"""Replace all reference info stored in a branch.
2860
:param info_dict: A dict of {file_id: (tree_path, branch_location)}
2863
writer = rio.RioWriter(s)
2864
for key, (tree_path, branch_location) in info_dict.iteritems():
2865
stanza = rio.Stanza(file_id=key, tree_path=tree_path,
2866
branch_location=branch_location)
2867
writer.write_stanza(stanza)
2868
self._transport.put_bytes('references', s.getvalue())
2869
self._reference_info = info_dict
2872
def _get_all_reference_info(self):
2873
"""Return all the reference info stored in a branch.
2875
:return: A dict of {file_id: (tree_path, branch_location)}
2877
if self._reference_info is not None:
2878
return self._reference_info
2879
rio_file = self._transport.get('references')
2881
stanzas = rio.read_stanzas(rio_file)
2882
info_dict = dict((s['file_id'], (s['tree_path'],
2883
s['branch_location'])) for s in stanzas)
2886
self._reference_info = info_dict
2889
def set_reference_info(self, file_id, tree_path, branch_location):
2890
"""Set the branch location to use for a tree reference.
2892
:param file_id: The file-id of the tree reference.
2893
:param tree_path: The path of the tree reference in the tree.
2894
:param branch_location: The location of the branch to retrieve tree
2897
info_dict = self._get_all_reference_info()
2898
info_dict[file_id] = (tree_path, branch_location)
2899
if None in (tree_path, branch_location):
2900
if tree_path is not None:
2901
raise ValueError('tree_path must be None when branch_location'
2903
if branch_location is not None:
2904
raise ValueError('branch_location must be None when tree_path'
2906
del info_dict[file_id]
2907
self._set_all_reference_info(info_dict)
2909
def get_reference_info(self, file_id):
2910
"""Get the tree_path and branch_location for a tree reference.
2912
:return: a tuple of (tree_path, branch_location)
2914
return self._get_all_reference_info().get(file_id, (None, None))
2916
def reference_parent(self, file_id, path, possible_transports=None):
2917
"""Return the parent branch for a tree-reference file_id.
2919
:param file_id: The file_id of the tree reference
2920
:param path: The path of the file_id in the tree
2921
:return: A branch associated with the file_id
2923
branch_location = self.get_reference_info(file_id)[1]
2924
if branch_location is None:
2925
return Branch.reference_parent(self, file_id, path,
2926
possible_transports)
2927
branch_location = urlutils.join(self.user_url, branch_location)
2928
return Branch.open(branch_location,
2929
possible_transports=possible_transports)
2931
def set_push_location(self, location):
2932
"""See Branch.set_push_location."""
2933
self._set_config_location('push_location', location)
2935
def set_bound_location(self, location):
2936
"""See Branch.set_push_location."""
2937
self._master_branch_cache = None
2939
config = self.get_config()
2940
if location is None:
2941
if config.get_user_option('bound') != 'True':
2944
config.set_user_option('bound', 'False', warn_masked=True)
2947
self._set_config_location('bound_location', location,
2949
config.set_user_option('bound', 'True', warn_masked=True)
2952
def _get_bound_location(self, bound):
2953
"""Return the bound location in the config file.
2955
Return None if the bound parameter does not match"""
2956
config = self.get_config()
2957
config_bound = (config.get_user_option('bound') == 'True')
2958
if config_bound != bound:
2960
return self._get_config_location('bound_location', config=config)
2962
def get_bound_location(self):
2963
"""See Branch.set_push_location."""
2964
return self._get_bound_location(True)
2966
def get_old_bound_location(self):
2967
"""See Branch.get_old_bound_location"""
2968
return self._get_bound_location(False)
2970
def get_stacked_on_url(self):
2971
# you can always ask for the URL; but you might not be able to use it
2972
# if the repo can't support stacking.
2973
## self._check_stackable_repo()
2974
# stacked_on_location is only ever defined in branch.conf, so don't
2975
# waste effort reading the whole stack of config files.
2976
config = self.get_config()._get_branch_data_config()
2977
stacked_url = self._get_config_location('stacked_on_location',
2979
if stacked_url is None:
2980
raise errors.NotStacked(self)
2984
def get_rev_id(self, revno, history=None):
2985
"""Find the revision id of the specified revno."""
2987
return _mod_revision.NULL_REVISION
2989
last_revno, last_revision_id = self.last_revision_info()
2990
if revno <= 0 or revno > last_revno:
2991
raise errors.NoSuchRevision(self, revno)
2993
if history is not None:
2994
return history[revno - 1]
2996
index = last_revno - revno
2997
if len(self._partial_revision_history_cache) <= index:
2998
self._extend_partial_history(stop_index=index)
2999
if len(self._partial_revision_history_cache) > index:
3000
return self._partial_revision_history_cache[index]
3002
raise errors.NoSuchRevision(self, revno)
3005
def revision_id_to_revno(self, revision_id):
3006
"""Given a revision id, return its revno"""
3007
if _mod_revision.is_null(revision_id):
3010
index = self._partial_revision_history_cache.index(revision_id)
3013
self._extend_partial_history(stop_revision=revision_id)
3014
except errors.RevisionNotPresent, e:
3015
raise errors.GhostRevisionsHaveNoRevno(revision_id, e.revision_id)
3016
index = len(self._partial_revision_history_cache) - 1
3017
if self._partial_revision_history_cache[index] != revision_id:
3018
raise errors.NoSuchRevision(self, revision_id)
3019
return self.revno() - index
3022
class BzrBranch7(BzrBranch8):
3023
"""A branch with support for a fallback repository."""
3025
def set_reference_info(self, file_id, tree_path, branch_location):
3026
Branch.set_reference_info(self, file_id, tree_path, branch_location)
3028
def get_reference_info(self, file_id):
3029
Branch.get_reference_info(self, file_id)
3031
def reference_parent(self, file_id, path, possible_transports=None):
3032
return Branch.reference_parent(self, file_id, path,
3033
possible_transports)
3036
class BzrBranch6(BzrBranch7):
3037
"""See BzrBranchFormat6 for the capabilities of this branch.
3039
This subclass of BzrBranch7 disables the new features BzrBranch7 added,
3043
def get_stacked_on_url(self):
3044
raise errors.UnstackableBranchFormat(self._format, self.user_url)
3047
######################################################################
3048
# results of operations
3051
class _Result(object):
3053
def _show_tag_conficts(self, to_file):
3054
if not getattr(self, 'tag_conflicts', None):
3056
to_file.write('Conflicting tags:\n')
3057
for name, value1, value2 in self.tag_conflicts:
3058
to_file.write(' %s\n' % (name, ))
3061
class PullResult(_Result):
3062
"""Result of a Branch.pull operation.
3064
:ivar old_revno: Revision number before pull.
3065
:ivar new_revno: Revision number after pull.
3066
:ivar old_revid: Tip revision id before pull.
3067
:ivar new_revid: Tip revision id after pull.
3068
:ivar source_branch: Source (local) branch object. (read locked)
3069
:ivar master_branch: Master branch of the target, or the target if no
3071
:ivar local_branch: target branch if there is a Master, else None
3072
:ivar target_branch: Target/destination branch object. (write locked)
3073
:ivar tag_conflicts: A list of tag conflicts, see BasicTags.merge_to
3074
:ivar tag_updates: A dict with new tags, see BasicTags.merge_to
3077
@deprecated_method(deprecated_in((2, 3, 0)))
3079
"""Return the relative change in revno.
3081
:deprecated: Use `new_revno` and `old_revno` instead.
3083
return self.new_revno - self.old_revno
3085
def report(self, to_file):
3086
tag_conflicts = getattr(self, "tag_conflicts", None)
3087
tag_updates = getattr(self, "tag_updates", None)
3089
if self.old_revid != self.new_revid:
3090
to_file.write('Now on revision %d.\n' % self.new_revno)
3092
to_file.write('%d tag(s) updated.\n' % len(tag_updates))
3093
if self.old_revid == self.new_revid and not tag_updates:
3094
if not tag_conflicts:
3095
to_file.write('No revisions or tags to pull.\n')
3097
to_file.write('No revisions to pull.\n')
3098
self._show_tag_conficts(to_file)
3101
class BranchPushResult(_Result):
3102
"""Result of a Branch.push operation.
3104
:ivar old_revno: Revision number (eg 10) of the target before push.
3105
:ivar new_revno: Revision number (eg 12) of the target after push.
3106
:ivar old_revid: Tip revision id (eg joe@foo.com-1234234-aoeua34) of target
3108
:ivar new_revid: Tip revision id (eg joe@foo.com-5676566-boa234a) of target
3110
:ivar source_branch: Source branch object that the push was from. This is
3111
read locked, and generally is a local (and thus low latency) branch.
3112
:ivar master_branch: If target is a bound branch, the master branch of
3113
target, or target itself. Always write locked.
3114
:ivar target_branch: The direct Branch where data is being sent (write
3116
:ivar local_branch: If the target is a bound branch this will be the
3117
target, otherwise it will be None.
3120
@deprecated_method(deprecated_in((2, 3, 0)))
3122
"""Return the relative change in revno.
3124
:deprecated: Use `new_revno` and `old_revno` instead.
3126
return self.new_revno - self.old_revno
3128
def report(self, to_file):
3129
# TODO: This function gets passed a to_file, but then
3130
# ignores it and calls note() instead. This is also
3131
# inconsistent with PullResult(), which writes to stdout.
3132
# -- JRV20110901, bug #838853
3133
tag_conflicts = getattr(self, "tag_conflicts", None)
3134
tag_updates = getattr(self, "tag_updates", None)
3136
if self.old_revid != self.new_revid:
3137
note('Pushed up to revision %d.' % self.new_revno)
3139
note('%d tag(s) updated.' % len(tag_updates))
3140
if self.old_revid == self.new_revid and not tag_updates:
3141
if not tag_conflicts:
3142
note('No new revisions or tags to push.')
3144
note('No new revisions to push.')
3145
self._show_tag_conficts(to_file)
3148
class BranchCheckResult(object):
3149
"""Results of checking branch consistency.
3154
def __init__(self, branch):
3155
self.branch = branch
3158
def report_results(self, verbose):
3159
"""Report the check results via trace.note.
3161
:param verbose: Requests more detailed display of what was checked,
3164
note('checked branch %s format %s', self.branch.user_url,
3165
self.branch._format)
3166
for error in self.errors:
3167
note('found error:%s', error)
3170
class Converter5to6(object):
3171
"""Perform an in-place upgrade of format 5 to format 6"""
3173
def convert(self, branch):
3174
# Data for 5 and 6 can peacefully coexist.
3175
format = BzrBranchFormat6()
3176
new_branch = format.open(branch.bzrdir, _found=True)
3178
# Copy source data into target
3179
new_branch._write_last_revision_info(*branch.last_revision_info())
3180
new_branch.set_parent(branch.get_parent())
3181
new_branch.set_bound_location(branch.get_bound_location())
3182
new_branch.set_push_location(branch.get_push_location())
3184
# New branch has no tags by default
3185
new_branch.tags._set_tag_dict({})
3187
# Copying done; now update target format
3188
new_branch._transport.put_bytes('format',
3189
format.get_format_string(),
3190
mode=new_branch.bzrdir._get_file_mode())
3192
# Clean up old files
3193
new_branch._transport.delete('revision-history')
3195
branch.set_parent(None)
3196
except errors.NoSuchFile:
3198
branch.set_bound_location(None)
3201
class Converter6to7(object):
3202
"""Perform an in-place upgrade of format 6 to format 7"""
3204
def convert(self, branch):
3205
format = BzrBranchFormat7()
3206
branch._set_config_location('stacked_on_location', '')
3207
# update target format
3208
branch._transport.put_bytes('format', format.get_format_string())
3211
class Converter7to8(object):
3212
"""Perform an in-place upgrade of format 7 to format 8"""
3214
def convert(self, branch):
3215
format = BzrBranchFormat8()
3216
branch._transport.put_bytes('references', '')
3217
# update target format
3218
branch._transport.put_bytes('format', format.get_format_string())
3221
class InterBranch(InterObject):
3222
"""This class represents operations taking place between two branches.
3224
Its instances have methods like pull() and push() and contain
3225
references to the source and target repositories these operations
3226
can be carried out on.
3230
"""The available optimised InterBranch types."""
3233
def _get_branch_formats_to_test(klass):
3234
"""Return an iterable of format tuples for testing.
3236
:return: An iterable of (from_format, to_format) to use when testing
3237
this InterBranch class. Each InterBranch class should define this
3240
raise NotImplementedError(klass._get_branch_formats_to_test)
3243
def pull(self, overwrite=False, stop_revision=None,
3244
possible_transports=None, local=False):
3245
"""Mirror source into target branch.
3247
The target branch is considered to be 'local', having low latency.
3249
:returns: PullResult instance
3251
raise NotImplementedError(self.pull)
3254
def push(self, overwrite=False, stop_revision=None, lossy=False,
3255
_override_hook_source_branch=None):
3256
"""Mirror the source branch into the target branch.
3258
The source branch is considered to be 'local', having low latency.
3260
raise NotImplementedError(self.push)
3263
def copy_content_into(self, revision_id=None):
3264
"""Copy the content of source into target
3266
revision_id: if not None, the revision history in the new branch will
3267
be truncated to end with revision_id.
3269
raise NotImplementedError(self.copy_content_into)
3272
def fetch(self, stop_revision=None, limit=None):
3275
:param stop_revision: Last revision to fetch
3276
:param limit: Optional rough limit of revisions to fetch
3278
raise NotImplementedError(self.fetch)
3281
class GenericInterBranch(InterBranch):
3282
"""InterBranch implementation that uses public Branch functions."""
3285
def is_compatible(klass, source, target):
3286
# GenericBranch uses the public API, so always compatible
3290
def _get_branch_formats_to_test(klass):
3291
return [(format_registry.get_default(), format_registry.get_default())]
3294
def unwrap_format(klass, format):
3295
if isinstance(format, remote.RemoteBranchFormat):
3296
format._ensure_real()
3297
return format._custom_format
3301
def copy_content_into(self, revision_id=None):
3302
"""Copy the content of source into target
3304
revision_id: if not None, the revision history in the new branch will
3305
be truncated to end with revision_id.
3307
self.source.update_references(self.target)
3308
self.source._synchronize_history(self.target, revision_id)
3310
parent = self.source.get_parent()
3311
except errors.InaccessibleParent, e:
3312
mutter('parent was not accessible to copy: %s', e)
3315
self.target.set_parent(parent)
3316
if self.source._push_should_merge_tags():
3317
self.source.tags.merge_to(self.target.tags)
3320
def fetch(self, stop_revision=None, limit=None):
3321
if self.target.base == self.source.base:
3323
self.source.lock_read()
3325
fetch_spec_factory = fetch.FetchSpecFactory()
3326
fetch_spec_factory.source_branch = self.source
3327
fetch_spec_factory.source_branch_stop_revision_id = stop_revision
3328
fetch_spec_factory.source_repo = self.source.repository
3329
fetch_spec_factory.target_repo = self.target.repository
3330
fetch_spec_factory.target_repo_kind = fetch.TargetRepoKinds.PREEXISTING
3331
fetch_spec_factory.limit = limit
3332
fetch_spec = fetch_spec_factory.make_fetch_spec()
3333
return self.target.repository.fetch(self.source.repository,
3334
fetch_spec=fetch_spec)
3336
self.source.unlock()
3339
def _update_revisions(self, stop_revision=None, overwrite=False,
3341
other_revno, other_last_revision = self.source.last_revision_info()
3342
stop_revno = None # unknown
3343
if stop_revision is None:
3344
stop_revision = other_last_revision
3345
if _mod_revision.is_null(stop_revision):
3346
# if there are no commits, we're done.
3348
stop_revno = other_revno
3350
# what's the current last revision, before we fetch [and change it
3352
last_rev = _mod_revision.ensure_null(self.target.last_revision())
3353
# we fetch here so that we don't process data twice in the common
3354
# case of having something to pull, and so that the check for
3355
# already merged can operate on the just fetched graph, which will
3356
# be cached in memory.
3357
self.fetch(stop_revision=stop_revision)
3358
# Check to see if one is an ancestor of the other
3361
graph = self.target.repository.get_graph()
3362
if self.target._check_if_descendant_or_diverged(
3363
stop_revision, last_rev, graph, self.source):
3364
# stop_revision is a descendant of last_rev, but we aren't
3365
# overwriting, so we're done.
3367
if stop_revno is None:
3369
graph = self.target.repository.get_graph()
3370
this_revno, this_last_revision = \
3371
self.target.last_revision_info()
3372
stop_revno = graph.find_distance_to_null(stop_revision,
3373
[(other_last_revision, other_revno),
3374
(this_last_revision, this_revno)])
3375
self.target.set_last_revision_info(stop_revno, stop_revision)
3378
def pull(self, overwrite=False, stop_revision=None,
3379
possible_transports=None, run_hooks=True,
3380
_override_hook_target=None, local=False):
3381
"""Pull from source into self, updating my master if any.
3383
:param run_hooks: Private parameter - if false, this branch
3384
is being called because it's the master of the primary branch,
3385
so it should not run its hooks.
3387
bound_location = self.target.get_bound_location()
3388
if local and not bound_location:
3389
raise errors.LocalRequiresBoundBranch()
3390
master_branch = None
3391
source_is_master = False
3393
# bound_location comes from a config file, some care has to be
3394
# taken to relate it to source.user_url
3395
normalized = urlutils.normalize_url(bound_location)
3397
relpath = self.source.user_transport.relpath(normalized)
3398
source_is_master = (relpath == '')
3399
except (errors.PathNotChild, errors.InvalidURL):
3400
source_is_master = False
3401
if not local and bound_location and not source_is_master:
3402
# not pulling from master, so we need to update master.
3403
master_branch = self.target.get_master_branch(possible_transports)
3404
master_branch.lock_write()
3407
# pull from source into master.
3408
master_branch.pull(self.source, overwrite, stop_revision,
3410
return self._pull(overwrite,
3411
stop_revision, _hook_master=master_branch,
3412
run_hooks=run_hooks,
3413
_override_hook_target=_override_hook_target,
3414
merge_tags_to_master=not source_is_master)
3417
master_branch.unlock()
3419
def push(self, overwrite=False, stop_revision=None, lossy=False,
3420
_override_hook_source_branch=None):
3421
"""See InterBranch.push.
3423
This is the basic concrete implementation of push()
3425
:param _override_hook_source_branch: If specified, run the hooks
3426
passing this Branch as the source, rather than self. This is for
3427
use of RemoteBranch, where push is delegated to the underlying
3431
raise errors.LossyPushToSameVCS(self.source, self.target)
3432
# TODO: Public option to disable running hooks - should be trivial but
3435
op = cleanup.OperationWithCleanups(self._push_with_bound_branches)
3436
op.add_cleanup(self.source.lock_read().unlock)
3437
op.add_cleanup(self.target.lock_write().unlock)
3438
return op.run(overwrite, stop_revision,
3439
_override_hook_source_branch=_override_hook_source_branch)
3441
def _basic_push(self, overwrite, stop_revision):
3442
"""Basic implementation of push without bound branches or hooks.
3444
Must be called with source read locked and target write locked.
3446
result = BranchPushResult()
3447
result.source_branch = self.source
3448
result.target_branch = self.target
3449
result.old_revno, result.old_revid = self.target.last_revision_info()
3450
self.source.update_references(self.target)
3451
if result.old_revid != stop_revision:
3452
# We assume that during 'push' this repository is closer than
3454
graph = self.source.repository.get_graph(self.target.repository)
3455
self._update_revisions(stop_revision, overwrite=overwrite,
3457
if self.source._push_should_merge_tags():
3458
result.tag_updates, result.tag_conflicts = (
3459
self.source.tags.merge_to(self.target.tags, overwrite))
3460
result.new_revno, result.new_revid = self.target.last_revision_info()
3463
def _push_with_bound_branches(self, operation, overwrite, stop_revision,
3464
_override_hook_source_branch=None):
3465
"""Push from source into target, and into target's master if any.
3468
if _override_hook_source_branch:
3469
result.source_branch = _override_hook_source_branch
3470
for hook in Branch.hooks['post_push']:
3473
bound_location = self.target.get_bound_location()
3474
if bound_location and self.target.base != bound_location:
3475
# there is a master branch.
3477
# XXX: Why the second check? Is it even supported for a branch to
3478
# be bound to itself? -- mbp 20070507
3479
master_branch = self.target.get_master_branch()
3480
master_branch.lock_write()
3481
operation.add_cleanup(master_branch.unlock)
3482
# push into the master from the source branch.
3483
master_inter = InterBranch.get(self.source, master_branch)
3484
master_inter._basic_push(overwrite, stop_revision)
3485
# and push into the target branch from the source. Note that
3486
# we push from the source branch again, because it's considered
3487
# the highest bandwidth repository.
3488
result = self._basic_push(overwrite, stop_revision)
3489
result.master_branch = master_branch
3490
result.local_branch = self.target
3492
master_branch = None
3494
result = self._basic_push(overwrite, stop_revision)
3495
# TODO: Why set master_branch and local_branch if there's no
3496
# binding? Maybe cleaner to just leave them unset? -- mbp
3498
result.master_branch = self.target
3499
result.local_branch = None
3503
def _pull(self, overwrite=False, stop_revision=None,
3504
possible_transports=None, _hook_master=None, run_hooks=True,
3505
_override_hook_target=None, local=False,
3506
merge_tags_to_master=True):
3509
This function is the core worker, used by GenericInterBranch.pull to
3510
avoid duplication when pulling source->master and source->local.
3512
:param _hook_master: Private parameter - set the branch to
3513
be supplied as the master to pull hooks.
3514
:param run_hooks: Private parameter - if false, this branch
3515
is being called because it's the master of the primary branch,
3516
so it should not run its hooks.
3517
is being called because it's the master of the primary branch,
3518
so it should not run its hooks.
3519
:param _override_hook_target: Private parameter - set the branch to be
3520
supplied as the target_branch to pull hooks.
3521
:param local: Only update the local branch, and not the bound branch.
3523
# This type of branch can't be bound.
3525
raise errors.LocalRequiresBoundBranch()
3526
result = PullResult()
3527
result.source_branch = self.source
3528
if _override_hook_target is None:
3529
result.target_branch = self.target
3531
result.target_branch = _override_hook_target
3532
self.source.lock_read()
3534
# We assume that during 'pull' the target repository is closer than
3536
self.source.update_references(self.target)
3537
graph = self.target.repository.get_graph(self.source.repository)
3538
# TODO: Branch formats should have a flag that indicates
3539
# that revno's are expensive, and pull() should honor that flag.
3541
result.old_revno, result.old_revid = \
3542
self.target.last_revision_info()
3543
self._update_revisions(stop_revision, overwrite=overwrite,
3545
# TODO: The old revid should be specified when merging tags,
3546
# so a tags implementation that versions tags can only
3547
# pull in the most recent changes. -- JRV20090506
3548
result.tag_updates, result.tag_conflicts = (
3549
self.source.tags.merge_to(self.target.tags, overwrite,
3550
ignore_master=not merge_tags_to_master))
3551
result.new_revno, result.new_revid = self.target.last_revision_info()
3553
result.master_branch = _hook_master
3554
result.local_branch = result.target_branch
3556
result.master_branch = result.target_branch
3557
result.local_branch = None
3559
for hook in Branch.hooks['post_pull']:
3562
self.source.unlock()
3566
InterBranch.register_optimiser(GenericInterBranch)