~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to zap.py

  • Committer: Aaron Bentley
  • Date: 2009-07-10 04:05:24 UTC
  • Revision ID: aaron@aaronbentley.com-20090710040524-slufgm4he3fx1r4f
Mirror the child_submit_to setting.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
4
 
#
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.
9
 
#
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.
14
 
#
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
18
 
 
19
1
from shutil import rmtree
20
2
 
21
3
from bzrlib import (
23
5
    revision as _mod_revision,
24
6
    )
25
7
from bzrlib.branch import Branch
26
 
from bzrlib.errors import BzrCommandError, NoWorkingTree, NotBranchError
27
 
from bzrlib import registry
 
8
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
28
9
from bzrlib.workingtree import WorkingTree
29
10
 
30
11
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
31
12
                    NoParent)
32
13
 
33
14
 
34
 
class AllowChanged(object):
35
 
 
36
 
    @classmethod
37
 
    def check_changed(klass, wt, remove_branch):
38
 
        pass
39
 
 
40
 
 
41
 
class CheckChanged(object):
42
 
 
43
 
    @classmethod
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)
48
 
 
49
 
 
50
 
class HaltOnChange(CheckChanged):
51
 
 
52
 
    @staticmethod
53
 
    def handle_changed(wt, remove_branch):
54
 
        raise UncommittedCheckout()
55
 
 
56
 
 
57
 
class StoreChanges(CheckChanged):
58
 
 
59
 
    @staticmethod
60
 
    def handle_changed(wt, remove_branch):
61
 
        from bzrlib.plugins.pipeline.pipeline import PipeManager
62
 
        if remove_branch:
63
 
            raise BzrCommandError('Cannot store changes in deleted branch.')
64
 
        PipeManager.from_checkout(wt).store_uncommitted()
65
 
 
66
 
 
67
 
change_policy_registry = registry.Registry()
68
 
 
69
 
 
70
 
change_policy_registry.register('force', AllowChanged,
71
 
                                'Delete tree even if contents are modified.')
72
 
 
73
 
 
74
 
change_policy_registry.register('store', StoreChanges,
75
 
                                'Store changes in branch.  (Requires'
76
 
                                ' bzr-pipeline.)')
77
 
 
78
 
 
79
 
change_policy_registry.register('check', HaltOnChange,
80
 
                                'Stop if tree contents are modified.')
81
 
 
82
 
 
83
 
change_policy_registry.default_key = 'check'
84
 
 
85
 
 
86
 
def zap(path, remove_branch=False, policy=HaltOnChange):
 
15
def zap(path, remove_branch=False, allow_modified=False):
87
16
    try:
88
17
        wt = bzrdir.BzrDir.open(path).open_workingtree(path,
89
18
                                                       recommend_upgrade=False)
94
23
    branch_base = branch.bzrdir.transport.base
95
24
    if tree_base == branch_base:
96
25
        raise NotCheckout(path)
97
 
    policy.check_changed(wt, remove_branch)
 
26
    if not allow_modified:
 
27
        delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
 
28
        if delta.has_changed():
 
29
            raise UncommittedCheckout()
98
30
    if remove_branch:
99
31
        parent_loc = branch.get_parent()
100
32
        if parent_loc is None:
118
50
    import os
119
51
    from unittest import makeSuite
120
52
 
 
53
    from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
 
54
    from bzrlib.branch import BranchReferenceFormat
121
55
    from bzrlib.tests import TestCaseInTempDir
122
56
 
123
 
 
124
 
    class PipelinePluginFeature:
125
 
 
126
 
        @staticmethod
127
 
        def available():
128
 
            try:
129
 
                import bzrlib.plugins.pipeline
130
 
            except ImportError:
131
 
                return False
132
 
            else:
133
 
                return True
134
 
 
135
 
 
136
57
    class TestZap(TestCaseInTempDir):
137
58
 
138
59
        def make_checkout(self):
139
 
            wt = bzrdir.BzrDir.create_standalone_workingtree('source')
140
 
            return wt.branch.create_checkout('checkout', lightweight=True)
 
60
            wt = BzrDir.create_standalone_workingtree('source')
 
61
            os.mkdir('checkout')
 
62
            checkout = BzrDirMetaFormat1().initialize('checkout')
 
63
            BranchReferenceFormat().initialize(checkout, wt.branch)
 
64
            return checkout.create_workingtree()
141
65
 
142
66
        def make_checkout2(self):
143
67
            wt = self.make_checkout()
144
68
            wt2 = wt.branch.bzrdir.sprout('source2').open_workingtree()
145
 
            return wt2.branch.create_checkout('checkout2', lightweight=True)
 
69
            os.mkdir('checkout2')
 
70
            checkout = BzrDirMetaFormat1().initialize('checkout2')
 
71
            BranchReferenceFormat().initialize(checkout, wt2.branch)
 
72
            return checkout.create_workingtree()
146
73
 
147
74
        def test_is_checkout(self):
148
75
            self.assertRaises(NotCheckout, zap, '.')
149
 
            wt = bzrdir.BzrDir.create_standalone_workingtree('.')
 
76
            wt = BzrDir.create_standalone_workingtree('.')
150
77
            self.assertRaises(NotCheckout, zap, '.')
151
78
 
152
79
        def test_zap_works(self):
156
83
            self.assertIs(False, os.path.exists('checkout'))
157
84
            self.assertIs(True, os.path.exists('source'))
158
85
 
159
 
        def test_zap_branch(self):
 
86
        def test_zap_branch(self):
160
87
            self.make_checkout2()
161
88
            base = WorkingTree.open('checkout').branch.base
162
89
            self.assertIs(True, os.path.exists('checkout'))
175
102
            checkout.commit('commit changes to branch')
176
103
            zap('checkout')
177
104
 
178
 
        def make_modified_checkout(self):
179
 
            checkout = self.make_checkout()
180
 
            os.mkdir('checkout/foo')
181
 
            checkout.add('foo')
182
 
            return checkout
183
 
 
184
105
        def test_allow_modified(self):
185
 
            self.make_modified_checkout()
 
106
            checkout = self.make_checkout()
 
107
            os.mkdir('checkout/foo')
 
108
            checkout.add('foo')
186
109
            self.assertRaises(UncommittedCheckout, zap, 'checkout')
187
 
            zap('checkout', policy=AllowChanged)
188
 
 
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'))
195
 
 
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))
204
 
 
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)
 
110
            zap('checkout', allow_modified=True)
211
111
 
212
112
    return makeSuite(TestZap)