~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revision.py

  • Committer: aaron.bentley at utoronto
  • Date: 2005-08-26 01:50:01 UTC
  • mto: (974.1.47)
  • mto: This revision was merged to the branch mainline in revision 1178.
  • Revision ID: aaron.bentley@utoronto.ca-20050826015001-d8b6e6330cb6401a
Merged from bzr.24

Show diffs side-by-side

added added

removed removed

Lines of Context:
176
176
    behaves like Branch's.
177
177
    """
178
178
 
 
179
    for ancestor_id, distance in iter_ancestors(revision_id, revision_source):
 
180
        if ancestor_id == candidate_id:
 
181
            return True
 
182
    return False
 
183
 
 
184
def iter_ancestors(revision_id, revision_source, only_present=False):
179
185
    ancestors = (revision_id,)
 
186
    distance = 0
180
187
    while len(ancestors) > 0:
181
188
        new_ancestors = []
182
189
        for ancestor in ancestors:
183
 
            if ancestor == candidate_id:
184
 
                return True
 
190
            if not only_present:
 
191
                yield ancestor, distance
185
192
            try:
186
193
                revision = revision_source.get_revision(ancestor)
187
194
            except bzrlib.errors.NoSuchRevision, e:
189
196
                    raise 
190
197
                else:
191
198
                    continue
 
199
            if only_present:
 
200
                yield ancestor, distance
192
201
            new_ancestors.extend([p.revision_id for p in revision.parents])
193
202
        ancestors = new_ancestors
194
 
 
 
203
        distance += 1
 
204
 
 
205
 
 
206
def find_present_ancestors(revision_id, revision_source):
 
207
    found_ancestors = {}
 
208
    count = 0
 
209
    anc_iter = enumerate(iter_ancestors(revision_id, revision_source,
 
210
                         only_present=True))
 
211
    for anc_order, (anc_id, anc_distance) in anc_iter:
 
212
        if not found_ancestors.has_key(anc_id):
 
213
            found_ancestors[anc_id] = (anc_order, anc_distance)
 
214
    return found_ancestors
 
215
    
 
216
class AmbiguousBase(bzrlib.errors.BzrError):
 
217
    def __init__(self, bases):
 
218
        msg = "The correct base is unclear, becase %s are all equally close" %\
 
219
            ", ".join(bases)
 
220
        bzrlib.errors.BzrError.__init__(self, msg)
 
221
        self.bases = bases
 
222
 
 
223
def common_ancestor(revision_a, revision_b, revision_source):
 
224
    """Find the ancestor common to both revisions that is closest to both.
 
225
    """
 
226
    from bzrlib.trace import mutter
 
227
    a_ancestors = find_present_ancestors(revision_a, revision_source)
 
228
    b_ancestors = find_present_ancestors(revision_b, revision_source)
 
229
    a_intersection = []
 
230
    b_intersection = []
 
231
    # a_order is used as a tie-breaker when two equally-good bases are found
 
232
    for revision, (a_order, a_distance) in a_ancestors.iteritems():
 
233
        if b_ancestors.has_key(revision):
 
234
            a_intersection.append((a_distance, a_order, revision))
 
235
            b_intersection.append((b_ancestors[revision][1], a_order, revision))
 
236
    mutter("a intersection: %r" % a_intersection)
 
237
    mutter("b intersection: %r" % b_intersection)
 
238
    def get_closest(intersection):
 
239
        intersection.sort()
 
240
        matches = [] 
 
241
        for entry in intersection:
 
242
            if entry[0] == intersection[0][0]:
 
243
                matches.append(entry[2])
 
244
        return matches
 
245
 
 
246
    a_closest = get_closest(a_intersection)
 
247
    if len(a_closest) == 0:
 
248
        return None
 
249
    b_closest = get_closest(b_intersection)
 
250
    assert len(b_closest) != 0
 
251
    mutter ("a_closest %r" % a_closest)
 
252
    mutter ("b_closest %r" % b_closest)
 
253
    if a_closest[0] in b_closest:
 
254
        return a_closest[0]
 
255
    elif b_closest[0] in a_closest:
 
256
        return b_closest[0]
 
257
    else:
 
258
        raise AmbiguousBase((a_closest[0], b_closest[0]))
 
259
    return a_closest[0]
195
260
 
196
261
class MultipleRevisionSources(object):
197
262
    def __init__(self, *args):
206
271
            except bzrlib.errors.NoSuchRevision, e:
207
272
                pass
208
273
        raise e
 
274
 
 
275
def get_intervening_revisions(ancestor_id, rev_id, rev_source, 
 
276
                              revision_history=None):
 
277
    """Find the longest line of descent from maybe_ancestor to revision.
 
278
    Revision history is followed where possible.
 
279
 
 
280
    If ancestor_id == rev_id, list will be empty.
 
281
    Otherwise, rev_id will be the last entry.  ancestor_id will never appear.
 
282
    If ancestor_id is not an ancestor, NotAncestor will be thrown
 
283
    """
 
284
    [rev_source.get_revision(r) for r in (ancestor_id, rev_id)]
 
285
    if ancestor_id == rev_id:
 
286
        return []
 
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:
 
316
        raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
 
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]