176
176
behaves like Branch's.
179
for ancestor_id, distance in iter_ancestors(revision_id, revision_source):
180
if ancestor_id == candidate_id:
184
def iter_ancestors(revision_id, revision_source, only_present=False):
179
185
ancestors = (revision_id,)
180
187
while len(ancestors) > 0:
181
188
new_ancestors = []
182
189
for ancestor in ancestors:
183
if ancestor == candidate_id:
191
yield ancestor, distance
186
193
revision = revision_source.get_revision(ancestor)
187
194
except bzrlib.errors.NoSuchRevision, e:
200
yield ancestor, distance
192
201
new_ancestors.extend([p.revision_id for p in revision.parents])
193
202
ancestors = new_ancestors
206
def find_present_ancestors(revision_id, revision_source):
209
anc_iter = enumerate(iter_ancestors(revision_id, revision_source,
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
216
class AmbiguousBase(bzrlib.errors.BzrError):
217
def __init__(self, bases):
218
msg = "The correct base is unclear, becase %s are all equally close" %\
220
bzrlib.errors.BzrError.__init__(self, msg)
223
def common_ancestor(revision_a, revision_b, revision_source):
224
"""Find the ancestor common to both revisions that is closest to both.
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)
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):
241
for entry in intersection:
242
if entry[0] == intersection[0][0]:
243
matches.append(entry[2])
246
a_closest = get_closest(a_intersection)
247
if len(a_closest) == 0:
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:
255
elif b_closest[0] in a_closest:
258
raise AmbiguousBase((a_closest[0], b_closest[0]))
196
261
class MultipleRevisionSources(object):
197
262
def __init__(self, *args):
206
271
except bzrlib.errors.NoSuchRevision, e:
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.
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
284
[rev_source.get_revision(r) for r in (ancestor_id, rev_id)]
285
if ancestor_id == rev_id:
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
294
for revision in line:
295
if revision in revision_history:
299
return good_count, bad_count
301
successful_lines = []
302
while len(active) > 0:
305
parent_ids = [p.revision_id for p in
306
rev_source.get_revision(line[-1]).parents]
307
for parent in parent_ids:
309
if parent == ancestor_id:
310
successful_lines.append(line_copy)
312
line_copy.append(parent)
313
new_active.append(line_copy)
315
if len(successful_lines) == 0:
316
raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
317
for line in successful_lines:
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]