~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
1
from shutil import rmtree
2
2
 
 
3
from bzrlib import (
 
4
    bzrdir,
 
5
    revision as _mod_revision,
 
6
    )
 
7
from bzrlib.branch import Branch
3
8
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
4
 
from bzrlib.delta import compare_trees
5
9
from bzrlib.workingtree import WorkingTree
6
10
 
7
 
from errors import NotCheckout, UncommittedCheckout
 
11
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
 
12
                    NoParent)
8
13
 
9
14
 
10
15
def zap(path, remove_branch=False):
11
16
    try:
12
 
        wt = WorkingTree.open(path)
 
17
        wt = bzrdir.BzrDir.open(path).open_workingtree(path,
 
18
                                                       recommend_upgrade=False)
13
19
    except (NoWorkingTree, NotBranchError):
14
20
        raise NotCheckout(path)
15
21
    tree_base = wt.bzrdir.transport.base
17
23
    branch_base = branch.bzrdir.transport.base
18
24
    if tree_base == branch_base:
19
25
        raise NotCheckout(path)
20
 
    delta = compare_trees(wt.basis_tree(), wt, want_unchanged=False)
 
26
    delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
21
27
    if delta.has_changed():
22
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())
23
39
    rmtree(path)
24
40
    if remove_branch:
25
41
        t = branch.bzrdir.transport
32
48
def test_suite():
33
49
    import os
34
50
    from unittest import makeSuite
35
 
    
 
51
 
36
52
    from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
37
53
    from bzrlib.branch import BranchReferenceFormat
38
54
    from bzrlib.tests import TestCaseInTempDir
46
62
            BranchReferenceFormat().initialize(checkout, wt.branch)
47
63
            return checkout.create_workingtree()
48
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
 
49
73
        def test_is_checkout(self):
50
74
            self.assertRaises(NotCheckout, zap, '.')
51
75
            wt = BzrDir.create_standalone_workingtree('.')
59
83
            self.assertIs(True, os.path.exists('source'))
60
84
 
61
85
        def test_zap_branch(self):
62
 
            self.make_checkout()
 
86
            self.make_checkout2()
63
87
            base = WorkingTree.open('checkout').branch.base
64
88
            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'))
 
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'))
68
95
 
69
96
        def test_checks_modified(self):
70
97
            checkout = self.make_checkout()