From 98ef42e0d3735d7ffb8cde2f1a7d00b85eaed45e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Val=C3=A9rio?= Date: Tue, 29 Sep 2020 11:08:12 +0100 Subject: [PATCH] step five: print hashed objects --- src/data.rs | 5 +++++ src/main.rs | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/data.rs b/src/data.rs index 57d5eaf..297dc13 100644 --- a/src/data.rs +++ b/src/data.rs @@ -24,3 +24,8 @@ pub fn hash_object(content: &Vec) -> String { return s; } + +pub fn get_object(hash: String) -> String { + return fs::read_to_string(format!("{}/{}/{}", RGIT_DIR, "objects", hash)) + .expect("Could not find a matching object"); +} diff --git a/src/main.rs b/src/main.rs index bb0fff3..39f1a2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,11 +13,17 @@ fn main() { .about("created an hash for an object") .arg(Arg::with_name("file").index(1).required(true)), ) + .subcommand( + SubCommand::with_name("cat-file") + .about("outputs the original object from the provided hash") + .arg(Arg::with_name("hash").index(1).required(true)), + ) .get_matches(); match matches.subcommand_name() { Some("init") => init(), Some("hash-object") => hash_object(matches), + Some("cat-file") => cat_file(matches), _ => println!("unknown sub command"), } } @@ -37,3 +43,10 @@ fn hash_object(matches: ArgMatches) { println!("{}", hash); } } + +fn cat_file(matches: ArgMatches) { + if let Some(cmd_matches) = matches.subcommand_matches("cat-file") { + let file_contents = data::get_object(cmd_matches.value_of("hash").unwrap().to_owned()); + println!("{}", file_contents) + } +}