~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to zap.py

  • Committer: Aaron Bentley
  • Date: 2006-06-14 18:09:53 UTC
  • mto: This revision was merged to the branch mainline in revision 395.
  • Revision ID: abentley@panoramicfeedback.com-20060614180953-4671478534fd8823
Update NEWS

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
8
3
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
 
4
from bzrlib.delta import compare_trees
9
5
from bzrlib.workingtree import WorkingTree
10
6
 
11
 
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
12
 
                    NoParent)
13
 
 
14
 
 
15
 
def zap(path, remove_branch=False, allow_modified=False):
 
7
from errors import NotCheckout, UncommittedCheckout
 
8
 
 
9
 
 
10
def zap(path, remove_branch=False):
16
11
    try:
17
 
        wt = bzrdir.BzrDir.open(path).open_workingtree(path,
18
 
                                                       recommend_upgrade=False)
 
12
        wt = WorkingTree.open(path)
19
13
    except (NoWorkingTree, NotBranchError):
20
14
        raise NotCheckout(path)
21
15
    tree_base = wt.bzrdir.transport.base
23
17
    branch_base = branch.bzrdir.transport.base
24
18
    if tree_base == branch_base:
25
19
        raise NotCheckout(path)
26
 
    if not allow_modified:
27
 
        delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
28
 
        if delta.has_changed():
29
 
            raise UncommittedCheckout()
30
 
    if remove_branch:
31
 
        parent_loc = branch.get_parent()
32
 
        if parent_loc is None:
33
 
            raise NoParent()
34
 
        parent = Branch.open(parent_loc)
35
 
        last_revision = _mod_revision.ensure_null(parent.last_revision())
36
 
        p_ancestry = parent.repository.get_ancestry(last_revision)
37
 
        if (last_revision != _mod_revision.NULL_REVISION and
38
 
            branch.last_revision() not in p_ancestry):
39
 
            raise ParentMissingRevisions(branch.get_parent())
 
20
    delta = compare_trees(wt.basis_tree(), wt, want_unchanged=False)
 
21
    if delta.has_changed():
 
22
        raise UncommittedCheckout()
40
23
    rmtree(path)
41
24
    if remove_branch:
42
25
        t = branch.bzrdir.transport
49
32
def test_suite():
50
33
    import os
51
34
    from unittest import makeSuite
52
 
 
 
35
    
53
36
    from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
54
37
    from bzrlib.branch import BranchReferenceFormat
55
38
    from bzrlib.tests import TestCaseInTempDir
63
46
            BranchReferenceFormat().initialize(checkout, wt.branch)
64
47
            return checkout.create_workingtree()
65
48
 
66
 
        def make_checkout2(self):
67
 
            wt = self.make_checkout()
68
 
            wt2 = wt.branch.bzrdir.sprout('source2').open_workingtree()
69
 
            os.mkdir('checkout2')
70
 
            checkout = BzrDirMetaFormat1().initialize('checkout2')
71
 
            BranchReferenceFormat().initialize(checkout, wt2.branch)
72
 
            return checkout.create_workingtree()
73
 
 
74
49
        def test_is_checkout(self):
75
50
            self.assertRaises(NotCheckout, zap, '.')
76
51
            wt = BzrDir.create_standalone_workingtree('.')
84
59
            self.assertIs(True, os.path.exists('source'))
85
60
 
86
61
        def test_zap_branch(self):
87
 
            self.make_checkout2()
 
62
            self.make_checkout()
88
63
            base = WorkingTree.open('checkout').branch.base
89
64
            self.assertIs(True, os.path.exists('checkout'))
90
 
            self.assertRaises(NoParent, zap, 'checkout', remove_branch=True)
91
 
            self.assertIs(True, os.path.exists('checkout'))
92
 
            self.assertIs(True, os.path.exists('source'))
93
 
            zap('checkout2', remove_branch=True)
94
 
            self.assertIs(False, os.path.exists('checkout2'))
95
 
            self.assertIs(False, os.path.exists('source2'))
 
65
            zap('checkout', remove_branch=True)
 
66
            self.assertIs(False, os.path.exists('checkout'))
 
67
            self.assertIs(False, os.path.exists('source'))
96
68
 
97
69
        def test_checks_modified(self):
98
70
            checkout = self.make_checkout()
102
74
            checkout.commit('commit changes to branch')
103
75
            zap('checkout')
104
76
 
105
 
        def test_allow_modified(self):
106
 
            checkout = self.make_checkout()
107
 
            os.mkdir('checkout/foo')
108
 
            checkout.add('foo')
109
 
            self.assertRaises(UncommittedCheckout, zap, 'checkout')
110
 
            zap('checkout', allow_modified=True)
111
 
 
112
77
    return makeSuite(TestZap)