/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.436.2 by Jelmer Vernooij
Add stubs for testsuite, rebase-continue and rebase-abort commands.
1
# Copyright (C) 2006-2007 by Jelmer Vernooij
2
# 
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0.436.16 by Jelmer Vernooij
Some more work on maptree.
16
"""Rebase."""
0.436.3 by Jelmer Vernooij
Fill in commands.
17
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
18
from bzrlib.config import Config
0.436.16 by Jelmer Vernooij
Some more work on maptree.
19
from bzrlib.errors import BzrError, NoSuchFile, UnknownFormatError
0.436.4 by Jelmer Vernooij
Add some tests.
20
from bzrlib.generate_ids import gen_revision_id
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
21
from bzrlib import osutils
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
22
from bzrlib.revision import NULL_REVISION
0.436.4 by Jelmer Vernooij
Add some tests.
23
from bzrlib.trace import mutter
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
24
import bzrlib.ui as ui
0.436.4 by Jelmer Vernooij
Add some tests.
25
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
26
from maptree import MapTree, map_file_ids
0.436.38 by Jelmer Vernooij
Handle directories in revert.
27
import os
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
28
0.436.3 by Jelmer Vernooij
Fill in commands.
29
REBASE_PLAN_FILENAME = 'rebase-plan'
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
30
REBASE_CURRENT_REVID_FILENAME = 'rebase-current'
0.436.4 by Jelmer Vernooij
Add some tests.
31
REBASE_PLAN_VERSION = 1
0.436.27 by Jelmer Vernooij
Note revision property 'rebase-of', add explanation of use of pregenerated revision ids.
32
REVPROP_REBASE_OF = 'rebase-of'
0.436.3 by Jelmer Vernooij
Fill in commands.
33
34
def rebase_plan_exists(wt):
35
    """Check whether there is a rebase plan present.
36
37
    :param wt: Working tree for which to check.
38
    :return: boolean
39
    """
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
40
    try:
41
        return wt._control_files.get(REBASE_PLAN_FILENAME).read() != ''
42
    except NoSuchFile:
43
        return False
0.436.3 by Jelmer Vernooij
Fill in commands.
44
45
46
def read_rebase_plan(wt):
47
    """Read a rebase plan file.
48
49
    :param wt: Working Tree for which to write the plan.
0.436.4 by Jelmer Vernooij
Add some tests.
50
    :return: Tuple with last revision info and replace map.
0.436.3 by Jelmer Vernooij
Fill in commands.
51
    """
52
    text = wt._control_files.get(REBASE_PLAN_FILENAME).read()
53
    if text == '':
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
54
        raise NoSuchFile(REBASE_PLAN_FILENAME)
0.436.4 by Jelmer Vernooij
Add some tests.
55
    return unmarshall_rebase_plan(text)
56
57
58
def write_rebase_plan(wt, replace_map):
0.436.3 by Jelmer Vernooij
Fill in commands.
59
    """Write a rebase plan file.
60
61
    :param wt: Working Tree for which to write the plan.
0.436.4 by Jelmer Vernooij
Add some tests.
62
    :param replace_map: Replace map (old revid -> (new revid, new parents))
0.436.3 by Jelmer Vernooij
Fill in commands.
63
    """
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
64
    wt._control_files.put_utf8(REBASE_PLAN_FILENAME, 
65
            marshall_rebase_plan(wt.branch.last_revision_info(), replace_map))
0.436.3 by Jelmer Vernooij
Fill in commands.
66
67
68
def remove_rebase_plan(wt):
69
    """Remove a rebase plan file.
70
71
    :param wt: Working Tree for which to remove the plan.
72
    """
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
73
    wt._control_files.put_utf8(REBASE_PLAN_FILENAME, '')
0.436.3 by Jelmer Vernooij
Fill in commands.
74
75
0.436.4 by Jelmer Vernooij
Add some tests.
76
def marshall_rebase_plan(last_rev_info, replace_map):
0.436.3 by Jelmer Vernooij
Fill in commands.
77
    """Marshall a rebase plan.
78
79
    :param last_rev_info: Last revision info tuple.
0.436.4 by Jelmer Vernooij
Add some tests.
80
    :param replace_map: Replace map (old revid -> (new revid, new parents))
0.436.3 by Jelmer Vernooij
Fill in commands.
81
    :return: string
82
    """
