~abentley/bzrtools/bzrtools.dev

345 by Aaron Bentley
Added zap command
1
from shutil import rmtree
2
554 by Aaron Bentley
Adjust to use NULL_REVISION in API
3
from bzrlib import (
4
    bzrdir,
5
    revision as _mod_revision,
6
    )
401 by Aaron Bentley
Add check whether branch has unique revisions
7
from bzrlib.branch import Branch
345 by Aaron Bentley
Added zap command
8
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
9
from bzrlib.workingtree import WorkingTree
10
531.2.2 by Charlie Shepherd
Remove all trailing whitespace
11
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
12
                    NoParent)
345 by Aaron Bentley
Added zap command
13
14
573.1.3 by Aaron Bentley
Allow zap --force to delete modified checkouts
15
def zap(path, remove_branch=False, allow_modified=False):
345 by Aaron Bentley
Added zap command
16
    try:
544.1.1 by Aaron Bentley
Zap doesn't recommend upgrading trees it's about to delete
17
        wt = bzrdir.BzrDir.open(path).open_workingtree(path,
18
                                                       recommend_upgrade=False)
345 by Aaron Bentley
Added zap command
19
    except (NoWorkingTree, NotBranchError):
20
        raise NotCheckout(path)
21
    tree_base = wt.bzrdir.transport.base
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
22
    branch = wt.branch
23
    branch_base = branch.bzrdir.transport.base
345 by Aaron Bentley
Added zap command
24
    if tree_base == branch_base:
25
        raise NotCheckout(path)
573.1.3 by Aaron Bentley
Allow zap --force to delete modified checkouts
26
    if not allow_modified:
27
        delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
28
        if delta.has_changed():
29
            raise UncommittedCheckout()
401 by Aaron Bentley
Add check whether branch has unique revisions
30
    if remove_branch:
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
31
        parent_loc = branch.get_parent()
32
        if parent_loc is None:
33
            raise NoParent()
34
        parent = Branch.open(parent_loc)
554 by Aaron Bentley
Adjust to use NULL_REVISION in API
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):
401 by Aaron Bentley
Add check whether branch has unique revisions
39
            raise ParentMissingRevisions(branch.get_parent())
345 by Aaron Bentley
Added zap command
40
    rmtree(path)
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
41
    if remove_branch:
42
        t = branch.bzrdir.transport
43
        while t.base != branch_base:
44
            t = t.clone('..')
45
        t = t.clone('..')
46
        t.delete_tree('')
345 by Aaron Bentley
Added zap command
47
48
49
def test_suite():
50
    import os
51
    from unittest import makeSuite
531.2.2 by Charlie Shepherd
Remove all trailing whitespace
52
345 by Aaron Bentley
Added zap command
53
    from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
54
    from bzrlib.branch import BranchReferenceFormat
55
    from bzrlib.tests import TestCaseInTempDir
56
57
    class TestZap(TestCaseInTempDir):
58
59
        def make_checkout(self):
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()
65
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
66
        def make_checkout2(self):
67
            wt = self.make_checkout()
68
            wt2 = wt.branch.bzrdir.sprout('source2').open_workingtree()
69
            os.mkdir('checkout2')
70
            checkout = BzrDirMetaFormat1().initialize('checkout2')
71
            BranchReferenceFormat().initialize(checkout, wt2.branch)
72
            return checkout.create_workingtree()
73
345 by Aaron Bentley
Added zap command
74
        def test_is_checkout(self):
75
            self.assertRaises(NotCheckout, zap, '.')
76
            wt = BzrDir.create_standalone_workingtree('.')
77
            self.assertRaises(NotCheckout, zap, '.')
78
79
        def test_zap_works(self):
80
            self.make_checkout()
81
            self.assertIs(True, os.path.exists('checkout'))
82
            zap('checkout')
83
            self.assertIs(False, os.path.exists('checkout'))
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
84
            self.assertIs(True, os.path.exists('source'))
85
86
	def test_zap_branch(self):
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
87
            self.make_checkout2()
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
88
            base = WorkingTree.open('checkout').branch.base
89
            self.assertIs(True, os.path.exists('checkout'))
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
90
            self.assertRaises(NoParent, zap, 'checkout', remove_branch=True)
91
            self.assertIs(True, os.path.exists('checkout'))
92
            self.assertIs(True, os.path.exists('source'))
93
            zap('checkout2', remove_branch=True)
94
            self.assertIs(False, os.path.exists('checkout2'))
95
            self.assertIs(False, os.path.exists('source2'))
345 by Aaron Bentley
Added zap command
96
97
        def test_checks_modified(self):
98
            checkout = self.make_checkout()
99
            os.mkdir('checkout/foo')
100
            checkout.add('foo')
101
            self.assertRaises(UncommittedCheckout, zap, 'checkout')
102
            checkout.commit('commit changes to branch')
103
            zap('checkout')
104
573.1.3 by Aaron Bentley
Allow zap --force to delete modified checkouts
105
        def test_allow_modified(self):
106
            checkout = self.make_checkout()
107
            os.mkdir('checkout/foo')
108
            checkout.add('foo')
109
            self.assertRaises(UncommittedCheckout, zap, 'checkout')
110
            zap('checkout', allow_modified=True)
111
345 by Aaron Bentley
Added zap command
112
    return makeSuite(TestZap)