From 74bc0eb6550524c5d803b5e5f890f9f76c004bae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Val=C3=A9rio?= Date: Mon, 26 Oct 2020 23:22:17 +0000 Subject: [PATCH] step thirteen: delete all existing stuff before reading --- src/base.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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(()) +}