/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to uncommit.py

  • Committer: John Arbash Meinel
  • Date: 2005-06-27 21:57:33 UTC
  • mto: (1185.31.24 bzr-jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1512.
  • Revision ID: john@arbash-meinel.com-20050627215733-f4419eee3c33dd37
Updated for new log_formatter.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
import os
7
7
import bzrlib
8
 
from bzrlib.errors import BoundBranchOutOfDate
 
8
 
 
9
try:
 
10
    set
 
11
except NameError:
 
12
    from sets import Set as set
9
13
 
10
14
def test_remove(filename):
11
15
    if os.path.exists(filename):
14
18
        print '* file does not exist: %r' % filename
15
19
 
16
20
 
17
 
def uncommit(branch, dry_run=False, verbose=False, revno=None, tree=None):
 
21
def uncommit(branch, remove_files=False,
 
22
        dry_run=False, verbose=False):
18
23
    """Remove the last revision from the supplied branch.
19
24
 
20
 
    :param dry_run: Don't actually change anything
21
 
    :param verbose: Print each step as you do it
22
 
    :param revno: Remove back to this revision
 
25
    :param remove_files: If True, remove files from the stores
 
26
        as well.
23
27
    """
24
28
    from bzrlib.atomicfile import AtomicFile
25
 
    unlockable = []
26
 
    try:
27
 
        if tree is not None:
28
 
            tree.lock_write()
29
 
            unlockable.append(tree)
30
 
        
31
 
        branch.lock_write()
32
 
        unlockable.append(branch)
33
 
 
34
 
        master = branch.get_master_branch()
35
 
        if master is not None:
36
 
            master.lock_write()
37
 
            unlockable.append(master)
38
 
        rh = branch.revision_history()
39
 
        if master is not None and rh[-1] != master.last_revision():
40
 
            raise BoundBranchOutOfDate(branch, master)
41
 
        if revno is None:
42
 
            revno = len(rh)
43
 
 
 
29
    rh = branch.revision_history()
 
30
    rev_id = rh.pop()
 
31
    rev = branch.get_revision(rev_id)
 
32
    inv = branch.get_inventory(rev.inventory_id)
 
33
    inv_prev = []
 
34
    for p in rev.parents:
 
35
        inv_prev.append(branch.get_revision_inventory(p.revision_id))
 
36
 
 
37
    new_rev_history = AtomicFile(branch.controlfilename('revision-history'))
 
38
    new_rev_history.write('\n'.join(rh))
 
39
    # Committing now, because even if we fail to remove all files
 
40
    # once we have removed at least one, all the rest are invalid.
 
41
    if not dry_run:
 
42
        new_rev_history.commit()
 
43
    else:
 
44
        new_rev_history.abort()
 
45
 
 
46
    if remove_files:
 
47
        # Figure out what text-store entries are new
44
48
        files_to_remove = []
45
 
        for r in range(revno-1, len(rh)):
46
 
            rev_id = rh.pop()
47
 
            if verbose:
48
 
                print 'Removing revno %d: %s' % (len(rh)+1, rev_id)
49
 
 
50
 
 
51
 
        # Committing before we start removing files, because
52
 
        # once we have removed at least one, all the rest are invalid.
 
49
        for file_id in inv:
 
50
            ie = inv[file_id]
 
51
            if not hasattr(ie, 'text_id'):
 
52
                continue
 
53
            for other_inv in inv_prev:
 
54
                if file_id in other_inv:
 
55
                    other_ie = other_inv[file_id]
 
56
                    if other_ie.text_id == ie.text_id:
 
57
                        break
 
58
            else:
 
59
                # None of the previous ancestors used
 
60
                # the same inventory
 
61
                files_to_remove.append(branch.controlfilename(['text-store',
 
62
                    ie.text_id + '.gz']))
 
63
        rev_file = branch.controlfilename(['revision-store',
 
64
                rev_id + '.gz'])
 
65
        files_to_remove.append(rev_file)
 
66
        inv_file = branch.controlfilename(['inventory-store',
 
67
                rev.inventory_id + '.gz'])
 
68
        files_to_remove.append(inv_file)
 
69
 
 
70
        if verbose:
 
71
            print 'Removing files:'
 
72
            for f in files_to_remove:
 
73
                print '\t%s' % branch.relpath(f)
 
74
 
53
75
        if not dry_run:
54
 
            if master is not None:
55
 
                master.set_revision_history(rh)
56
 
            branch.set_revision_history(rh)
57
 
            if tree is not None:
58
 
                tree.set_last_revision(branch.last_revision())
59
 
    finally:
60
 
        for item in reversed(unlockable):
61
 
            item.unlock()
 
76
            # Actually start removing files
 
77
            for f in files_to_remove:
 
78
                test_remove(f)
 
79