190
190
raise bzrlib.errors.AmbiguousBase((a_closest[0], b_closest[0]))
191
191
return a_closest[0]
193
def revision_graph(revision, revision_source):
194
"""Produce a graph of the ancestry of the specified revision.
195
Return root, ancestors map, descendants map
197
TODO: Produce graphs with the NULL revision as root, so that we can find
198
a common even when trees are not branches don't represent a single line
205
descendants[revision] = {}
206
while len(lines) > 0:
210
rev = revision_source.get_revision(line)
211
parents = [p.revision_id for p in rev.parents]
212
if len(parents) == 0:
214
except bzrlib.errors.NoSuchRevision:
218
if parents is not None:
219
for parent in parents:
220
if parent not in ancestors:
221
new_lines.add(parent)
222
if parent not in descendants:
223
descendants[parent] = {}
224
descendants[parent][line] = 1
225
if parents is not None:
226
ancestors[line] = set(parents)
228
assert root not in descendants[root]
229
assert root not in ancestors[root]
230
return root, ancestors, descendants
232
def combined_graph(revision_a, revision_b, revision_source):
233
"""Produce a combined ancestry graph.
234
Return graph root, ancestors map, descendants map, set of common nodes"""
235
root, ancestors, descendants = revision_graph(revision_a, revision_source)
236
root_b, ancestors_b, descendants_b = revision_graph(revision_b,
238
assert root == root_b
240
for node, node_anc in ancestors_b.iteritems():
241
if node in ancestors:
244
ancestors[node] = set()
245
ancestors[node].update(node_anc)
246
for node, node_dec in descendants_b.iteritems():
247
if node not in descendants:
248
descendants[node] = set()
249
descendants[node].update(node_dec)
250
return root, ancestors, descendants, common
252
def common_ancestor(revision_a, revision_b, revision_source):
253
root, ancestors, descendants, common = \
254
combined_graph(revision_a, revision_b, revision_source)
255
nodes = farthest_nodes(descendants, ancestors, root)
193
260
class MultipleRevisionSources(object):
194
261
"""Proxy that looks in multiple branches for revisions."""
195
262
def __init__(self, *args):
214
281
Otherwise, rev_id will be the last entry. ancestor_id will never appear.
215
282
If ancestor_id is not an ancestor, NotAncestor will be thrown
217
[rev_source.get_revision(r) for r in (ancestor_id, rev_id)]
218
if ancestor_id == rev_id:
220
def historical_lines(line):
221
"""Return a tuple of historical/non_historical lines, for sorting.
222
The non_historical count is negative, since non_historical lines are
227
for revision in line:
228
if revision in revision_history:
232
return good_count, bad_count
234
successful_lines = []
235
while len(active) > 0:
238
parent_ids = [p.revision_id for p in
239
rev_source.get_revision(line[-1]).parents]
240
for parent in parent_ids:
242
if parent == ancestor_id:
243
successful_lines.append(line_copy)
245
line_copy.append(parent)
246
new_active.append(line_copy)
248
if len(successful_lines) == 0:
249
raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
250
for line in successful_lines:
252
if revision_history is not None:
253
by_historical_lines = []
254
for line in successful_lines:
255
count = historical_lines(line)
256
by_historical_lines.append((count, line))
257
by_historical_lines.sort()
258
if by_historical_lines[-1][0][0] > 0:
259
return by_historical_lines[-1][1]
260
assert len(successful_lines)
261
successful_lines.sort(cmp, len)
262
return successful_lines[-1]
284
root, ancestors, descendants = revision_graph(rev_id, rev_source)
285
if len(descendants) == 0:
286
raise NoSuchRevision(rev_source, rev_id)
287
if ancestor_id not in descendants:
288
rev_source.get_revision(ancestor_id)
289
raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
290
root_descendants = all_descendants(descendants, ancestor_id)
291
root_descendants.add(ancestor_id)
292
if rev_id not in root_descendants:
293
raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
294
distances = node_distances(descendants, ancestors, ancestor_id,
295
root_descendants=root_descendants)
297
def best_ancestor(rev_id):
299
for anc_id in ancestors[rev_id]:
301
distance = distances[anc_id]
304
if revision_history is not None and anc_id in revision_history:
306
elif best is None or distance > best[1]:
307
best = (anc_id, distance)
312
while next != ancestor_id:
314
next = best_ancestor(next)