Debian Codegraph @

vcsgraph/graph.py

raw symbols SCIP
1 # Copyright (C) 2007-2011 Canonical Ltd
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17 """Graph algorithms for version control systems."""
18
19 __all__ = [
20 "CachingParentsProvider",
21 "CallableToParentsProviderAdapter",
22 "DictParentsProvider",
23 "FrozenHeadsCache",
24 "GraphThunkIdsToKeys",
25 "HeadsCache",
26 "StackedParentsProvider",
27 "collapse_linear_regions",
28 "invert_parent_map",
29 ]
30
31 from ._graph_rs import (
32 CachingParentsProvider,
33 CallableToParentsProviderAdapter,
34 DictParentsProvider,
35 FrozenHeadsCache,
36 GraphThunkIdsToKeys,
37 HeadsCache,
38 StackedParentsProvider,
39 _RustGraph,
40 collapse_linear_regions,
41 invert_parent_map,
42 )
43 from ._graph_rs import (
44 _BreadthFirstSearcher as _RustBreadthFirstSearcher,
45 )
46
47 # NULL_REVISION constant
48 NULL_REVISION = b"null:"
49
50 # DIAGRAM of terminology
51 # A
52 # /\
53 # B C
54 # | |\
55 # D E F
56 # |\/| |
57 # |/\|/
58 # G H
59 #
60 # In this diagram, relative to G and H:
61 # A, B, C, D, E are common ancestors.
62 # C, D and E are border ancestors, because each has a non-common descendant.
63 # D and E are least common ancestors because none of their descendants are
64 # common ancestors.
65 # C is not a least common ancestor because its descendant, E, is a common
66 # ancestor.
67 #
68 # The find_unique_lca algorithm will pick A in two steps:
69 # 1. find_lca('G', 'H') => ['D', 'E']
70 # 2. Since len(['D', 'E']) > 1, find_lca('D', 'E') => ['A']
71
72
73 class Graph:
74 """Provide incremental access to revision graphs.
75
76 This is the generic implementation; it is intended to be subclassed to
77 specialize it for other repository types.
78 """
79
80 def __init__(self, parents_provider):
81 """Construct a Graph that uses several graphs as its input.
82
83 This should not normally be invoked directly, because there may be
84 specialized implementations for particular repository types. See
85 Repository.get_graph().
86
87 :param parents_provider: An object providing a get_parent_map call
88 conforming to the behavior of
89 StackedParentsProvider.get_parent_map.
90 """
91 if getattr(parents_provider, "get_parents", None) is not None:
92 self.get_parents = parents_provider.get_parents
93 if getattr(parents_provider, "get_parent_map", None) is not None:
94 self.get_parent_map = parents_provider.get_parent_map
95 self._parents_provider = parents_provider
96 # Rust-backed helper for methods that have been ported. Uses the same
97 # parents provider; the Rust side calls back into Python via a GIL
98 # adapter when it needs a parent lookup.
99 self._rs = _RustGraph(parents_provider)
100
101 def __repr__(self):
102 return f"Graph({self._parents_provider!r})"
103
104 def find_lca(self, *revisions):
105 """Determine the lowest common ancestors of the provided revisions.
106
107 A lowest common ancestor is a common ancestor none of whose
108 descendants are common ancestors. In graphs, unlike trees, there may
109 be multiple lowest common ancestors.
110
111 This algorithm has two phases. Phase 1 identifies border ancestors,
112 and phase 2 filters border ancestors to determine lowest common
113 ancestors.
114
115 In phase 1, border ancestors are identified, using a breadth-first
116 search starting at the bottom of the graph. Searches are stopped
117 whenever a node or one of its descendants is determined to be common
118
119 In phase 2, the border ancestors are filtered to find the least
120 common ancestors. This is done by searching the ancestries of each
121 border ancestor.
122
123 Phase 2 is perfomed on the principle that a border ancestor that is
124 not an ancestor of any other border ancestor is a least common
125 ancestor.
126
127 Searches are stopped when they find a node that is determined to be a
128 common ancestor of all border ancestors, because this shows that it
129 cannot be a descendant of any border ancestor.
130
131 The scaling of this operation should be proportional to:
132
133 1. The number of uncommon ancestors
134 2. The number of border ancestors
135 3. The length of the shortest path between a border ancestor and an
136 ancestor of all border ancestors.
137 """
138 return self._rs.find_lca(revisions)
139
140 def find_difference(self, left_revision, right_revision):
141 """Determine the graph difference between two revisions."""
142 return self._rs.find_difference(left_revision, right_revision)
143
144 def find_descendants(self, old_key, new_key):
145 """Find descendants of old_key that are ancestors of new_key."""
146 return self._rs.find_descendants(old_key, new_key)
147
148 def _find_descendant_ancestors(self, old_key, new_key):
149 """Find ancestors of new_key that may be descendants of old_key."""
150 return self._rs._find_descendant_ancestors(old_key, new_key)
151
152 def _remove_simple_descendants(self, revisions, parent_map):
153 """Remove revisions which are children of other ones in the set.
154
155 This doesn't do any graph searching, it just checks the immediate
156 parent_map to find if there are any children which can be removed.
157
158 :param revisions: A set of revision_ids
159 :return: A set of revision_ids with the children removed
160 """
161 return self._rs._remove_simple_descendants(revisions, parent_map)
162
163 def get_child_map(self, keys):
164 """Get a mapping from parents to children of the specified keys.
165
166 This is simply the inversion of get_parent_map. Only supplied keys
167 will be discovered as children.
168 :return: a dict of key:child_list for keys.
169 """
170 parent_map = self._parents_provider.get_parent_map(keys)
171 parent_child = {}
172 for child, parents in sorted(parent_map.items()):
173 for parent in parents:
174 parent_child.setdefault(parent, []).append(child)
175 return parent_child
176
177 def find_distance_to_null(self, target_revision_id, known_revision_ids):
178 """Find the left-hand distance to the NULL_REVISION.
179
180 (This can also be considered the revno of a branch at
181 target_revision_id.)
182
183 :param target_revision_id: A revision_id which we would like to know
184 the revno for.
185 :param known_revision_ids: [(revision_id, revno)] A list of known
186 revno, revision_id tuples. We'll use this to seed the search.
187 """
188 return self._rs.find_distance_to_null(target_revision_id, known_revision_ids)
189
190 def find_lefthand_distances(self, keys):
191 """Find the distance to null for all the keys in keys.
192
193 :param keys: keys to lookup.
194 :return: A dict key->distance for all of keys.
195 """
196 return self._rs.find_lefthand_distances(keys)
197
198 def find_unique_ancestors(self, unique_revision, common_revisions):
199 """Find the unique ancestors for a revision versus others.
200
201 This returns the ancestry of unique_revision, excluding all revisions
202 in the ancestry of common_revisions. If unique_revision is in the
203 ancestry, then the empty set will be returned.
204
205 :param unique_revision: The revision_id whose ancestry we are
206 interested in.
207 (XXX: Would this API be better if we allowed multiple revisions on
208 to be searched here?)
209 :param common_revisions: Revision_ids of ancestries to exclude.
210 :return: A set of revisions in the ancestry of unique_revision
211 """
212 return self._rs.find_unique_ancestors(unique_revision, common_revisions)
213
214 def get_parent_map(self, revisions): # type: ignore
215 """Get a map of key:parent_list for revisions.
216
217 This implementation delegates to get_parents, for old parent_providers
218 that do not supply get_parent_map.
219 """
220 result = {}
221 for rev, parents in self.get_parents(revisions):
222 if parents is not None:
223 result[rev] = parents
224 return result
225
226 def _make_breadth_first_searcher(self, revisions):
227 return _RustBreadthFirstSearcher(revisions, self)
228
229 def heads(self, keys):
230 """Return the heads from amongst keys.
231
232 This is done by searching the ancestries of each key. Any key that is
233 reachable from another key is not returned; all the others are.
234
235 This operation scales with the relative depth between any two keys. If
236 any two keys are completely disconnected all ancestry of both sides
237 will be retrieved.
238
239 :param keys: An iterable of keys.
240 :return: A set of the heads. Note that as a set there is no ordering
241 information. Callers will need to filter their input to create
242 order if they need it.
243 """
244 return self._rs.heads(keys)
245
246 def find_merge_order(self, tip_revision_id, lca_revision_ids):
247 """Find the order that each revision was merged into tip.
248
249 This basically just walks backwards with a stack, and walks left-first
250 until it finds a node to stop.
251 """
252 return self._rs.find_merge_order(tip_revision_id, lca_revision_ids)
253
254 def find_lefthand_merger(self, merged_key, tip_key):
255 """Find the first lefthand ancestor of tip_key that merged merged_key.
256
257 We do this by first finding the descendants of merged_key, then
258 walking through the lefthand ancestry of tip_key until we find a key
259 that doesn't descend from merged_key. Its child is the key that
260 merged merged_key.
261
262 :return: The first lefthand ancestor of tip_key to merge merged_key.
263 merged_key if it is a lefthand ancestor of tip_key.
264 None if no ancestor of tip_key merged merged_key.
265 """
266 return self._rs.find_lefthand_merger(merged_key, tip_key)
267
268 def find_unique_lca(self, left_revision, right_revision, count_steps=False):
269 """Find a unique LCA.
270
271 Find lowest common ancestors. If there is no unique common
272 ancestor, find the lowest common ancestors of those ancestors.
273
274 Iteration stops when a unique lowest common ancestor is found.
275 The graph origin is necessarily a unique lowest common ancestor.
276
277 Note that None is not an acceptable substitute for NULL_REVISION.
278 in the input for this method.
279
280 :param count_steps: If True, the return value will be a tuple of
281 (unique_lca, steps) where steps is the number of times that
282 find_lca was run. If False, only unique_lca is returned.
283 """
284 return self._rs.find_unique_lca(left_revision, right_revision, count_steps)
285
286 def iter_ancestry(self, revision_ids):
287 """Iterate the ancestry of this revision.
288
289 :param revision_ids: Nodes to start the search
290 :return: Yield tuples mapping a revision_id to its parents for the
291 ancestry of revision_id.
292 Ghosts will be returned with None as their parents, and nodes
293 with no parents will have NULL_REVISION as their only parent. (As
294 defined by get_parent_map.)
295 There will also be a node for (NULL_REVISION, ())
296 """
297 yield from self._rs.iter_ancestry(revision_ids)
298
299 def iter_lefthand_ancestry(self, start_key, stop_keys=None):
300 """Iterate the lefthand ancestry of start_key.
301
302 Yields revisions in lefthand order. Callers may break early; the
303 walk is lazy and does not query further than the last yielded key.
304 Raises RevisionNotPresent if the walk reaches a key that is not in
305 the parents provider.
306 """
307 return self._rs.iter_lefthand_ancestry(start_key, stop_keys)
308
309 def iter_topo_order(self, revisions):
310 """Iterate through the input revisions in topological order.
311
312 This sorting only ensures that parents come before their children.
313 An ancestor may sort after a descendant if the relationship is not
314 visible in the supplied list of revisions.
315 """
316 return iter(self._rs.iter_topo_order(revisions))
317
318 def is_ancestor(self, candidate_ancestor, candidate_descendant):
319 """Determine whether a revision is an ancestor of another.
320
321 We answer this using heads() as heads() has the logic to perform the
322 smallest number of parent lookups to determine the ancestral
323 relationship between N revisions.
324 """
325 return self._rs.is_ancestor(candidate_ancestor, candidate_descendant)
326
327 def is_between(self, revid, lower_bound_revid, upper_bound_revid):
328 """Determine whether a revision is between two others.
329
330 returns true if and only if:
331 lower_bound_revid <= revid <= upper_bound_revid
332 """
333 return self._rs.is_between(revid, lower_bound_revid, upper_bound_revid)
334
335
336 _counters = [0, 0, 0, 0, 0, 0, 0]
337
338 # Import KnownGraph to make it available through this module for compatibility