mirror of
https://github.com/java-diff-utils/java-diff-utils.git
synced 2026-03-13 10:11:17 +08:00
add apply patch example
This commit is contained in:
27
test/examples/diffutils/ApplyPatch.java
Normal file
27
test/examples/diffutils/ApplyPatch.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package diffutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import difflib.DiffUtils;
|
||||
import difflib.Patch;
|
||||
import difflib.PatchFailedException;
|
||||
|
||||
public class ApplyPatch extends Example {
|
||||
static final String FS = File.separator;
|
||||
static final String ORIGINAL = "test" + FS + "mocks" + FS + "issue10_base.txt";
|
||||
static final String PATCH = "test" + FS + "mocks" + FS + "issue10_patch.txt";
|
||||
|
||||
public static void main(String[] args) throws PatchFailedException {
|
||||
List<String> original = fileToLines(ORIGINAL);
|
||||
List<String> patched = fileToLines(PATCH);
|
||||
|
||||
// At first, parse the unified diff file and get the patch
|
||||
Patch patch = DiffUtils.parseUnifiedDiff(patched);
|
||||
|
||||
// Then apply the computed patch to the given text
|
||||
List result = DiffUtils.patch(original, patch);
|
||||
System.out.println(result);
|
||||
/// Or we can call patch.applyTo(original). There is no difference.
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user