From 9365c473fdc8a839555dbd4a0d2d063ad3e19532 Mon Sep 17 00:00:00 2001 From: Nathan McRae Date: Sun, 7 Jul 2024 12:07:24 -0700 Subject: [PATCH] Update object_find to accept more object spec types --- libwyag.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/libwyag.py b/libwyag.py index bf4ce4c..db54616 100644 --- a/libwyag.py +++ b/libwyag.py @@ -284,7 +284,34 @@ def cat_file(repo, obj, fmt=None): sys.stdout.buffer.write(obj.serialize()) def object_find(repo, name, fmt=None, follow=True): - return name + sha = object_resolve(repo, name) + + if not sha: + raise Exception(f"No such reference {name}") + + if len(sha) > 1: + raise Exception(f"Ambiguous reference {0}: Candidates are:\n - {1}.".format(name, "\n - ".join(sha))) + + sha = sha[0] + + if not fmt: + return sha + + while True: + obj = object_read(repo, sha) + + if obj.fmt == fmt: + return sha + + if not follow: + return None + + if obj.fmt == b'tag': + sha = obj.kvlm[b'object'].decode("ascii") + elif obj.fmt == b'commit' and fmt == b'tree': + sha = obj.kvlm[b'tree'].decode("ascii") + else: + return None argsp = argsubparsers.add_parser( "hash-object",