Read-tree extract tree from object

This commit is contained in:
2024-03-02 16:18:48 +01:00
parent 6f5fe864a9
commit db8c1379c2
2 changed files with 43 additions and 4 deletions

View File

@@ -1,4 +1,4 @@
from pathlib import Path
from pathlib import Path, PurePath
from . import data
@@ -24,5 +24,36 @@ def write_tree(directory="."):
return data.hash_object(tree.encode(), "tree")
def _iter_tree_entries(oid):
if not oid:
return
tree = data.get_object(oid, "tree")
for entry in tree.decode().splitlines():
type_, oid, name = entry.split(" ", 2)
yield type_, oid, name
def get_tree(oid, base_path=""):
result = {}
for type_, oid, name in _iter_tree_entries(oid):
assert "/" not in name
assert name not in ("..", ".")
path = base_path + name
if type_ == "blob":
result[path] = oid
elif type_ == "tree":
result.update(get_tree(oid, f"{path}/"))
else:
assert False, f"Unknown tree entry {type_}"
return result
def read_tree(tree_oid):
for path, oid in get_tree(tree_oid, base_path="./").items():
Path.mkdir(PurePath.parent(path), exist_ok=True)
with open(path, "wb") as f:
f.write(data.get_object(oid))
def is_ignored(path):
return ".ugit" in path.split("/")