Changeset View
Changeset View
Standalone View
Standalone View
swh/model/identifiers.py
Show First 20 Lines • Show All 851 Lines • ▼ Show 20 Lines | |||||
def extid_identifier(extid: Dict[str, Any]) -> str: | def extid_identifier(extid: Dict[str, Any]) -> str: | ||||
"""Return the intrinsic identifier for an ExtID object. | """Return the intrinsic identifier for an ExtID object. | ||||
An ExtID identifier is a salted sha1 (using the git hashing algorithm with | An ExtID identifier is a salted sha1 (using the git hashing algorithm with | ||||
the ``extid`` object type) of a manifest following the format: | the ``extid`` object type) of a manifest following the format: | ||||
``` | ``` | ||||
extid_type $StrWithoutSpaces | extid_type $StrWithoutSpaces | ||||
[extid_version $Str] | |||||
extid $Bytes | extid $Bytes | ||||
target $CoreSwhid | target $CoreSwhid | ||||
``` | ``` | ||||
$StrWithoutSpaces is an ASCII string, and may not contain spaces. | $StrWithoutSpaces is an ASCII string, and may not contain spaces. | ||||
Newlines in $Bytes are escaped as with other git fields, ie. by adding a | Newlines in $Bytes are escaped as with other git fields, ie. by adding a | ||||
space after them. | space after them. | ||||
The extid_version line is only generated if the version is non-zero. | |||||
Returns: | Returns: | ||||
str: the intrinsic identifier for `extid` | str: the intrinsic identifier for `extid` | ||||
""" | """ | ||||
headers = [ | headers = [ | ||||
(b"extid_type", extid["extid_type"].encode("ascii")), | (b"extid_type", extid["extid_type"].encode("ascii")), | ||||
(b"extid", extid["extid"]), | |||||
(b"target", str(extid["target"]).encode("ascii")), | |||||
] | ] | ||||
extid_version = extid.get("extid_version", 0) | |||||
if extid_version != 0: | |||||
headers.append((b"extid_version", str(extid_version).encode("ascii"))) | |||||
headers.extend( | |||||
[(b"extid", extid["extid"]), (b"target", str(extid["target"]).encode("ascii")),] | |||||
) | |||||
git_object = format_git_object_from_headers("extid", headers) | git_object = format_git_object_from_headers("extid", headers) | ||||
return hashlib.new("sha1", git_object).hexdigest() | return hashlib.new("sha1", git_object).hexdigest() | ||||
# type of the "object_type" attribute of the SWHID class; either | # type of the "object_type" attribute of the SWHID class; either | ||||
# ObjectType or ExtendedObjectType | # ObjectType or ExtendedObjectType | ||||
_TObjectType = TypeVar("_TObjectType", ObjectType, ExtendedObjectType) | _TObjectType = TypeVar("_TObjectType", ObjectType, ExtendedObjectType) | ||||
▲ Show 20 Lines • Show All 382 Lines • Show Last 20 Lines |