40
48
import bzrlib.inventory as inventory
41
49
from bzrlib.inventory import Inventory
42
from bzrlib.lockable_files import LockableFiles, TransportLock
43
from bzrlib.lockdir import LockDir
50
from bzrlib.lockable_files import LockableFiles
44
51
from bzrlib.osutils import (isdir, quotefn,
45
52
rename, splitpath, sha_file,
46
53
file_kind, abspath, normpath, pathjoin,
50
56
from bzrlib.textui import show_status
51
57
from bzrlib.trace import mutter, note
52
58
from bzrlib.tree import EmptyTree, RevisionTree
53
59
from bzrlib.repository import Repository
54
60
from bzrlib.revision import (
61
get_intervening_revisions,
684
614
- a revision-history file.
685
615
- a format string
686
- a lock dir guarding the branch itself
687
- all of this stored in a branch/ subdirectory
688
617
- works with shared repositories.
690
This format is new in bzr 0.8.
693
620
def get_format_string(self):
694
621
"""See BranchFormat.get_format_string()."""
695
622
return "Bazaar-NG branch format 5\n"
697
def get_format_description(self):
698
"""See BranchFormat.get_format_description()."""
699
return "Branch format 5"
701
624
def initialize(self, a_bzrdir):
702
625
"""Create a branch of this format in a_bzrdir."""
703
mutter('creating branch %r in %s', self, a_bzrdir.transport.base)
626
mutter('creating branch in %s', a_bzrdir.transport.base)
704
627
branch_transport = a_bzrdir.get_branch_transport(self)
705
629
utf8_files = [('revision-history', ''),
706
630
('branch-name', ''),
708
control_files = LockableFiles(branch_transport, 'lock', LockDir)
709
control_files.create_lock()
633
branch_transport.put(lock_file, StringIO()) # TODO get the file mode from the bzrdir lock files., mode=file_mode)
634
control_files = LockableFiles(branch_transport, 'lock')
710
635
control_files.lock_write()
711
636
control_files.put_utf8('format', self.get_format_string())
826
744
really matter if it's on an nfs/smb/afs/coda/... share, as long as
827
745
it's writable, and can be accessed via the normal filesystem API.
747
# We actually expect this class to be somewhat short-lived; part of its
748
# purpose is to try to isolate what bits of the branch logic are tied to
749
# filesystem access, so that in a later step, we can extricate them to
750
# a separarte ("storage") class.
751
_inventory_weave = None
753
# Map some sort of prefix into a namespace
754
# stuff like "revno:10", "revid:", etc.
755
# This should match a prefix with a function which accepts
756
REVISION_NAMESPACES = {}
830
758
def __init__(self, transport=DEPRECATED_PARAMETER, init=DEPRECATED_PARAMETER,
831
759
relax_version_check=DEPRECATED_PARAMETER, _format=None,
832
760
_control_files=None, a_bzrdir=None, _repository=None):
1057
965
# transaction.register_clean(history, precious=True)
1058
966
return list(history)
1061
968
def update_revisions(self, other, stop_revision=None):
1062
969
"""See Branch.update_revisions."""
970
if stop_revision is None:
971
stop_revision = other.last_revision()
972
### Should this be checking is_ancestor instead of revision_history?
973
if (stop_revision is not None and
974
stop_revision in self.revision_history()):
976
self.fetch(other, stop_revision)
977
pullable_revs = self.pullable_revisions(other, stop_revision)
978
if len(pullable_revs) > 0:
979
self.append_revision(*pullable_revs)
981
def pullable_revisions(self, other, stop_revision):
982
"""See Branch.pullable_revisions."""
983
other_revno = other.revision_id_to_revno(stop_revision)
1065
if stop_revision is None:
1066
stop_revision = other.last_revision()
1067
if stop_revision is None:
1068
# if there are no commits, we're done.
1070
# whats the current last revision, before we fetch [and change it
1072
last_rev = self.last_revision()
1073
# we fetch here regardless of whether we need to so that we pickup
1075
self.fetch(other, stop_revision)
1076
my_ancestry = self.repository.get_ancestry(last_rev)
1077
if stop_revision in my_ancestry:
1078
# last_revision is a descendant of stop_revision
1080
# stop_revision must be a descendant of last_revision
1081
stop_graph = self.repository.get_revision_graph(stop_revision)
1082
if last_rev is not None and last_rev not in stop_graph:
1083
# our previous tip is not merged into stop_revision
1084
raise errors.DivergedBranches(self, other)
1085
# make a new revision history from the graph
1086
current_rev_id = stop_revision
1088
while current_rev_id not in (None, NULL_REVISION):
1089
new_history.append(current_rev_id)
1090
current_rev_id_parents = stop_graph[current_rev_id]
1092
current_rev_id = current_rev_id_parents[0]
1094
current_rev_id = None
1095
new_history.reverse()
1096
self.set_revision_history(new_history)
985
return self.missing_revisions(other, other_revno)
986
except DivergedBranches, e:
988
pullable_revs = get_intervening_revisions(self.last_revision(),
991
assert self.last_revision() not in pullable_revs
993
except bzrlib.errors.NotAncestor:
994
if is_ancestor(self.last_revision(), stop_revision, self):
1100
999
def basis_tree(self):
1101
1000
"""See Branch.basis_tree."""
1102
1001
return self.repository.revision_tree(self.last_revision())
1159
1058
# FIXUP this and get_parent in a future branch format bump:
1160
1059
# read and rewrite the file, and have the new format code read
1161
1060
# using .get not .get_utf8. RBC 20060125
1163
self.control_files._transport.delete('parent')
1165
self.control_files.put_utf8('parent', url + '\n')
1061
self.control_files.put_utf8('parent', url + '\n')
1167
1063
def tree_config(self):
1168
1064
return TreeConfig(self)
1171
class BzrBranch5(BzrBranch):
1172
"""A format 5 branch. This supports new features over plan branches.
1174
It has support for a master_branch which is the data for bound branches.
1182
super(BzrBranch5, self).__init__(_format=_format,
1183
_control_files=_control_files,
1185
_repository=_repository)
1188
def pull(self, source, overwrite=False, stop_revision=None):
1189
"""Updates branch.pull to be bound branch aware."""
1190
bound_location = self.get_bound_location()
1191
if source.base != bound_location:
1192
# not pulling from master, so we need to update master.
1193
master_branch = self.get_master_branch()
1195
master_branch.pull(source)
1196
source = master_branch
1197
return super(BzrBranch5, self).pull(source, overwrite, stop_revision)
1199
def get_bound_location(self):
1066
def _get_truncated_history(self, revision_id):
1067
history = self.revision_history()
1068
if revision_id is None:
1201
return self.control_files.get_utf8('bound').read()[:-1]
1202
except errors.NoSuchFile:
1071
idx = history.index(revision_id)
1073
raise InvalidRevisionId(revision_id=revision, branch=self)
1074
return history[:idx+1]
1205
1076
@needs_read_lock
1206
def get_master_branch(self):
1207
"""Return the branch we are bound to.
1209
:return: Either a Branch, or None
1211
This could memoise the branch, but if thats done
1212
it must be revalidated on each new lock.
1213
So for now we just dont memoise it.
1214
# RBC 20060304 review this decision.
1216
bound_loc = self.get_bound_location()
1220
return Branch.open(bound_loc)
1221
except (errors.NotBranchError, errors.ConnectionError), e:
1222
raise errors.BoundBranchConnectionFailure(
1226
def set_bound_location(self, location):
1227
"""Set the target where this branch is bound to.
1229
:param location: URL to the target branch
1232
self.control_files.put_utf8('bound', location+'\n')
1235
self.control_files._transport.delete('bound')
1241
def bind(self, other):
1242
"""Bind the local branch the other branch.
1244
:param other: The branch to bind to
1247
# TODO: jam 20051230 Consider checking if the target is bound
1248
# It is debatable whether you should be able to bind to
1249
# a branch which is itself bound.
1250
# Committing is obviously forbidden,
1251
# but binding itself may not be.
1252
# Since we *have* to check at commit time, we don't
1253
# *need* to check here
1256
# we are now equal to or a suffix of other.
1258
# Since we have 'pulled' from the remote location,
1259
# now we should try to pull in the opposite direction
1260
# in case the local tree has more revisions than the
1262
# There may be a different check you could do here
1263
# rather than actually trying to install revisions remotely.
1264
# TODO: capture an exception which indicates the remote branch
1266
# If it is up-to-date, this probably should not be a failure
1268
# lock other for write so the revision-history syncing cannot race
1272
# if this does not error, other now has the same last rev we do
1273
# it can only error if the pull from other was concurrent with
1274
# a commit to other from someone else.
1276
# until we ditch revision-history, we need to sync them up:
1277
self.set_revision_history(other.revision_history())
1278
# now other and self are up to date with each other and have the
1279
# same revision-history.
1283
self.set_bound_location(other.base)
1287
"""If bound, unbind"""
1288
return self.set_bound_location(None)
1292
"""Synchronise this branch with the master branch if any.
1294
:return: None or the last_revision that was pivoted out during the
1297
master = self.get_master_branch()
1298
if master is not None:
1299
old_tip = self.last_revision()
1300
self.pull(master, overwrite=True)
1301
if old_tip in self.repository.get_ancestry(self.last_revision()):
1077
def _clone_weave(self, to_location, revision=None, basis_branch=None):
1079
from bzrlib.workingtree import WorkingTree
1080
assert isinstance(to_location, basestring)
1081
if basis_branch is not None:
1082
note("basis_branch is not supported for fast weave copy yet.")
1084
history = self._get_truncated_history(revision)
1085
if not bzrlib.osutils.lexists(to_location):
1086
os.mkdir(to_location)
1087
bzrdir_to = self.bzrdir._format.initialize(to_location)
1088
self.repository.clone(bzrdir_to)
1089
branch_to = bzrdir_to.create_branch()
1090
mutter("copy branch from %s to %s", self, branch_to)
1092
# FIXME duplicate code with base .clone().
1093
# .. would template method be useful here? RBC 20051207
1094
branch_to.set_parent(self.base)
1095
branch_to.append_revision(*history)
1096
WorkingTree.create(branch_to, branch_to.base)
1307
1101
class BranchTestProviderAdapter(object):