~abentley/bzrtools/bzrtools.dev

612 by Aaron Bentley
Update email address
1
# Copyright (C) 2006, 2008 Aaron Bentley
2
# <aaron@aaronbentley.com>
349 by Aaron Bentley
Added cbranch command
3
#
4
#    This program is free software; you can redistribute it and/or modify
5
#    it under the terms of the GNU General Public License as published by
6
#    the Free Software Foundation; either version 2 of the License, or
7
#    (at your option) any later version.
8
#
9
#    This program is distributed in the hope that it will be useful,
10
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
#    GNU General Public License for more details.
13
#
14
#    You should have received a copy of the GNU General Public License
15
#    along with this program; if not, write to the Free Software
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
626 by Aaron Bentley
cbranch creates parent directories as needed
18
from bzrlib import ui
596.1.1 by Aaron Bentley
Update cbranch to accelerate checkouts
19
from bzrlib.bzrdir import BzrDir
349 by Aaron Bentley
Added cbranch command
20
from bzrlib.config import LocationConfig
626 by Aaron Bentley
cbranch creates parent directories as needed
21
from bzrlib.errors import BzrCommandError, NoSuchFile
596.1.1 by Aaron Bentley
Update cbranch to accelerate checkouts
22
from bzrlib.osutils import pathjoin, basename, abspath
626 by Aaron Bentley
cbranch creates parent directories as needed
23
from bzrlib.transport import get_transport
596.1.1 by Aaron Bentley
Update cbranch to accelerate checkouts
24
from bzrlib.workingtree import WorkingTree
25
from bzrlib.urlutils import derive_to_location
349 by Aaron Bentley
Added cbranch command
26
486 by Aaron Bentley
Support deep cbranch hierarcy via appendpath
27
def cbranch(from_location, to_location=None, revision=None,
619 by Aaron Bentley
Add support for hard-link in cbranch
28
            lightweight=False, files_from=None, hardlink=False):
355.1.3 by Aaron Bentley
Fixed cbranch with empty to_location
29
    if to_location is None:
596.1.1 by Aaron Bentley
Update cbranch to accelerate checkouts
30
        to_location = derive_to_location(from_location)
349 by Aaron Bentley
Added cbranch command
31
    config = LocationConfig(abspath(to_location))
486 by Aaron Bentley
Support deep cbranch hierarcy via appendpath
32
    b_loc = config.get_user_option("cbranch_target")
33
    if b_loc is None:
34
        b_root = config.get_user_option("cbranch_root")
35
        if b_root is None:
36
            raise BzrCommandError("Can't find cbranch_target in"
37
                                  " locations.conf")
38
        b_loc = pathjoin(b_root, basename(to_location))
596.1.1 by Aaron Bentley
Update cbranch to accelerate checkouts
39
    accelerator_tree, old_branch = BzrDir.open_tree_or_branch(from_location)
600 by Aaron Bentley
Rename from-files to files-from, to match bzr proper
40
    if files_from is not None:
41
        accelerator_tree = WorkingTree.open(files_from)
596.1.1 by Aaron Bentley
Update cbranch to accelerate checkouts
42
    if revision is None or len(revision) == 0:
43
        revision_id = old_branch.last_revision()
44
    elif len(revision) == 1:
45
        revision_id = revision[0].in_history(old_branch)[1]
46
    else:
47
        raise BzrCommandError('At most one revision may be supplied.')
626 by Aaron Bentley
cbranch creates parent directories as needed
48
    b_transport = get_transport(b_loc)
49
    ensure_base_recursive(b_transport.clone('..'))
596.1.1 by Aaron Bentley
Update cbranch to accelerate checkouts
50
    pb = ui.ui_factory.nested_progress_bar()
51
    try:
52
        pb.update('Creating branch', 0, 2)
53
        new_branch = old_branch.bzrdir.sprout(b_loc, revision_id,
54
            accelerator_tree=accelerator_tree).open_branch()
55
        pb.update('Creating checkout', 1, 2)
56
        new_branch.create_checkout(to_location, lightweight=lightweight,
605.1.1 by Aaron Bentley
Use hardlinks in cbranch. Always
57
                                   accelerator_tree=accelerator_tree,
619 by Aaron Bentley
Add support for hard-link in cbranch
58
                                   hardlink=hardlink)
596.1.1 by Aaron Bentley
Update cbranch to accelerate checkouts
59
    finally:
60
        pb.finished()
626 by Aaron Bentley
cbranch creates parent directories as needed
61
62
63
def ensure_base_recursive(transport):
64
    """Ensure that the transport base and any its parents exist"""
65
    pending_transports = [transport]
66
    while len(pending_transports) > 0:
67
        transport = pending_transports.pop()
68
        try:
69
            transport.ensure_base()
70
        except NoSuchFile, e:
71
            pending_transports.append(transport)
72
            parent = transport.clone('..')
73
            if parent.base == transport.base:
74
                raise e
75
            pending_transports.append(parent)