0.436.4 by Jelmer Vernooij
Add some tests.
83
    ret = "# Bazaar rebase plan %d\n" % REBASE_PLAN_VERSION
84
    ret += "%d %s\n" % last_rev_info
85
    for oldrev in replace_map:
86
        (newrev, newparents) = replace_map[oldrev]
87
        ret += "%s %s" % (oldrev, newrev) + \
88
            "".join([" %s" % p for p in newparents]) + "\n"
89
    return ret
90
91
92
def unmarshall_rebase_plan(text):
0.436.3 by Jelmer Vernooij
Fill in commands.
93
    """Unmarshall a rebase plan.
94
95
    :param text: Text to parse
0.436.4 by Jelmer Vernooij
Add some tests.
96
    :return: Tuple with last revision info, replace map.
0.436.3 by Jelmer Vernooij
Fill in commands.
97
    """
0.436.4 by Jelmer Vernooij
Add some tests.
98
    lines = text.split('\n')
99
    # Make sure header is there
100
    if lines[0] != "# Bazaar rebase plan %d" % REBASE_PLAN_VERSION:
101
        raise UnknownFormatError(lines[0])
102
103
    pts = lines[1].split(" ", 1)
104
    last_revision_info = (int(pts[0]), pts[1])
105
    replace_map = {}
106
    for l in lines[2:]:
107
        if l == "":
108
            # Skip empty lines
109
            continue
110
        pts = l.split(" ")
111
        replace_map[pts[0]] = (pts[1], pts[2:])
112
    return (last_revision_info, replace_map)
113
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
114
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
115
def regenerate_default_revid(repository, revid):
116
    rev = repository.get_revision(revid)
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
117
    return gen_revision_id(rev.committer, rev.timestamp)
118
119
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
120
def generate_simple_plan(history, start_revid, onto_revid, 
121
                         get_parents, generate_revid):
0.436.3 by Jelmer Vernooij
Fill in commands.
122
    """Create a simple rebase plan that replays history based 
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
123
    on one revision being replayed on top of another.
0.436.3 by Jelmer Vernooij
Fill in commands.
124
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
125
    :param history: Revision history
126
    :param start_revid: Id of revision at which to start replaying
127
    :param onto_revid: Id of revision on top of which to replay
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
128
    :param get_parents: Function for obtaining the parents of a revision
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
129
    :param generate_revid: Function for generating new revision ids
0.436.3 by Jelmer Vernooij
Fill in commands.
130
0.436.4 by Jelmer Vernooij
Add some tests.
131
    :return: replace map
0.436.3 by Jelmer Vernooij
Fill in commands.
132
    """
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
133
    assert start_revid in history
134
    replace_map = {}
135
    i = history.index(start_revid)
136
    new_parent = onto_revid
137
    for oldrevid in history[i:]: 
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
138
        parents = get_parents(oldrevid)
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
139
        assert len(parents) == 0 or \
140
                parents[0] == history[history.index(oldrevid)-1]
141
        parents[0] = new_parent
0.436.39 by Jelmer Vernooij
Some more refactoring, add test that demonstrates #126743.
142
        newrevid = generate_revid(oldrevid)
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
143
        assert newrevid != oldrevid
144
        replace_map[oldrevid] = (newrevid, parents)
145
        new_parent = newrevid
146
    return replace_map
147
148
0.436.31 by Jelmer Vernooij
Refactor generate_transpose_plan() to not take a repository object but
149
def generate_transpose_plan(graph, renames, get_parents, generate_revid):
150
    """Create a rebase plan that replaces a bunch of revisions
151
    in a revision graph.
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
152
153
    :param graph: Revision graph in which to operate
154
    :param renames: Renames of revision
0.436.31 by Jelmer Vernooij
Refactor generate_transpose_plan() to not take a repository object but
155
    :param get_parents: Function for determining parents
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
156
    :param generate_revid: Function for creating new revision ids
157
    """
