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):