[yabridgectl] Recursively prune empty directories

If pruning results in a directory becoming empty, then we should remove
that directory. This approach won't touch any directories that we aren't
pruning from.
This commit is contained in:
Robbert van der Helm
2021-07-03 18:35:51 +02:00
parent 70c40970aa
commit c13d8f2ee3
2 changed files with 13 additions and 2 deletions
+10 -2
View File
@@ -451,11 +451,10 @@ pub fn do_sync(config: &mut Config, options: &SyncOptions) -> Result<()> {
);
}
// TODO: Prune empty subdirectories
for file in orphan_files {
println!("- {}", file.path().display());
if options.prune {
match file {
match &file {
NativeFile::Regular(path) | NativeFile::Symlink(path) => {
utils::remove_file(path)?;
}
@@ -463,6 +462,15 @@ pub fn do_sync(config: &mut Config, options: &SyncOptions) -> Result<()> {
utils::remove_dir_all(path)?;
}
}
// If the directory `file` was in is now empty, then we'll also recursively prune
// the empty subdirectory
let mut parent_dir = file.path().parent();
while let Some(dir) =
parent_dir.and_then(|dir| fs::remove_dir(dir).ok().map(|_| dir))
{
parent_dir = dir.parent();
}
}
}