/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.5.17 by John Arbash Meinel
adding apply-changset, plus more meta information.
1
#!/usr/bin/env python
2
"""\
3
This contains the apply changset function for bzr
4
"""
5
6
import bzrlib
0.5.67 by John Arbash Meinel
Working on apply_changeset
7
import os
8
9
def _install_info(branch, cset_info, cset_tree, cset_inv):
10
    """Make sure that there is a text entry for each 
11
    file in the changeset.
12
    """
13
    from bzrlib.xml import pack_xml
14
    from cStringIO import StringIO
15
16
    # First, install all required texts
17
    for file_id, text_id in cset_info.text_ids.iteritems():
18
        if text_id not in branch.text_store:
19
            branch.text_store.add(cset_tree.get_file(file_id), text_id)
20
21
    # Now install the final inventory
22
    if cset_info.target not in branch.inventory_store:
23
        # bzrlib.commit uses a temporary file, but store.add
24
        # reads in the entire file anyway
25
        sio = StringIO()
26
        pack_xml(cset_inv, sio)
27
        branch.inventory_store.add(sio.getvalue(), cset_info.target)
28
        del sio
29
30
    # Now that we have installed the inventory and texts
31
    # install the revision entries.
32
    for rev in cset_info.real_revisions:
33
        if rev.revision_id not in branch.revision_store:
34
            sio = StringIO()
35
            pack_xml(rev, sio)
36
            branch.inventory_store.add(sio.getvalue(), rev.revision_id)
37
            del sio
38
39
def merge_revs(branch, rev_base, rev_other,
40
        ignore_zero=False, check_clean=True):
41
    """This will merge the tree of rev_other into 
42
    the working tree of branch using the base given by rev_base.
43
    All the revision XML should be inside branch.
44
    """
45
    import tempfile, shutil
46
    from bzrlib.merge import merge_inner, MergeTree
47
    from bzrlib.errors import BzrCommandError
48
49
    tempdir = tempfile.mkdtemp(prefix='bzr-')
50
    try:
51
        if check_clean:
52
            from bzrlib.diff import compare_trees
53
            changes = compare_trees(branch.working_tree(), 
54
                                    branch.basis_tree(), False)
55
56
            if changes.has_changed():
57
                raise BzrCommandError("Working tree has uncommitted changes.")
58
59
        other_dir = os.path.join(tempdir, 'other')
60
        other_tree = MergeTree(branch.revision_tree(rev_other), other_dir)
61
62
        base_dir = os.path.join(tempdir, 'base')
63
        base_tree = MergeTree(branch.revision_tree(rev_base), base_dir)
64
65
        merge_inner(branch, other_tree, base_tree, tempdir,
66
            ignore_zero=ignore_zero)
67
    finally:
68
        shutil.rmtree(tempdir)
69
0.5.17 by John Arbash Meinel
adding apply-changset, plus more meta information.
70
0.5.69 by John Arbash Meinel
Applying patch from Robey Pointer to clean up apply_changeset.
71
def apply_changeset(branch, from_file, reverse=False, auto_commit=False):
0.5.17 by John Arbash Meinel
adding apply-changset, plus more meta information.
72
    import sys, read_changeset
73
0.5.69 by John Arbash Meinel
Applying patch from Robey Pointer to clean up apply_changeset.
74
    if reverse:
75
        raise Exception('reverse not implemented yet')
76
        
0.5.67 by John Arbash Meinel
Working on apply_changeset
77
    cset_info, cset_tree, cset_inv = \
0.5.69 by John Arbash Meinel
Applying patch from Robey Pointer to clean up apply_changeset.
78
            read_changeset.read_changeset(from_file, branch)
0.5.67 by John Arbash Meinel
Working on apply_changeset
79
80
    _install_info(branch, cset_info, cset_tree, cset_inv)
81
82
    # We could technically optimize more, by using the ChangesetTree
83
    # we already have in memory, but after installing revisions
84
    # this code can work the way merge should work in the
85
    # future.
86
    #
87
    # TODO:
88
    #   This shouldn't use the base of the changeset as the base
89
    #   for the merge, the merge code should pick the best merge
90
    #   based on the ancestry of both trees.
91
    #
92
    merge_revs(branch, cset_info.base, cset_info.target)
0.5.17 by John Arbash Meinel
adding apply-changset, plus more meta information.
93
94
    if auto_commit:
95
        from bzrlib.commit import commit
0.5.67 by John Arbash Meinel
Working on apply_changeset
96
97
        # When merging, if the revision to be merged has a parent
98
        # of the current revision, then it can be installed
99
        # directly.
100
        #
101
        # TODO: 
102
        #   There is actually a slightly stronger statement
103
        #   whereby if the current revision is in the ancestry
104
        #   of the merged revisions, it doesn't need to be the
105
        #   immediate ancestry, but that requires searching
106
        #   a potentially branching history.
107
        #
108
        target_has_parent = False
109
        target_rev = branch.get_revision(cset_info.target)
110
        lastrev_id = branch.last_patch()
111
        for parent in target_rev.parents:
112
            if parent.revision_id == lastrev_id:
113
                target_has_parent = True
114
115
        if target_has_parent:
116
            branch.append_revision(target_rev.revision_id)
117
        else:
118
            print '** Could not auto-commit.'
0.5.17 by John Arbash Meinel
adding apply-changset, plus more meta information.
119