~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to zap.py

  • Committer: Aaron Bentley
  • Date: 2011-01-26 00:55:38 UTC
  • Revision ID: aaron@aaronbentley.com-20110126005538-zly6t4gawmjsgl74
Tags: release-2.3.0
Update release.py to use The Kraken

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from shutil import rmtree
2
2
 
3
 
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
4
 
from bzrlib.delta import compare_trees
 
3
from bzrlib import (
 
4
    bzrdir,
 
5
    revision as _mod_revision,
 
6
    )
 
7
from bzrlib.branch import Branch
 
8
from bzrlib.errors import NoWorkingTree, NotBranchError
5
9
from bzrlib.workingtree import WorkingTree
6
10
 
7
 
from errors import NotCheckout, UncommittedCheckout
8
 
 
9
 
 
10
 
def zap(path, remove_branch=False):
 
11
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
 
12
                    NoParent)
 
13
 
 
14
 
 
15
def zap(path, remove_branch=False, allow_modified=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)
21
 
    if delta.has_changed():
22
 
        raise UncommittedCheckout()
 
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())
23
40
    rmtree(path)
24
41
    if remove_branch:
25
42
        t = branch.bzrdir.transport
32
49
def test_suite():
33
50
    import os
34
51
    from unittest import makeSuite
35
 
    
36
 
    from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
37
 
    from bzrlib.branch import BranchReferenceFormat
 
52
 
38
53
    from bzrlib.tests import TestCaseInTempDir
39
54
 
40
55
    class TestZap(TestCaseInTempDir):
41
56
 
42
57
        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()
 
58
            wt = bzrdir.BzrDir.create_standalone_workingtree('source')
 
59
            return wt.branch.create_checkout('checkout', lightweight=True)
 
60
 
 
61
        def make_checkout2(self):
 
62
            wt = self.make_checkout()
 
63
            wt2 = wt.branch.bzrdir.sprout('source2').open_workingtree()
 
64
            return wt2.branch.create_checkout('checkout2', lightweight=True)
48
65
 
49
66
        def test_is_checkout(self):
50
67
            self.assertRaises(NotCheckout, zap, '.')
51
 
            wt = BzrDir.create_standalone_workingtree('.')
 
68
            wt = bzrdir.BzrDir.create_standalone_workingtree('.')
52
69
            self.assertRaises(NotCheckout, zap, '.')
53
70
 
54
71
        def test_zap_works(self):
58
75
            self.assertIs(False, os.path.exists('checkout'))
59
76
            self.assertIs(True, os.path.exists('source'))
60
77
 
61
 
        def test_zap_branch(self):
62
 
            self.make_checkout()
 
78
        def test_zap_branch(self):
 
79
            self.make_checkout2()
63
80
            base = WorkingTree.open('checkout').branch.base
64
81
            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'))
 
82
            self.assertRaises(NoParent, zap, 'checkout', remove_branch=True)
 
83
            self.assertIs(True, os.path.exists('checkout'))
 
84
            self.assertIs(True, os.path.exists('source'))
 
85
            zap('checkout2', remove_branch=True)
 
86
            self.assertIs(False, os.path.exists('checkout2'))
 
87
            self.assertIs(False, os.path.exists('source2'))
68
88
 
69
89
        def test_checks_modified(self):
70
90
            checkout = self.make_checkout()
74
94
            checkout.commit('commit changes to branch')
75
95
            zap('checkout')
76
96
 
 
97
        def test_allow_modified(self):
 
98
            checkout = self.make_checkout()
 
99
            os.mkdir('checkout/foo')
 
100
            checkout.add('foo')
 
101
            self.assertRaises(UncommittedCheckout, zap, 'checkout')
 
102
            zap('checkout', allow_modified=True)
 
103
 
77
104
    return makeSuite(TestZap)