### Eclipse Workspace Patch 1.0
#P org.eclipse.compare.core
Index: src/org/eclipse/compare/patch/IHunk.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.compare/plugins/org.eclipse.compare.core/src/org/eclipse/compare/patch/IHunk.java,v
retrieving revision 1.4
diff -u -r1.4 IHunk.java
--- src/org/eclipse/compare/patch/IHunk.java 16 Feb 2009 14:08:16 -0000 1.4
+++ src/org/eclipse/compare/patch/IHunk.java 25 Feb 2009 11:23:59 -0000
@@ -40,7 +40,23 @@
* @return the start position of the hunk in the target file.
*/
public int getStartPosition();
-
+
+ /**
+ * Returns hunk's content in a unified format. This is an internal format in
+ * which hunk stores it's content and is always the same even if the hunk
+ * was extracted from a patch stored in a different format. In a unified
+ * format each line is prefixed with one of the following:
+ *
+ * -
' '
for context
+ * -
'+'
for addition
+ * -
'+'
for removal
+ *
+ *
+ * @return hunk's content in a 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
Index: src/org/eclipse/compare/internal/core/patch/Hunk.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.compare/plugins/org.eclipse.compare.core/src/org/eclipse/compare/internal/core/patch/Hunk.java,v
retrieving revision 1.5
diff -u -r1.5 Hunk.java
--- src/org/eclipse/compare/internal/core/patch/Hunk.java 23 Feb 2009 16:05:44 -0000 1.5
+++ src/org/eclipse/compare/internal/core/patch/Hunk.java 25 Feb 2009 11:23:59 -0000
@@ -145,6 +145,12 @@
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)}
#P org.eclipse.compare.tests
Index: src/org/eclipse/compare/tests/PatchBuilderTest.java
===================================================================
RCS file: /cvsroot/eclipse/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchBuilderTest.java,v
retrieving revision 1.1
diff -u -r1.1 PatchBuilderTest.java
--- src/org/eclipse/compare/tests/PatchBuilderTest.java 23 Feb 2009 16:18:59 -0000 1.1
+++ src/org/eclipse/compare/tests/PatchBuilderTest.java 25 Feb 2009 11:24:04 -0000
@@ -226,6 +226,9 @@
" [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 @@
"+[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 @@
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 @@
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 @@
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";