Skip to content

Commit

Permalink
Add delete operation
Browse files Browse the repository at this point in the history
Fix file deletion for empty folders
  • Loading branch information
TheMeinerLP committed Apr 28, 2024
1 parent 07808bf commit 3942757
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 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 @@ -331,6 +332,23 @@ public boolean doPatch(FileCollector oCollector, FileCollector rCollector, Patch

for (String file : removedFiles) {
summary.removedFiles++;
Path deletedFile = this.outputPath.toPath().resolve(file);
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 3942757

Please sign in to comment.