1
1
from shutil import rmtree
5
revision as _mod_revision,
7
from bzrlib.branch import Branch
3
8
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
4
from bzrlib.delta import compare_trees
5
9
from bzrlib.workingtree import WorkingTree
7
from errors import NotCheckout, UncommittedCheckout
11
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
15
def zap(path, remove_branch=False, allow_modified=False):
12
wt = WorkingTree.open(path)
17
wt = bzrdir.BzrDir.open(path).open_workingtree(path,
18
recommend_upgrade=False)
13
19
except (NoWorkingTree, NotBranchError):
14
20
raise NotCheckout(path)
15
21
tree_base = wt.bzrdir.transport.base
16
branch_base = wt.branch.bzrdir.transport.base
23
branch_base = branch.bzrdir.transport.base
17
24
if tree_base == branch_base:
18
25
raise NotCheckout(path)
19
delta = compare_trees(wt.basis_tree(), wt, want_unchanged=False)
20
if delta.has_changed():
21
raise UncommittedCheckout()
26
if not allow_modified:
27
delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
28
if delta.has_changed():
29
raise UncommittedCheckout()
31
parent_loc = branch.get_parent()
32
if parent_loc is None:
34
parent = Branch.open(parent_loc)
35
last_revision = _mod_revision.ensure_null(parent.last_revision())
36
p_ancestry = parent.repository.get_ancestry(last_revision)
37
if (last_revision != _mod_revision.NULL_REVISION and
38
branch.last_revision() not in p_ancestry):
39
raise ParentMissingRevisions(branch.get_parent())
42
t = branch.bzrdir.transport
43
while t.base != branch_base:
27
51
from unittest import makeSuite
29
53
from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
30
54
from bzrlib.branch import BranchReferenceFormat
31
55
from bzrlib.tests import TestCaseInTempDir
39
63
BranchReferenceFormat().initialize(checkout, wt.branch)
40
64
return checkout.create_workingtree()
66
def make_checkout2(self):
67
wt = self.make_checkout()
68
wt2 = wt.branch.bzrdir.sprout('source2').open_workingtree()
70
checkout = BzrDirMetaFormat1().initialize('checkout2')
71
BranchReferenceFormat().initialize(checkout, wt2.branch)
72
return checkout.create_workingtree()
42
74
def test_is_checkout(self):
43
75
self.assertRaises(NotCheckout, zap, '.')
44
76
wt = BzrDir.create_standalone_workingtree('.')
49
81
self.assertIs(True, os.path.exists('checkout'))
51
83
self.assertIs(False, os.path.exists('checkout'))
84
self.assertIs(True, os.path.exists('source'))
86
def test_zap_branch(self):
88
base = WorkingTree.open('checkout').branch.base
89
self.assertIs(True, os.path.exists('checkout'))
90
self.assertRaises(NoParent, zap, 'checkout', remove_branch=True)
91
self.assertIs(True, os.path.exists('checkout'))
92
self.assertIs(True, os.path.exists('source'))
93
zap('checkout2', remove_branch=True)
94
self.assertIs(False, os.path.exists('checkout2'))
95
self.assertIs(False, os.path.exists('source2'))
53
97
def test_checks_modified(self):
54
98
checkout = self.make_checkout()
58
102
checkout.commit('commit changes to branch')
105
def test_allow_modified(self):
106
checkout = self.make_checkout()
107
os.mkdir('checkout/foo')
109
self.assertRaises(UncommittedCheckout, zap, 'checkout')
110
zap('checkout', allow_modified=True)
61
112
return makeSuite(TestZap)