diff --git a/swh/provenance/model.py b/swh/provenance/model.py --- a/swh/provenance/model.py +++ b/swh/provenance/model.py @@ -35,6 +35,12 @@ ) return (x for x in self._revisions) + def __str__(self): + return ( + f"" + ) + class RevisionEntry: def __init__( @@ -48,29 +54,43 @@ self.date = date assert self.date is None or self.date.tzinfo is not None self.root = root - self._parents = parents - self._nodes: List[RevisionEntry] = [] - - def parents(self, archive: ArchiveInterface): - if self._parents is None: - revision = list(archive.revision_get([self.id])) - if revision: - self._parents = revision[0].parents - if self._parents and not self._nodes: - self._nodes = [ + self._parents_ids = parents + self._parents_entries: Optional[List[RevisionEntry]] = None + + def retrieve_parents(self, archive: ArchiveInterface): + if self._parents_entries is None: + if self._parents_ids is None: + revision = list(archive.revision_get([self.id])) + if revision: + self._parents_ids = revision[0].parents + else: + self._parents_ids = [] + + self._parents_entries = [ RevisionEntry( id=rev.id, root=rev.directory, date=rev.date.to_datetime(), parents=rev.parents, ) - for rev in archive.revision_get(self._parents) + for rev in archive.revision_get(self._parents_ids) if rev.date is not None ] - yield from self._nodes + + @property + def parents(self) -> Iterator["RevisionEntry"]: + if self._parents_entries is None: + raise RuntimeError( + "Parents of this node has not yet been retrieved. " + "Please call retrieve_parents() before using this property." + ) + return (x for x in self._parents_entries) def __str__(self): - return f"" + return ( + f"" + ) class DirectoryEntry: diff --git a/swh/provenance/origin.py b/swh/provenance/origin.py --- a/swh/provenance/origin.py +++ b/swh/provenance/origin.py @@ -96,7 +96,8 @@ else: # This revision is a parent of another one in the history of the # relative revision. - for parent in current.parents(archive): + current.retrieve_parents(archive) + for parent in current.parents: visited = provenance.revision_visited(parent) if not visited: