~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revision.py

  • Committer: Martin Pool
  • Date: 2006-03-23 19:00:53 UTC
  • mto: This revision was merged to the branch mainline in revision 1626.
  • Revision ID: mbp@sourcefrog.net-20060323190053-ac6b735ff74f56d7
Handle 'bzr ?', etc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
2
 
#
 
1
# (C) 2005 Canonical
 
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
21
22
import bzrlib.errors as errors
22
 
from bzrlib.deprecated_graph import (
23
 
    all_descendants,
24
 
    Graph,
25
 
    node_distances,
26
 
    select_farthest,
27
 
    )
 
23
from bzrlib.graph import node_distances, select_farthest, all_descendants, Graph
28
24
from bzrlib.osutils import contains_whitespace
29
25
from bzrlib.progress import DummyProgress
30
 
from bzrlib.symbol_versioning import (deprecated_function,
31
 
        zero_eight,
32
 
        )
33
26
 
34
27
NULL_REVISION="null:"
35
 
CURRENT_REVISION="current:"
36
 
 
37
28
 
38
29
class Revision(object):
39
30
    """Single revision on a branch.
59
50
        self._check_properties()
60
51
        self.parent_ids = []
61
52
        self.parent_sha1s = []
62
 
        """Not used anymore - legacy from for 4."""
63
53
        self.__dict__.update(args)
64
54
 
65
55
    def __repr__(self):
82
72
        return not self.__eq__(other)
83
73
 
84
74
    def _check_properties(self):
85
 
        """Verify that all revision properties are OK."""
 
75
        """Verify that all revision properties are OK.
 
76
        """
86
77
        for name, value in self.properties.iteritems():
87
78
            if not isinstance(name, basestring) or contains_whitespace(name):
88
79
                raise ValueError("invalid property name %r" % name)
109
100
        reversed_result.reverse()
110
101
        return reversed_result
111
102
 
112
 
    def get_summary(self):
113
 
        """Get the first line of the log message for this revision.
114
 
        """
115
 
        return self.message.split('\n', 1)[0]
116
 
 
117
103
 
118
104
def is_ancestor(revision_id, candidate_id, branch):
119
105
    """Return true if candidate_id is an ancestor of revision_id.
124
110
    revisions_source is an object supporting a get_revision operation that
125
111
    behaves like Branch's.
