1
1
from shutil import rmtree
5
revision as _mod_revision,
3
7
from bzrlib.branch import Branch
4
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
5
from bzrlib.delta import compare_trees
8
from bzrlib.errors import NoWorkingTree, NotBranchError
6
9
from bzrlib.workingtree import WorkingTree
8
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
11
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
12
def zap(path, remove_branch=False):
15
def zap(path, remove_branch=False, allow_modified=False):
14
wt = WorkingTree.open(path)
17
wt = bzrdir.BzrDir.open(path).open_workingtree(path,
18
recommend_upgrade=False)
15
19
except (NoWorkingTree, NotBranchError):
16
20
raise NotCheckout(path)
17
21
tree_base = wt.bzrdir.transport.base
19
23
branch_base = branch.bzrdir.transport.base
20
24
if tree_base == branch_base:
21
25
raise NotCheckout(path)
22
delta = compare_trees(wt.basis_tree(), wt, want_unchanged=False)
23
if delta.has_changed():
24
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()
26
31
parent_loc = branch.get_parent()
27
32
if parent_loc is None:
29
34
parent = Branch.open(parent_loc)
30
p_ancestry = parent.repository.get_ancestry(parent.last_revision())
31
if branch.last_revision() not in p_ancestry:
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):
32
39
raise ParentMissingRevisions(branch.get_parent())
44
51
from unittest import makeSuite
46
from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
47
from bzrlib.branch import BranchReferenceFormat
48
53
from bzrlib.tests import TestCaseInTempDir
50
55
class TestZap(TestCaseInTempDir):
52
57
def make_checkout(self):
53
wt = BzrDir.create_standalone_workingtree('source')
55
checkout = BzrDirMetaFormat1().initialize('checkout')
56
BranchReferenceFormat().initialize(checkout, wt.branch)
57
return checkout.create_workingtree()
58
wt = bzrdir.BzrDir.create_standalone_workingtree('source')
59
return wt.branch.create_checkout('checkout', lightweight=True)
59
61
def make_checkout2(self):
60
62
wt = self.make_checkout()
61
63
wt2 = wt.branch.bzrdir.sprout('source2').open_workingtree()
63
checkout = BzrDirMetaFormat1().initialize('checkout2')
64
BranchReferenceFormat().initialize(checkout, wt2.branch)
65
return checkout.create_workingtree()
64
return wt2.branch.create_checkout('checkout2', lightweight=True)
67
66
def test_is_checkout(self):
68
67
self.assertRaises(NotCheckout, zap, '.')
69
wt = BzrDir.create_standalone_workingtree('.')
68
wt = bzrdir.BzrDir.create_standalone_workingtree('.')
70
69
self.assertRaises(NotCheckout, zap, '.')
72
71
def test_zap_works(self):
76
75
self.assertIs(False, os.path.exists('checkout'))
77
76
self.assertIs(True, os.path.exists('source'))
79
def test_zap_branch(self):
78
def test_zap_branch(self):
80
79
self.make_checkout2()
81
80
base = WorkingTree.open('checkout').branch.base
82
81
self.assertIs(True, os.path.exists('checkout'))
95
94
checkout.commit('commit changes to branch')
97
def test_allow_modified(self):
98
checkout = self.make_checkout()
99
os.mkdir('checkout/foo')
101
self.assertRaises(UncommittedCheckout, zap, 'checkout')
102
zap('checkout', allow_modified=True)
98
104
return makeSuite(TestZap)