~bzr-pqm/bzr/bzr.dev

1393.1.5 by Martin Pool
- move copy_branch into bzrlib.clone
1
# Copyright (C) 2004, 2005 by Canonical Ltd
2
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.
7
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.
12
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
# TODO: This could be done *much* more efficiently by just copying
18
# all the whole weaves and revisions, rather than getting one
19
# revision at a time.
20
1393.1.11 by Martin Pool
- copy_branch creates destination if it doesn't exist
21
import os
1393.1.5 by Martin Pool
- move copy_branch into bzrlib.clone
22
import sys
23
1393.1.10 by Martin Pool
- factor out stereotyped use of merge to build working dir
24
from bzrlib.merge import build_working_dir
1393.1.5 by Martin Pool
- move copy_branch into bzrlib.clone
25
from bzrlib.branch import Branch
26
from bzrlib.trace import mutter
27
1393.1.12 by Martin Pool
- merge Transport from John into newformat
28
# TODO: Optionally, after copying, discard any irrelevant information from
29
# the destination - e.g. 
1393.1.5 by Martin Pool
- move copy_branch into bzrlib.clone
30
31
def copy_branch(branch_from, to_location, revision=None, basis_branch=None):
32
    """Copy branch_from into the existing directory to_location.
33
34
    revision
35
        If not None, only revisions up to this point will be copied.
36
        The head of the new branch will be that revision.  Must be a
37
        revid or None.
38
1393.1.11 by Martin Pool
- copy_branch creates destination if it doesn't exist
39
    to_location -- The destination directory; must either exist and be 
40
        empty, or not exist, in which case it is created.
1393.1.5 by Martin Pool
- move copy_branch into bzrlib.clone
41
42
    revno
43
        The revision to copy up to
44
45
    basis_branch
1393.1.12 by Martin Pool
- merge Transport from John into newformat
46
        A local branch to copy revisions from, related to branch_from. 
47
        This is used when branching from a remote (slow) branch, and we have
48
        a local branch that might contain some relevant revisions.
1393.1.5 by Martin Pool
- move copy_branch into bzrlib.clone
49
    """
50
    assert isinstance(branch_from, Branch)
51
    assert isinstance(to_location, basestring)
1393.1.11 by Martin Pool
- copy_branch creates destination if it doesn't exist
52
    if not os.path.exists(to_location):
53
        os.mkdir(to_location)
1393.1.5 by Martin Pool
- move copy_branch into bzrlib.clone
54
    br_to = Branch.initialize(to_location)
55
    mutter("copy branch from %s to %s", branch_from, br_to)
56
    if basis_branch is not None:
57
        basis_branch.push_stores(br_to)
58
    br_to.set_root_id(branch_from.get_root_id())
59
    if revision is None:
60
        revision = branch_from.last_revision()
61
    br_to.update_revisions(branch_from, stop_revision=revision)
1393.1.10 by Martin Pool
- factor out stereotyped use of merge to build working dir
62
    build_working_dir(to_location)
1393.1.5 by Martin Pool
- move copy_branch into bzrlib.clone
63
    br_to.set_parent(branch_from.base)
64
    mutter("copied")
65
    return br_to