diff --git a/src/store/object.rs b/src/store/object.rs index bb3f6c4..a7d5789 100644 --- a/src/store/object.rs +++ b/src/store/object.rs @@ -45,6 +45,15 @@ fn _object_path(obj: &str) -> PathBuf { root } +fn rm(hash: &str) -> io::Result<()> { + let mut root = path::objects(); + let (dir, rest) = hash.split_at(2); + root.push(dir); + root.push(rest); + fs::remove_file(root)?; + Ok(()) +} + fn rm_node(path: &Path, node: &str) -> io::Result<()> { let mut root = path::objects(); let (dir, rest) = hash_obj(path.clone().to_str().unwrap()); diff --git a/src/store/object/tree.rs b/src/store/object/tree.rs index a60ca97..3f9d9a3 100644 --- a/src/store/object/tree.rs +++ b/src/store/object/tree.rs @@ -3,7 +3,7 @@ use std::io::{self}; use std::path::Path; use crate::utils::{read, path}; use crate::store::head; -use crate::store::object::{parse_path, hash_obj, add_node, create_obj}; +use crate::store::object::{self, parse_path, hash_obj, add_node, create_obj}; pub fn add(path: &Path, date: &str) -> io::Result<()> { let (line, hash, name) = parse_path(path.clone(), false); @@ -24,6 +24,43 @@ pub fn add(path: &Path, date: &str) -> io::Result<()> { Ok(()) } +pub fn rm(path: &Path) -> io::Result<()> { + let (_, lines) = read(path.to_path_buf().to_str().unwrap().to_string()).unwrap(); + for line in lines { + let (ftype, hash, _) = parse_line(line.unwrap()); + if ftype == String::from("blob") { + object::rm(&hash); + } else { + rm_hash(hash); + } + } + Ok(()) +} + +fn rm_hash(hash: String) { + let mut obj_p = path::objects(); + let (dir, res) = hash.split_at(2); + obj_p.push(dir); + obj_p.push(res); + + match read::read_lines(obj_p) { + Ok(mut reader) => { + reader.next(); + for line in reader { + let (ftype, hash, _) = parse_line(line.unwrap()); + if ftype == String::from("blob") { + object::rm(&hash); + } else { + rm_hash(hash); + } + } + }, + Err(err) => { + eprintln!("error reading tree: {}", err); + }, + } +} + pub fn read(tree: String) -> Option<(String, io::Lines>)> { let mut obj_p = path::objects(); @@ -57,4 +94,4 @@ pub fn parse_line(line: String) -> (String, String, String) { let hash = split.next().unwrap(); let ftype = split.next().unwrap(); (String::from(ftype), String::from(hash), String::from(name)) -} \ No newline at end of file +}