diff --git a/src/base.rs b/src/base.rs index a02956e..51d5d4f 100644 --- a/src/base.rs +++ b/src/base.rs @@ -49,6 +49,7 @@ pub fn write_tree(directory: String) -> String { } pub fn read_tree(oid: String) { + empty_current_directory(".").unwrap(); for (path, object_id) in get_tree(oid, "./".to_owned()).iter() { let mut dirs = Path::new(path).ancestors(); dirs.next(); @@ -101,3 +102,25 @@ fn get_tree(oid: String, base_path: String) -> HashMap { } result } + +fn empty_current_directory(dir: &str) -> io::Result<()> { + // Delete current directory, less the ignored directories + for entry in fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + if is_ignored(&path.to_str().unwrap().to_owned()) { + continue; + } + + if path.is_dir() { + empty_current_directory(path.to_str().unwrap())?; + match fs::remove_dir(&path) { + Ok(()) => (), + _ => println!("Unable to remove dir {}", path.clone().to_str().unwrap()), + }; + } else { + fs::remove_file(&path)? + } + } + Ok(()) +}