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) + } +}