52
52
from bzrlib.branch import Branch
53
53
from bzrlib.trace import mutter, note
54
54
from bzrlib.store import copy_all
55
from bzrlib.errors import InvalidRevisionId
56
57
def copy_branch(branch_from, to_location, revision=None, basis_branch=None):
57
58
"""Copy branch_from into the existing directory to_location.
75
76
assert isinstance(to_location, basestring)
76
77
if basis_branch is not None:
77
78
note("basis_branch is not supported for fast weave copy yet.")
78
if not (branch_from.weave_store.listable()
79
and branch_from.revision_store.listable()):
80
return copy_branch_slower(branch_from, to_location, revision,
82
history = _get_truncated_history(branch_from, revision)
83
if not bzrlib.osutils.lexists(to_location):
85
branch_to = Branch.initialize(to_location)
86
mutter("copy branch from %s to %s", branch_from, branch_to)
87
branch_to.set_root_id(branch_from.get_root_id())
88
branch_to.append_revision(*history)
89
_copy_control_weaves(branch_from, branch_to)
90
_copy_text_weaves(branch_from, branch_to)
91
_copy_revision_store(branch_from, branch_to)
92
build_working_dir(to_location)
93
branch_to.set_parent(branch_from.base)
98
def _get_truncated_history(branch_from, revision):
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.working_tree().set_root_id(branch_from.get_root_id())
91
_copy_control_weaves(branch_from, branch_to)
92
_copy_text_weaves(branch_from, branch_to)
93
_copy_revision_store(branch_from, branch_to)
94
branch_to.set_parent(branch_from.base)
95
# must be done *after* history is copied across
96
branch_to.append_revision(*history)
97
build_working_dir(to_location)
104
def _get_truncated_history(branch_from, revision_id):
99
105
history = branch_from.revision_history()
106
if revision_id is None:
103
idx = history.index(revision)
109
idx = history.index(revision_id)
104
110
except ValueError:
105
raise InvalidRevisionId('revision {%s} is not on the mainline of %s'
106
% (revision, branch_from))
111
raise InvalidRevisionId(revision_id=revision, branch=branch_from)
107
112
return history[:idx+1]
109
114
def _copy_text_weaves(branch_from, branch_to):
147
152
mutter("copy branch from %s to %s", branch_from, br_to)
148
153
if basis_branch is not None:
149
154
basis_branch.push_stores(br_to)
150
br_to.set_root_id(branch_from.get_root_id())
155
br_to.working_tree().set_root_id(branch_from.get_root_id())
151
156
if revision is None:
152
157
revision = branch_from.last_revision()
153
158
br_to.update_revisions(branch_from, stop_revision=revision)