Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/mercurial/from_disk.py
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | FLAG_PERMS = { | ||||
b"l": DentryPerms.symlink, | b"l": DentryPerms.symlink, | ||||
b"x": DentryPerms.executable_content, | b"x": DentryPerms.executable_content, | ||||
b"": DentryPerms.content, | b"": DentryPerms.content, | ||||
} # type: Dict[bytes, DentryPerms] | } # type: Dict[bytes, DentryPerms] | ||||
TEMPORARY_DIR_PREFIX_PATTERN = "swh.loader.mercurial.from_disk" | TEMPORARY_DIR_PREFIX_PATTERN = "swh.loader.mercurial.from_disk" | ||||
EXTID_TYPE = "hg-nodeid" | EXTID_TYPE = "hg-nodeid" | ||||
EXTID_VERSION: int = 1 | |||||
T = TypeVar("T") | T = TypeVar("T") | ||||
class CorruptedRevision(ValueError): | class CorruptedRevision(ValueError): | ||||
"""Raised when a revision is corrupted.""" | """Raised when a revision is corrupted.""" | ||||
def __init__(self, hg_nodeid: HgNodeId) -> None: | def __init__(self, hg_nodeid: HgNodeId) -> None: | ||||
▲ Show 20 Lines • Show All 204 Lines • ▼ Show 20 Lines | def _set_recorded_state(self, latest_snapshot: Snapshot) -> None: | ||||
self._latest_heads.extend( | self._latest_heads.extend( | ||||
extid.extid for extid in self._get_extids_for_targets(heads) | extid.extid for extid in self._get_extids_for_targets(heads) | ||||
) | ) | ||||
self._saved_tags.update( | self._saved_tags.update( | ||||
extid.extid for extid in self._get_extids_for_targets(tags) | extid.extid for extid in self._get_extids_for_targets(tags) | ||||
) | ) | ||||
def _get_extids_for_targets(self, targets: List[bytes]) -> List[ExtID]: | def _get_extids_for_targets(self, targets: List[bytes]) -> List[ExtID]: | ||||
# Get all Mercurial ExtIDs for the targets in the latest snapshot | """Get all Mercurial ExtIDs for the targets in the latest snapshot""" | ||||
extids = [ | extids = [ | ||||
extid | extid | ||||
for extid in self.storage.extid_get_from_target( | for extid in self.storage.extid_get_from_target( | ||||
identifiers.ObjectType.REVISION, targets | identifiers.ObjectType.REVISION, targets | ||||
) | ) | ||||
if extid.extid_type == EXTID_TYPE | if extid.extid_type == EXTID_TYPE and extid.extid_version == EXTID_VERSION | ||||
] | ] | ||||
if extids: | if extids: | ||||
# Filter out dangling extids, we need to load their target again | # Filter out dangling extids, we need to load their target again | ||||
revisions_missing = self.storage.revision_missing( | revisions_missing = self.storage.revision_missing( | ||||
[extid.target.object_id for extid in extids] | [extid.target.object_id for extid in extids] | ||||
) | ) | ||||
extids = [ | extids = [ | ||||
▲ Show 20 Lines • Show All 170 Lines • ▼ Show 20 Lines | def get_revision_id_from_hg_nodeid(self, hg_nodeid: HgNodeId) -> Sha1Git: | ||||
Returns: | Returns: | ||||
the sha1_git of the revision. | the sha1_git of the revision. | ||||
""" | """ | ||||
from_cache = self._revision_nodeid_to_sha1git.get(hg_nodeid) | from_cache = self._revision_nodeid_to_sha1git.get(hg_nodeid) | ||||
if from_cache is not None: | if from_cache is not None: | ||||
return from_cache | return from_cache | ||||
# The parent was not loaded in this run, get it from storage | # The parent was not loaded in this run, get it from storage | ||||
from_storage = self.storage.extid_get_from_extid(EXTID_TYPE, ids=[hg_nodeid]) | from_storage = [ | ||||
extid | |||||
for extid in self.storage.extid_get_from_extid(EXTID_TYPE, ids=[hg_nodeid]) | |||||
if extid.extid_version == EXTID_VERSION | |||||
] | |||||
msg = "Expected 1 match from storage for hg node %r, got %d" | msg = "Expected 1 match from storage for hg node %r, got %d" | ||||
assert len(from_storage) == 1, msg % (hg_nodeid.hex(), len(from_storage)) | assert len(from_storage) == 1, msg % (hg_nodeid.hex(), len(from_storage)) | ||||
return from_storage[0].target.object_id | return from_storage[0].target.object_id | ||||
def get_revision_parents(self, rev_ctx: hgutil.BaseContext) -> Tuple[Sha1Git, ...]: | def get_revision_parents(self, rev_ctx: hgutil.BaseContext) -> Tuple[Sha1Git, ...]: | ||||
"""Return the git sha1 of the parent revisions. | """Return the git sha1 of the parent revisions. | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | def store_revision(self, rev_ctx: hgutil.BaseContext) -> None: | ||||
self._revision_nodeid_to_sha1git[hg_nodeid] = revision.id | self._revision_nodeid_to_sha1git[hg_nodeid] = revision.id | ||||
self.storage.revision_add([revision]) | self.storage.revision_add([revision]) | ||||
# Save the mapping from SWHID to hg id | # Save the mapping from SWHID to hg id | ||||
revision_swhid = identifiers.CoreSWHID( | revision_swhid = identifiers.CoreSWHID( | ||||
object_type=identifiers.ObjectType.REVISION, object_id=revision.id, | object_type=identifiers.ObjectType.REVISION, object_id=revision.id, | ||||
) | ) | ||||
self.storage.extid_add( | self.storage.extid_add( | ||||
[ExtID(extid_type=EXTID_TYPE, extid=hg_nodeid, target=revision_swhid)] | [ | ||||
ExtID( | |||||
extid_type=EXTID_TYPE, | |||||
extid_version=EXTID_VERSION, | |||||
extid=hg_nodeid, | |||||
target=revision_swhid, | |||||
) | |||||
] | |||||
) | ) | ||||
def store_release(self, name: bytes, target: Sha1Git) -> Sha1Git: | def store_release(self, name: bytes, target: Sha1Git) -> Sha1Git: | ||||
"""Store a release given its name and its target. | """Store a release given its name and its target. | ||||
A release correspond to a user defined tag in mercurial. | A release correspond to a user defined tag in mercurial. | ||||
The mercurial api as a `tip` tag that must be ignored. | The mercurial api as a `tip` tag that must be ignored. | ||||
▲ Show 20 Lines • Show All 190 Lines • Show Last 20 Lines |