diff options
3 files changed, 42 insertions, 1 deletions
diff --git a/bundles/org.eclipse.compare/plugins/org.eclipse.compare.core/src/org/eclipse/compare/internal/core/patch/Hunk.java b/bundles/org.eclipse.compare/plugins/org.eclipse.compare.core/src/org/eclipse/compare/internal/core/patch/Hunk.java index 5a3d26531..f0b7e01c9 100644 --- a/bundles/org.eclipse.compare/plugins/org.eclipse.compare.core/src/org/eclipse/compare/internal/core/patch/Hunk.java +++ b/bundles/org.eclipse.compare/plugins/org.eclipse.compare.core/src/org/eclipse/compare/internal/core/patch/Hunk.java @@ -145,6 +145,12 @@ public class Hunk implements IHunk { return fLines; } + public String[] getUnifiedLines() { + String[] ret = new String[fLines.length]; + System.arraycopy(fLines, 0, ret, 0, fLines.length); + return ret; + } + /** * Set the parent of this hunk. This method * should only be invoked from {@link FileDiff#add(Hunk)} diff --git a/bundles/org.eclipse.compare/plugins/org.eclipse.compare.core/src/org/eclipse/compare/patch/IHunk.java b/bundles/org.eclipse.compare/plugins/org.eclipse.compare.core/src/org/eclipse/compare/patch/IHunk.java index 1ba923c0a..b918e0d0f 100644 --- a/bundles/org.eclipse.compare/plugins/org.eclipse.compare.core/src/org/eclipse/compare/patch/IHunk.java +++ b/bundles/org.eclipse.compare/plugins/org.eclipse.compare.core/src/org/eclipse/compare/patch/IHunk.java @@ -40,7 +40,23 @@ public interface IHunk { * @return the start position of the hunk in the target file. */ public int getStartPosition(); - + + /** + * Returns hunk's content in the unified format. This is an internal format in + * which hunk stores its content and is always the same even if the hunk was + * extracted from a patch stored in a different format. In the unified format + * each line is prefixed with one of the following: + * <ul> + * <li> <code>' '</code> for context + * <li> <code>'+'</code> for addition + * <li> <code>'-'</code> for removal + * </ul> + * + * @return hunk's content in the unified format + * @since org.eclipse.compare 3.5 + */ + public String[] getUnifiedLines(); + /** * Return the original contents from which the hunk was generated. * The returned contents usually only represent a portion of the diff --git a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchBuilderTest.java b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchBuilderTest.java index 177f82030..a69656f33 100644 --- a/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchBuilderTest.java +++ b/tests/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchBuilderTest.java @@ -226,6 +226,9 @@ public class PatchBuilderTest extends TestCase { " [b]", "-[c]", " [d]", " [e]", " [f]" }; addLineDelimiters(lines); Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); + String[] actual = hunk.getUnifiedLines(); + assertTrue(lines != actual); + assertLinesEquals(lines, actual); assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); } @@ -241,6 +244,9 @@ public class PatchBuilderTest extends TestCase { "+[j2]", " [k]", " [l]", " [m]" }; addLineDelimiters(lines); Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); + String[] actual = hunk.getUnifiedLines(); + assertTrue(lines != actual); + assertLinesEquals(lines, actual); assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); } @@ -254,6 +260,9 @@ public class PatchBuilderTest extends TestCase { String[] lines = new String[] { "+[aa]", "+[bb]", "+[cc]" }; addLineDelimiters(lines); Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); + String[] actual = hunk.getUnifiedLines(); + assertTrue(lines != actual); + assertLinesEquals(lines, actual); assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); } @@ -267,6 +276,9 @@ public class PatchBuilderTest extends TestCase { String[] lines = new String[] { "-[aa]", "-[bb]", "-[cc]", "-[dd]" }; addLineDelimiters(lines); Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); + String[] actual = hunk.getUnifiedLines(); + assertTrue(lines != actual); + assertLinesEquals(lines, actual); assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); } @@ -287,6 +299,13 @@ public class PatchBuilderTest extends TestCase { assertEquals(h1.getHunkType(true), h2.getHunkType(true)); } + private void assertLinesEquals(String[] expected, String[] actual) { + assertEquals(expected.length, actual.length); + for (int i = 0; i < expected.length; i++) { + assertEquals(expected[i], actual[i]); + } + } + private void addLineDelimiters(String[] lines) { for (int i = 0; i < lines.length; i++) { lines[i] = lines[i] + "\r\n"; |