~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revision.py

  • Committer: Wouter van Heyst
  • Date: 2006-06-07 16:05:27 UTC
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: larstiq@larstiq.dyndns.org-20060607160527-2b3649154d0e2e84
more code cleanup

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
 
        )
 
26
from bzrlib.symbol_versioning import *
33
27
 
34
28
NULL_REVISION="null:"
35
 
CURRENT_REVISION="current:"
36
 
 
37
29
 
38
30
class Revision(object):
39
31
    """Single revision on a branch.
59
51
        self._check_properties()
60
52
        self.parent_ids = []
61
53
        self.parent_sha1s = []
62
 
        """Not used anymore - legacy from for 4."""
63
54
        self.__dict__.update(args)
64
55
 
65
56
    def __repr__(self):
82
73
        return not self.__eq__(other)
83
74
 
84
75
    def _check_properties(self):
85
 
        """Verify that all revision properties are OK."""
 
76
        """Verify that all revision properties are OK.
 
77
        """
86
78
        for name, value in self.properties.iteritems():
87
79
            if not isinstance(name, basestring) or contains_whitespace(name):
88
80
                raise ValueError("invalid property name %r" % name)
124
116
    revisions_source is an object supporting a get_revision operation that
125
117
    behaves like Branch's.
126
118
    """
127
 
    return (candidate_id in branch.repository.get_ancestry(revision_id,
128
 
            topo_sorted=False))
 
119
    return candidate_id in branch.repository.get_ancestry(revision_id)
129
120
 
130
121
 
131
122
def iter_ancestors(revision_id, revision_source, only_present=False):
138
129
                yield ancestor, distance
139
130
            try:
140
131
                revision = revision_source.get_revision(ancestor)
141
 
            except errors.NoSuchRevision, e:
 
132
            except bzrlib.errors.NoSuchRevision, e:
142
133
                if e.revision == revision_id:
143
134
                    raise 
144
135
                else:
161
152
    anc_iter = enumerate(iter_ancestors(revision_id, revision_source,
162
153
                         only_present=True))
163
154
    for anc_order, (anc_id, anc_distance) in anc_iter:
164
 
        if anc_id not in found_ancestors:
 
155
        if not found_ancestors.has_key(anc_id):
165
156
            found_ancestors[anc_id] = (anc_order, anc_distance)
166
157
    return found_ancestors
167
158
    
228
219
    root_b, ancestors_b, descendants_b = revision_graph(
229
220
        revision_b, revision_source)
230
221
    if root != root_b:
231
 
        raise errors.NoCommonRoot(revision_a, revision_b)
 
222
        raise bzrlib.errors.NoCommonRoot(revision_a, revision_b)
232
223
    common = set()
233
224
    for node, node_anc in ancestors_b.iteritems():
234
225
        if node in ancestors:
247
238
                    pb=DummyProgress()):
248
239
    if None in (revision_a, revision_b):
249
240
        return None
250
 
    if NULL_REVISION in (revision_a, revision_b):
251
 
        return NULL_REVISION
252
241
    # trivial optimisation
253
242
    if revision_a == revision_b:
254
243
        return revision_a
257
246
            pb.update('Picking ancestor', 1, 3)
258
247
            graph = revision_source.get_revision_graph_with_ghosts(
259
248
                [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
249
            # convert to a NULL_REVISION based graph.
269
250
            ancestors = graph.get_ancestors()
270
251
            descendants = graph.get_descendants()
271
 
            common = set(ancestry_a)
272
 
            common.intersection_update(ancestry_b)
 
252
            common = set(graph.get_ancestry(revision_a)).intersection(
 
253
                     set(graph.get_ancestry(revision_b)))
273
254
            descendants[NULL_REVISION] = {}
274
255
            ancestors[NULL_REVISION] = []
275
256
            for root in graph.roots:
291
272
                
292
273
            root = NULL_REVISION
293
274
            common.add(NULL_REVISION)
294
 
        except errors.NoCommonRoot:
295
 
            raise errors.NoCommonAncestor(revision_a, revision_b)
 
275
        except bzrlib.errors.NoCommonRoot:
 
276
            raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
296
277
            
297
278
        pb.update('Picking ancestor', 2, 3)
298
279
        distances = node_distances (descendants, ancestors, root)
299
280
        pb.update('Picking ancestor', 3, 2)
300
281
        farthest = select_farthest(distances, common)
301
282
        if farthest is None or farthest == NULL_REVISION:
302
 
            raise errors.NoCommonAncestor(revision_a, revision_b)
 
283
            raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
303
284
    finally:
304
285
        pb.clear()
305
286
    return farthest
324
305
        for source in self._revision_sources:
325
306
            try:
326
307
                return source.get_revision(revision_id)
327
 
            except errors.NoSuchRevision, e:
 
308
            except bzrlib.errors.NoSuchRevision, e:
328
309
                pass
329
310
        raise e
330
311
 
437
418
    """
438
419
    root, ancestors, descendants = revision_graph(rev_id, rev_source)
439
420
    if len(descendants) == 0:
440
 
        raise errors.NoSuchRevision(rev_source, rev_id)
 
421
        raise NoSuchRevision(rev_source, rev_id)
441
422
    if ancestor_id not in descendants:
442
423
        rev_source.get_revision(ancestor_id)
443
 
        raise errors.NotAncestor(rev_id, ancestor_id)
 
424
        raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
444
425
    root_descendants = all_descendants(descendants, ancestor_id)
445
426
    root_descendants.add(ancestor_id)
446
427
    if rev_id not in root_descendants:
447
 
        raise errors.NotAncestor(rev_id, ancestor_id)
 
428
        raise bzrlib.errors.NotAncestor(rev_id, ancestor_id)
448
429
    distances = node_distances(descendants, ancestors, ancestor_id,
449
430
                               root_descendants=root_descendants)
450
431
 
468
449
        next = best_ancestor(next)
469
450
    path.reverse()
470
451
    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