~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revision.py

  • Committer: Aaron Bentley
  • Date: 2005-08-25 22:05:37 UTC
  • mto: (1092.1.42) (1185.3.4)
  • mto: This revision was merged to the branch mainline in revision 1178.
  • Revision ID: abentley@panoramicfeedback.com-20050825220537-3a8e6ffc843ad8ca
Improved intermediate revision tracer
Now, when asked, it always finds the longest lines.
It maximizes the number of revision-history lines and, secondarily, minimizes
the number of non-revision-history lines.

Show diffs side-by-side

added added

removed removed

Lines of Context:
275
275
def get_intervening_revisions(ancestor_id, rev_id, rev_source, 
276
276
                              revision_history=None):
277
277
    """Find the longest line of descent from maybe_ancestor to revision.
278
 
    Revision history is followed where possible
 
278
    Revision history is followed where possible.
279
279
 
280
280
    If ancestor_id == rev_id, list will be empty.
281
281
    Otherwise, rev_id will be the last entry.  ancestor_id will never appear.
282
282
    If ancestor_id is not an ancestor, NotAncestor will be thrown
283
283
    """
284
 
    # This lists only the first descendant we encounter
 
284
    [rev_source.get_revision(r) for r in (ancestor_id, rev_id)]
285
285
    if ancestor_id == rev_id:
286
286
        return []
287
 
 
288
 
    def in_history(myrev):
289
 
        return revision_history is not None and myrev in revision_history
290
 
 
291
 
    def remove_broken_lines(revisions, rev_id, descendant_map):
292
 
        broken = set()
293
 
        for revision in revisions:
294
 
            cur_revision = revision
295
 
            while cur_revision != rev_id:
296
 
                cur_revision = descendant_map.get(cur_revision)
297
 
                if cur_revision is None:
298
 
                    broken.add(revision)
299
 
                    break
300
 
        return [r for r in revisions if r not in broken]
301
 
 
302
 
    def trace_ancestry():
303
 
        revisions = [rev_id]
304
 
        descendant_map = {}
305
 
        while len(revisions) > 0:
306
 
            remove_broken = False
307
 
            new_revisions = []
308
 
            for revision in revisions:
309
 
                parent_ids = [p.revision_id for p in 
310
 
                              rev_source.get_revision(revision).parents]
311
 
                for parent_id in parent_ids:
312
 
                    if parent_id == ancestor_id:
313
 
                        return revision, descendant_map
314
 
                    if descendant_map.has_key(parent_id):
315
 
                        if in_history(descendant_map[parent_id]):
316
 
                            continue
317
 
                        else:
318
 
                            remove_broken = True
319
 
                    descendant_map[parent_id] = revision
320
 
                    new_revisions.append(parent_id)
321
 
            if remove_broken:
322
 
                new_revisions = remove_broken_lines(new_revisions, rev_id,
323
 
                                                    descendant_map,)
324
 
            revisions = new_revisions
325
 
        raise Exception(revision, parent_ids)
326
 
        return None, descendant_map
327
 
 
328
 
    list_start, descendant_map = trace_ancestry()
329
 
    if list_start is None:
330
 
        raise Exception(descendant_map)
 
287
    def historical_lines(line):
 
288
        """Return a tuple of historical/non_historical lines, for sorting.
 
289
        The non_historical count is negative, since non_historical lines are
 
290
        a bad thing.
 
291
        """
 
292
        good_count = 0
 
293
        bad_count = 0
 
294
        for revision in line:
 
295
            if revision in revision_history:
 
296
                good_count += 1
 
297
            else:
 
298
                bad_count -= 1
 
299
        return good_count, bad_count
 
300
    active = [[rev_id]]
 
301
    successful_lines = []
 
302
    while len(active) > 0:
 
303
        new_active = []
 
304
        for line in active:
 
305
            parent_ids = [p.revision_id for p in 
 
306
                          rev_source.get_revision(line[-1]).parents]
 
307
            for parent in parent_ids:
 
308
                line_copy = line[:]
 
309
                if parent == ancestor_id:
 
310
                    successful_lines.append(line_copy)
 
311
                else:
 
312
                    line_copy.append(parent)
 
313
                    new_active.append(line_copy)
 
314
        active = new_active
 
315
    if len(successful_lines) == 0:
331
316
        raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
332
 
    intermediate_revisions = [list_start]
333
 
    next_revision = list_start
334
 
    while next_revision != rev_id:
335
 
        next_revision = descendant_map[next_revision]
336
 
        intermediate_revisions.append(next_revision)
337
 
    return intermediate_revisions
 
317
    for line in successful_lines:
 
318
        line.reverse()
 
319
    if revision_history is not None:
 
320
        by_historical_lines = []
 
321
        for line in successful_lines:
 
322
            count = historical_lines(line)
 
323
            by_historical_lines.append((count, line))
 
324
        by_historical_lines.sort()
 
325
        if by_historical_lines[-1][0][0] > 0:
 
326
            return by_historical_lines[-1][1]
 
327
    assert len(successful_lines)
 
328
    successful_lines.sort(cmp, len)
 
329
    return successful_lines[-1]