/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
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
9
from bzrlib.trace import mutter, warning
10
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
11
def _install_info(branch, cset_info, cset_tree):
0.5.67 by John Arbash Meinel
Working on apply_changeset
12
    """Make sure that there is a text entry for each 
13
    file in the changeset.
14
    """
15
    from bzrlib.xml import pack_xml
16
    from cStringIO import StringIO
17
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
18
    inv = cset_tree.inventory
0.5.67 by John Arbash Meinel
Working on apply_changeset
19
    # First, install all required texts
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
20
    for path, ie in inv.iter_entries():
21
        if ie.text_id is not None and ie.text_id not in branch.text_store:
0.5.82 by John Arbash Meinel
Lots of changes, changing separators, updating tests, updated ChangesetTree to include text_ids
22
            branch.text_store.add(cset_tree.get_file(ie.file_id), ie.text_id)
0.5.67 by John Arbash Meinel
Working on apply_changeset
23
24
    # Now install the final inventory
25
    if cset_info.target not in branch.inventory_store:
26
        # bzrlib.commit uses a temporary file, but store.add
27
        # reads in the entire file anyway
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
28
        if cset_info.target in branch.inventory_store:
29
            warning('Target inventory already exists in destination.')
30
        else:
31
            sio = StringIO()
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
32
            pack_xml(inv, sio)
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
33
            branch.inventory_store.add(sio.getvalue(), cset_info.target)
34
            del sio
0.5.67 by John Arbash Meinel
Working on apply_changeset
35
36
    # Now that we have installed the inventory and texts
37
    # install the revision entries.
38
    for rev in cset_info.real_revisions:
39
        if rev.revision_id not in branch.revision_store:
40
            sio = StringIO()
41
            pack_xml(rev, sio)
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
42
            branch.revision_store.add(sio.getvalue(), rev.revision_id)
0.5.67 by John Arbash Meinel
Working on apply_changeset
43
            del sio
44
45
def merge_revs(branch, rev_base, rev_other,
46
        ignore_zero=False, check_clean=True):
47
    """This will merge the tree of rev_other into 
48
    the working tree of branch using the base given by rev_base.
49
    All the revision XML should be inside branch.
50
    """
51
    import tempfile, shutil
52
    from bzrlib.merge import merge_inner, MergeTree
53
    from bzrlib.errors import BzrCommandError
54
55
    tempdir = tempfile.mkdtemp(prefix='bzr-')
56
    try:
57
        if check_clean:
58
            from bzrlib.diff import compare_trees
59
            changes = compare_trees(branch.working_tree(), 
60
                                    branch.basis_tree(), False)
61
62
            if changes.has_changed():
63
                raise BzrCommandError("Working tree has uncommitted changes.")
64
65
        other_dir = os.path.join(tempdir, 'other')
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
66
        os.mkdir(other_dir)
0.5.67 by John Arbash Meinel
Working on apply_changeset
67
        other_tree = MergeTree(branch.revision_tree(rev_other), other_dir)
68
69
        base_dir = os.path.join(tempdir, 'base')
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
70
        os.mkdir(base_dir)
0.5.67 by John Arbash Meinel
Working on apply_changeset
71
        base_tree = MergeTree(branch.revision_tree(rev_base), base_dir)
72
73
        merge_inner(branch, other_tree, base_tree, tempdir,
74
            ignore_zero=ignore_zero)
75
    finally:
76
        shutil.rmtree(tempdir)
77
0.5.69 by John Arbash Meinel
Applying patch from Robey Pointer to clean up apply_changeset.
78
def apply_changeset(branch, from_file, reverse=False, auto_commit=False):
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
79
    """Read in a changeset from the given file, and apply it to
80
    the supplied branch.
0.5.86 by John Arbash Meinel
Updated the auto-commit functionality, and adding to pending-merges, more testing.
81
82
    :return: True if the changeset was automatically committed to the
83
             ancestry, False otherwise.
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
84
    """
0.5.17 by John Arbash Meinel
adding apply-changset, plus more meta information.
85
    import sys, read_changeset
86
0.5.69 by John Arbash Meinel
Applying patch from Robey Pointer to clean up apply_changeset.
87
    if reverse:
88
        raise Exception('reverse not implemented yet')
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
89
90
    cset = read_changeset.read_changeset(from_file, branch)
91
0.5.86 by John Arbash Meinel
Updated the auto-commit functionality, and adding to pending-merges, more testing.
92
    return _apply_cset(branch, cset, reverse=reverse, auto_commit=auto_commit)
0.5.69 by John Arbash Meinel
Applying patch from Robey Pointer to clean up apply_changeset.
93
        
