From 2d853e5a2fc0e9efa0544181b149aebea5faebdb Mon Sep 17 00:00:00 2001 From: CalDescent Date: Thu, 23 Dec 2021 16:59:49 +0000 Subject: [PATCH] Added support of patch creation from files without a newline at the end The simplest solution was to only include a newline at the end of the patch file if the source file ended with a newline. This is used to inform the merge code as to whether to add the newline to the end of the resulting file. Without this, the checksums do not match (and therefore previously the complete file would have been included as a result). --- .../arbitrary/patch/UnifiedDiffPatch.java | 25 ++++++++++++++++--- .../org/qortal/utils/FilesystemUtils.java | 21 ++++++++++++++++ .../arbitrary/ArbitraryDataMergeTests.java | 7 +++--- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/qortal/arbitrary/patch/UnifiedDiffPatch.java b/src/main/java/org/qortal/arbitrary/patch/UnifiedDiffPatch.java index d35d9f4a..2a6ad89b 100644 --- a/src/main/java/org/qortal/arbitrary/patch/UnifiedDiffPatch.java +++ b/src/main/java/org/qortal/arbitrary/patch/UnifiedDiffPatch.java @@ -10,6 +10,7 @@ import org.apache.logging.log4j.Logger; import org.qortal.crypto.Crypto; import org.qortal.repository.DataException; import org.qortal.settings.Settings; +import org.qortal.utils.FilesystemUtils; import java.io.BufferedWriter; import java.io.File; @@ -72,6 +73,9 @@ public class UnifiedDiffPatch { List original = FileUtils.readLines(before.toFile(), StandardCharsets.UTF_8); List revised = FileUtils.readLines(after.toFile(), StandardCharsets.UTF_8); + // Check if the original file ends with a newline + boolean endsWithNewline = FilesystemUtils.fileEndsWithNewline(before); + // Generate diff information Patch diff = DiffUtils.diff(original, revised); @@ -83,9 +87,13 @@ public class UnifiedDiffPatch { // Write the diff to the destination directory FileWriter fileWriter = new FileWriter(destination.toString(), true); BufferedWriter writer = new BufferedWriter(fileWriter); - for (String line : unifiedDiff) { + for (int i=0; i originalContents = FileUtils.readLines(originalPath.toFile(), StandardCharsets.UTF_8); List patchContents = FileUtils.readLines(patchPath.toFile(), StandardCharsets.UTF_8); + // Check if the patch file (and therefore the original file) ends with a newline + boolean endsWithNewline = FilesystemUtils.fileEndsWithNewline(patchPath); + // At first, parse the unified diff file and get the patch Patch patch = UnifiedDiffUtils.parseUnifiedDiff(patchContents); @@ -183,9 +194,15 @@ public class UnifiedDiffPatch { // Write the patched file to the merge directory FileWriter fileWriter = new FileWriter(mergePath.toString(), true); BufferedWriter writer = new BufferedWriter(fileWriter); - for (String line : patchedContents) { + for (int i=0; i