~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/uncommit.py

  • Committer: John Arbash Meinel
  • Date: 2008-05-28 23:20:33 UTC
  • mto: This revision was merged to the branch mainline in revision 3458.
  • Revision ID: john@arbash-meinel.com-20080528232033-cx3l3yg845udklps
Bring back always in the form of 'override'.
Change the functions so that they are compatible with the released
definition, and just allow callers to supply override=False
if they want the new behavior.

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 import (
 
24
    errors,
 
25
    revision as _mod_revision,
 
26
    )
 
27
from bzrlib.branch import Branch
22
28
from bzrlib.errors import BoundBranchOutOfDate
23
29
 
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
 
 
31
 
def uncommit(branch, dry_run=False, verbose=False, revno=None, tree=None):
 
30
 
 
31
def uncommit(branch, dry_run=False, verbose=False, revno=None, tree=None,
 
32
             local=False):
32
33
    """Remove the last revision from the supplied branch.
33
34
 
34
35
    :param dry_run: Don't actually change anything
35
36
    :param verbose: Print each step as you do it
36
37
    :param revno: Remove back to this revision
37
38
    """
38
 
    from bzrlib.atomicfile import AtomicFile
39
39
    unlockable = []
40
40
    try:
41
41
        if tree is not None:
45
45
        branch.lock_write()
46
46
        unlockable.append(branch)
47
47
 
48
 
        master = branch.get_master_branch()
49
 
        if master is not None:
50
 
            master.lock_write()
51
 
            unlockable.append(master)
 
48
        pending_merges = []
 
49
        if tree is not None:
 
50
            pending_merges = tree.get_parent_ids()[1:]
 
51
 
 
52
        if local:
 
53
            master = None
 
54
            if branch.get_bound_location() is None:
 
55
                raise errors.LocalRequiresBoundBranch()
 
56
        else:
 
57
            master = branch.get_master_branch()
 
58
            if master is not None:
 
59
                master.lock_write()
 
60
                unlockable.append(master)
52
61
        rh = branch.revision_history()
53
62
        if master is not None and rh[-1] != master.last_revision():
54
63
            raise BoundBranchOutOfDate(branch, master)
55
64
        if revno is None:
56
65
            revno = len(rh)
 
66
        old_revno, old_tip = branch.last_revision_info()
 
67
        new_revno = revno -1
57
68
 
58
69
        files_to_remove = []
59
70
        for r in range(revno-1, len(rh)):
60
71
            rev_id = rh.pop()
 
72
            # NB: performance would be better using the revision graph rather
 
73
            # than the whole revision.
 
74
            rev = branch.repository.get_revision(rev_id)
 
75
            # When we finish popping off the pending merges, we want
 
76
            # them to stay in the order that they used to be.
 
77
            # but we pop from the end, so reverse the order, and
 
78
            # then get the order right at the end
 
79
            pending_merges.extend(reversed(rev.parent_ids[1:]))
61
80
            if verbose:
62
81
                print 'Removing revno %d: %s' % (len(rh)+1, rev_id)
63
82
 
64
 
 
65
83
        # Committing before we start removing files, because
66
84
        # once we have removed at least one, all the rest are invalid.
67
85
        if not dry_run:
68
86
            if master is not None:
69
87
                master.set_revision_history(rh)
70
88
            branch.set_revision_history(rh)
 
89
            new_tip = _mod_revision.ensure_null(branch.last_revision())
 
90
            if master is None:
 
91
                hook_local = None
 
92
                hook_master = branch
 
93
            else:
 
94
                hook_local = branch
 
95
                hook_master = master
 
96
            for hook in Branch.hooks['post_uncommit']:
 
97
                hook_new_tip = new_tip
 
98
                if hook_new_tip == _mod_revision.NULL_REVISION:
 
99
                    hook_new_tip = None
 
100
                hook(hook_local, hook_master, old_revno, old_tip, new_revno,
 
101
                     hook_new_tip)
71
102
            if tree is not None:
72
 
                tree.set_last_revision(branch.last_revision())
 
103
                if not _mod_revision.is_null(new_tip):
 
104
                    parents = [new_tip]
 
105
                else:
 
106
                    parents = []
 
107
                parents.extend(reversed(pending_merges))
 
108
                tree.set_parent_ids(parents)
73
109
    finally:
74
110
        for item in reversed(unlockable):
75
111
            item.unlock()