From a010615cf2ffdc572f1e41caab47ff236cdb4778 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Wed, 14 Feb 2024 20:24:33 +0100 Subject: [PATCH] Print hashed objects --- ugit/cli.py | 13 ++++++++++++- ugit/data.py | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ugit/cli.py b/ugit/cli.py index 5c6bd3c..56edbd8 100644 --- a/ugit/cli.py +++ b/ugit/cli.py @@ -1,6 +1,8 @@ -import argparse from pathlib import Path +import argparse +import sys + from . import data @@ -18,6 +20,10 @@ def parse_args(): init_parser = commands.add_parser("init") init_parser.set_defaults(func=init) + cat_file_parser = commands.add_parser("cat-file") + cat_file_parser.set_defaults(func=cat_file) + cat_file_parser.add_argument("object") + hash_object_parser = commands.add_parser("hash-object") hash_object_parser.set_defaults(func=hash_object) hash_object_parser.add_argument("file") @@ -33,3 +39,8 @@ def init(args): def hash_object(args): with open(args.file, "rb") as f: print(data.hash_object(f.read())) + + +def cat_file(args): + sys.stdout.flush() + sys.stdout.buffer.write(data.get_object(args.object)) diff --git a/ugit/data.py b/ugit/data.py index 684f1a2..756df6d 100644 --- a/ugit/data.py +++ b/ugit/data.py @@ -15,3 +15,8 @@ def hash_object(data): with open(f"{GIT_DIR}/objects/{oid}", "wb") as out: out.write(data) return oid + + +def get_object(oid): + with open(f"{GIT_DIR}/objects/{oid}", "rb") as f: + return f.read()