1
1
from shutil import rmtree
3
from bzrlib.branch import Branch
3
4
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
4
from bzrlib.delta import compare_trees
5
5
from bzrlib.workingtree import WorkingTree
7
from errors import NotCheckout, UncommittedCheckout
7
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
11
def zap(path, remove_branch=False):
12
13
wt = WorkingTree.open(path)
13
14
except (NoWorkingTree, NotBranchError):
14
15
raise NotCheckout(path)
15
16
tree_base = wt.bzrdir.transport.base
16
branch_base = wt.branch.bzrdir.transport.base
18
branch_base = branch.bzrdir.transport.base
17
19
if tree_base == branch_base:
18
20
raise NotCheckout(path)
19
delta = compare_trees(wt.basis_tree(), wt, want_unchanged=False)
21
delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
20
22
if delta.has_changed():
21
23
raise UncommittedCheckout()
25
parent_loc = branch.get_parent()
26
if parent_loc is None:
28
parent = Branch.open(parent_loc)
29
p_ancestry = parent.repository.get_ancestry(parent.last_revision())
30
if branch.last_revision() not in p_ancestry:
31
raise ParentMissingRevisions(branch.get_parent())
34
t = branch.bzrdir.transport
35
while t.base != branch_base:
39
55
BranchReferenceFormat().initialize(checkout, wt.branch)
40
56
return checkout.create_workingtree()
58
def make_checkout2(self):
59
wt = self.make_checkout()
60
wt2 = wt.branch.bzrdir.sprout('source2').open_workingtree()
62
checkout = BzrDirMetaFormat1().initialize('checkout2')
63
BranchReferenceFormat().initialize(checkout, wt2.branch)
64
return checkout.create_workingtree()
42
66
def test_is_checkout(self):
43
67
self.assertRaises(NotCheckout, zap, '.')
44
68
wt = BzrDir.create_standalone_workingtree('.')
49
73
self.assertIs(True, os.path.exists('checkout'))
51
75
self.assertIs(False, os.path.exists('checkout'))
76
self.assertIs(True, os.path.exists('source'))
78
def test_zap_branch(self):
80
base = WorkingTree.open('checkout').branch.base
81
self.assertIs(True, os.path.exists('checkout'))
82
self.assertRaises(NoParent, zap, 'checkout', remove_branch=True)
83
self.assertIs(True, os.path.exists('checkout'))
84
self.assertIs(True, os.path.exists('source'))
85
zap('checkout2', remove_branch=True)
86
self.assertIs(False, os.path.exists('checkout2'))
87
self.assertIs(False, os.path.exists('source2'))
53
89
def test_checks_modified(self):
54
90
checkout = self.make_checkout()