step five: print hashed objects

This commit is contained in:
Gonçalo Valério 2020-09-29 11:08:12 +01:00
parent 8a22a84861
commit 98ef42e0d3
2 changed files with 18 additions and 0 deletions

View File

@ -24,3 +24,8 @@ pub fn hash_object(content: &Vec<u8>) -> 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");
}

View File

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