158
    replace_map = {}
159
    todo = []
0.436.13 by Jelmer Vernooij
Add progress bar, some optimizations. Make merge type configurable.
160
    children = {}
161
    for r in graph:
162
        if not children.has_key(r):
163
            children[r] = []
164
        for p in graph[r]:
165
            if not children.has_key(p):
166
                children[p] = []
167
            children[p].append(r)
168
0.436.31 by Jelmer Vernooij
Refactor generate_transpose_plan() to not take a repository object but
169
    # todo contains a list of revisions that need to 
170
    # be rewritten
171
    for r in renames:
172
        replace_map[r] = (renames[r], get_parents(renames[r]))
173
        todo.append(r)
174
0.436.13 by Jelmer Vernooij
Add progress bar, some optimizations. Make merge type configurable.
175
    total = len(todo)
176
    processed = set()
177
    i = 0
178
    pb = ui.ui_factory.nested_progress_bar()
179
    try:
180
        while len(todo) > 0:
181
            r = todo.pop()
182
            i += 1
183
            pb.update('determining dependencies', i, total)
184
            # Add entry for them in replace_map
185
            for c in children[r]:
186
                if c in renames:
187
                    continue
188
                if replace_map.has_key(c):
189
                    parents = replace_map[c][1]
190
                else:
0.436.36 by Jelmer Vernooij
Fix compatibility with bzr 0.19.
191
                    parents = list(graph[c])
192
                assert isinstance(parents, list), \
193
                        "Expected list of parents, got: %r" % parents
0.436.13 by Jelmer Vernooij
Add progress bar, some optimizations. Make merge type configurable.
194
                # replace r in parents with replace_map[r][0]
195
                if not replace_map[r][0] in parents:
196
                    parents[parents.index(r)] = replace_map[r][0]
0.436.31 by Jelmer Vernooij
Refactor generate_transpose_plan() to not take a repository object but
197
                replace_map[c] = (generate_revid(c), parents)
198
                assert replace_map[c][0] != c
0.436.13 by Jelmer Vernooij
Add progress bar, some optimizations. Make merge type configurable.
199
            processed.add(r)
200
            # Add them to todo[]
0.436.15 by Jelmer Vernooij
Fix inverse bug - needs tests.
201
            todo.extend(filter(lambda x: not x in processed, children[r]))
0.436.13 by Jelmer Vernooij
Add progress bar, some optimizations. Make merge type configurable.
202
    finally:
203
        pb.finished()
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
204
0.436.31 by Jelmer Vernooij
Refactor generate_transpose_plan() to not take a repository object but
205
    # Remove items from the map that already exist
206
    for revid in renames:
207
        if replace_map.has_key(revid):
208
            del replace_map[revid]
209
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
210
    return replace_map
211
212
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
213
def rebase_todo(repository, replace_map):
214
    """Figure out what revisions still need to be rebased.
215
216
    :param repository: Repository that contains the revisions
217
    :param replace_map: Replace map
218
    """
219
    for revid in replace_map:
220
        if not repository.has_revision(replace_map[revid][0]):
221
            yield revid
222
223
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
224
def rebase(repository, replace_map, replay_fn):
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
225
    """Rebase a working tree according to the specified map.
226
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
227
    :param repository: Repository that contains the revisions
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
228
    :param replace_map: Dictionary with revisions to (optionally) rewrite
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
229
    :param merge_fn: Function for replaying a revision
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
230
    """
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
231
    todo = list(rebase_todo(repository, replace_map))
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
232
    dependencies = {}
233
234
    # Figure out the dependencies
235
    for revid in todo:
236
        for p in replace_map[revid][1]:
237
            if repository.has_revision(p):
238
                continue
239
            if not dependencies.has_key(p):
240
                dependencies[p] = []
241
            dependencies[p].append(revid)
242
243
    pb = ui.ui_factory.nested_progress_bar()
0.436.22 by Jelmer Vernooij
Try to improve the progress bar a bit.
244
    total = len(todo)
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
245
    i = 0
