Changeset View
Changeset View
Standalone View
Standalone View
swh/provenance/origin.py
Show First 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | ) -> None: | ||||
) | ) | ||||
def origin_add_revision( | def origin_add_revision( | ||||
provenance: ProvenanceInterface, | provenance: ProvenanceInterface, | ||||
origin: OriginEntry, | origin: OriginEntry, | ||||
graph: HistoryNode, | graph: HistoryNode, | ||||
) -> None: | ) -> None: | ||||
# XXX: simplified version of the origin-revision algorithm. This is generating flat | |||||
# models for the history of all head revisions. No previous result is reused now! | |||||
# The previous implementation was missing some paths from origins to certain | |||||
# revisions due to a wrong reuse logic. | |||||
# head is treated separately since it should always be added to the given origin | # head is treated separately since it should always be added to the given origin | ||||
head = graph.entry | head = graph.entry | ||||
check_preferred_origin(provenance, origin, head) | check_preferred_origin(provenance, origin, head) | ||||
provenance.revision_add_to_origin(origin, head) | provenance.revision_add_to_origin(origin, head) | ||||
# head's history should be recursively iterated starting from its parents | # head's history should be recursively iterated starting from its parents | ||||
stack = list(graph.parents) | stack = list(graph.parents) | ||||
while stack: | while stack: | ||||
current = stack.pop() | current = stack.pop() | ||||
check_preferred_origin(provenance, origin, current.entry) | check_preferred_origin(provenance, origin, current.entry) | ||||
if current.visited: | # create a link between it and the head, and recursively walk its history | ||||
# if current revision was already visited just add it to the current origin | |||||
# and stop recursion (its history has already been flattened) | |||||
provenance.revision_add_to_origin(origin, current.entry) | |||||
else: | |||||
# if current revision was not visited before create a link between it and | |||||
# the head, and recursively walk its history | |||||
provenance.revision_add_before_revision(head, current.entry) | provenance.revision_add_before_revision(head, current.entry) | ||||
for parent in current.parents: | for parent in current.parents: | ||||
stack.append(parent) | stack.append(parent) | ||||
def check_preferred_origin( | def check_preferred_origin( | ||||
provenance: ProvenanceInterface, | provenance: ProvenanceInterface, | ||||
origin: OriginEntry, | origin: OriginEntry, | ||||
revision: RevisionEntry, | revision: RevisionEntry, | ||||
) -> None: | ) -> None: | ||||
# if the revision has no preferred origin just set the given origin as the | # if the revision has no preferred origin just set the given origin as the | ||||
# preferred one. TODO: this should be improved in the future! | # preferred one. TODO: this should be improved in the future! | ||||
preferred = provenance.revision_get_preferred_origin(revision) | preferred = provenance.revision_get_preferred_origin(revision) | ||||
if preferred is None: | if preferred is None: | ||||
provenance.revision_set_preferred_origin(origin, revision) | provenance.revision_set_preferred_origin(origin, revision) |