~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to zap.py

  • Committer: Aaron Bentley
  • Date: 2011-04-12 04:13:00 UTC
  • mto: This revision was merged to the branch mainline in revision 757.
  • Revision ID: aaron@aaronbentley.com-20110412041300-k13os9o9cq8hlox0
Revamp zap's changed-file handling.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
    )
25
25
from bzrlib.branch import Branch
26
26
from bzrlib.errors import NoWorkingTree, NotBranchError
 
27
from bzrlib import registry
27
28
from bzrlib.workingtree import WorkingTree
28
29
 
29
30
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
30
31
                    NoParent)
31
32
 
32
33
 
33
 
class ChangedFilesPolicy(object):
34
 
 
35
 
    @staticmethod
36
 
    def check_remove_branch():
37
 
        pass
38
 
 
39
 
 
40
 
class AllowChanged(ChangedFilesPolicy):
41
 
 
42
 
    @classmethod
43
 
    def check_changed(klass, wt):
44
 
        pass
45
 
 
46
 
 
47
 
class CheckChanged(ChangedFilesPolicy):
48
 
 
49
 
    @classmethod
50
 
    def check_changed(klass, wt):
 
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):
51
45
        delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
52
46
        if delta.has_changed():
53
 
            klass.handle_changed(wt)
 
47
            klass.handle_changed(wt, remove_branch)
54
48
 
55
49
 
56
50
class HaltOnChange(CheckChanged):
57
51
 
58
52
    @staticmethod
59
 
    def handle_changed(wt):
 
53
    def handle_changed(wt, remove_branch):
60
54
        raise UncommittedCheckout()
61
55
 
62
56
 
63
57
class StoreChanges(CheckChanged):
64
58
 
65
59
    @staticmethod
66
 
    def check_remove_branch():
67
 
        raise AssertionError('Cannot store changes in deleted branch.')
68
 
 
69
 
    @staticmethod
70
 
    def handle_changed(wt):
 
60
    def handle_changed(wt, remove_branch):
71
61
        from bzrlib.plugins.pipeline.pipeline import PipeManager
 
62
        if remove_branch:
 
63
            raise AssertionError('Cannot store changes in deleted branch.')
72
64
        PipeManager.from_checkout(wt).store_uncommitted()
73
65
 
74
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', StoreChanges,
 
80
                                'Stop if tree contents are modified.')
 
81
 
 
82
 
75
83
def zap(path, remove_branch=False, policy=HaltOnChange):
76
 
    if remove_branch:
77
 
        policy.check_remove_branch()
78
84
    try:
79
85
        wt = bzrdir.BzrDir.open(path).open_workingtree(path,
80
86
                                                       recommend_upgrade=False)
85
91
    branch_base = branch.bzrdir.transport.base
86
92
    if tree_base == branch_base:
87
93
        raise NotCheckout(path)
88
 
    policy.check_changed(wt)
 
94
    policy.check_changed(wt, remove_branch)
89
95
    if remove_branch:
90
96
        parent_loc = branch.get_parent()
91
97
        if parent_loc is None:
186
192
 
187
193
        def test_store_remove_branch(self):
188
194
            self.requireFeature(PipelinePluginFeature)
189
 
            checkout = self.make_checkout()
 
195
            checkout = self.make_modified_checkout()
190
196
            branch = self.make_branch('branch')
191
197
            checkout.branch.set_parent(branch.base)
192
198
            e = self.assertRaises(AssertionError, zap, 'checkout',
193
199
                                  policy=StoreChanges, remove_branch=True)
194
200
            self.assertEqual('Cannot store changes in deleted branch.', str(e))
195
201
 
 
202
        def test_store_remove_branch_unmodified(self):
 
203
            self.requireFeature(PipelinePluginFeature)
 
204
            checkout = self.make_checkout()
 
205
            branch = self.make_branch('branch')
 
206
            checkout.branch.set_parent(branch.base)
 
207
            zap('checkout', policy=StoreChanges, remove_branch=True)
 
208
 
196
209
    return makeSuite(TestZap)