246
    try:
247
        while len(todo) > 0:
0.436.22 by Jelmer Vernooij
Try to improve the progress bar a bit.
248
            pb.update('rebase revisions', i, total)
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
249
            i += 1
250
            revid = todo.pop()
251
            (newrevid, newparents) = replace_map[revid]
0.436.19 by Jelmer Vernooij
- Add blackbox tests
252
            if filter(repository.has_revision, newparents) != newparents:
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
253
                # Not all parents present yet, avoid for now
254
                continue
255
            if repository.has_revision(newrevid):
256
                # Was already converted, no need to worry about it again
257
                continue
258
            replay_fn(repository, revid, newrevid, newparents)
259
            assert repository.has_revision(newrevid)
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
260
            assert repository.revision_parents(newrevid) == newparents, \
261
                   "expected parents %r, got %r" % (newparents, 
262
                           repository.revision_parents(newrevid))
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
263
            if dependencies.has_key(newrevid):
264
                todo.extend(dependencies[newrevid])
265
                del dependencies[newrevid]
266
    finally:
267
        pb.finished()
268
        
0.436.19 by Jelmer Vernooij
- Add blackbox tests
269
    #assert all(map(repository.has_revision, 
270
    #           [replace_map[r][0] for r in replace_map]))
0.436.16 by Jelmer Vernooij
Some more work on maptree.
271
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
272
273
0.436.34 by Jelmer Vernooij
Some more tests.
274
def replay_snapshot(repository, oldrevid, newrevid, new_parents, 
0.436.35 by Jelmer Vernooij
Make revid_renames argument mandatory.
275
                    revid_renames):
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
276
    """Replay a commit by simply commiting the same snapshot with different 
277
    parents.
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
278
279
    :param repository: Repository in which the revision is present.
280
    :param oldrevid: Revision id of the revision to copy.
281
    :param newrevid: Revision id of the revision to create.
282
    :param new_parents: Revision ids of the new parent revisions.
0.436.34 by Jelmer Vernooij
Some more tests.
283
    :param revid_renames: Revision id renames for texts.
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
284
    """
285
    assert isinstance(new_parents, list)
0.436.16 by Jelmer Vernooij
Some more work on maptree.
286
    mutter('creating copy %r of %r with new parents %r' % 
287
                               (newrevid, oldrevid, new_parents))
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
288
    oldrev = repository.get_revision(oldrevid)
289
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
290
    revprops = dict(oldrev.properties)
0.436.27 by Jelmer Vernooij
Note revision property 'rebase-of', add explanation of use of pregenerated revision ids.
291
    revprops[REVPROP_REBASE_OF] = oldrevid
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
292
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
293
    builder = repository.get_commit_builder(branch=None, parents=new_parents, 
294
                                  config=Config(),
295
                                  committer=oldrev.committer,
296
                                  timestamp=oldrev.timestamp,
297
                                  timezone=oldrev.timezone,
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
298
                                  revprops=revprops,
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
299
                                  revision_id=newrevid)
300
301
    # Check what new_ie.file_id should be
302
    # use old and new parent inventories to generate new_id map
0.436.16 by Jelmer Vernooij
Some more work on maptree.
303
    fileid_map = map_file_ids(repository, oldrev.parent_ids, new_parents)
304
    oldtree = MapTree(repository.revision_tree(oldrevid), fileid_map)
305
    total = len(oldtree.inventory)
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
306
    pb = ui.ui_factory.nested_progress_bar()
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
307
    i = 0
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
308
    try:
0.436.14 by Jelmer Vernooij
More speed optimizations, deal with already created revisions.
309
        parent_invs = map(repository.get_revision_inventory, new_parents)
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
310
        transact = repository.get_transaction()
0.436.16 by Jelmer Vernooij
Some more work on maptree.
311
        for path, ie in oldtree.inventory.iter_entries():
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
312
            pb.update('upgrading file', i, total)
0.436.34 by Jelmer Vernooij
Some more tests.
313
            ie = ie.copy()
314
            # Either this file was modified last in this revision, 
