Generalize HEAD to refs

This commit is contained in:
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): def commit(message):
commit = f"tree {write_tree()}\n" commit = f"tree {write_tree()}\n"
HEAD = data.get_HEAD() HEAD = data.get_ref("HEAD")
if HEAD: if HEAD:
commit += f"parent {HEAD}\n" commit += f"parent {HEAD}\n"
@@ -92,7 +92,7 @@ def commit(message):
oid = data.hash_object(commit.encode(), "commit") oid = data.hash_object(commit.encode(), "commit")
data.set_HEAD(oid) data.update_ref("HEAD", oid)
return oid return oid
@@ -100,7 +100,7 @@ def commit(message):
def checkout(oid): def checkout(oid):
commit = get_commit(oid) commit = get_commit(oid)
read_tree(commit.tree) read_tree(commit.tree)
data.set_HEAD(oid) data.update_ref("HEAD", oid)
Commit = namedtuple("Commit", ["tree", "parent", "message"]) Commit = namedtuple("Commit", ["tree", "parent", "message"])

View File

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

View File

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