Add types to objects
This commit is contained in:
@@ -43,4 +43,4 @@ def hash_object(args):
|
|||||||
|
|
||||||
def cat_file(args):
|
def cat_file(args):
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
sys.stdout.buffer.write(data.get_object(args.object))
|
sys.stdout.buffer.write(data.get_object(args.object), expected=None)
|
||||||
|
|||||||
18
ugit/data.py
18
ugit/data.py
@@ -10,13 +10,21 @@ def init():
|
|||||||
Path.mkdir(f"{GIT_DIR}/objects")
|
Path.mkdir(f"{GIT_DIR}/objects")
|
||||||
|
|
||||||
|
|
||||||
def hash_object(data):
|
def hash_object(data, type_="blob"):
|
||||||
oid = hashlib.sha1(data).hexdigest()
|
obj = type_.encode() + b"\x00" + data
|
||||||
|
oid = hashlib.sha1(obj).hexdigest()
|
||||||
with open(f"{GIT_DIR}/objects/{oid}", "wb") as out:
|
with open(f"{GIT_DIR}/objects/{oid}", "wb") as out:
|
||||||
out.write(data)
|
out.write(obj)
|
||||||
return oid
|
return oid
|
||||||
|
|
||||||
|
|
||||||
def get_object(oid):
|
def get_object(oid, expected="blob"):
|
||||||
with open(f"{GIT_DIR}/objects/{oid}", "rb") as f:
|
with open(f"{GIT_DIR}/objects/{oid}", "rb") as f:
|
||||||
return f.read()
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user