~bzr-pqm/bzr/bzr.dev

0.3.1 by John Arbash Meinel
Creating a plugin for uncommitting the last revision.
1
#!/usr/bin/env python
2
"""\
3
Remove the last revision from the history of the current branch.
4
"""
5
6
import os
7
import bzrlib
8
9
def test_remove(filename):
10
    if os.path.exists(filename):
11
        os.remove(filename)
12
    else:
13
        print '* file does not exist: %r' % filename
14
15
1558.1.12 by Aaron Bentley
Got uncommit working properly with checkouts
16
def uncommit(branch, dry_run=False, verbose=False, revno=None, tree=None):
0.3.1 by John Arbash Meinel
Creating a plugin for uncommitting the last revision.
17
    """Remove the last revision from the supplied branch.
18
1185.62.10 by John Arbash Meinel
Removed --all from bzr uncommit, it was broken anyway.
19
    :param dry_run: Don't actually change anything
20
    :param verbose: Print each step as you do it
21
    :param revno: Remove back to this revision
0.3.1 by John Arbash Meinel
Creating a plugin for uncommitting the last revision.
22
    """
23
    from bzrlib.atomicfile import AtomicFile
24
    rh = branch.revision_history()
0.3.6 by John Arbash Meinel
Adding ability to revert back to earlier revisions.
25
    if revno is None:
26
        revno = len(rh)
0.3.1 by John Arbash Meinel
Creating a plugin for uncommitting the last revision.
27
0.3.6 by John Arbash Meinel
Adding ability to revert back to earlier revisions.
28
    files_to_remove = []
1185.65.13 by Robert Collins
Merge from integration
29
    new_rev_history = AtomicFile(branch.control_files.controlfilename('revision-history'))
0.3.6 by John Arbash Meinel
Adding ability to revert back to earlier revisions.
30
    for r in range(revno-1, len(rh)):
31
        rev_id = rh.pop()
32
        if verbose:
33
            print 'Removing revno %d: %s' % (len(rh)+1, rev_id)
34
0.3.1 by John Arbash Meinel
Creating a plugin for uncommitting the last revision.
35
    new_rev_history.write('\n'.join(rh))
0.3.6 by John Arbash Meinel
Adding ability to revert back to earlier revisions.
36
37
    # Committing before we start removing files, because
0.3.1 by John Arbash Meinel
Creating a plugin for uncommitting the last revision.
38
    # once we have removed at least one, all the rest are invalid.
39
    if not dry_run:
40
        new_rev_history.commit()
1558.1.12 by Aaron Bentley
Got uncommit working properly with checkouts
41
        if tree is not None:
42
            tree.set_last_revision(branch.last_revision())
0.3.6 by John Arbash Meinel
Adding ability to revert back to earlier revisions.
43
    else:
44
        new_rev_history.abort()
45
46