~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to zap.py

  • Committer: Aaron Bentley
  • Date: 2006-03-22 22:18:19 UTC
  • Revision ID: abentley@panoramicfeedback.com-20060322221819-0e627e73d1232926
Added zap command

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from shutil import rmtree
2
2
 
3
 
from bzrlib.branch import Branch
4
3
from bzrlib.errors import NoWorkingTree, NotLocalUrl, NotBranchError
 
4
from bzrlib.delta import compare_trees
5
5
from bzrlib.workingtree import WorkingTree
6
6
 
7
 
from errors import (NotCheckout, UncommittedCheckout, ParentMissingRevisions,
8
 
                    NoParent)
9
 
 
10
 
 
11
 
def zap(path, remove_branch=False):
 
7
from errors import NotCheckout, UncommittedCheckout
 
8
 
 
9
 
 
10
def zap(path):
12
11
    try:
13
12
        wt = WorkingTree.open(path)
14
13
    except (NoWorkingTree, NotBranchError):
15
14
        raise NotCheckout(path)
16
15
    tree_base = wt.bzrdir.transport.base
17
 
    branch = wt.branch
18
 
    branch_base = branch.bzrdir.transport.base
 
16
    branch_base = wt.branch.bzrdir.transport.base
19
17
    if tree_base == branch_base:
20
18
        raise NotCheckout(path)
21
 
    delta = wt.changes_from(wt.basis_tree(), want_unchanged=False)
 
19
    delta = compare_trees(wt.basis_tree(), wt, want_unchanged=False)
22
20
    if delta.has_changed():
23
21
        raise UncommittedCheckout()
24
 
    if remove_branch:
25
 
        parent_loc = branch.get_parent()
26
 
        if parent_loc is None:
27
 
            raise NoParent()
28
 
        parent = Branch.open(parent_loc)
29
 
        p_ancestry = parent.repository.get_ancestry(parent.last_revision())
30
 
        if branch.last_revision() not in p_ancestry:
31
 
            raise ParentMissingRevisions(branch.get_parent())
32
22
    rmtree(path)
33
 
    if remove_branch:
34
 
        t = branch.bzrdir.transport
35
 
        while t.base != branch_base:
36
 
            t = t.clone('..')
37
 
        t = t.clone('..')
38
 
        t.delete_tree('')
39
23
 
40
24
 
41
25
def test_suite():
42
26
    import os
43
27
    from unittest import makeSuite
44
 
 
 
28
    
45
29
    from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
46
30
    from bzrlib.branch import BranchReferenceFormat
47
31
    from bzrlib.tests import TestCaseInTempDir
55
39
            BranchReferenceFormat().initialize(checkout, wt.branch)
56
40
            return checkout.create_workingtree()
57
41
 
58
 
        def make_checkout2(self):
59
 
            wt = self.make_checkout()
60
 
            wt2 = wt.branch.bzrdir.sprout('source2').open_workingtree()
61
 
            os.mkdir('checkout2')
62
 
            checkout = BzrDirMetaFormat1().initialize('checkout2')
63
 
            BranchReferenceFormat().initialize(checkout, wt2.branch)
64
 
            return checkout.create_workingtree()
65
 
 
66
42
        def test_is_checkout(self):
67
43
            self.assertRaises(NotCheckout, zap, '.')
68
44
            wt = BzrDir.create_standalone_workingtree('.')
73
49
            self.assertIs(True, os.path.exists('checkout'))
74
50
            zap('checkout')
75
51
            self.assertIs(False, os.path.exists('checkout'))
76
 
            self.assertIs(True, os.path.exists('source'))
77
 
 
78
 
        def test_zap_branch(self):
79
 
            self.make_checkout2()
80
 
            base = WorkingTree.open('checkout').branch.base
81
 
            self.assertIs(True, os.path.exists('checkout'))
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'))
88
52
 
89
53
        def test_checks_modified(self):
90
54
            checkout = self.make_checkout()