1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic) <szilveszter.farkas@gmail.com>
 
 
2
# Some parts of the code are:
 
 
3
# Copyright (C) 2005, 2006 by Canonical Ltd
 
 
5
# This program is free software; you can redistribute it and/or modify
 
 
6
# it under the terms of the GNU General Public License as published by
 
 
7
# the Free Software Foundation; either version 2 of the License, or
 
 
8
# (at your option) any later version.
 
 
10
# This program is distributed in the hope that it will be useful,
 
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
13
# GNU General Public License for more details.
 
 
15
# You should have received a copy of the GNU General Public License
 
 
16
# along with this program; if not, write to the Free Software
 
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
23
import bzrlib.bzrdir as bzrdir
 
 
24
import bzrlib.errors as errors
 
 
26
from bzrlib.branch import Branch
 
 
28
from errors import (AlreadyBranchError, BranchExistsWithoutWorkingTree,
 
 
29
                    NonExistingParent, NonExistingRevision,
 
 
30
                    NonExistingSource, NotBranchError, TargetAlreadyExists)
 
 
32
def branch(from_location, to_location, revision=None):
 
 
33
    """ Create a branch from a local/remote location.
 
 
35
    :param from_location: the original location of the branch
 
 
37
    :param to_location: the directory where the branch should be created
 
 
39
    :param revision: if specified, the given revision will be branched
 
 
41
    :return: number of revisions branched
 
 
43
    from bzrlib.transport import get_transport
 
 
46
        br_from = Branch.open(from_location)
 
 
48
        if e.errno == errno.ENOENT:
 
 
49
            raise NonExistingSource(from_location)
 
 
52
    except errors.NotBranchError:
 
 
53
        raise NotBranchError(from_location)
 
 
59
        if revision is not None:
 
 
60
            revision_id = br_from.get_rev_id(revision)
 
 
64
        to_location = to_location + '/' + os.path.basename(from_location.rstrip("/\\"))
 
 
66
        to_transport = get_transport(to_location)
 
 
69
            to_transport.mkdir('.')
 
 
70
        except errors.FileExists:
 
 
71
            raise TargetAlreadyExists(to_location)
 
 
72
        except errors.NoSuchFile:
 
 
73
            raise NonExistingParent(to_location)
 
 
76
            dir = br_from.bzrdir.sprout(to_transport.base, revision_id, basis_dir)
 
 
77
            branch = dir.open_branch()
 
 
78
        except errors.NoSuchRevision:
 
 
79
            to_transport.delete_tree('.')
 
 
80
            raise NonExistingRevision(from_location, revision[0])
 
 
83
            branch.control_files.put_utf8('branch-name', name)
 
 
89
def checkout(branch_location, to_location, revision=None, lightweight=False):
 
 
90
    """ Create a new checkout of an existing branch.
 
 
92
    :param branch_location: the location of the branch you'd like to checkout
 
 
94
    :param to_location: the directory where the checkout should be created
 
 
96
    :param revision: the given revision will be checkout'ed (be aware!)
 
 
98
    :param lightweight: perform a lightweight checkout (be aware!)
 
 
101
        source = Branch.open(branch_location)
 
 
102
    except errors.NotBranchError:
 
 
103
        raise NotBranchError(branch_location)
 
 
105
    if revision is not None:
 
 
106
        revision_id = source.get_rev_id(revision)
 
 
110
    # if the source and to_location are the same, 
 
 
111
    # and there is no working tree,
 
 
112
    # then reconstitute a branch
 
 
113
    if (bzrlib.osutils.abspath(to_location) ==
 
 
114
        bzrlib.osutils.abspath(branch_location)):
 
 
116
            source.bzrdir.open_workingtree()
 
 
117
        except errors.NoWorkingTree:
 
 
118
            source.bzrdir.create_workingtree()
 
 
121
    to_location = to_location + '/' + os.path.basename(branch_location.rstrip("/\\"))
 
 
124
        os.mkdir(to_location)
 
 
126
        if e.errno == errno.EEXIST:
 
 
127
            raise TargetAlreadyExists(to_location)
 
 
128
        if e.errno == errno.ENOENT:
 
 
129
            raise NonExistingParent(to_location)
 
 
133
    old_format = bzrlib.bzrdir.BzrDirFormat.get_default_format()
 
 
134
    bzrlib.bzrdir.BzrDirFormat.set_default_format(bzrdir.BzrDirMetaFormat1())
 
 
138
            checkout = bzrdir.BzrDirMetaFormat1().initialize(to_location)
 
 
139
            bzrlib.branch.BranchReferenceFormat().initialize(checkout, source)
 
 
141
            checkout_branch = bzrlib.bzrdir.BzrDir.create_branch_convenience(
 
 
142
                to_location, force_new_tree=False)
 
 
143
            checkout = checkout_branch.bzrdir
 
 
144
            checkout_branch.bind(source)
 
 
145
            if revision_id is not None:
 
 
146
                rh = checkout_branch.revision_history()
 
 
147
                checkout_branch.set_revision_history(rh[:rh.index(revision_id) + 1])
 
 
149
        checkout.create_workingtree(revision_id)
 
 
151
        bzrlib.bzrdir.BzrDirFormat.set_default_format(old_format)
 
 
154
    """ Initialize a directory.
 
 
156
    :param location: full path to the directory you want to be versioned
 
 
158
    from bzrlib.builtins import get_format_type
 
 
160
    format = get_format_type('default')
 
 
162
    if not os.path.exists(location):
 
 
166
        existing_bzrdir = bzrdir.BzrDir.open(location)
 
 
167
    except errors.NotBranchError:
 
 
168
        bzrdir.BzrDir.create_branch_convenience(location, format=format)
 
 
170
        if existing_bzrdir.has_branch():
 
 
171
            if existing_bzrdir.has_workingtree():
 
 
172
                raise AlreadyBranchError(location)
 
 
174
                raise BranchExistsWithoutWorkingTree(location)
 
 
176
            existing_bzrdir.create_branch()
 
 
177
            existing_bzrdir.create_workingtree()