~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
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
15
def zap(path, remove_branch=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)
423.1.7 by Aaron Bentley
More updates for 0.9
26
    delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
345 by Aaron Bentley
Added zap command
27
    if delta.has_changed():
28
        raise UncommittedCheckout()
401 by Aaron Bentley
Add check whether branch has unique revisions
29
    if remove_branch:
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
30
        parent_loc = branch.get_parent()
31
        if parent_loc is None:
32
            raise NoParent()
33
        parent = Branch.open(parent_loc)
554 by Aaron Bentley
Adjust to use NULL_REVISION in API
34
        last_revision = _mod_revision.ensure_null(parent.last_revision())
35
        p_ancestry = parent.repository.get_ancestry(last_revision)
36
        if (last_revision != _mod_revision.NULL_REVISION and
37
            branch.last_revision() not in p_ancestry):
401 by Aaron Bentley
Add check whether branch has unique revisions
38
            raise ParentMissingRevisions(branch.get_parent())
345 by Aaron Bentley
Added zap command
39
    rmtree(path)
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
40
    if remove_branch:
41
        t = branch.bzrdir.transport
42
        while t.base != branch_base:
43
            t = t.clone('..')
44
        t = t.clone('..')
45
        t.delete_tree('')
345 by Aaron Bentley
Added zap command
46
47
48
def test_suite():
49
    import os
50
    from unittest import makeSuite
531.2.2 by Charlie Shepherd
Remove all trailing whitespace
51
345 by Aaron Bentley
Added zap command
52
    from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
53
    from bzrlib.branch import BranchReferenceFormat
54
    from bzrlib.tests import TestCaseInTempDir
55
56
    class TestZap(TestCaseInTempDir):
57
58
        def make_checkout(self):
59
            wt = BzrDir.create_standalone_workingtree('source')
60
            os.mkdir('checkout')
61
            checkout = BzrDirMetaFormat1().initialize('checkout')
62
            BranchReferenceFormat().initialize(checkout, wt.branch)
63
            return checkout.create_workingtree()
64
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
65
        def make_checkout2(self):
66
            wt = self.make_checkout()
67
            wt2 = wt.branch.bzrdir.sprout('source2').open_workingtree()
68
            os.mkdir('checkout2')
69
            checkout = BzrDirMetaFormat1().initialize('checkout2')
70
            BranchReferenceFormat().initialize(checkout, wt2.branch)
71
            return checkout.create_workingtree()
72
345 by Aaron Bentley
Added zap command
73
        def test_is_checkout(self):
74
            self.assertRaises(NotCheckout, zap, '.')
75
            wt = BzrDir.create_standalone_workingtree('.')
76
            self.assertRaises(NotCheckout, zap, '.')
77
78
        def test_zap_works(self):
79
            self.make_checkout()
80
            self.assertIs(True, os.path.exists('checkout'))
81
            zap('checkout')
82
            self.assertIs(False, os.path.exists('checkout'))
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
83
            self.assertIs(True, os.path.exists('source'))
84
85
	def test_zap_branch(self):
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
86
            self.make_checkout2()
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
87
            base = WorkingTree.open('checkout').branch.base
88
            self.assertIs(True, os.path.exists('checkout'))
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
89
            self.assertRaises(NoParent, zap, 'checkout', remove_branch=True)
90
            self.assertIs(True, os.path.exists('checkout'))
91
            self.assertIs(True, os.path.exists('source'))
92
            zap('checkout2', remove_branch=True)
93
            self.assertIs(False, os.path.exists('checkout2'))
94
            self.assertIs(False, os.path.exists('source2'))
345 by Aaron Bentley
Added zap command
95
96
        def test_checks_modified(self):
97
            checkout = self.make_checkout()
98
            os.mkdir('checkout/foo')
99
            checkout.add('foo')
100
            self.assertRaises(UncommittedCheckout, zap, 'checkout')
101
            checkout.commit('commit changes to branch')
102
            zap('checkout')
103
104
    return makeSuite(TestZap)