0.5.80 by John Arbash Meinel
Starting to write tests for changeset, discovering some errors as I go.
94
def _apply_cset(branch, cset, reverse=False, auto_commit=False):
95
    """Apply an in-memory changeset to a given branch.
96
    """
97
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
98
    cset_info, cset_tree = cset
0.5.67 by John Arbash Meinel
Working on apply_changeset
99
0.5.83 by John Arbash Meinel
Tests pass. Now ChangesetTree has it's own inventory.
100
    _install_info(branch, cset_info, cset_tree)
0.5.67 by John Arbash Meinel
Working on apply_changeset
101
102
    # We could technically optimize more, by using the ChangesetTree
103
    # we already have in memory, but after installing revisions
104
    # this code can work the way merge should work in the
105
    # future.
106
    #
107
    # TODO:
108
    #   This shouldn't use the base of the changeset as the base
109
    #   for the merge, the merge code should pick the best merge
110
    #   based on the ancestry of both trees.
111
    #
112
    merge_revs(branch, cset_info.base, cset_info.target)
0.5.17 by John Arbash Meinel
adding apply-changset, plus more meta information.
113
0.5.86 by John Arbash Meinel
Updated the auto-commit functionality, and adding to pending-merges, more testing.
114
    auto_committed = False
115
    
116
    # There are 2 cases where I am allowing automatic committing.
117
    # 1) If the final revision has a parent of the current last revision
118
    #    (branch.last_patch() in cset.target.parents)
119
    #    that means that the changeset target has already merged the current
120
    #    tree.
121
    # 2) A cset contains a list of revisions. If the first entry has a parent
122
    #    of branch.last_patch(), then we can start merging there, and add the
123
    #    rest of the revisions. But it gets better. Some of the entries in the
124
    #    list might already be in the revision list, so we keep going until
125
    #    we find the first revision *not* in the list. If it's parent is
126
    #    branch.last_patch(), then we can also append history from there.
127
    #    This second part is a little more controversial, because the cset
128
    #    probably does not include all of the inventories. So you would have
129
    #    entries in branch.revision_history() without an associated inventory.
130
    #    we could just explicitly disable this. But if we had the inventory
131
    #    entries available, it is what 'bzr merge' would do.
132
    #    If we disable this, the target will just show up as a pending_merge
0.5.17 by John Arbash Meinel
adding apply-changset, plus more meta information.
133
    if auto_commit:
0.5.67 by John Arbash Meinel
Working on apply_changeset
134
        # When merging, if the revision to be merged has a parent
135
        # of the current revision, then it can be installed
136
        # directly.
137
        #
138
        # TODO: 
139
        #   There is actually a slightly stronger statement
140
        #   whereby if the current revision is in the ancestry
141
        #   of the merged revisions, it doesn't need to be the
142
        #   immediate ancestry, but that requires searching
143
        #   a potentially branching history.
144
        #
0.5.86 by John Arbash Meinel
Updated the auto-commit functionality, and adding to pending-merges, more testing.
145
        rh = branch.revision_history()
146
        revs_to_merge = None
147
        found_parent = False
148
        if len(rh) == 0 and len(cset_info.real_revisions[0].parents) == 0:
149
            found_parent = True
150
            revs_to_merge = cset_info.real_revisions
151
        else:
152
            for rev_num, rev in enumerate(cset_info.real_revisions):
153
                if rev.revision_id not in rh:
154
                    for parent in rev.parents:
155
                        if parent.revision_id == rh[-1]:
156
                            found_parent = True
157
                    if found_parent:
158
                        # All revisions up until now already
159
                        # existed in the target history
160
                        # and this last one is a child of the
161
                        # last entry in the history.
162
                        # so we can add the rest
163
                        revs_to_merge = cset_info.real_revisions[rev_num:]
164
                    # Even if we don't find anything, we need to
165
                    # stop here
166
                    break
167
168
        if found_parent:
169
            rev_ids = [r.revision_id for r in revs_to_merge]
170
            branch.append_revision(*rev_ids)
171
            auto_committed = True
172
        else:
173
            # We can also merge if the *last* revision has an
174
            # appropriate parent.
175
            target_has_parent = False
176
            target_rev = branch.get_revision(cset_info.target)
177
            lastrev_id = branch.last_patch()
178
            for parent in target_rev.parents:
179
                if parent.revision_id == lastrev_id:
180
                    target_has_parent = True
181
182
            if target_has_parent:
183
                branch.append_revision(target_rev.revision_id)
184
            else:
185
                print '** Could not auto-commit.'
186
187
    if not auto_committed:
188
        branch.add_pending_merge(cset_info.target)
189
190
    return auto_committed
0.5.17 by John Arbash Meinel
adding apply-changset, plus more meta information.
191