315
            # in which case it has to be rewritten
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
316
            if ie.revision == oldrevid:
317
                ie.revision = None
318
            else:
0.436.34 by Jelmer Vernooij
Some more tests.
319
                # or it was already there before the commit, in 
320
                # which case the right revision should be used
321
                if revid_renames.has_key(ie.revision):
322
                    ie.revision = revid_renames[ie.revision]
323
                # make sure at least one of the new parents contains 
324
                # the ie.file_id, ie.revision combination
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
325
                if len(filter(lambda inv: ie.file_id in inv and inv[ie.file_id].revision == ie.revision, parent_invs)) == 0:
0.436.34 by Jelmer Vernooij
Some more tests.
326
                    raise ReplayParentsInconsistent(ie.file_id, ie.revision)
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
327
            i += 1
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
328
            builder.record_entry_contents(ie, parent_invs, path, oldtree)
0.436.5 by Jelmer Vernooij
Import change_revision_parent from bzr-svn.
329
    finally:
330
        pb.finished()
331
332
    builder.finish_inventory()
333
    return builder.commit(oldrev.message)
334
335
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
336
def commit_rebase(wt, oldrev, newrevid):
337
    """Commit a rebase.
338
    
339
    :param wt: Mutable tree with the changes.
340
    :param oldrev: Revision info of new revision to commit.
341
    :param newrevid: New revision id."""
342
    assert oldrev.revision_id != newrevid
343
    revprops = dict(oldrev.properties)
0.436.27 by Jelmer Vernooij
Note revision property 'rebase-of', add explanation of use of pregenerated revision ids.
344
    revprops[REVPROP_REBASE_OF] = oldrev.revision_id
0.436.37 by Jelmer Vernooij
Store parents correctly.
345
    wt.commit(message=oldrev.message, timestamp=oldrev.timestamp, 
346
              timezone=oldrev.timezone, revprops=revprops, rev_id=newrevid)
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
347
    write_active_rebase_revid(wt, None)
348
349
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
350
def replay_delta_workingtree(wt, oldrevid, newrevid, newparents, map_ids=False,
351
        merge_type=None):
352
    """Replay a commit in a working tree, with a different base.
353
354
    :param wt: Working tree in which to do the replays.
355
    :param oldrevid: Old revision id
356
    :param newrevid: New revision id
357
    :param newparents: New parent revision ids
358
    :param map_ids: Whether to map file ids from the rebased revision using 
359
        the old and new parent tree file ids.
360
    """
361
    repository = wt.branch.repository
362
    if merge_type is None:
363
        from bzrlib.merge import Merge3Merger
364
        merge_type = Merge3Merger
365
    oldrev = wt.branch.repository.get_revision(oldrevid)
366
    # Make sure there are no conflicts or pending merges/changes 
367
    # in the working tree
368
    if wt.changes_from(wt.basis_tree()).has_changed():
369
        raise BzrError("Working tree has uncommitted changes.")
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
370
    complete_revert(wt, newparents)
0.436.8 by Jelmer Vernooij
Couple more minor fixes.
371
    assert not wt.changes_from(wt.basis_tree()).has_changed()
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
372
373
    oldtree = repository.revision_tree(oldrevid)
374
    basetree = repository.revision_tree(oldrev.parent_ids[0])
375
    if map_ids:
0.436.16 by Jelmer Vernooij
Some more work on maptree.
376
        fileid_map = map_file_ids(repository, oldrev.parent_ids, new_parents)
377
        oldtree = MapTree(repository, oldtree, fileid_map)
378
        basetree = MapTree(repository, basetree, fileid_map)
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
379
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
380
    write_active_rebase_revid(wt, oldrevid)
0.436.36 by Jelmer Vernooij
Fix compatibility with bzr 0.19.
381
    merge = merge_type(working_tree=wt, this_tree=wt, base_tree=basetree,
382
                       other_tree=oldtree)
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
383
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
384
    commit_rebase(wt, oldrev, newrevid)
