~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to zap.py

  • Committer: Aaron Bentley
  • Date: 2007-07-12 20:42:54 UTC
  • Revision ID: abentley@panoramicfeedback.com-20070712204254-7xk9fvrxj7cdzku5
Update version number

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from shutil import rmtree
 
2
 
 
3
from bzrlib import (
 
4
    bzrdir,
 
5
    revision as _mod_revision,
 
6
    )
 
7
from bzrlib.branch import Branch
 
8
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
 
9
from bzrlib.workingtree import WorkingTree
 
10
 
 
11
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
 
12
                    NoParent)
 
13
 
 
14
 
 
15
def zap(path, remove_branch=False):
 
16
    try:
 
17
        wt = bzrdir.BzrDir.open(path).open_workingtree(path,
 
18
                                                       recommend_upgrade=False)
 
19
    except (NoWorkingTree, NotBranchError):
 
20
        raise NotCheckout(path)
 
21
    tree_base = wt.bzrdir.transport.base
 
22
    branch = wt.branch
 
23
    branch_base = branch.bzrdir.transport.base
 
24
    if tree_base == branch_base:
 
25
        raise NotCheckout(path)
 
26
    delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
 
27
    if delta.has_changed():
 
28
        raise UncommittedCheckout()
 
29
    if remove_branch:
 
30
        parent_loc = branch.get_parent()
 
31
        if parent_loc is None:
 
32
            raise NoParent()
 
33
        parent = Branch.open(parent_loc)
 
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):
 
38
            raise ParentMissingRevisions(branch.get_parent())
 
39
    rmtree(path)
 
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('')
 
46
 
 
47
 
 
48
def test_suite():
 
49
    import os
 
50
    from unittest import makeSuite
 
51
 
 
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
 
 
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
 
 
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'))
 
83
            self.assertIs(True, os.path.exists('source'))
 
84
 
 
85
        def test_zap_branch(self):
 
86
            self.make_checkout2()
 
87
            base = WorkingTree.open('checkout').branch.base
 
88
            self.assertIs(True, os.path.exists('checkout'))
 
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'))
 
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)