DIY_GIT_in_Python/ugit/base.py

29 lines
824 B
Python
Raw Normal View History

2024-02-26 18:59:58 +01:00
from pathlib import Path
2024-02-21 20:46:59 +01:00
from . import data
2024-02-26 18:59:58 +01:00
def write_tree(directory="."):
2024-03-02 16:02:42 +01:00
entries = []
2024-02-26 18:59:58 +01:00
with Path.iterdir(directory) as it:
for entry in it:
full = f"{directory}/{entry.name}"
2024-02-28 19:45:34 +01:00
if is_ignored(full):
continue
2024-02-26 18:59:58 +01:00
if entry.is_file(follow_symlinks=False):
2024-03-02 16:02:42 +01:00
type_ = "blob"
2024-02-28 19:51:24 +01:00
with open(full, "rb") as f:
2024-03-02 16:02:42 +01:00
oid = data.hash_object(f.read())
2024-02-26 18:59:58 +01:00
elif entry.is_dir(follow_symlinks=False):
2024-03-02 16:02:42 +01:00
type_ = "tree"
oid = write_tree(full)
entries.append((entry.name, oid, type_))
tree = "".join(f"{type_} {oid} {name}\n" for name, oid, type_ in sorted(entries))
return data.hash_object(tree.encode(), "tree")
2024-02-28 19:45:34 +01:00
def is_ignored(path):
return ".ugit" in path.split("/")