Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/algos/snapshot.py
# Copyright (C) 2018-2020 The Software Heritage developers | # Copyright (C) 2018-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from typing import Iterator, List, Optional, Tuple, cast | from typing import Iterator, List, Optional, Tuple | ||||
from swh.model.hashutil import hash_to_hex | from swh.model.hashutil import hash_to_hex | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
OriginVisit, | OriginVisit, | ||||
OriginVisitStatus, | OriginVisitStatus, | ||||
Sha1Git, | Sha1Git, | ||||
Snapshot, | Snapshot, | ||||
SnapshotBranch, | SnapshotBranch, | ||||
▲ Show 20 Lines • Show All 176 Lines • ▼ Show 20 Lines | snapshot = storage.snapshot_get_branches( | ||||
snapshot_id, branches_from=alias_name, branches_count=1 | snapshot_id, branches_from=alias_name, branches_count=1 | ||||
) | ) | ||||
if snapshot is None: | if snapshot is None: | ||||
return None | return None | ||||
if alias_name not in snapshot["branches"]: | if alias_name not in snapshot["branches"]: | ||||
return ([], None) | return ([], None) | ||||
branch_info = snapshot["branches"][alias_name] | last_branch = snapshot["branches"][alias_name] | ||||
branches = [branch_info] | branches = [] | ||||
seen_aliases = {alias_name} | seen_aliases = {alias_name} | ||||
while ( | while ( | ||||
branch_info is not None | last_branch is not None | ||||
and branch_info.target_type == TargetType.ALIAS | and last_branch.target_type == TargetType.ALIAS | ||||
and branch_info.target not in seen_aliases | and last_branch.target not in seen_aliases | ||||
): | ): | ||||
alias_target = branch_info.target | branches.append(last_branch) | ||||
alias_target = last_branch.target | |||||
snapshot = storage.snapshot_get_branches( | snapshot = storage.snapshot_get_branches( | ||||
snapshot_id, branches_from=alias_target, branches_count=1 | snapshot_id, branches_from=alias_target, branches_count=1 | ||||
) | ) | ||||
assert snapshot is not None | assert snapshot is not None | ||||
if alias_target not in snapshot["branches"]: | last_branch = snapshot["branches"].get(alias_target) | ||||
anlambert: You can remove these instructions as the while iteration will stop when `last_branch` is `None` | |||||
Done Inline Actionsgood catch! vlorentz: good catch! | |||||
break | |||||
seen_aliases.add(alias_target) | seen_aliases.add(alias_target) | ||||
branch_info = snapshot["branches"][alias_target] | |||||
branches.append(branch_info) | |||||
return (cast(List[SnapshotBranch], branches[:-1]), branches[-1]) | return (branches, last_branch) |
You can remove these instructions as the while iteration will stop when last_branch is None