/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
try:
10
    set
11
except NameError:
12
    from sets import Set as set
13
14
def test_remove(filename):
15
    if os.path.exists(filename):
16
        os.remove(filename)
17
    else:
18
        print '* file does not exist: %r' % filename
19
20
21
def uncommit(branch, remove_files=False,
0.3.6 by John Arbash Meinel
Adding ability to revert back to earlier revisions.
22
        dry_run=False, verbose=False, revno=None):
0.3.1 by John Arbash Meinel
Creating a plugin for uncommitting the last revision.
23
    """Remove the last revision from the supplied branch.
24
25
    :param remove_files: If True, remove files from the stores
26
        as well.
27
    """
28
    from bzrlib.atomicfile import AtomicFile
29
    rh = branch.revision_history()
0.3.6 by John Arbash Meinel
Adding ability to revert back to earlier revisions.
30
    if revno is None:
31
        revno = len(rh)
0.3.1 by John Arbash Meinel
Creating a plugin for uncommitting the last revision.
32
0.3.6 by John Arbash Meinel
Adding ability to revert back to earlier revisions.
33
    files_to_remove = []
0.3.1 by John Arbash Meinel
Creating a plugin for uncommitting the last revision.
34
    new_rev_history = AtomicFile(branch.controlfilename('revision-history'))
0.3.6 by John Arbash Meinel
Adding ability to revert back to earlier revisions.
35
    for r in range(revno-1, len(rh)):
36
        rev_id = rh.pop()
37
        if verbose:
38
            print 'Removing revno %d: %s' % (len(rh)+1, rev_id)
39
        rev = branch.get_revision(rev_id)
40
        inv = branch.get_inventory(rev.inventory_id)
41
        inv_prev = []
42
        for p in rev.parents:
43
            inv_prev.append(branch.get_revision_inventory(p.revision_id))
44
45
        if remove_files:
46
            # Figure out what text-store entries are new
47
48
            # In the future, when we have text_version instead of
49
            # text_id, we can just check to see if the text_version
50
            # equals the current revision id.
51
            for file_id in inv:
52
                ie = inv[file_id]
53
                if not hasattr(ie, 'text_id'):
54
                    continue
55
                for other_inv in inv_prev:
56
                    if file_id in other_inv:
57
                        other_ie = other_inv[file_id]
58
                        if other_ie.text_id == ie.text_id:
59
                            break
60
                else:
61
                    # None of the previous ancestors used
62
                    # the same inventory
63
                    files_to_remove.append(branch.controlfilename(['text-store',
64
                        ie.text_id + '.gz']))
65
            rev_file = branch.controlfilename(['revision-store',
66
                    rev_id + '.gz'])
67
            files_to_remove.append(rev_file)
68
            inv_file = branch.controlfilename(['inventory-store',
69
                    rev.inventory_id + '.gz'])
70
            files_to_remove.append(inv_file)
71
72
    if verbose:
73
        print 'Removing files:'
74
        for f in files_to_remove:
75
            print '\t%s' % branch.relpath(f)
76
0.3.1 by John Arbash Meinel
Creating a plugin for uncommitting the last revision.
77
    new_rev_history.write('\n'.join(rh))
0.3.6 by John Arbash Meinel
Adding ability to revert back to earlier revisions.
78
79
    # Committing before we start removing files, because
0.3.1 by John Arbash Meinel
Creating a plugin for uncommitting the last revision.
80
    # once we have removed at least one, all the rest are invalid.
81
    if not dry_run:
82
        new_rev_history.commit()
0.3.6 by John Arbash Meinel
Adding ability to revert back to earlier revisions.
83
        if remove_files:
0.3.1 by John Arbash Meinel
Creating a plugin for uncommitting the last revision.
84
            # Actually start removing files
85
            for f in files_to_remove:
86
                test_remove(f)
87
0.3.6 by John Arbash Meinel
Adding ability to revert back to earlier revisions.
88
    else:
89
        new_rev_history.abort()
90
91