0.436.8 by Jelmer Vernooij
Couple more minor fixes.
385
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
386
0.436.13 by Jelmer Vernooij
Add progress bar, some optimizations. Make merge type configurable.
387
def workingtree_replay(wt, map_ids=False, merge_type=None):
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
388
    """Returns a function that can replay revisions in wt.
389
390
    :param wt: Working tree in which to do the replays.
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
391
    :param map_ids: Whether to try to map between file ids (False for path-based merge)
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
392
    """
393
    def replay(repository, oldrevid, newrevid, newparents):
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
394
        assert wt.branch.repository == repository
0.436.16 by Jelmer Vernooij
Some more work on maptree.
395
        return replay_delta_workingtree(wt, oldrevid, newrevid, newparents, 
396
                                        merge_type=merge_type)
0.436.6 by Jelmer Vernooij
Add somewhat more complex plan generation function, rebase implementation.
397
    return replay
0.436.7 by Jelmer Vernooij
Add more test, make basic rebase work.
398
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
399
400
def write_active_rebase_revid(wt, revid):
0.436.16 by Jelmer Vernooij
Some more work on maptree.
401
    """Write the id of the revision that is currently being rebased. 
402
403
    :param wt: Working Tree that is being used for the rebase.
404
    :param revid: Revision id to write
405
    """
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
406
    if revid is None:
407
        revid = NULL_REVISION
408
    wt._control_files.put_utf8(REBASE_CURRENT_REVID_FILENAME, revid)
409
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
410
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
411
def read_active_rebase_revid(wt):
0.436.16 by Jelmer Vernooij
Some more work on maptree.
412
    """Read the id of the revision that is currently being rebased.
413
414
    :param wt: Working Tree that is being used for the rebase.
415
    :return: Id of the revision that is being rebased.
416
    """
0.436.9 by Jelmer Vernooij
Add rebase-todo command, fix rebase-continue.
417
    try:
418
        text = wt._control_files.get(REBASE_CURRENT_REVID_FILENAME).read().rstrip("\n")
419
        if text == NULL_REVISION:
420
            return None
421
        return text
422
    except NoSuchFile:
423
        return None
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
424
425
426
def complete_revert(wt, newparents):
0.436.16 by Jelmer Vernooij
Some more work on maptree.
427
    """Simple helper that reverts to specified new parents and makes sure none 
428
    of the extra files are left around.
429
430
    :param wt: Working tree to use for rebase
431
    :param newparents: New parents of the working tree
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
432
    """
433
    newtree = wt.branch.repository.revision_tree(newparents[0])
434
    delta = wt.changes_from(newtree)
435
    wt.branch.generate_revision_history(newparents[0])
0.436.38 by Jelmer Vernooij
Handle directories in revert.
436
    wt.set_parent_ids(newparents[:1])
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
437
    for (f, _, _) in delta.added:
438
        abs_path = wt.abspath(f)
439
        if osutils.lexists(abs_path):
0.436.38 by Jelmer Vernooij
Handle directories in revert.
440
            if osutils.isdir(abs_path):
441
                osutils.rmtree(abs_path)
442
            else:
443
                os.unlink(abs_path)
0.436.10 by Jelmer Vernooij
Add more agressive version of revert.
444
    wt.revert([], old_tree=newtree, backups=False)
0.436.38 by Jelmer Vernooij
Handle directories in revert.
445
    assert not wt.changes_from(wt.basis_tree()).has_changed()
0.436.37 by Jelmer Vernooij
Store parents correctly.
446
    wt.set_parent_ids(newparents)
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
447
448
0.436.34 by Jelmer Vernooij
Some more tests.
449
class ReplaySnapshotError(BzrError):
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
450
    _fmt = """Replaying the snapshot failed: %(message)s."""
451
452
    def __init__(self, message):
453
        BzrError.__init__(self)
454
        self.message = message
0.436.34 by Jelmer Vernooij
Some more tests.
455
456
457
class ReplayParentsInconsistent(BzrError):
458
    _fmt = """Parents were inconsistent while replaying commit for file id %(fileid)s, revision %(revid)s."""
459
460
    def __init__(self, fileid, revid):
461
        BzrError.__init__(self)
462
        self.fileid = fileid
463
        self.revid = revid