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",