1
# Copyright (C) 2006-2007, 2010-2011 Aaron Bentley <aaron@aaronbentley.com>
2
# Copyright (C) 2007 Charlie Shepherd <masterdriverz@gentoo.org>
3
# Copyright (C) 2011 Canonical Ltd.
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1
19
from shutil import rmtree
23
revision as _mod_revision,
3
25
from bzrlib.branch import Branch
4
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
5
from bzrlib.delta import compare_trees
26
from bzrlib.errors import BzrCommandError, NoWorkingTree, NotBranchError
27
from bzrlib import registry
6
28
from bzrlib.workingtree import WorkingTree
8
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
30
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
12
def zap(path, remove_branch=False):
34
class AllowChanged(object):
37
def check_changed(klass, wt, remove_branch):
41
class CheckChanged(object):
44
def check_changed(klass, wt, remove_branch):
45
delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
46
if delta.has_changed():
47
klass.handle_changed(wt, remove_branch)
50
class HaltOnChange(CheckChanged):
53
def handle_changed(wt, remove_branch):
54
raise UncommittedCheckout()
57
class StoreChanges(CheckChanged):
60
def handle_changed(wt, remove_branch):
61
from bzrlib.plugins.pipeline.pipeline import PipeManager
63
raise BzrCommandError('Cannot store changes in deleted branch.')
64
PipeManager.from_checkout(wt).store_uncommitted()
67
change_policy_registry = registry.Registry()
70
change_policy_registry.register('force', AllowChanged,
71
'Delete tree even if contents are modified.')
74
change_policy_registry.register('store', StoreChanges,
75
'Store changes in branch. (Requires'
79
change_policy_registry.register('check', HaltOnChange,
80
'Stop if tree contents are modified.')
83
change_policy_registry.default_key = 'check'
86
def zap(path, remove_branch=False, policy=HaltOnChange):
14
wt = WorkingTree.open(path)
88
wt = bzrdir.BzrDir.open(path).open_workingtree(path,
89
recommend_upgrade=False)
15
90
except (NoWorkingTree, NotBranchError):
16
91
raise NotCheckout(path)
17
92
tree_base = wt.bzrdir.transport.base
19
94
branch_base = branch.bzrdir.transport.base
20
95
if tree_base == branch_base:
21
96
raise NotCheckout(path)
22
delta = compare_trees(wt.basis_tree(), wt, want_unchanged=False)
23
if delta.has_changed():
24
raise UncommittedCheckout()
97
policy.check_changed(wt, remove_branch)
26
99
parent_loc = branch.get_parent()
27
100
if parent_loc is None:
29
102
parent = Branch.open(parent_loc)
30
p_ancestry = parent.repository.get_ancestry(parent.last_revision())
31
if branch.last_revision() not in p_ancestry:
103
last_revision = _mod_revision.ensure_null(parent.last_revision())
104
p_ancestry = parent.repository.get_ancestry(last_revision)
105
if (last_revision != _mod_revision.NULL_REVISION and
106
branch.last_revision() not in p_ancestry):
32
107
raise ParentMissingRevisions(branch.get_parent())
44
119
from unittest import makeSuite
46
from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
47
from bzrlib.branch import BranchReferenceFormat
48
121
from bzrlib.tests import TestCaseInTempDir
124
class PipelinePluginFeature:
129
import bzrlib.plugins.pipeline
50
136
class TestZap(TestCaseInTempDir):
52
138
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()
139
wt = bzrdir.BzrDir.create_standalone_workingtree('source')
140
return wt.branch.create_checkout('checkout', lightweight=True)
59
142
def make_checkout2(self):
60
143
wt = self.make_checkout()
61
144
wt2 = wt.branch.bzrdir.sprout('source2').open_workingtree()
63
checkout = BzrDirMetaFormat1().initialize('checkout2')
64
BranchReferenceFormat().initialize(checkout, wt2.branch)
65
return checkout.create_workingtree()
145
return wt2.branch.create_checkout('checkout2', lightweight=True)
67
147
def test_is_checkout(self):
68
148
self.assertRaises(NotCheckout, zap, '.')
69
wt = BzrDir.create_standalone_workingtree('.')
149
wt = bzrdir.BzrDir.create_standalone_workingtree('.')
70
150
self.assertRaises(NotCheckout, zap, '.')
72
152
def test_zap_works(self):
95
175
checkout.commit('commit changes to branch')
178
def make_modified_checkout(self):
179
checkout = self.make_checkout()
180
os.mkdir('checkout/foo')
184
def test_allow_modified(self):
185
self.make_modified_checkout()
186
self.assertRaises(UncommittedCheckout, zap, 'checkout')
187
zap('checkout', policy=AllowChanged)
189
def test_store(self):
190
self.requireFeature(PipelinePluginFeature)
191
checkout = self.make_modified_checkout()
192
zap('checkout', policy=StoreChanges)
193
self.assertIn('stored-transform',
194
checkout.branch.bzrdir.transport.list_dir('branch'))
196
def test_store_remove_branch(self):
197
self.requireFeature(PipelinePluginFeature)
198
checkout = self.make_modified_checkout()
199
branch = self.make_branch('branch')
200
checkout.branch.set_parent(branch.base)
201
e = self.assertRaises(BzrCommandError, zap, 'checkout',
202
policy=StoreChanges, remove_branch=True)
203
self.assertEqual('Cannot store changes in deleted branch.', str(e))
205
def test_store_remove_branch_unmodified(self):
206
self.requireFeature(PipelinePluginFeature)
207
checkout = self.make_checkout()
208
branch = self.make_branch('branch')
209
checkout.branch.set_parent(branch.base)
210
zap('checkout', policy=StoreChanges, remove_branch=True)
98
212
return makeSuite(TestZap)