Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/git/tests/test_from_disk.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 | ||||
import copy | import copy | ||||
import datetime | import datetime | ||||
import os.path | import os.path | ||||
import dulwich.repo | import dulwich.repo | ||||
import pytest | import pytest | ||||
from unittest import TestCase | from unittest import TestCase | ||||
from swh.model.model import Snapshot, SnapshotBranch, TargetType | from swh.model.model import Snapshot | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
from swh.loader.git.from_disk import GitLoaderFromDisk, GitLoaderFromArchive | from swh.loader.git.from_disk import GitLoaderFromDisk, GitLoaderFromArchive | ||||
from swh.loader.tests import ( | from swh.loader.tests import ( | ||||
assert_last_visit_matches, | assert_last_visit_matches, | ||||
check_snapshot, | check_snapshot, | ||||
get_stats, | get_stats, | ||||
prepare_repository_from_archive, | prepare_repository_from_archive, | ||||
) | ) | ||||
SNAPSHOT_ID = "a23699280a82a043f8c0994cf1631b568f716f95" | SNAPSHOT_ID = "a23699280a82a043f8c0994cf1631b568f716f95" | ||||
SNAPSHOT1 = { | SNAPSHOT1 = { | ||||
"id": SNAPSHOT_ID, | "id": SNAPSHOT_ID, | ||||
"branches": { | "branches": { | ||||
"HEAD": {"target": "refs/heads/master", "target_type": "alias",}, | b"HEAD": {"target": b"refs/heads/master", "target_type": "alias",}, | ||||
"refs/heads/master": { | b"refs/heads/master": { | ||||
"target": "2f01f5ca7e391a2f08905990277faf81e709a649", | "target": hash_to_bytes("2f01f5ca7e391a2f08905990277faf81e709a649"), | ||||
"target_type": "revision", | "target_type": "revision", | ||||
}, | }, | ||||
"refs/heads/branch1": { | b"refs/heads/branch1": { | ||||
"target": "b0a77609903f767a2fd3d769904ef9ef68468b87", | "target": hash_to_bytes("b0a77609903f767a2fd3d769904ef9ef68468b87"), | ||||
"target_type": "revision", | "target_type": "revision", | ||||
}, | }, | ||||
"refs/heads/branch2": { | b"refs/heads/branch2": { | ||||
"target": "bd746cd1913721b269b395a56a97baf6755151c2", | "target": hash_to_bytes("bd746cd1913721b269b395a56a97baf6755151c2"), | ||||
"target_type": "revision", | "target_type": "revision", | ||||
}, | }, | ||||
"refs/tags/branch2-after-delete": { | b"refs/tags/branch2-after-delete": { | ||||
"target": "bd746cd1913721b269b395a56a97baf6755151c2", | "target": hash_to_bytes("bd746cd1913721b269b395a56a97baf6755151c2"), | ||||
"target_type": "revision", | "target_type": "revision", | ||||
}, | }, | ||||
"refs/tags/branch2-before-delete": { | b"refs/tags/branch2-before-delete": { | ||||
"target": "1135e94ccf73b5f9bd6ef07b3fa2c5cc60bba69b", | "target": hash_to_bytes("1135e94ccf73b5f9bd6ef07b3fa2c5cc60bba69b"), | ||||
"target_type": "revision", | "target_type": "revision", | ||||
}, | }, | ||||
}, | }, | ||||
} | } | ||||
# directory hashes obtained with: | # directory hashes obtained with: | ||||
# gco b6f40292c4e94a8f7e7b4aff50e6c7429ab98e2a | # gco b6f40292c4e94a8f7e7b4aff50e6c7429ab98e2a | ||||
# swh-hashtree --ignore '.git' --path . | # swh-hashtree --ignore '.git' --path . | ||||
▲ Show 20 Lines • Show All 249 Lines • ▼ Show 20 Lines | def test_load_filter_branches(self): | ||||
# Add branches to the repository on disk; some should be filtered by | # Add branches to the repository on disk; some should be filtered by | ||||
# the loader, some should not. | # the loader, some should not. | ||||
for branch_name in filtered_branches | unfiltered_branches: | for branch_name in filtered_branches | unfiltered_branches: | ||||
self.repo[branch_name] = self.repo[b"refs/heads/master"] | self.repo[branch_name] = self.repo[b"refs/heads/master"] | ||||
# Generate the expected snapshot from SNAPSHOT1 (which is the original | # Generate the expected snapshot from SNAPSHOT1 (which is the original | ||||
# state of the git repo)... | # state of the git repo)... | ||||
branches = {} | branches = SNAPSHOT1["branches"].copy() | ||||
for branch_name, branch_dict in SNAPSHOT1["branches"].items(): | |||||
target_type_name = branch_dict["target_type"] | |||||
target_obj = branch_dict["target"] | |||||
if target_type_name != "alias": | |||||
target = bytes.fromhex(target_obj) | |||||
else: | |||||
target = target_obj.encode() | |||||
branch = SnapshotBranch( | |||||
target=target, target_type=TargetType(target_type_name) | |||||
) | |||||
branches[branch_name.encode()] = branch | |||||
# ... and the unfiltered_branches, which are all pointing to the same | # ... and the unfiltered_branches, which are all pointing to the same | ||||
# commit as "refs/heads/master". | # commit as "refs/heads/master". | ||||
for branch_name in unfiltered_branches: | for branch_name in unfiltered_branches: | ||||
branches[branch_name] = branches[b"refs/heads/master"] | branches[branch_name] = branches[b"refs/heads/master"] | ||||
expected_snapshot = Snapshot(branches=branches) | expected_snapshot = Snapshot(branches=branches) | ||||
▲ Show 20 Lines • Show All 88 Lines • Show Last 20 Lines |