Skip to content

Commit

Permalink
Fix file deletion for empty folders
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMeinerLP committed Apr 28, 2024
1 parent d76586c commit 3ddf4bf
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/main/java/codechicken/diffpatch/cli/PatchOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

import static codechicken.diffpatch.util.LogLevel.*;
import static codechicken.diffpatch.util.Utils.filterPrefixed;
Expand Down Expand Up @@ -332,20 +333,22 @@ public boolean doPatch(FileCollector oCollector, FileCollector rCollector, Patch
for (String file : removedFiles) {
summary.removedFiles++;
Path deletedFile = this.outputPath.toPath().resolve(file);

Files.walk(deletedFile)
.sorted(Comparator.reverseOrder())
.filter(Files::isDirectory)
.filter(path -> {
try {
return !Files.list(path).findFirst().isPresent();
} catch (IOException e) {
return true;
}
})
.map(Path::toFile)
.forEach(File::delete);
Files.delete(deletedFile);
try(Stream<Path> walker = Files.walk(this.outputPath.toPath())){
walker.sorted(Comparator.reverseOrder())
.filter(Files::isDirectory)
.filter(path -> {
try(Stream<Path> folders = Files.list(path)) {
return !folders.findFirst().isPresent();
} catch (IOException e) {
return false;
}
})
.map(Path::toFile)
.forEach(File::delete);
}


log(DEBUG, "Removed: " + file);
}

Expand Down

0 comments on commit 3ddf4bf

Please sign in to comment.