~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to zap.py

  • Committer: Aaron Bentley
  • Date: 2008-05-12 18:06:07 UTC
  • Revision ID: aaron@aaronbentley.com-20080512180607-dn6a55if3pk4zdju
Update to avoid deprecated API

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
    )
3
7
from bzrlib.branch import Branch
4
8
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
5
 
from bzrlib.delta import compare_trees
6
9
from bzrlib.workingtree import WorkingTree
7
10
 
8
 
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions, 
 
11
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
9
12
                    NoParent)
10
13
 
11
14
 
12
 
def zap(path, remove_branch=False):
 
15
def zap(path, remove_branch=False, allow_modified=False):
13
16
    try:
14
 
        wt = WorkingTree.open(path)
 
17
        wt = bzrdir.BzrDir.open(path).open_workingtree(path,
 
18
                                                       recommend_upgrade=False)
15
19
    except (NoWorkingTree, NotBranchError):
16
20
        raise NotCheckout(path)
17
21
    tree_base = wt.bzrdir.transport.base
19
23
    branch_base = branch.bzrdir.transport.base
20
24
    if tree_base == branch_base:
21
25
        raise NotCheckout(path)
22
 
    delta = compare_trees(wt.basis_tree(), wt, want_unchanged=False)
23
 
    if delta.has_changed():
24
 
        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()
25
30
    if remove_branch:
26
31
        parent_loc = branch.get_parent()
27
32
        if parent_loc is None:
28
33
            raise NoParent()
29
34
        parent = Branch.open(parent_loc)
30
 
        p_ancestry = parent.repository.get_ancestry(parent.last_revision())
31
 
        if branch.last_revision() not in p_ancestry:
 
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):
32
39
            raise ParentMissingRevisions(branch.get_parent())
33
40
    rmtree(path)
34
41
    if remove_branch:
42
49
def test_suite():
43
50
    import os
44
51
    from unittest import makeSuite
45
 
    
 
52
 
46
53
    from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
47
54
    from bzrlib.branch import BranchReferenceFormat
48
55
    from bzrlib.tests import TestCaseInTempDir
95
102
            checkout.commit('commit changes to branch')
96
103
            zap('checkout')
97
104
 
 
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
 
98
112
    return makeSuite(TestZap)