~abentley/bzrtools/bzrtools.dev

345 by Aaron Bentley
Added zap command
1
from shutil import rmtree
2
401 by Aaron Bentley
Add check whether branch has unique revisions
3
from bzrlib.branch import Branch
345 by Aaron Bentley
Added zap command
4
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
5
from bzrlib.workingtree import WorkingTree
6
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
7
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions, 
8
                    NoParent)
345 by Aaron Bentley
Added zap command
9
10
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
11
def zap(path, remove_branch=False):
345 by Aaron Bentley
Added zap command
12
    try:
13
        wt = WorkingTree.open(path)
14
    except (NoWorkingTree, NotBranchError):
15
        raise NotCheckout(path)
16
    tree_base = wt.bzrdir.transport.base
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
17
    branch = wt.branch
18
    branch_base = branch.bzrdir.transport.base
345 by Aaron Bentley
Added zap command
19
    if tree_base == branch_base:
20
        raise NotCheckout(path)
423.1.7 by Aaron Bentley
More updates for 0.9
21
    delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
345 by Aaron Bentley
Added zap command
22
    if delta.has_changed():
23
        raise UncommittedCheckout()
401 by Aaron Bentley
Add check whether branch has unique revisions
24
    if remove_branch:
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
25
        parent_loc = branch.get_parent()
26
        if parent_loc is None:
27
            raise NoParent()
28
        parent = Branch.open(parent_loc)
401 by Aaron Bentley
Add check whether branch has unique revisions
29
        p_ancestry = parent.repository.get_ancestry(parent.last_revision())
30
        if branch.last_revision() not in p_ancestry:
31
            raise ParentMissingRevisions(branch.get_parent())
345 by Aaron Bentley
Added zap command
32
    rmtree(path)
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
33
    if remove_branch:
34
        t = branch.bzrdir.transport
35
        while t.base != branch_base:
36
            t = t.clone('..')
37
        t = t.clone('..')
38
        t.delete_tree('')
345 by Aaron Bentley
Added zap command
39
40
41
def test_suite():
42
    import os
43
    from unittest import makeSuite
44
    
45
    from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
46
    from bzrlib.branch import BranchReferenceFormat
47
    from bzrlib.tests import TestCaseInTempDir
48
49
    class TestZap(TestCaseInTempDir):
50
51
        def make_checkout(self):
52
            wt = BzrDir.create_standalone_workingtree('source')
53
            os.mkdir('checkout')
54
            checkout = BzrDirMetaFormat1().initialize('checkout')
55
            BranchReferenceFormat().initialize(checkout, wt.branch)
56
            return checkout.create_workingtree()
57
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
58
        def make_checkout2(self):
59
            wt = self.make_checkout()
60
            wt2 = wt.branch.bzrdir.sprout('source2').open_workingtree()
61
            os.mkdir('checkout2')
62
            checkout = BzrDirMetaFormat1().initialize('checkout2')
63
            BranchReferenceFormat().initialize(checkout, wt2.branch)
64
            return checkout.create_workingtree()
65
345 by Aaron Bentley
Added zap command
66
        def test_is_checkout(self):
67
            self.assertRaises(NotCheckout, zap, '.')
68
            wt = BzrDir.create_standalone_workingtree('.')
69
            self.assertRaises(NotCheckout, zap, '.')
70
71
        def test_zap_works(self):
72
            self.make_checkout()
73
            self.assertIs(True, os.path.exists('checkout'))
74
            zap('checkout')
75
            self.assertIs(False, os.path.exists('checkout'))
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
76
            self.assertIs(True, os.path.exists('source'))
77
78
	def test_zap_branch(self):
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
79
            self.make_checkout2()
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
80
            base = WorkingTree.open('checkout').branch.base
81
            self.assertIs(True, os.path.exists('checkout'))
407 by Aaron Bentley
Fix zap for checkouts of branches with no parents
82
            self.assertRaises(NoParent, zap, 'checkout', remove_branch=True)
83
            self.assertIs(True, os.path.exists('checkout'))
84
            self.assertIs(True, os.path.exists('source'))
85
            zap('checkout2', remove_branch=True)
86
            self.assertIs(False, os.path.exists('checkout2'))
87
            self.assertIs(False, os.path.exists('source2'))
345 by Aaron Bentley
Added zap command
88
89
        def test_checks_modified(self):
90
            checkout = self.make_checkout()
91
            os.mkdir('checkout/foo')
92
            checkout.add('foo')
93
            self.assertRaises(UncommittedCheckout, zap, 'checkout')
94
            checkout.commit('commit changes to branch')
95
            zap('checkout')
96
97
    return makeSuite(TestZap)