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