1
# Copyright (C) 2005, 2006 Canonical Ltd
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.
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.
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.
22
21
import bzrlib.errors as errors
23
22
from bzrlib.graph import node_distances, select_farthest, all_descendants, Graph
24
23
from bzrlib.osutils import contains_whitespace
25
24
from bzrlib.progress import DummyProgress
26
from bzrlib.symbol_versioning import *
25
from bzrlib.symbol_versioning import (deprecated_function,
28
29
NULL_REVISION="null:"
30
CURRENT_REVISION="current:"
30
33
class Revision(object):
31
34
"""Single revision on a branch.
73
77
return not self.__eq__(other)
75
79
def _check_properties(self):
76
"""Verify that all revision properties are OK.
80
"""Verify that all revision properties are OK."""
78
81
for name, value in self.properties.iteritems():
79
82
if not isinstance(name, basestring) or contains_whitespace(name):
80
83
raise ValueError("invalid property name %r" % name)
101
104
reversed_result.reverse()
102
105
return reversed_result
107
def get_summary(self):
108
"""Get the first line of the log message for this revision.
110
return self.message.split('\n', 1)[0]
105
113
def is_ancestor(revision_id, candidate_id, branch):
106
114
"""Return true if candidate_id is an ancestor of revision_id.
147
155
anc_iter = enumerate(iter_ancestors(revision_id, revision_source,
148
156
only_present=True))
149
157
for anc_order, (anc_id, anc_distance) in anc_iter:
150
if not found_ancestors.has_key(anc_id):
158
if anc_id not in found_ancestors:
151
159
found_ancestors[anc_id] = (anc_order, anc_distance)
152
160
return found_ancestors
214
222
root_b, ancestors_b, descendants_b = revision_graph(
215
223
revision_b, revision_source)
216
224
if root != root_b:
217
raise bzrlib.errors.NoCommonRoot(revision_a, revision_b)
225
raise errors.NoCommonRoot(revision_a, revision_b)
219
227
for node, node_anc in ancestors_b.iteritems():
220
228
if node in ancestors:
241
251
pb.update('Picking ancestor', 1, 3)
242
252
graph = revision_source.get_revision_graph_with_ghosts(
243
253
[revision_a, revision_b])
254
# Shortcut the case where one of the tips is already included in
255
# the other graphs ancestry.
256
ancestry_a = graph.get_ancestry(revision_a)
257
if revision_b in ancestry_a:
259
ancestry_b = graph.get_ancestry(revision_b)
260
if revision_a in ancestry_b:
244
262
# convert to a NULL_REVISION based graph.
245
263
ancestors = graph.get_ancestors()
246
264
descendants = graph.get_descendants()
247
common = set(graph.get_ancestry(revision_a)).intersection(
248
set(graph.get_ancestry(revision_b)))
265
common = set(ancestry_a)
266
common.intersection_update(ancestry_b)
249
267
descendants[NULL_REVISION] = {}
250
268
ancestors[NULL_REVISION] = []
251
269
for root in graph.roots:
268
286
root = NULL_REVISION
269
287
common.add(NULL_REVISION)
270
except bzrlib.errors.NoCommonRoot:
271
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
288
except errors.NoCommonRoot:
289
raise errors.NoCommonAncestor(revision_a, revision_b)
273
291
pb.update('Picking ancestor', 2, 3)
274
292
distances = node_distances (descendants, ancestors, root)
275
293
pb.update('Picking ancestor', 3, 2)
276
294
farthest = select_farthest(distances, common)
277
295
if farthest is None or farthest == NULL_REVISION:
278
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
296
raise errors.NoCommonAncestor(revision_a, revision_b)
399
417
def unlock(self):
400
418
for source in self._revision_sources:
422
@deprecated_function(zero_eight)
423
def get_intervening_revisions(ancestor_id, rev_id, rev_source,
424
revision_history=None):
425
"""Find the longest line of descent from maybe_ancestor to revision.
426
Revision history is followed where possible.
428
If ancestor_id == rev_id, list will be empty.
429
Otherwise, rev_id will be the last entry. ancestor_id will never appear.
430
If ancestor_id is not an ancestor, NotAncestor will be thrown
432
root, ancestors, descendants = revision_graph(rev_id, rev_source)
433
if len(descendants) == 0:
434
raise errors.NoSuchRevision(rev_source, rev_id)
435
if ancestor_id not in descendants:
436
rev_source.get_revision(ancestor_id)
437
raise errors.NotAncestor(rev_id, ancestor_id)
438
root_descendants = all_descendants(descendants, ancestor_id)
439
root_descendants.add(ancestor_id)
440
if rev_id not in root_descendants:
441
raise errors.NotAncestor(rev_id, ancestor_id)
442
distances = node_distances(descendants, ancestors, ancestor_id,
443
root_descendants=root_descendants)
445
def best_ancestor(rev_id):
447
for anc_id in ancestors[rev_id]:
449
distance = distances[anc_id]
452
if revision_history is not None and anc_id in revision_history:
454
elif best is None or distance > best[1]:
455
best = (anc_id, distance)
460
while next != ancestor_id:
462
next = best_ancestor(next)
467
def is_reserved_id(revision_id):
468
"""Determine whether a revision id is reserved
470
:return: True if the revision is is reserved, False otherwise
472
return isinstance(revision_id, basestring) and revision_id.endswith(':')
475
def check_not_reserved_id(revision_id):
476
"""Raise ReservedId if the supplied revision_id is reserved"""
477
if is_reserved_id(revision_id):
478
raise errors.ReservedId(revision_id)