1
# Copyright (C) 2006 by Szilveszter Farkas (Phanatic)
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
22
import bzrlib.errors as errors
24
from errors import (AlreadyBranchError, BranchExistsWithoutWorkingTree,
25
NonExistingParent, NonExistingRevision, NonExistingSource,
26
RevisionValueError, TargetAlreadyExists)
29
from bzrlib.builtins import get_format_type
30
import bzrlib.bzrdir as bzrdir
32
format = get_format_type('default')
34
if not os.path.exists(location):
38
existing_bzrdir = bzrdir.BzrDir.open(location)
39
except errors.NotBranchError:
40
bzrdir.BzrDir.create_branch_convenience(location, format=format)
42
if existing_bzrdir.has_branch():
43
if existing_bzrdir.has_workingtree():
44
raise AlreadyBranchError(location)
46
raise BranchExistsWithoutWorkingTree(location)
48
existing_bzrdir.create_branch()
49
existing_bzrdir.create_workingtree()
51
def branch(from_location, to_location, revision=None):
52
from bzrlib.branch import Branch
53
from bzrlib.transport import get_transport
57
elif len(revision) > 1:
58
raise RevisionValueError('bzr branch --revision takes exactly 1 revision value')
61
br_from = Branch.open(from_location)
63
if e.errno == errno.ENOENT:
64
raise NonExistingSource(from_location)
72
if len(revision) == 1 and revision[0] is not None:
73
revision_id = revision[0].in_history(br_from)[1]
75
revision_id = br_from.last_revision()
77
to_location = to_location + '/' + os.path.basename(from_location.rstrip("/\\"))
79
to_transport = get_transport(to_location)
82
to_transport.mkdir('.')
83
except errors.FileExists:
84
raise TargetAlreadyExists(to_location)
85
except errors.NoSuchFile:
86
raise NonExistingParent(to_location)
89
dir = br_from.bzrdir.sprout(to_transport.base, revision_id, basis_dir)
90
branch = dir.open_branch()
91
except errors.NoSuchRevision:
92
to_transport.delete_tree('.')
93
raise NonExistingRevision(from_location, revision[0])
96
branch.control_files.put_utf8('branch-name', name)
100
return branch.revno()
103
""" FIXME - will be implemented later """