Read-tree extract tree from object
This commit is contained in:
33
ugit/base.py
33
ugit/base.py
@@ -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("/")
|
||||
|
||||
Reference in New Issue
Block a user