51
50
from bzrlib.merge import build_working_dir
52
51
from bzrlib.branch import Branch
53
52
from bzrlib.trace import mutter, note
54
53
from bzrlib.store import copy_all
55
from bzrlib.errors import InvalidRevisionId
57
55
def copy_branch(branch_from, to_location, revision=None, basis_branch=None):
58
56
"""Copy branch_from into the existing directory to_location.
76
74
assert isinstance(to_location, basestring)
77
75
if basis_branch is not None:
78
76
note("basis_branch is not supported for fast weave copy yet.")
79
branch_from.lock_read()
81
if not (branch_from.weave_store.listable()
82
and branch_from.revision_store.listable()):
83
return copy_branch_slower(branch_from, to_location, revision,
85
history = _get_truncated_history(branch_from, revision)
86
if not bzrlib.osutils.lexists(to_location):
88
branch_to = Branch.initialize(to_location)
89
mutter("copy branch from %s to %s", branch_from, branch_to)
90
branch_to.set_root_id(branch_from.get_root_id())
91
branch_to.append_revision(*history)
92
_copy_control_weaves(branch_from, branch_to)
93
_copy_text_weaves(branch_from, branch_to)
94
_copy_revision_store(branch_from, branch_to)
95
build_working_dir(to_location)
96
branch_to.set_parent(branch_from.base)
77
history = _get_truncated_history(branch_from, revision)
78
if not os.path.exists(to_location):
80
branch_to = Branch.initialize(to_location)
81
mutter("copy branch from %s to %s", branch_from, branch_to)
82
branch_to.set_root_id(branch_from.get_root_id())
83
branch_to.append_revision(*history)
84
_copy_control_weaves(branch_from, branch_to)
85
_copy_text_weaves(branch_from, branch_to)
86
_copy_revision_store(branch_from, branch_to)
87
build_working_dir(to_location)
88
branch_to.set_parent(branch_from.base)
103
94
def _get_truncated_history(branch_from, revision):
108
99
idx = history.index(revision)
109
100
except ValueError:
110
raise InvalidRevisionId(revision_id=revision, branch=branch_from)
101
raise InvalidRevisionId('revision {%s} is not on the mainline of %s'
102
% (revision, branch_from))
111
103
return history[:idx+1]
113
105
def _copy_text_weaves(branch_from, branch_to):
106
# TODO: Handle UnlistableStore and fall back to getting a list of
107
# all file-ids and copying them one by one.
114
108
copy_all(branch_from.weave_store, branch_to.weave_store)
117
111
def _copy_revision_store(branch_from, branch_to):
112
# TODO: Copy all revisions mentioned in the ancestry of the selected revision
118
113
copy_all(branch_from.revision_store, branch_to.revision_store)
121
116
def _copy_control_weaves(branch_from, branch_to):
122
117
to_control = branch_to.control_weaves
123
118
from_control = branch_from.control_weaves
124
to_control.copy_multi(from_control, ['inventory'])
119
to_control.copy_multi(from_control, ['inventory', 'ancestry'])
127
122
def copy_branch_slower(branch_from, to_location, revision=None, basis_branch=None):
146
141
assert isinstance(branch_from, Branch)
147
142
assert isinstance(to_location, basestring)
148
if not bzrlib.osutils.lexists(to_location):
143
if not os.path.exists(to_location):
149
144
os.mkdir(to_location)
150
145
br_to = Branch.initialize(to_location)
151
146
mutter("copy branch from %s to %s", branch_from, br_to)