126
112
    """
127
 
    return (candidate_id in branch.repository.get_ancestry(revision_id,
128
 
            topo_sorted=False))
 
113
    return candidate_id in branch.repository.get_ancestry(revision_id)
129
114
 
130
115
 
131
116
def iter_ancestors(revision_id, revision_source, only_present=False):
138
123
                yield ancestor, distance
139
124
            try:
140
125
                revision = revision_source.get_revision(ancestor)
141
 
            except errors.NoSuchRevision, e:
 
126
            except bzrlib.errors.NoSuchRevision, e:
142
127
                if e.revision == revision_id:
143
128
                    raise 
144
129
                else:
161
146
    anc_iter = enumerate(iter_ancestors(revision_id, revision_source,
162
147
                         only_present=True))
163
148
    for anc_order, (anc_id, anc_distance) in anc_iter:
164
 
        if anc_id not in found_ancestors:
 
149
        if not found_ancestors.has_key(anc_id):
165
150
            found_ancestors[anc_id] = (anc_order, anc_distance)
166
151
    return found_ancestors
167
152
    
228
213
    root_b, ancestors_b, descendants_b = revision_graph(
229
214
        revision_b, revision_source)
230
215
    if root != root_b:
231
 
        raise errors.NoCommonRoot(revision_a, revision_b)
 
216
        raise bzrlib.errors.NoCommonRoot(revision_a, revision_b)
232
217
    common = set()
233
218
    for node, node_anc in ancestors_b.iteritems():
234
219
        if node in ancestors:
247
232
                    pb=DummyProgress()):
248
233
    if None in (revision_a, revision_b):
249
234
        return None
250
 
    if NULL_REVISION in (revision_a, revision_b):
251
 
        return NULL_REVISION
252
235
    # trivial optimisation
253
236
    if revision_a == revision_b:
254
237
        return revision_a
257
240
            pb.update('Picking ancestor', 1, 3)
258
241
            graph = revision_source.get_revision_graph_with_ghosts(
259
242
                [revision_a, revision_b])
260
 
            # Shortcut the case where one of the tips is already included in
261
 
            # the other graphs ancestry.
262
 
            ancestry_a = graph.get_ancestry(revision_a, topo_sorted=False)
263
 
            if revision_b in ancestry_a:
264
 
                return revision_b
265
 
            ancestry_b = graph.get_ancestry(revision_b, topo_sorted=False)
266
 
            if revision_a in ancestry_b:
267
 
                return revision_a
268
243
            # convert to a NULL_REVISION based graph.
269
244
            ancestors = graph.get_ancestors()
270
245
            descendants = graph.get_descendants()
271
 
            common = set(ancestry_a)
272
 
            common.intersection_update(ancestry_b)
 
246
            common = set(graph.get_ancestry(revision_a)).intersection(
 
247
                     set(graph.get_ancestry(revision_b)))
273
248
            descendants[NULL_REVISION] = {}
274
249
            ancestors[NULL_REVISION] = []
275
250
            for root in graph.roots:
276
251
                descendants[NULL_REVISION][root] = 1
277
252
                ancestors[root].append(NULL_REVISION)
278
 
            for ghost in graph.ghosts:
279
 
                # ghosts act as roots for the purpose of finding 
280
 
                # the longest paths from the root: any ghost *might*
281
 
                # be directly attached to the root, so we treat them
282
 
                # as being such.
283
 
                # ghost now descends from NULL
284
 
                descendants[NULL_REVISION][ghost] = 1
285
 
                # that is it has an ancestor of NULL
286
 
                ancestors[ghost] = [NULL_REVISION]
287
 
                # ghost is common if any of ghosts descendants are common:
288
 
                for ghost_descendant in descendants[ghost]:
289
 
                    if ghost_descendant in common:
290
 
                        common.add(ghost)
291
 
                
 
253
            if len(graph.roots) == 0:
 
254
                # no reachable roots - not handled yet.
 
255
                raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
292
256
            root = NULL_REVISION
293
257
            common.add(NULL_REVISION)
294
 
        except errors.NoCommonRoot:
295
 
            raise errors.NoCommonAncestor(revision_a, revision_b)
 
258
        except bzrlib.errors.NoCommonRoot:
 
259
            raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
296
260
            
297
261
        pb.update('Picking ancestor', 2, 3)
298
262
        distances = node_distances (descendants, ancestors, root)
299
263
        pb.update('Picking ancestor', 3, 2)
300
264
        farthest = select_farthest(distances, common)
301
265
        if farthest is None or farthest == NULL_REVISION:
302
 
            raise errors.NoCommonAncestor(revision_a, revision_b)
 
266
            raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
303
267
    finally:
304
268
        pb.clear()
305
269
    return farthest
324
288
        for source in self._revision_sources:
325
289
            try:
326
290
                return source.get_revision(revision_id)
327
 
            except errors.NoSuchRevision, e:
 
291
            except bzrlib.errors.NoSuchRevision, e:
328
292
                pass
329
293
        raise e
330
294
 
425
389
            source.unlock()
426
390
 
427
391
 
428
 
@deprecated_function(zero_eight)
429
 
def get_intervening_revisions(ancestor_id, rev_id, rev_source,
 
392
def get_intervening_revisions(ancestor_id, rev_id, rev_source, 
430
393
                              revision_history=None):
431
394
    """Find the longest line of descent from maybe_ancestor to revision.
432
395
    Revision history is followed where possible.
437
400
    """
438
401
    root, ancestors, descendants = revision_graph(rev_id, rev_source)
439
402
    if len(descendants) == 0:
440
 
        raise errors.NoSuchRevision(rev_source, rev_id)
 
403
        raise NoSuchRevision(rev_source, rev_id)
441
404
    if ancestor_id not in descendants:
442
405
        rev_source.get_revision(ancestor_id)
443
 
        raise errors.NotAncestor(rev_id, ancestor_id)
 
406
        raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
444
407
    root_descendants = all_descendants(descendants, ancestor_id)
445
408
    root_descendants.add(ancestor_id)
446
409
    if rev_id not in root_descendants:
447
 
        raise errors.NotAncestor(rev_id, ancestor_id)
 
410
        raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
448
411
    distances = node_distances(descendants, ancestors, ancestor_id,
449
412
                               root_descendants=root_descendants)
450
413
 
468
431
        next = best_ancestor(next)
469
432
    path.reverse()
470
433
    return path
471
 
 
472
 
 
473
 
def is_reserved_id(revision_id):
474
 
    """Determine whether a revision id is reserved
475
 
 
476
 
    :return: True if the revision is is reserved, False otherwise
477
 
    """
478
 
    return isinstance(revision_id, basestring) and revision_id.endswith(':')
479
 
 
480
 
 
481
 
def check_not_reserved_id(revision_id):
482
 
    """Raise ReservedId if the supplied revision_id is reserved"""
483
 
    if is_reserved_id(revision_id):
484
 
        raise errors.ReservedId(revision_id)
485
 
 
486
 
def ensure_null(revision_id):
487
 
    """Ensure only NULL_REVISION is used to represent the null revisionn"""
488
 
    if revision_id is None:
489
 
        return NULL_REVISION
490
 
    else:
491
 
        return revision_id