Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/git/converters.py
Show All 21 Lines | from swh.model.model import ( | ||||
Revision, | Revision, | ||||
RevisionType, | RevisionType, | ||||
SkippedContent, | SkippedContent, | ||||
TargetType, | TargetType, | ||||
Timestamp, | Timestamp, | ||||
TimestampWithTimezone, | TimestampWithTimezone, | ||||
) | ) | ||||
COMMIT_MODE_MASK = 0o160000 | |||||
"""Mode/perms of tree entries that point to a commit. | |||||
They are normally equal to this mask, but may have more bits set to 1.""" | |||||
TREE_MODE_MASK = 0o040000 | |||||
"""Mode/perms of tree entries that point to a tree. | |||||
They are normally equal to this mask, but may have more bits set to 1.""" | |||||
class HashMismatch(Exception): | class HashMismatch(Exception): | ||||
pass | pass | ||||
def check_id(obj: HashableObject) -> None: | def check_id(obj: HashableObject) -> None: | ||||
real_id = obj.compute_hash() | real_id = obj.compute_hash() | ||||
if obj.id != real_id: | if obj.id != real_id: | ||||
Show All 39 Lines | |||||
def dulwich_tree_to_directory(obj: ShaFile) -> Directory: | def dulwich_tree_to_directory(obj: ShaFile) -> Directory: | ||||
"""Format a tree as a directory""" | """Format a tree as a directory""" | ||||
if obj.type_name != b"tree": | if obj.type_name != b"tree": | ||||
raise ValueError("Argument is not a tree.") | raise ValueError("Argument is not a tree.") | ||||
tree = cast(Tree, obj) | tree = cast(Tree, obj) | ||||
entries = [] | entries = [] | ||||
entry_mode_map = { | |||||
0o040000: "dir", | |||||
0o160000: "rev", | |||||
0o100644: "file", | |||||
0o100755: "file", | |||||
0o120000: "file", | |||||
} | |||||
for entry in tree.iteritems(): | for entry in tree.iteritems(): | ||||
if entry.mode & COMMIT_MODE_MASK == COMMIT_MODE_MASK: | |||||
type_ = "rev" | |||||
elif entry.mode & TREE_MODE_MASK == TREE_MODE_MASK: | |||||
type_ = "dir" | |||||
else: | |||||
type_ = "file" | |||||
entries.append( | entries.append( | ||||
DirectoryEntry( | DirectoryEntry( | ||||
type=entry_mode_map.get(entry.mode, "file"), | type=type_, | ||||
perms=entry.mode, | perms=entry.mode, | ||||
name=entry.path, | name=entry.path, | ||||
target=hash_to_bytes(entry.sha.decode("ascii")), | target=hash_to_bytes(entry.sha.decode("ascii")), | ||||
) | ) | ||||
) | ) | ||||
dir_ = Directory(id=tree.sha().digest(), entries=tuple(entries),) | dir_ = Directory(id=tree.sha().digest(), entries=tuple(entries),) | ||||
check_id(dir_) | check_id(dir_) | ||||
▲ Show 20 Lines • Show All 112 Lines • Show Last 20 Lines |