~abentley/bzrtools/bzrtools.dev

345 by Aaron Bentley
Added zap command
1
from shutil import rmtree
2
3
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
4
from bzrlib.delta import compare_trees
5
from bzrlib.workingtree import WorkingTree
6
7
from errors import NotCheckout, UncommittedCheckout
8
9
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
10
def zap(path, remove_branch=False):
345 by Aaron Bentley
Added zap command
11
    try:
12
        wt = WorkingTree.open(path)
13
    except (NoWorkingTree, NotBranchError):
14
        raise NotCheckout(path)
15
    tree_base = wt.bzrdir.transport.base
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
16
    branch = wt.branch
17
    branch_base = branch.bzrdir.transport.base
345 by Aaron Bentley
Added zap command
18
    if tree_base == branch_base:
19
        raise NotCheckout(path)
20
    delta = compare_trees(wt.basis_tree(), wt, want_unchanged=False)
21
    if delta.has_changed():
22
        raise UncommittedCheckout()
23
    rmtree(path)
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
24
    if remove_branch:
25
        t = branch.bzrdir.transport
26
        while t.base != branch_base:
27
            t = t.clone('..')
28
        t = t.clone('..')
29
        t.delete_tree('')
345 by Aaron Bentley
Added zap command
30
31
32
def test_suite():
33
    import os
34
    from unittest import makeSuite
35
    
36
    from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
37
    from bzrlib.branch import BranchReferenceFormat
38
    from bzrlib.tests import TestCaseInTempDir
39
40
    class TestZap(TestCaseInTempDir):
41
42
        def make_checkout(self):
43
            wt = BzrDir.create_standalone_workingtree('source')
44
            os.mkdir('checkout')
45
            checkout = BzrDirMetaFormat1().initialize('checkout')
46
            BranchReferenceFormat().initialize(checkout, wt.branch)
47
            return checkout.create_workingtree()
48
49
        def test_is_checkout(self):
50
            self.assertRaises(NotCheckout, zap, '.')
51
            wt = BzrDir.create_standalone_workingtree('.')
52
            self.assertRaises(NotCheckout, zap, '.')
53
54
        def test_zap_works(self):
55
            self.make_checkout()
56
            self.assertIs(True, os.path.exists('checkout'))
57
            zap('checkout')
58
            self.assertIs(False, os.path.exists('checkout'))
355.1.1 by Aaron Bentley
Provided --branch option to for zapping branches
59
            self.assertIs(True, os.path.exists('source'))
60
61
	def test_zap_branch(self):
62
            self.make_checkout()
63
            base = WorkingTree.open('checkout').branch.base
64
            self.assertIs(True, os.path.exists('checkout'))
65
            zap('checkout', remove_branch=True)
66
            self.assertIs(False, os.path.exists('checkout'))
67
            self.assertIs(False, os.path.exists('source'))
345 by Aaron Bentley
Added zap command
68
69
        def test_checks_modified(self):
70
            checkout = self.make_checkout()
71
            os.mkdir('checkout/foo')
72
            checkout.add('foo')
73
            self.assertRaises(UncommittedCheckout, zap, 'checkout')
74
            checkout.commit('commit changes to branch')
75
            zap('checkout')
76
77
    return makeSuite(TestZap)