~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revision.py

  • Committer: Aaron Bentley
  • Date: 2006-03-07 05:59:14 UTC
  • mfrom: (1558.1.20 Aaron's integration)
  • mto: This revision was merged to the branch mainline in revision 1595.
  • Revision ID: aaron.bentley@utoronto.ca-20060307055914-a88728997afceb90
MergeĀ fromĀ mainline

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# TODO: Some kind of command-line display of revision properties: 
18
18
# perhaps show them in log -v and allow them as options to the commit command.
19
19
 
 
20
 
20
21
import bzrlib.errors
 
22
import bzrlib.errors as errors
21
23
from bzrlib.graph import node_distances, select_farthest, all_descendants
22
24
from bzrlib.osutils import contains_whitespace
23
25
from bzrlib.progress import DummyProgress
158
160
    return matches
159
161
 
160
162
 
161
 
def old_common_ancestor(revision_a, revision_b, revision_source):
162
 
    """Find the ancestor common to both revisions that is closest to both.
163
 
    """
164
 
    from bzrlib.trace import mutter
165
 
    a_ancestors = find_present_ancestors(revision_a, revision_source)
166
 
    b_ancestors = find_present_ancestors(revision_b, revision_source)
167
 
    a_intersection = []
168
 
    b_intersection = []
169
 
    # a_order is used as a tie-breaker when two equally-good bases are found
170
 
    for revision, (a_order, a_distance) in a_ancestors.iteritems():
171
 
        if b_ancestors.has_key(revision):
172
 
            a_intersection.append((a_distance, a_order, revision))
173
 
            b_intersection.append((b_ancestors[revision][1], a_order, revision))
174
 
    mutter("a intersection: %r", a_intersection)
175
 
    mutter("b intersection: %r", b_intersection)
176
 
 
177
 
    a_closest = __get_closest(a_intersection)
178
 
    if len(a_closest) == 0:
179
 
        return None
180
 
    b_closest = __get_closest(b_intersection)
181
 
    assert len(b_closest) != 0
182
 
    mutter ("a_closest %r", a_closest)
183
 
    mutter ("b_closest %r", b_closest)
184
 
    if a_closest[0] in b_closest:
185
 
        return a_closest[0]
186
 
    elif b_closest[0] in a_closest:
187
 
        return b_closest[0]
188
 
    else:
189
 
        raise bzrlib.errors.AmbiguousBase((a_closest[0], b_closest[0]))
190
 
    return a_closest[0]
191
 
 
192
163
def revision_graph(revision, revision_source):
193
164
    """Produce a graph of the ancestry of the specified revision.
194
 
    Return root, ancestors map, descendants map
195
 
 
196
 
    TODO: Produce graphs with the NULL revision as root, so that we can find
197
 
    a common even when trees are not branches don't represent a single line
198
 
    of descent.
199
 
    RBC: 20051024: note that when we have two partial histories, this may not
200
 
         be possible. But if we are willing to pretend :)... sure.
 
165
    
 
166
    :return: root, ancestors map, descendants map
201
167
    """
 
168
    revision_source.lock_read()
 
169
    try:
 
170
        return _revision_graph(revision, revision_source)
 
171
    finally:
 
172
        revision_source.unlock()
 
173
 
 
174
 
 
175
def _revision_graph(revision, revision_source):
 
176
    """See revision_graph."""
 
177
    from bzrlib.tsort import topo_sort
 
178
    graph = revision_source.get_revision_graph(revision)
 
179
    # mark all no-parent revisions as being NULL_REVISION parentage.
 
180
    for node, parents in graph.items():
 
181
        if len(parents) == 0:
 
182
            graph[node] = [NULL_REVISION]
 
183
    # add NULL_REVISION to the graph
 
184
    graph[NULL_REVISION] = []
 
185
 
 
186
    # pick a root. If there are multiple roots
 
187
    # this could pick a random one.
 
188
    topo_order = topo_sort(graph.items())
 
189
    root = topo_order[0]
 
190
 
202
191
    ancestors = {}
203
192
    descendants = {}
204
 
    lines = [revision]
205
 
    root = None
206
 
    descendants[revision] = {}
207
 
    while len(lines) > 0:
208
 
        new_lines = set()
209
 
        for line in lines:
210
 
            if line == NULL_REVISION:
211
 
                parents = []
212
 
                root = NULL_REVISION
213
 
            else:
214
 
                try:
215
 
                    rev = revision_source.get_revision(line)
216
 
                    parents = list(rev.parent_ids)
217
 
                    if len(parents) == 0:
218
 
                        parents = [NULL_REVISION]
219
 
                except bzrlib.errors.NoSuchRevision:
220
 
                    if line == revision:
221
 
                        raise
222
 
                    parents = None
223
 
            if parents is not None:
224
 
                for parent in parents:
225
 
                    if parent not in ancestors:
226
 
                        new_lines.add(parent)
227
 
                    if parent not in descendants:
228
 
                        descendants[parent] = {}
229
 
                    descendants[parent][line] = 1
230
 
            if parents is not None:
231
 
                ancestors[line] = set(parents)
232
 
        lines = new_lines
233
 
    if root is None:
234
 
        # The history for revision becomes inaccessible without
235
 
        # actually hitting a no-parents revision. This then
236
 
        # makes these asserts below trigger. So, if root is None
237
 
        # determine the actual root by walking the accessible tree
238
 
        # and then stash NULL_REVISION at the end.
239
 
        root = NULL_REVISION
240
 
        descendants[root] = {}
241
 
        # for every revision, check we can access at least
242
 
        # one parent, if we cant, add NULL_REVISION and
243
 
        # a link
244
 
        for rev in ancestors:
245
 
            if len(ancestors[rev]) == 0:
246
 
                raise RuntimeError('unreachable code ?!')
247
 
            ok = False
248
 
            for parent in ancestors[rev]:
249
 
                if parent in ancestors:
250
 
                    ok = True
251
 
            if ok:
252
 
                continue
253
 
            descendants[root][rev] = 1
254
 
            ancestors[rev].add(root)
255
 
        ancestors[root] = set()
 
193
 
 
194
    # map the descendants of the graph.
 
195
    # and setup our set based return graph.
 
196
    for node in graph.keys():
 
197
        descendants[node] = {}
 
198
    for node, parents in graph.items():
 
199
        for parent in parents:
 
200
            descendants[parent][node] = 1
 
201
        ancestors[node] = set(parents)
 
202
 
256
203
    assert root not in descendants[root]
257
204
    assert root not in ancestors[root]
258
205
    return root, ancestors, descendants
261
208
def combined_graph(revision_a, revision_b, revision_source):
262
209
    """Produce a combined ancestry graph.
263
210
    Return graph root, ancestors map, descendants map, set of common nodes"""
264
 
    root, ancestors, descendants = revision_graph(revision_a, revision_source)
265
 
    root_b, ancestors_b, descendants_b = revision_graph(revision_b, 
266
 
                                                        revision_source)
 
211
    root, ancestors, descendants = revision_graph(
 
212
        revision_a, revision_source)
 
213
    root_b, ancestors_b, descendants_b = revision_graph(
 
214
        revision_b, revision_source)
267
215
    if root != root_b:
268
216
        raise bzrlib.errors.NoCommonRoot(revision_a, revision_b)
269
217
    common = set()
282
230
 
283
231
def common_ancestor(revision_a, revision_b, revision_source, 
284
232
                    pb=DummyProgress()):
 
233
    if None in (revision_a, revision_b):
 
234
        return None
285
235
    try:
286
236
        try:
287
237
            pb.update('Picking ancestor', 1, 3)
288
238
            root, ancestors, descendants, common = \
289
 
                combined_graph(revision_a, revision_b, revision_source)
 
239
                combined_graph(revision_a,
 
240
                               revision_b,
 
241
                               revision_source)
290
242
        except bzrlib.errors.NoCommonRoot:
291
243
            raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
292
244
            
308
260
        assert len(args) != 0
309
261
        self._revision_sources = args
310
262
 
 
263
    def revision_parents(self, revision_id):
 
264
        for source in self._revision_sources:
 
265
            try:
 
266
                return source.revision_parents(revision_id)
 
267
            except (errors.WeaveRevisionNotPresent, errors.NoSuchRevision), e:
 
268
                pass
 
269
        raise e
 
270
 
311
271
    def get_revision(self, revision_id):
312
272
        for source in self._revision_sources:
313
273
            try:
316
276
                pass
317
277
        raise e
318
278
 
 
279
    def get_revision_graph(self, revision_id):
 
280
        result = {}
 
281
        for source in self._revision_sources:
 
282
            try:
 
283
                result.update(source.get_revision_graph(revision_id))
 
284
            except (errors.WeaveRevisionNotPresent, errors.NoSuchRevision), e:
 
285
                pass
 
286
        if result:
 
287
            return result
 
288
        raise e
 
289
 
 
290
    def lock_read(self):
 
291
        for source in self._revision_sources:
 
292
            source.lock_read()
 
293
 
 
294
    def unlock(self):
 
295
        for source in self._revision_sources:
 
296
            source.unlock()
 
297
 
 
298
 
319
299
def get_intervening_revisions(ancestor_id, rev_id, rev_source, 
320
300
                              revision_history=None):
321
301
    """Find the longest line of descent from maybe_ancestor to revision.