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,
239
raise bzrlib.errors.NoCommonRoot(revision_a, revision_b)
241
for node, node_anc in ancestors_b.iteritems():
242
if node in ancestors:
245
ancestors[node] = set()
246
ancestors[node].update(node_anc)
247
for node, node_dec in descendants_b.iteritems():
248
if node not in descendants:
249
descendants[node] = set()
250
descendants[node].update(node_dec)
251
return root, ancestors, descendants, common
253
def common_ancestor(revision_a, revision_b, revision_source):
255
root, ancestors, descendants, common = \
256
combined_graph(revision_a, revision_b, revision_source)
257
except bzrlib.errors.NoCommonRoot:
258
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
260
nodes = farthest_nodes(descendants, ancestors, root)
264
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
266
193
class MultipleRevisionSources(object):
267
194
"""Proxy that looks in multiple branches for revisions."""
268
195
def __init__(self, *args):
287
214
Otherwise, rev_id will be the last entry. ancestor_id will never appear.
288
215
If ancestor_id is not an ancestor, NotAncestor will be thrown
290
root, ancestors, descendants = revision_graph(rev_id, rev_source)
291
if len(descendants) == 0:
292
raise NoSuchRevision(rev_source, rev_id)
293
if ancestor_id not in descendants:
294
rev_source.get_revision(ancestor_id)
295
raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
296
root_descendants = all_descendants(descendants, ancestor_id)
297
root_descendants.add(ancestor_id)
298
if rev_id not in root_descendants:
299
raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
300
distances = node_distances(descendants, ancestors, ancestor_id,
301
root_descendants=root_descendants)
303
def best_ancestor(rev_id):
305
for anc_id in ancestors[rev_id]:
307
distance = distances[anc_id]
310
if revision_history is not None and anc_id in revision_history:
312
elif best is None or distance > best[1]:
313
best = (anc_id, distance)
318
while next != ancestor_id:
320
next = best_ancestor(next)
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]