1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
from shutil import rmtree
from bzrlib import (
bzrdir,
revision as _mod_revision,
)
from bzrlib.branch import Branch
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
from bzrlib.workingtree import WorkingTree
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
NoParent)
def zap(path, remove_branch=False):
try:
wt = bzrdir.BzrDir.open(path).open_workingtree(path,
recommend_upgrade=False)
except (NoWorkingTree, NotBranchError):
raise NotCheckout(path)
tree_base = wt.bzrdir.transport.base
branch = wt.branch
branch_base = branch.bzrdir.transport.base
if tree_base == branch_base:
raise NotCheckout(path)
delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
if delta.has_changed():
raise UncommittedCheckout()
if remove_branch:
parent_loc = branch.get_parent()
if parent_loc is None:
raise NoParent()
parent = Branch.open(parent_loc)
last_revision = _mod_revision.ensure_null(parent.last_revision())
p_ancestry = parent.repository.get_ancestry(last_revision)
if (last_revision != _mod_revision.NULL_REVISION and
branch.last_revision() not in p_ancestry):
raise ParentMissingRevisions(branch.get_parent())
rmtree(path)
if remove_branch:
t = branch.bzrdir.transport
while t.base != branch_base:
t = t.clone('..')
t = t.clone('..')
t.delete_tree('')
def test_suite():
import os
from unittest import makeSuite
from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
from bzrlib.branch import BranchReferenceFormat
from bzrlib.tests import TestCaseInTempDir
class TestZap(TestCaseInTempDir):
def make_checkout(self):
wt = BzrDir.create_standalone_workingtree('source')
os.mkdir('checkout')
checkout = BzrDirMetaFormat1().initialize('checkout')
BranchReferenceFormat().initialize(checkout, wt.branch)
return checkout.create_workingtree()
def make_checkout2(self):
wt = self.make_checkout()
wt2 = wt.branch.bzrdir.sprout('source2').open_workingtree()
os.mkdir('checkout2')
checkout = BzrDirMetaFormat1().initialize('checkout2')
BranchReferenceFormat().initialize(checkout, wt2.branch)
return checkout.create_workingtree()
def test_is_checkout(self):
self.assertRaises(NotCheckout, zap, '.')
wt = BzrDir.create_standalone_workingtree('.')
self.assertRaises(NotCheckout, zap, '.')
def test_zap_works(self):
self.make_checkout()
self.assertIs(True, os.path.exists('checkout'))
zap('checkout')
self.assertIs(False, os.path.exists('checkout'))
self.assertIs(True, os.path.exists('source'))
def test_zap_branch(self):
self.make_checkout2()
base = WorkingTree.open('checkout').branch.base
self.assertIs(True, os.path.exists('checkout'))
self.assertRaises(NoParent, zap, 'checkout', remove_branch=True)
self.assertIs(True, os.path.exists('checkout'))
self.assertIs(True, os.path.exists('source'))
zap('checkout2', remove_branch=True)
self.assertIs(False, os.path.exists('checkout2'))
self.assertIs(False, os.path.exists('source2'))
def test_checks_modified(self):
checkout = self.make_checkout()
os.mkdir('checkout/foo')
checkout.add('foo')
self.assertRaises(UncommittedCheckout, zap, 'checkout')
checkout.commit('commit changes to branch')
zap('checkout')
return makeSuite(TestZap)
|