~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/uncommit.py

  • Committer: Robert Collins
  • Date: 2007-03-08 04:06:06 UTC
  • mfrom: (2323.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 2442.
  • Revision ID: robertc@robertcollins.net-20070308040606-84gsniv56huiyjt4
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
"""Remove the last revision from the history of the current branch.
18
 
"""
 
17
"""Remove the last revision from the history of the current branch."""
 
18
 
 
19
# TODO: make the guts of this methods on tree, branch.
19
20
 
20
21
import os
21
 
import bzrlib
 
22
 
 
23
from bzrlib.branch import Branch
22
24
from bzrlib.errors import BoundBranchOutOfDate
23
25
 
24
 
def test_remove(filename):
25
 
    if os.path.exists(filename):
26
 
        os.remove(filename)
27
 
    else:
28
 
        print '* file does not exist: %r' % filename
29
 
 
30
26
 
31
27
def uncommit(branch, dry_run=False, verbose=False, revno=None, tree=None):
32
28
    """Remove the last revision from the supplied branch.
35
31
    :param verbose: Print each step as you do it
36
32
    :param revno: Remove back to this revision
37
33
    """
38
 
    from bzrlib.atomicfile import AtomicFile
39
34
    unlockable = []
40
35
    try:
41
36
        if tree is not None:
45
40
        branch.lock_write()
46
41
        unlockable.append(branch)
47
42
 
 
43
        pending_merges = []
 
44
        if tree is not None:
 
45
            pending_merges = tree.get_parent_ids()[1:]
 
46
 
48
47
        master = branch.get_master_branch()
49
48
        if master is not None:
50
49
            master.lock_write()
54
53
            raise BoundBranchOutOfDate(branch, master)
55
54
        if revno is None:
56
55
            revno = len(rh)
 
56
        old_revno, old_tip = branch.last_revision_info()
 
57
        new_revno = revno -1
57
58
 
58
59
        files_to_remove = []
59
60
        for r in range(revno-1, len(rh)):
60
61
            rev_id = rh.pop()
 
62
            # NB: performance would be better using the revision graph rather
 
63
            # than the whole revision.
 
64
            rev = branch.repository.get_revision(rev_id)
 
65
            # When we finish popping off the pending merges, we want
 
66
            # them to stay in the order that they used to be.
 
67
            # but we pop from the end, so reverse the order, and
 
68
            # then get the order right at the end
 
69
            pending_merges.extend(reversed(rev.parent_ids[1:]))
61
70
            if verbose:
62
71
                print 'Removing revno %d: %s' % (len(rh)+1, rev_id)
63
72
 
64
 
 
65
73
        # Committing before we start removing files, because
66
74
        # once we have removed at least one, all the rest are invalid.
67
75
        if not dry_run:
68
76
            if master is not None:
69
77
                master.set_revision_history(rh)
70
78
            branch.set_revision_history(rh)
 
79
            new_tip = branch.last_revision()
 
80
            if master is None:
 
81
                hook_local = None
 
82
                hook_master = branch
 
83
            else:
 
84
                hook_local = branch
 
85
                hook_master = master
 
86
            for hook in Branch.hooks['post_uncommit']:
 
87
                hook(hook_local, hook_master, old_revno, old_tip, new_revno,
 
88
                    new_tip)
71
89
            if tree is not None:
72
 
                tree.set_last_revision(branch.last_revision())
 
90
                if new_tip is not None:
 
91
                    parents = [new_tip]
 
92
                else:
 
93
                    parents = []
 
94
                parents.extend(reversed(pending_merges))
 
95
                tree.set_parent_ids(parents)
73
96
    finally:
74
97
        for item in reversed(unlockable):
75
98
            item.unlock()