~ubuntu-branches/debian/lenny/bzrtools/lenny

« back to all changes in this revision

Viewing changes to cbranch.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2008-05-12 20:25:35 UTC
  • mfrom: (1.2.2 upstream) (4.1.34 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080512202535-y92gjpczwr98htxe
Tags: 1.5.0-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Aaron Bentley
2
 
# <aaron.bentley@utoronto.ca>
 
1
# Copyright (C) 2006, 2008 Aaron Bentley
 
2
# <aaron@aaronbentley.com>
3
3
#
4
4
#    This program is free software; you can redistribute it and/or modify
5
5
#    it under the terms of the GNU General Public License as published by
15
15
#    along with this program; if not, write to the Free Software
16
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
 
18
from bzrlib import ui
 
19
from bzrlib.bzrdir import BzrDir
18
20
from bzrlib.branch import Branch
19
 
from bzrlib.builtins import cmd_branch, cmd_checkout
20
21
from bzrlib.config import LocationConfig
21
 
from bzrlib.errors import BzrCommandError
22
 
from bzrlib.osutils import pathjoin, basename, abspath, getcwd
23
 
from bzrlib.urlutils import local_path_to_url
 
22
from bzrlib.errors import BzrCommandError, NoSuchFile
 
23
from bzrlib.osutils import pathjoin, basename, abspath
 
24
from bzrlib.transport import get_transport
 
25
from bzrlib.workingtree import WorkingTree
 
26
from bzrlib.urlutils import derive_to_location
24
27
 
25
 
def cbranch(from_location, to_location=None, revision=None, lightweight=False):
26
 
    from_location = from_location.rstrip('/')
 
28
def cbranch(from_location, to_location=None, revision=None,
 
29
            lightweight=False, files_from=None, hardlink=False):
27
30
    if to_location is None:
28
 
        to_location = pathjoin(getcwd(), basename(from_location))
 
31
        to_location = derive_to_location(from_location)
29
32
    config = LocationConfig(abspath(to_location))
30
 
    b_root = config.get_user_option("cbranch_root")
31
 
    if b_root is None:
32
 
        raise BzrCommandError("Can't find cbranch_root in branches.conf")
33
 
    b_loc = pathjoin(b_root, basename(to_location))
34
 
    cmd_branch().run(from_location, b_loc, revision=revision)
35
 
    cmd_checkout().run(b_loc, to_location, lightweight=lightweight)
36
 
    b = Branch.open(to_location)
37
 
    b.set_parent(local_path_to_url(from_location))
 
33
    b_loc = config.get_user_option("cbranch_target")
 
34
    if b_loc is None:
 
35
        b_root = config.get_user_option("cbranch_root")
 
36
        if b_root is None:
 
37
            raise BzrCommandError("Can't find cbranch_target in"
 
38
                                  " locations.conf")
 
39
        b_loc = pathjoin(b_root, basename(to_location))
 
40
    accelerator_tree, old_branch = BzrDir.open_tree_or_branch(from_location)
 
41
    if files_from is not None:
 
42
        accelerator_tree = WorkingTree.open(files_from)
 
43
    if revision is None or len(revision) == 0:
 
44
        revision_id = old_branch.last_revision()
 
45
    elif len(revision) == 1:
 
46
        revision_id = revision[0].in_history(old_branch)[1]
 
47
    else:
 
48
        raise BzrCommandError('At most one revision may be supplied.')
 
49
    b_transport = get_transport(b_loc)
 
50
    ensure_base_recursive(b_transport.clone('..'))
 
51
    pb = ui.ui_factory.nested_progress_bar()
 
52
    try:
 
53
        pb.update('Creating branch', 0, 2)
 
54
        new_branch = old_branch.bzrdir.sprout(b_loc, revision_id,
 
55
            accelerator_tree=accelerator_tree).open_branch()
 
56
        pb.update('Creating checkout', 1, 2)
 
57
        new_branch.create_checkout(to_location, lightweight=lightweight,
 
58
                                   accelerator_tree=accelerator_tree,
 
59
                                   hardlink=hardlink)
 
60
    finally:
 
61
        pb.finished()
 
62
 
 
63
 
 
64
def ensure_base_recursive(transport):
 
65
    """Ensure that the transport base and any its parents exist"""
 
66
    pending_transports = [transport]
 
67
    while len(pending_transports) > 0:
 
68
        transport = pending_transports.pop()
 
69
        try:
 
70
            transport.ensure_base()
 
71
        except NoSuchFile, e:
 
72
            pending_transports.append(transport)
 
73
            parent = transport.clone('..')
 
74
            if parent.base == transport.base:
 
75
                raise e
 
76
            pending_transports.append(parent)