Generalize HEAD to refs

This commit is contained in:
David Doblas Jiménez 2024-04-12 17:19:14 +02:00
parent cb8e744794
commit 1f947e6343
3 changed files with 9 additions and 9 deletions

View File

@ -83,7 +83,7 @@ def read_tree(tree_oid):
def commit(message):
commit = f"tree {write_tree()}\n"
HEAD = data.get_HEAD()
HEAD = data.get_ref("HEAD")
if HEAD:
commit += f"parent {HEAD}\n"
@ -92,7 +92,7 @@ def commit(message):
oid = data.hash_object(commit.encode(), "commit")
data.set_HEAD(oid)
data.update_ref("HEAD", oid)
return oid
@ -100,7 +100,7 @@ def commit(message):
def checkout(oid):
commit = get_commit(oid)
read_tree(commit.tree)
data.set_HEAD(oid)
data.update_ref("HEAD", oid)
Commit = namedtuple("Commit", ["tree", "parent", "message"])

View File

@ -85,7 +85,7 @@ def commit(args):
def log(args):
oid = args.oid or data.get_HEAD()
oid = args.oid or data.get_ref("HEAD")
while oid:
commit = base.get_commit(oid)
@ -101,5 +101,5 @@ def checkout(args):
def tag(args):
oid = args.oid or data.get_HEAD()
oid = args.oid or data.get_ref("HEAD")
base.create_tag(args.name, oid)

View File

@ -10,13 +10,13 @@ def init():
Path.mkdir(f"{GIT_DIR}/objects")
def set_HEAD(oid):
with open(f"{GIT_DIR}/HEAD", "w") as f:
def update_ref(ref, oid):
with open(f"{GIT_DIR}/{ref}", "w") as f:
f.write(oid)
def get_HEAD():
if Path.is_file(f"{GIT_DIR}/HEAD"):
def get_ref(ref):
if Path.is_file(f"{GIT_DIR}/{ref}"):
with open(f"{GIT_DIR}/HEAD") as f:
return f.read().strip()