14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Make a copy of an entire branch and all its history.
19
This is the underlying function for the branch/get/clone commands."""
17
21
# TODO: This could be done *much* more efficiently by just copying
18
22
# all the whole weaves and revisions, rather than getting one
19
23
# revision at a time.
25
# TODO: Optionally, after copying, discard any irrelevant information from
26
# the destination, such as revisions committed after the last one we're interested
27
# in. This needs to apply a weave prune operation (not written yet) to each
30
# Copying must be done in a way that supports http transports, where we
31
# can't list a directory, and therefore have to rely on information
32
# retrieved from top-level objects whose names we do know.
34
# In practice this means we first fetch the revision history and ancestry.
35
# These give us a list of all the revisions that need to be fetched. We
36
# also get the inventory weave. We then just need to get a list of all
37
# file-ids ever referenced by this tree. (It might be nice to keep a list
38
# of them directly.) This is done by walking over the inventories of all
39
# copied revisions and accumulating a list of file ids.
41
# For local branches it is possible to optimize this considerably in two
42
# ways. One is to hardlink the files (if possible and requested), rather
43
# than copying them. Another is to simply list the directory rather than
44
# walking through the inventories to find out what files are present -- but
45
# there it may be better to just be consistent with remote branches.
24
50
from bzrlib.merge import build_working_dir
25
51
from bzrlib.branch import Branch
26
from bzrlib.trace import mutter
28
# TODO: Optionally, after copying, discard any irrelevant information from
29
# the destination - e.g.
52
from bzrlib.trace import mutter, note
53
from bzrlib.store import copy_all
31
55
def copy_branch(branch_from, to_location, revision=None, basis_branch=None):
32
56
"""Copy branch_from into the existing directory to_location.
58
Returns the newly created branch object.
61
If not None, only revisions up to this point will be copied.
62
The head of the new branch will be that revision. Must be a
65
to_location -- The destination directory; must either exist and be
66
empty, or not exist, in which case it is created.
68
revno -- This revision should be the head of the new branch.
71
A local branch to copy revisions from, related to branch_from.
72
This is used when branching from a remote (slow) branch, and we have
73
a local branch that might contain some relevant revisions.
75
assert isinstance(branch_from, Branch)
76
assert isinstance(to_location, basestring)
77
if basis_branch is not None:
78
note("basis_branch is not supported for fast weave copy yet.")
79
if not os.path.exists(to_location):
81
branch_to = Branch.initialize(to_location)
82
mutter("copy branch from %s to %s", branch_from, branch_to)
83
branch_to.set_root_id(branch_from.get_root_id())
85
revision = branch_from.last_revision()
86
_copy_control_weaves(branch_from, branch_to)
87
_copy_revision_history(branch_from, branch_to)
88
_copy_text_weaves(branch_from, branch_to)
89
_copy_revision_store(branch_from, branch_to)
90
build_working_dir(to_location)
91
branch_to.set_parent(branch_from.base)
96
def _copy_text_weaves(branch_from, branch_to):
97
# TODO: Handle UnlistableStore and fall back to getting a list of
98
# all file-ids and copying them one by one.
99
copy_all(branch_from.weave_store, branch_to.weave_store)
102
def _copy_revision_store(branch_from, branch_to):
103
# TODO: Copy all revisions mentioned in the ancestry of the selected revision
104
copy_all(branch_from.revision_store, branch_to.revision_store)
107
def _copy_revision_history(branch_from, branch_to):
108
ff = branch_from.controlfile('revision-history')
109
tf = branch_to.put_controlfile('revision-history', ff.read())
112
def _copy_control_weaves(branch_from, branch_to):
113
to_control = branch_to.control_weaves
114
from_control = branch_from.control_weaves
115
to_control.copy_multi(from_control, ['inventory', 'ancestry'])
118
def copy_branch_slower(branch_from, to_location, revision=None, basis_branch=None):
119
"""Copy branch_from into the existing directory to_location.
35
122
If not None, only revisions up to this point will be copied.
36
123
The head of the new branch will be that revision. Must be a