2024-02-10 19:09:11 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
2024-02-12 19:35:14 +01:00
|
|
|
import hashlib
|
|
|
|
|
2024-02-10 19:09:11 +01:00
|
|
|
GIT_DIR = ".ugit"
|
|
|
|
|
|
|
|
|
|
|
|
def init():
|
|
|
|
Path.mkdir(GIT_DIR)
|
2024-02-12 19:35:14 +01:00
|
|
|
Path.mkdir(f"{GIT_DIR}/objects")
|
|
|
|
|
|
|
|
|
2024-02-15 20:20:00 +01:00
|
|
|
def hash_object(data, type_="blob"):
|
|
|
|
obj = type_.encode() + b"\x00" + data
|
|
|
|
oid = hashlib.sha1(obj).hexdigest()
|
2024-02-12 19:35:14 +01:00
|
|
|
with open(f"{GIT_DIR}/objects/{oid}", "wb") as out:
|
2024-02-15 20:20:00 +01:00
|
|
|
out.write(obj)
|
2024-02-12 19:35:14 +01:00
|
|
|
return oid
|
2024-02-14 20:24:33 +01:00
|
|
|
|
|
|
|
|
2024-02-15 20:20:00 +01:00
|
|
|
def get_object(oid, expected="blob"):
|
2024-02-14 20:24:33 +01:00
|
|
|
with open(f"{GIT_DIR}/objects/{oid}", "rb") as f:
|
2024-02-15 20:20:00 +01:00
|
|
|
obj = f.read()
|
|
|
|
|
|
|
|
type_, _, content = obj.partition(b"\x00")
|
|
|
|
type_ = type_.decode()
|
|
|
|
|
|
|
|
if expected is not None:
|
|
|
|
assert type_ == expected, f"Expected {expected}, got {type_}"
|
|
|
|
return content
|