step thirteen: delete all existing stuff before reading

This commit is contained in:
Gonçalo Valério 2020-10-26 23:22:17 +00:00
parent 3436deb267
commit 74bc0eb655
1 changed files with 23 additions and 0 deletions

View File

@ -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<String, String> {
}
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(())
}