1
from shutil import rmtree
5
revision as _mod_revision,
7
from bzrlib.branch import Branch
8
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
9
from bzrlib.workingtree import WorkingTree
11
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
15
def zap(path, remove_branch=False):
17
wt = bzrdir.BzrDir.open(path).open_workingtree(path,
18
recommend_upgrade=False)
19
except (NoWorkingTree, NotBranchError):
20
raise NotCheckout(path)
21
tree_base = wt.bzrdir.transport.base
23
branch_base = branch.bzrdir.transport.base
24
if tree_base == branch_base:
25
raise NotCheckout(path)
26
delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
27
if delta.has_changed():
28
raise UncommittedCheckout()
30
parent_loc = branch.get_parent()
31
if parent_loc is None:
33
parent = Branch.open(parent_loc)
34
last_revision = _mod_revision.ensure_null(parent.last_revision())
35
p_ancestry = parent.repository.get_ancestry(last_revision)
36
if (last_revision != _mod_revision.NULL_REVISION and
37
branch.last_revision() not in p_ancestry):
38
raise ParentMissingRevisions(branch.get_parent())
41
t = branch.bzrdir.transport
42
while t.base != branch_base:
50
from unittest import makeSuite
52
from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
53
from bzrlib.branch import BranchReferenceFormat
54
from bzrlib.tests import TestCaseInTempDir
56
class TestZap(TestCaseInTempDir):
58
def make_checkout(self):
59
wt = BzrDir.create_standalone_workingtree('source')
61
checkout = BzrDirMetaFormat1().initialize('checkout')
62
BranchReferenceFormat().initialize(checkout, wt.branch)
63
return checkout.create_workingtree()
65
def make_checkout2(self):
66
wt = self.make_checkout()
67
wt2 = wt.branch.bzrdir.sprout('source2').open_workingtree()
69
checkout = BzrDirMetaFormat1().initialize('checkout2')
70
BranchReferenceFormat().initialize(checkout, wt2.branch)
71
return checkout.create_workingtree()
73
def test_is_checkout(self):
74
self.assertRaises(NotCheckout, zap, '.')
75
wt = BzrDir.create_standalone_workingtree('.')
76
self.assertRaises(NotCheckout, zap, '.')
78
def test_zap_works(self):
80
self.assertIs(True, os.path.exists('checkout'))
82
self.assertIs(False, os.path.exists('checkout'))
83
self.assertIs(True, os.path.exists('source'))
85
def test_zap_branch(self):
87
base = WorkingTree.open('checkout').branch.base
88
self.assertIs(True, os.path.exists('checkout'))
89
self.assertRaises(NoParent, zap, 'checkout', remove_branch=True)
90
self.assertIs(True, os.path.exists('checkout'))
91
self.assertIs(True, os.path.exists('source'))
92
zap('checkout2', remove_branch=True)
93
self.assertIs(False, os.path.exists('checkout2'))
94
self.assertIs(False, os.path.exists('source2'))
96
def test_checks_modified(self):
97
checkout = self.make_checkout()
98
os.mkdir('checkout/foo')
100
self.assertRaises(UncommittedCheckout, zap, 'checkout')
101
checkout.commit('commit changes to branch')
104
return makeSuite(TestZap)