~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revision.py

Nathaniel McCallums patch for urandom friendliness on aix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
 
18
18
import bzrlib.errors
19
 
from bzrlib.graph import node_distances, select_farthest, all_descendants
20
19
 
21
 
NULL_REVISION="null:"
22
20
 
23
21
class RevisionReference(object):
24
22
    """
108
106
    revisions_source is an object supporting a get_revision operation that
109
107
    behaves like Branch's.
110
108
    """
111
 
    if candidate_id is None:
112
 
        return True
 
109
 
113
110
    for ancestor_id, distance in iter_ancestors(revision_id, revision_source):
114
111
        if ancestor_id == candidate_id:
115
112
            return True
162
159
    return matches
163
160
 
164
161
 
165
 
def old_common_ancestor(revision_a, revision_b, revision_source):
 
162
def common_ancestor(revision_a, revision_b, revision_source):
166
163
    """Find the ancestor common to both revisions that is closest to both.
167
164
    """
168
165
    from bzrlib.trace import mutter
193
190
        raise bzrlib.errors.AmbiguousBase((a_closest[0], b_closest[0]))
194
191
    return a_closest[0]
195
192
 
196
 
def revision_graph(revision, revision_source):
197
 
    """Produce a graph of the ancestry of the specified revision.
198
 
    Return root, ancestors map, descendants map
199
 
 
200
 
    TODO: Produce graphs with the NULL revision as root, so that we can find
201
 
    a common even when trees are not branches don't represent a single line
202
 
    of descent.
203
 
    """
204
 
    ancestors = {}
205
 
    descendants = {}
206
 
    lines = [revision]
207
 
    root = None
208
 
    descendants[revision] = {}
209
 
    while len(lines) > 0:
210
 
        new_lines = set()
211
 
        for line in lines:
212
 
            if line == NULL_REVISION:
213
 
                parents = []
214
 
                root = NULL_REVISION
215
 
            else:
216
 
                try:
217
 
                    rev = revision_source.get_revision(line)
218
 
                    parents = [p.revision_id for p in rev.parents]
219
 
                    if len(parents) == 0:
220
 
                        parents = [NULL_REVISION]
221
 
                except bzrlib.errors.NoSuchRevision:
222
 
                    if line == revision:
223
 
                        raise
224
 
                    parents = None
225
 
            if parents is not None:
226
 
                for parent in parents:
227
 
                    if parent not in ancestors:
228
 
                        new_lines.add(parent)
229
 
                    if parent not in descendants:
230
 
                        descendants[parent] = {}
231
 
                    descendants[parent][line] = 1
232
 
            if parents is not None:
233
 
                ancestors[line] = set(parents)
234
 
        lines = new_lines
235
 
    assert root not in descendants[root]
236
 
    assert root not in ancestors[root]
237
 
    return root, ancestors, descendants
238
 
 
239
 
def combined_graph(revision_a, revision_b, revision_source):
240
 
    """Produce a combined ancestry graph.
241
 
    Return graph root, ancestors map, descendants map, set of common nodes"""
242
 
    root, ancestors, descendants = revision_graph(revision_a, revision_source)
243
 
    root_b, ancestors_b, descendants_b = revision_graph(revision_b, 
244
 
                                                        revision_source)
245
 
    if root != root_b:
246
 
        raise bzrlib.errors.NoCommonRoot(revision_a, revision_b)
247
 
    common = set()
248
 
    for node, node_anc in ancestors_b.iteritems():
249
 
        if node in ancestors:
250
 
            common.add(node)
251
 
        else:
252
 
            ancestors[node] = set()
253
 
        ancestors[node].update(node_anc)
254
 
    for node, node_dec in descendants_b.iteritems():
255
 
        if node not in descendants:
256
 
            descendants[node] = set()
257
 
        descendants[node].update(node_dec)
258
 
    return root, ancestors, descendants, common
259
 
 
260
 
def common_ancestor(revision_a, revision_b, revision_source):
261
 
    try:
262
 
        root, ancestors, descendants, common = \
263
 
            combined_graph(revision_a, revision_b, revision_source)
264
 
    except bzrlib.errors.NoCommonRoot:
265
 
        raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
266
 
        
267
 
    distances = node_distances (descendants, ancestors, root)
268
 
    farthest = select_farthest(distances, common)
269
 
    if farthest is None or farthest == NULL_REVISION:
270
 
        raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
271
 
    return farthest
272
 
 
273
193
class MultipleRevisionSources(object):
274
194
    """Proxy that looks in multiple branches for revisions."""
275
195
    def __init__(self, *args):
294
214
    Otherwise, rev_id will be the last entry.  ancestor_id will never appear.
295
215
    If ancestor_id is not an ancestor, NotAncestor will be thrown
296
216
    """
297
 
    root, ancestors, descendants = revision_graph(rev_id, rev_source)
298
 
    if len(descendants) == 0:
299
 
        raise NoSuchRevision(rev_source, rev_id)
300
 
    if ancestor_id not in descendants:
301
 
        rev_source.get_revision(ancestor_id)
302
 
        raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
303
 
    root_descendants = all_descendants(descendants, ancestor_id)
304
 
    root_descendants.add(ancestor_id)
305
 
    if rev_id not in root_descendants:
306
 
        raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
307
 
    distances = node_distances(descendants, ancestors, ancestor_id,
308
 
                               root_descendants=root_descendants)
309
 
 
310
 
    def best_ancestor(rev_id):
311
 
        best = None
312
 
        for anc_id in ancestors[rev_id]:
313
 
            try:
314
 
                distance = distances[anc_id]
315
 
            except KeyError:
316
 
                continue
317
 
            if revision_history is not None and anc_id in revision_history:
318
 
                return anc_id
319
 
            elif best is None or distance > best[1]:
320
 
                best = (anc_id, distance)
321
 
        return best[0]
322
 
 
323
 
    next = rev_id
324
 
    path = []
325
 
    while next != ancestor_id:
326
 
        path.append(next)
327
 
        next = best_ancestor(next)
328
 
    path.reverse()
329
 
    return path
 
217
    [rev_source.get_revision(r) for r in (ancestor_id, rev_id)]
 
218
    if ancestor_id == rev_id:
 
219
        return []
 
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
 
223
        a bad thing.
 
224
        """
 
225
        good_count = 0
 
226
        bad_count = 0
 
227
        for revision in line:
 
228
            if revision in revision_history:
 
229
                good_count += 1
 
230
            else:
 
231
                bad_count -= 1
 
232
        return good_count, bad_count
 
233
    active = [[rev_id]]
 
234
    successful_lines = []
 
235
    while len(active) > 0:
 
236
        new_active = []
 
237
        for line in active:
 
238
            parent_ids = [p.revision_id for p in 
 
239
                          rev_source.get_revision(line[-1]).parents]
 
240
            for parent in parent_ids:
 
241
                line_copy = line[:]
 
242
                if parent == ancestor_id:
 
243
                    successful_lines.append(line_copy)
 
244
                else:
 
245
                    line_copy.append(parent)
 
246
                    new_active.append(line_copy)
 
247
        active = new_active
 
248
    if len(successful_lines) == 0:
 
249
        raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
 
250
    for line in successful_lines:
 
251
        line.reverse()
 
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]