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.
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
161
163
def revision_graph(revision, revision_source):
162
164
"""Produce a graph of the ancestry of the specified revision.
163
Return root, ancestors map, descendants map
165
TODO: Produce graphs with the NULL revision as root, so that we can find
166
a common even when trees are not branches don't represent a single line
168
RBC: 20051024: note that when we have two partial histories, this may not
169
be possible. But if we are willing to pretend :)... sure.
166
:return: root, ancestors map, descendants map
171
168
revision_source.lock_read()
175
172
revision_source.unlock()
177
175
def _revision_graph(revision, revision_source):
178
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] = []
186
# pick a root. If there are multiple roots
187
# this could pick a random one.
188
topo_order = topo_sort(graph.items())
183
descendants[revision] = {}
184
while len(lines) > 0:
187
if line == NULL_REVISION:
192
rev = revision_source.get_revision(line)
193
parents = list(rev.parent_ids)
194
if len(parents) == 0:
195
parents = [NULL_REVISION]
196
except bzrlib.errors.NoSuchRevision:
200
if parents is not None:
201
for parent in parents:
202
if parent not in ancestors:
203
new_lines.add(parent)
204
if parent not in descendants:
205
descendants[parent] = {}
206
descendants[parent][line] = 1
207
if parents is not None:
208
ancestors[line] = set(parents)
211
# The history for revision becomes inaccessible without
212
# actually hitting a no-parents revision. This then
213
# makes these asserts below trigger. So, if root is None
214
# determine the actual root by walking the accessible tree
215
# and then stash NULL_REVISION at the end.
217
descendants[root] = {}
218
# for every revision, check we can access at least
219
# one parent, if we cant, add NULL_REVISION and
221
for rev in ancestors:
222
if len(ancestors[rev]) == 0:
223
raise RuntimeError('unreachable code ?!')
225
for parent in ancestors[rev]:
226
if parent in ancestors:
230
descendants[root][rev] = 1
231
ancestors[rev].add(root)
232
ancestors[root] = set()
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)
233
203
assert root not in descendants[root]
234
204
assert root not in ancestors[root]
235
205
return root, ancestors, descendants
238
208
def combined_graph(revision_a, revision_b, revision_source):
239
209
"""Produce a combined ancestry graph.
240
210
Return graph root, ancestors map, descendants map, set of common nodes"""
241
root, ancestors, descendants = revision_graph(revision_a, revision_source)
242
root_b, ancestors_b, descendants_b = revision_graph(revision_b,
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)
244
215
if root != root_b:
245
216
raise bzrlib.errors.NoCommonRoot(revision_a, revision_b)
266
237
pb.update('Picking ancestor', 1, 3)
267
238
root, ancestors, descendants, common = \
268
combined_graph(revision_a, revision_b, revision_source)
239
combined_graph(revision_a,
269
242
except bzrlib.errors.NoCommonRoot:
270
243
raise bzrlib.errors.NoCommonAncestor(revision_a, revision_b)
287
260
assert len(args) != 0
288
261
self._revision_sources = args
263
def revision_parents(self, revision_id):
264
for source in self._revision_sources:
266
return source.revision_parents(revision_id)
267
except (errors.WeaveRevisionNotPresent, errors.NoSuchRevision), e:
290
271
def get_revision(self, revision_id):
291
272
for source in self._revision_sources:
279
def get_revision_graph(self, revision_id):
281
for source in self._revision_sources:
283
result.update(source.get_revision_graph(revision_id))
284
except (errors.WeaveRevisionNotPresent, errors.NoSuchRevision), e:
298
290
def lock_read(self):
299
291
for source in self._revision_sources:
300
292
source.lock_read()