Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 126468 Details for
Bug 183226
[Apply Patch] API to instantiate and apply arbitrary IHunks
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
Patch_v04
183226_v04.txt (text/plain), 47.78 KB, created by
Pawel Pogorzelski
on 2009-02-23 10:47:14 EST
(
hide
)
Description:
Patch_v04
Filename:
MIME Type:
Creator:
Pawel Pogorzelski
Created:
2009-02-23 10:47:14 EST
Size:
47.78 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.compare.tests >Index: src/org/eclipse/compare/tests/PatchTest.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.compare.tests/src/org/eclipse/compare/tests/PatchTest.java,v >retrieving revision 1.23 >diff -u -r1.23 PatchTest.java >--- src/org/eclipse/compare/tests/PatchTest.java 16 Feb 2009 14:08:18 -0000 1.23 >+++ src/org/eclipse/compare/tests/PatchTest.java 23 Feb 2009 15:45:21 -0000 >@@ -10,13 +10,11 @@ > *******************************************************************************/ > package org.eclipse.compare.tests; > >-import java.io.BufferedInputStream; > import java.io.BufferedReader; > import java.io.ByteArrayInputStream; > import java.io.File; > import java.io.FileFilter; > import java.io.FileInputStream; >-import java.io.FileNotFoundException; > import java.io.IOException; > import java.io.InputStream; > import java.io.InputStreamReader; >@@ -38,7 +36,6 @@ > import junit.framework.AssertionFailedError; > import junit.framework.TestCase; > >-import org.eclipse.compare.internal.Utilities; > import org.eclipse.compare.internal.core.patch.FileDiff; > import org.eclipse.compare.internal.core.patch.FileDiffResult; > import org.eclipse.compare.internal.core.patch.LineReader; >@@ -49,8 +46,10 @@ > import org.eclipse.compare.patch.IHunk; > import org.eclipse.compare.patch.IHunkFilter; > import org.eclipse.compare.patch.PatchConfiguration; >+import org.eclipse.compare.tests.PatchUtils.JarEntryStorage; >+import org.eclipse.compare.tests.PatchUtils.PatchTestConfiguration; >+import org.eclipse.compare.tests.PatchUtils.StringStorage; > import org.eclipse.core.resources.IStorage; >-import org.eclipse.core.resources.ResourcesPlugin; > import org.eclipse.core.runtime.CoreException; > import org.eclipse.core.runtime.FileLocator; > import org.eclipse.core.runtime.IPath; >@@ -59,103 +58,13 @@ > import org.eclipse.core.runtime.Path; > import org.eclipse.core.runtime.Platform; > import org.eclipse.core.runtime.Status; >-import org.osgi.framework.Bundle; > > public class PatchTest extends TestCase { > >- private static final String PATCHDATA = "patchdata"; > private static final String PATCH_CONFIGURATION = "patchConfiguration.properties"; > > Properties defaultPatchProperties; > >- class StringStorage implements IStorage { >- String fileName; >- public StringStorage(String old) { >- fileName = old; >- } >- public Object getAdapter(Class adapter) { >- return null; >- } >- public boolean isReadOnly() { >- return false; >- } >- public String getName() { >- return fileName; >- } >- public IPath getFullPath() { >- return null; >- } >- public InputStream getContents() throws CoreException { >- return new BufferedInputStream(asInputStream(fileName)); >- } >- } >- >- class FileStorage implements IStorage { >- File file; >- public FileStorage(File file) { >- this.file = file; >- } >- public InputStream getContents() throws CoreException { >- try { >- return new FileInputStream(file); >- } catch (FileNotFoundException e) { >- // ignore, should never happen >- } >- return null; >- } >- public IPath getFullPath() { >- return new Path(file.getAbsolutePath()); >- } >- public String getName() { >- return file.getName(); >- } >- public boolean isReadOnly() { >- return true; >- } >- public Object getAdapter(Class adapter) { >- return null; >- } >- } >- >- class JarEntryStorage implements IStorage { >- JarEntry jarEntry; >- JarFile jarFile; >- public JarEntryStorage(JarEntry jarEntry, JarFile jarFile) { >- this.jarEntry = jarEntry; >- this.jarFile = jarFile; >- } >- public InputStream getContents() throws CoreException { >- try { >- return jarFile.getInputStream(jarEntry); >- } catch (IOException e) { >- // ignore, should never happen >- } >- return null; >- } >- public IPath getFullPath() { >- return new Path(jarFile.getName()); >- } >- public String getName() { >- return jarEntry.getName(); >- } >- public boolean isReadOnly() { >- return true; >- } >- public Object getAdapter(Class adapter) { >- return null; >- } >- } >- >- class PatchTestConfiguration { >- String subfolderName; >- PatchConfiguration pc; >- String patchFileName; >- String[] originalFileNames; >- String[] expectedFileNames; >- String[] actualFileNames; >- // TODO: getters, setters >- } >- > public PatchTest(String name) { > super(name); > defaultPatchProperties = new Properties(); >@@ -217,11 +126,11 @@ > > InputStream actual = result.getPatchedContents(); > >- LineReader lr = new LineReader(getReader("exp_hunkFilter.txt")); >+ LineReader lr = new LineReader(PatchUtils.getReader("exp_hunkFilter.txt")); > List inLines = lr.readLines(); > String expected = LineReader.createString(false, inLines); > >- assertEquals(expected, asString(actual)); >+ assertEquals(expected, PatchUtils.asString(actual)); > } > > public void testContext3PatchWithHeader() throws CoreException, IOException { >@@ -298,7 +207,8 @@ > private List failures = new ArrayList(); > > public void testPatchdataSubfolders() throws IOException, CoreException { >- URL patchdataUrl = new URL(getBundle().getEntry("/"), new Path(PATCHDATA).toString()); >+ URL patchdataUrl = new URL(PatchUtils.getBundle().getEntry("/"), >+ new Path(PatchUtils.PATCHDATA).toString()); > patchdataUrl = FileLocator.resolve(patchdataUrl); > > Map map = null; >@@ -321,7 +231,8 @@ > PatchConfiguration pc = ptc.pc; > > // create a message to distinguish tests from different subfolders >- String msg = "Test for subfolder [" + PATCHDATA + "/" + sf + "] failed."; >+ String msg = "Test for subfolder [" + PatchUtils.PATCHDATA + "/" >+ + sf + "] failed."; > > try { > // test with expected result >@@ -339,8 +250,8 @@ > continue; // continue with a next subfolder > } > failures.add(new AssertionFailedError( >- "\npatchWorkspace should fail for folder [" + PATCHDATA >- + "/" + sf + "].")); >+ "\npatchWorkspace should fail for folder [" >+ + PatchUtils.PATCHDATA + "/" + sf + "].")); > } > } > >@@ -391,7 +302,7 @@ > while (entries.hasMoreElements()) { > JarEntry entry = (JarEntry) entries.nextElement(); > String entryName = entry.getName(); >- if (entryName.endsWith("/" + PATCHDATA + "/")) { >+ if (entryName.endsWith("/" + PatchUtils.PATCHDATA + "/")) { > patchdataName = entryName; > break; > } >@@ -512,32 +423,13 @@ > result.put(subfolderName, tpc); > } > >- >- // Test changing >- private BufferedReader getReader(String name) { >- InputStream resourceAsStream = asInputStream(name); >- InputStreamReader reader2= new InputStreamReader(resourceAsStream); >- return new BufferedReader(reader2); >- } >- >- private InputStream asInputStream(String name) { >- IPath path= new Path(PATCHDATA).append(name); >- try { >- URL url= new URL(getBundle().getEntry("/"), path.toString()); >- return url.openStream(); >- } catch (IOException e) { >- fail("Failed while reading " + name); >- return null; // never reached >- } >- } >- > private void patch(final String old, String patch, String expt) throws CoreException, IOException { > patcherPatch(old, patch, expt); > filePatch(old, patch, expt); > } > > private void filePatch(final String old, String patch, String expt) throws CoreException, IOException { >- LineReader lr= new LineReader(getReader(expt)); >+ LineReader lr= new LineReader(PatchUtils.getReader(expt)); > List inLines= lr.readLines(); > String expected = LineReader.createString(false, inLines); > >@@ -549,21 +441,17 @@ > assertTrue(result.hasMatches()); > assertFalse(result.hasRejects()); > InputStream actualStream = result.getPatchedContents(); >- String actual = asString(actualStream); >+ String actual = PatchUtils.asString(actualStream); > assertEquals(expected, actual); > } > >- private String asString(InputStream exptStream) throws IOException { >- return Utilities.readString(exptStream, ResourcesPlugin.getEncoding()); >- } >- > private void patcherPatch(String old, String patch, String expt) { >- LineReader lr= new LineReader(getReader(old)); >+ LineReader lr= new LineReader(PatchUtils.getReader(old)); > List inLines= lr.readLines(); > > WorkspacePatcher patcher= new WorkspacePatcher(); > try { >- patcher.parse(getReader(patch)); >+ patcher.parse(PatchUtils.getReader(patch)); > } catch (IOException e) { > e.printStackTrace(); > } >@@ -574,7 +462,7 @@ > FileDiffResult diffResult = patcher.getDiffResult(diffs[0]); > diffResult.patch(inLines, null); > >- LineReader expectedContents= new LineReader(getReader(expt)); >+ LineReader expectedContents= new LineReader(PatchUtils.getReader(expt)); > List expectedLines= expectedContents.readLines(); > > Object[] expected= expectedLines.toArray(); >@@ -618,7 +506,7 @@ > patcher.getConfiguration().setFuzz(patchConfiguration.getFuzz()); > patcher.getConfiguration().setIgnoreWhitespace(patchConfiguration.isIgnoreWhitespace()); > patcher.getConfiguration().setPrefixSegmentStripCount(patchConfiguration.getPrefixSegmentStripCount()); >- patcher.parse(getReader(patch)); >+ patcher.parse(PatchUtils.getReader(patch)); > patcher.setReversed(patchConfiguration.isReversed()); > } catch (IOException e) { > e.printStackTrace(); >@@ -630,13 +518,13 @@ > //Iterate through all of the original files, apply the diffs that belong to the file and compare > //with the corresponding outcome file > for (int i = 0; i < originalFiles.length; i++) { >- LineReader lr= new LineReader(getReader(originalFiles[i])); >+ LineReader lr= new LineReader(PatchUtils.getReader(originalFiles[i])); > List inLines= lr.readLines(); > > FileDiffResult diffResult = patcher.getDiffResult(diffs[i]); > diffResult.patch(inLines, null); > >- LineReader expectedContents= new LineReader(getReader(expectedOutcomeFiles[i])); >+ LineReader expectedContents= new LineReader(PatchUtils.getReader(expectedOutcomeFiles[i])); > List expectedLines= expectedContents.readLines(); > > Object[] expected= expectedLines.toArray(); >@@ -652,7 +540,4 @@ > } > } > >- private Bundle getBundle() { >- return CompareTestPlugin.getDefault().getBundle(); >- } > } >Index: src/org/eclipse/compare/tests/AllTests.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.compare.tests/src/org/eclipse/compare/tests/AllTests.java,v >retrieving revision 1.15 >diff -u -r1.15 AllTests.java >--- src/org/eclipse/compare/tests/AllTests.java 3 Jun 2008 13:36:11 -0000 1.15 >+++ src/org/eclipse/compare/tests/AllTests.java 23 Feb 2009 15:45:21 -0000 >@@ -1,5 +1,5 @@ > /******************************************************************************* >- * Copyright (c) 2000, 2008 IBM Corporation and others. >+ * Copyright (c) 2000, 2009 IBM Corporation and others. > * All rights reserved. This program and the accompanying materials > * are made available under the terms of the Eclipse Public License v1.0 > * which accompanies this distribution, and is available at >@@ -28,6 +28,7 @@ > suite.addTestSuite(DocLineComparatorTest.class); > suite.addTestSuite(FilterTest.class); > suite.addTestSuite(PatchTest.class); >+ suite.addTestSuite(PatchBuilderTest.class); > suite.addTestSuite(AsyncExecTests.class); > suite.addTestSuite(DiffTest.class); > suite.addTestSuite(FileDiffResultTest.class); >Index: src/org/eclipse/compare/tests/PatchUtils.java >=================================================================== >RCS file: src/org/eclipse/compare/tests/PatchUtils.java >diff -N src/org/eclipse/compare/tests/PatchUtils.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/compare/tests/PatchUtils.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,170 @@ >+/******************************************************************************* >+ * Copyright (c) 2009 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.compare.tests; >+ >+import java.io.BufferedInputStream; >+import java.io.BufferedReader; >+import java.io.File; >+import java.io.FileInputStream; >+import java.io.FileNotFoundException; >+import java.io.IOException; >+import java.io.InputStream; >+import java.io.InputStreamReader; >+import java.net.URL; >+import java.util.jar.JarEntry; >+import java.util.jar.JarFile; >+ >+import junit.framework.Assert; >+ >+import org.eclipse.compare.internal.Utilities; >+import org.eclipse.compare.patch.PatchConfiguration; >+import org.eclipse.core.resources.IStorage; >+import org.eclipse.core.resources.ResourcesPlugin; >+import org.eclipse.core.runtime.CoreException; >+import org.eclipse.core.runtime.IPath; >+import org.eclipse.core.runtime.Path; >+import org.osgi.framework.Bundle; >+ >+public class PatchUtils { >+ >+ public static final String PATCHDATA = "patchdata"; >+ >+ public static class PatchTestConfiguration { >+ String subfolderName; >+ PatchConfiguration pc; >+ String patchFileName; >+ String[] originalFileNames; >+ String[] expectedFileNames; >+ String[] actualFileNames; >+ } >+ >+ public static class StringStorage implements IStorage { >+ String fileName; >+ >+ public StringStorage(String old) { >+ fileName = old; >+ } >+ >+ public Object getAdapter(Class adapter) { >+ return null; >+ } >+ >+ public boolean isReadOnly() { >+ return false; >+ } >+ >+ public String getName() { >+ return fileName; >+ } >+ >+ public IPath getFullPath() { >+ return null; >+ } >+ >+ public InputStream getContents() throws CoreException { >+ return new BufferedInputStream(asInputStream(fileName)); >+ } >+ } >+ >+ public static class FileStorage implements IStorage { >+ File file; >+ >+ public FileStorage(File file) { >+ this.file = file; >+ } >+ >+ public InputStream getContents() throws CoreException { >+ try { >+ return new FileInputStream(file); >+ } catch (FileNotFoundException e) { >+ // ignore, should never happen >+ } >+ return null; >+ } >+ >+ public IPath getFullPath() { >+ return new Path(file.getAbsolutePath()); >+ } >+ >+ public String getName() { >+ return file.getName(); >+ } >+ >+ public boolean isReadOnly() { >+ return true; >+ } >+ >+ public Object getAdapter(Class adapter) { >+ return null; >+ } >+ } >+ >+ public static class JarEntryStorage implements IStorage { >+ JarEntry jarEntry; >+ JarFile jarFile; >+ >+ public JarEntryStorage(JarEntry jarEntry, JarFile jarFile) { >+ this.jarEntry = jarEntry; >+ this.jarFile = jarFile; >+ } >+ >+ public InputStream getContents() throws CoreException { >+ try { >+ return jarFile.getInputStream(jarEntry); >+ } catch (IOException e) { >+ // ignore, should never happen >+ } >+ return null; >+ } >+ >+ public IPath getFullPath() { >+ return new Path(jarFile.getName()); >+ } >+ >+ public String getName() { >+ return jarEntry.getName(); >+ } >+ >+ public boolean isReadOnly() { >+ return true; >+ } >+ >+ public Object getAdapter(Class adapter) { >+ return null; >+ } >+ } >+ >+ public static String asString(InputStream exptStream) throws IOException { >+ return Utilities.readString(exptStream, ResourcesPlugin.getEncoding()); >+ } >+ >+ public static InputStream asInputStream(String name) { >+ IPath path = new Path(PATCHDATA).append(name); >+ try { >+ URL url = new URL(getBundle().getEntry("/"), path.toString()); >+ return url.openStream(); >+ } catch (IOException e) { >+ Assert.fail("Failed while reading " + name); >+ return null; // never reached >+ } >+ } >+ >+ public static BufferedReader getReader(String name) { >+ InputStream resourceAsStream = PatchUtils.asInputStream(name); >+ InputStreamReader reader2 = new InputStreamReader(resourceAsStream); >+ return new BufferedReader(reader2); >+ } >+ >+ public static Bundle getBundle() { >+ return CompareTestPlugin.getDefault().getBundle(); >+ } >+ >+} >Index: patchdata/patch_addHunks.txt >=================================================================== >RCS file: patchdata/patch_addHunks.txt >diff -N patchdata/patch_addHunks.txt >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ patchdata/patch_addHunks.txt 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,34 @@ >+### Eclipse Workspace Patch 1.0 >+#P Bug202944 >+Index: tmp/context_full.txt >+=================================================================== >+RCS file: /TEST/Bug202944/tmp/context_full.txt,v >+retrieving revision 1.1 >+diff -u -r1.1 context_full.txt >+--- tmp/context_full.txt 23 Feb 2009 09:39:56 -0000 1.1 >++++ tmp/context_full.txt 23 Feb 2009 09:40:51 -0000 >+@@ -1,3 +1,5 @@ >++[a1] >++[a2] >+ [a] >+ [b] >+ [c] >+@@ -20,8 +22,6 @@ >+ [t] >+ [u] >+ [v] >+-[w] >+-[x] >+ [y] >+ [z] >+ [A] >+@@ -47,6 +47,8 @@ >+ [U] >+ [V] >+ [W] >+-[X] >++[W1] >++[W2] >++[W3] >+ [Y] >+ [Z] >Index: patchdata/patch_createHunk0.txt >=================================================================== >RCS file: patchdata/patch_createHunk0.txt >diff -N patchdata/patch_createHunk0.txt >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ patchdata/patch_createHunk0.txt 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,19 @@ >+### Eclipse Workspace Patch 1.0 >+#P Bug202944 >+Index: tmp/list.txt >+=================================================================== >+RCS file: /TEST/Bug202944/tmp/list.txt,v >+retrieving revision 1.2 >+diff -u -r1.2 list.txt >+--- tmp/list.txt 20 Feb 2009 11:02:27 -0000 1.2 >++++ tmp/list.txt 20 Feb 2009 11:03:29 -0000 >+@@ -1,6 +1,8 @@ >++[a1] >++[a2] >++[a3] >+ [a] >+ [b] >+-[c] >+ [d] >+ [e] >+ [f] >Index: patchdata/patch_removeHunks.txt >=================================================================== >RCS file: patchdata/patch_removeHunks.txt >diff -N patchdata/patch_removeHunks.txt >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ patchdata/patch_removeHunks.txt 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,55 @@ >+### Eclipse Workspace Patch 1.0 >+#P Bug202944 >+Index: tmp/context_full.txt >+=================================================================== >+RCS file: /TEST/Bug202944/tmp/context_full.txt,v >+retrieving revision 1.1 >+diff -u -r1.1 context_full.txt >+--- tmp/context_full.txt 23 Feb 2009 09:39:56 -0000 1.1 >++++ tmp/context_full.txt 23 Feb 2009 10:03:51 -0000 >+@@ -1,3 +1,5 @@ >++[a1] >++[a2] >+ [a] >+ [b] >+ [c] >+@@ -11,6 +13,8 @@ >+ [k] >+ [l] >+ [m] >++[m1] >++[m2] >+ [n] >+ [o] >+ [p] >+@@ -20,8 +24,6 @@ >+ [t] >+ [u] >+ [v] >+-[w] >+-[x] >+ [y] >+ [z] >+ [A] >+@@ -30,10 +32,9 @@ >+ [D] >+ [E] >+ [F] >+-[G] >+-[H] >+ [I] >+ [J] >++[J1] >+ [K] >+ [L] >+ [M] >+@@ -47,6 +48,8 @@ >+ [U] >+ [V] >+ [W] >+-[X] >++[W1] >++[W2] >++[W3] >+ [Y] >+ [Z] >Index: patchdata/context_full.txt >=================================================================== >RCS file: patchdata/context_full.txt >diff -N patchdata/context_full.txt >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ patchdata/context_full.txt 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,52 @@ >+[a] >+[b] >+[c] >+[d] >+[e] >+[f] >+[g] >+[h] >+[i] >+[j] >+[k] >+[l] >+[m] >+[n] >+[o] >+[p] >+[q] >+[r] >+[s] >+[t] >+[u] >+[v] >+[w] >+[x] >+[y] >+[z] >+[A] >+[B] >+[C] >+[D] >+[E] >+[F] >+[G] >+[H] >+[I] >+[J] >+[K] >+[L] >+[M] >+[N] >+[O] >+[P] >+[Q] >+[R] >+[S] >+[T] >+[U] >+[V] >+[W] >+[X] >+[Y] >+[Z] >\ No newline at end of file >Index: patchdata/patch_createHunk3.txt >=================================================================== >RCS file: patchdata/patch_createHunk3.txt >diff -N patchdata/patch_createHunk3.txt >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ patchdata/patch_createHunk3.txt 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,14 @@ >+### Eclipse Workspace Patch 1.0 >+#P Bug202944 >+Index: tmp/a.txt >+=================================================================== >+RCS file: /TEST/Bug202944/tmp/a.txt,v >+retrieving revision 1.1 >+diff -u -r1.1 a.txt >+--- tmp/a.txt 20 Feb 2009 11:37:35 -0000 1.1 >++++ tmp/a.txt 20 Feb 2009 11:38:40 -0000 >+@@ -1,4 +0,0 @@ >+-[aa] >+-[bb] >+-[cc] >+-[dd] >Index: patchdata/exp_removeHunks.txt >=================================================================== >RCS file: patchdata/exp_removeHunks.txt >diff -N patchdata/exp_removeHunks.txt >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ patchdata/exp_removeHunks.txt 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,51 @@ >+[a] >+[b] >+[c] >+[d] >+[e] >+[f] >+[g] >+[h] >+[i] >+[j] >+[k] >+[l] >+[m] >+[n] >+[o] >+[p] >+[q] >+[r] >+[s] >+[t] >+[u] >+[v] >+[y] >+[z] >+[A] >+[B] >+[C] >+[D] >+[E] >+[F] >+[I] >+[J] >+[J1] >+[K] >+[L] >+[M] >+[N] >+[O] >+[P] >+[Q] >+[R] >+[S] >+[T] >+[U] >+[V] >+[W] >+[W1] >+[W2] >+[W3] >+[Y] >+[Z] >\ No newline at end of file >Index: patchdata/exp_addHunks.txt >=================================================================== >RCS file: patchdata/exp_addHunks.txt >diff -N patchdata/exp_addHunks.txt >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ patchdata/exp_addHunks.txt 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,59 @@ >+[a1] >+[a2] >+[a] >+[b] >+[c] >+[d] >+[d1] >+[d2] >+[d3] >+[d4] >+[e] >+[f] >+[g] >+[h] >+[i] >+[j] >+[k] >+[l] >+[m] >+[n] >+[o] >+[p] >+[q] >+[r] >+[s] >+[t] >+[u] >+[v] >+[y] >+[z] >+[A] >+[B] >+[C] >+[D] >+[E] >+[F] >+[G] >+[H] >+[I] >+[J] >+[K] >+[L] >+[N] >+[N1] >+[N2] >+[O] >+[P] >+[Q] >+[R] >+[S] >+[T] >+[U] >+[V] >+[W] >+[W1] >+[W2] >+[W3] >+[Y] >+[Z] >\ No newline at end of file >Index: src/org/eclipse/compare/tests/PatchBuilderTest.java >=================================================================== >RCS file: src/org/eclipse/compare/tests/PatchBuilderTest.java >diff -N src/org/eclipse/compare/tests/PatchBuilderTest.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/compare/tests/PatchBuilderTest.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,296 @@ >+/******************************************************************************* >+ * Copyright (c) 2009 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.compare.tests; >+ >+import java.io.IOException; >+import java.io.InputStream; >+import java.util.List; >+ >+import junit.framework.TestCase; >+ >+import org.eclipse.compare.internal.core.patch.FileDiff; >+import org.eclipse.compare.internal.core.patch.Hunk; >+import org.eclipse.compare.internal.core.patch.LineReader; >+import org.eclipse.compare.internal.patch.FileDiffWrapper; >+import org.eclipse.compare.patch.ApplyPatchOperation; >+import org.eclipse.compare.patch.IFilePatch; >+import org.eclipse.compare.patch.IFilePatch2; >+import org.eclipse.compare.patch.IFilePatchResult; >+import org.eclipse.compare.patch.IHunk; >+import org.eclipse.compare.patch.PatchBuilder; >+import org.eclipse.compare.patch.PatchConfiguration; >+import org.eclipse.compare.tests.PatchUtils.StringStorage; >+import org.eclipse.core.resources.IStorage; >+import org.eclipse.core.runtime.CoreException; >+import org.eclipse.core.runtime.NullProgressMonitor; >+import org.eclipse.core.runtime.Path; >+ >+public class PatchBuilderTest extends TestCase { >+ >+ public PatchBuilderTest() { >+ >+ } >+ >+ protected void setUp() throws Exception { >+ // Nothing to do >+ } >+ >+ protected void tearDown() throws Exception { >+ super.tearDown(); >+ } >+ >+ public void testModifyHunks() throws CoreException, IOException { >+ IStorage patchStorage = new StringStorage("patch_modifyHunks.txt"); >+ IStorage contextStorage = new StringStorage("context.txt"); >+ IFilePatch[] patches = ApplyPatchOperation.parsePatch(patchStorage); >+ assertEquals(1, patches.length); >+ IHunk[] hunksBefore = patches[0].getHunks(); >+ assertEquals(5, hunksBefore.length); >+ >+ String[] lines = new String[] { " [d]", "+[d1]", "+[d2]", "+[d3]", >+ "+[d4]", " [e]" }; >+ addLineDelimiters(lines); >+ IHunk[] toAdd = new IHunk[] { PatchBuilder.createHunk(3, lines) }; >+ IFilePatch2 filePatch = PatchBuilder.addHunks(patches[0], toAdd); >+ >+ IHunk[] toRemove = new IHunk[] { hunksBefore[0], hunksBefore[2] }; >+ filePatch = PatchBuilder.removeHunks(filePatch, toRemove); >+ >+ IHunk[] hunksAfter = filePatch.getHunks(); >+ assertEquals(4, hunksAfter.length); >+ assertEquals(3, ((Hunk) hunksAfter[0]).getStart(false)); >+ assertEquals(3, ((Hunk) hunksAfter[0]).getStart(true)); >+ assertEquals(7, ((Hunk) hunksAfter[1]).getStart(false)); >+ assertEquals(11, ((Hunk) hunksAfter[1]).getStart(true)); >+ assertEquals(18, ((Hunk) hunksAfter[2]).getStart(false)); >+ assertEquals(22, ((Hunk) hunksAfter[2]).getStart(true)); >+ assertEquals(28, ((Hunk) hunksAfter[3]).getStart(false)); >+ assertEquals(33, ((Hunk) hunksAfter[3]).getStart(true)); >+ >+ FileDiffWrapper wrapper = new FileDiffWrapper((FileDiff) filePatch); >+ IFilePatchResult result = wrapper.apply(contextStorage, >+ new PatchConfiguration(), new NullProgressMonitor()); >+ >+ IHunk[] rejects = result.getRejects(); >+ assertEquals(1, rejects.length); >+ >+ InputStream actual = result.getPatchedContents(); >+ >+ LineReader lr = new LineReader(PatchUtils >+ .getReader("exp_modifyHunks.txt")); >+ List inLines = lr.readLines(); >+ String expected = LineReader.createString(false, inLines); >+ >+ assertEquals(expected, PatchUtils.asString(actual)); >+ } >+ >+ public void testAddHunks() throws CoreException, IOException { >+ IStorage patchStorage = new StringStorage("patch_addHunks.txt"); >+ IStorage contextStorage = new StringStorage("context_full.txt"); >+ IFilePatch[] patches = ApplyPatchOperation.parsePatch(patchStorage); >+ assertEquals(1, patches.length); >+ IHunk[] hunksBefore = patches[0].getHunks(); >+ assertEquals(3, hunksBefore.length); >+ >+ String[] lines0 = new String[] { " [d]", "+[d1]", "+[d2]", "+[d3]", >+ "+[d4]", " [e]" }; >+ addLineDelimiters(lines0); >+ IHunk hunk0 = PatchBuilder.createHunk(3, lines0); >+ >+ String[] lines1 = new String[] { " [K]", " [L]", "-[M]", " [N]", >+ "+[N1]", "+[N2]", " [O]", " [P]" }; >+ addLineDelimiters(lines1); >+ IHunk hunk1 = PatchBuilder.createHunk(36, lines1); >+ >+ IHunk[] toAdd = new IHunk[] { hunk0, hunk1 }; >+ IFilePatch2 filePatch = PatchBuilder.addHunks(patches[0], toAdd); >+ >+ IHunk[] hunksAfter = filePatch.getHunks(); >+ assertEquals(5, hunksAfter.length); >+ assertEquals(0, ((Hunk) hunksAfter[0]).getStart(false)); >+ assertEquals(0, ((Hunk) hunksAfter[0]).getStart(true)); >+ assertEquals(3, ((Hunk) hunksAfter[1]).getStart(false)); >+ assertEquals(5, ((Hunk) hunksAfter[1]).getStart(true)); >+ assertEquals(19, ((Hunk) hunksAfter[2]).getStart(false)); >+ assertEquals(25, ((Hunk) hunksAfter[2]).getStart(true)); >+ assertEquals(36, ((Hunk) hunksAfter[3]).getStart(false)); >+ assertEquals(40, ((Hunk) hunksAfter[3]).getStart(true)); >+ assertEquals(46, ((Hunk) hunksAfter[4]).getStart(false)); >+ assertEquals(51, ((Hunk) hunksAfter[4]).getStart(true)); >+ >+ FileDiffWrapper wrapper = new FileDiffWrapper((FileDiff) filePatch); >+ IFilePatchResult result = wrapper.apply(contextStorage, >+ new PatchConfiguration(), new NullProgressMonitor()); >+ >+ IHunk[] rejects = result.getRejects(); >+ assertEquals(0, rejects.length); >+ >+ InputStream actual = result.getPatchedContents(); >+ >+ LineReader lr = new LineReader(PatchUtils.getReader("exp_addHunks.txt")); >+ List inLines = lr.readLines(); >+ String expected = LineReader.createString(false, inLines); >+ >+ assertEquals(expected, PatchUtils.asString(actual)); >+ } >+ >+ public void testRemoveHunks() throws CoreException, IOException { >+ IStorage patchStorage = new StringStorage("patch_removeHunks.txt"); >+ IStorage contextStorage = new StringStorage("context_full.txt"); >+ IFilePatch[] patches = ApplyPatchOperation.parsePatch(patchStorage); >+ assertEquals(1, patches.length); >+ IHunk[] hunksBefore = patches[0].getHunks(); >+ assertEquals(5, hunksBefore.length); >+ >+ IHunk[] toRemove = new IHunk[] { hunksBefore[0], hunksBefore[1] }; >+ IFilePatch2 filePatch = PatchBuilder.removeHunks(patches[0], toRemove); >+ >+ IHunk[] hunksAfter = filePatch.getHunks(); >+ assertEquals(3, hunksAfter.length); >+ assertEquals(19, ((Hunk) hunksAfter[0]).getStart(false)); >+ assertEquals(19, ((Hunk) hunksAfter[0]).getStart(true)); >+ assertEquals(29, ((Hunk) hunksAfter[1]).getStart(false)); >+ assertEquals(27, ((Hunk) hunksAfter[1]).getStart(true)); >+ assertEquals(46, ((Hunk) hunksAfter[2]).getStart(false)); >+ assertEquals(43, ((Hunk) hunksAfter[2]).getStart(true)); >+ >+ FileDiffWrapper wrapper = new FileDiffWrapper((FileDiff) filePatch); >+ IFilePatchResult result = wrapper.apply(contextStorage, >+ new PatchConfiguration(), new NullProgressMonitor()); >+ >+ IHunk[] rejects = result.getRejects(); >+ assertEquals(0, rejects.length); >+ >+ InputStream actual = result.getPatchedContents(); >+ >+ LineReader lr = new LineReader(PatchUtils >+ .getReader("exp_removeHunks.txt")); >+ List inLines = lr.readLines(); >+ String expected = LineReader.createString(false, inLines); >+ >+ assertEquals(expected, PatchUtils.asString(actual)); >+ } >+ >+ public void testCreateFilePatch() throws CoreException, IOException { >+ IStorage contextStorage = new StringStorage("context.txt"); >+ >+ String[] lines0 = new String[] { "+[a1]", "+[a2]", "+[a3]", " [a]" }; >+ addLineDelimiters(lines0); >+ Hunk hunk0 = (Hunk) PatchBuilder.createHunk(0, lines0); >+ >+ String[] lines1 = new String[] { " [b]", " [c]", "-[d]", "-[e]", >+ " [f]", " [g]", " [h]", "+[h1]", " [i]", " [j]", "+[j1]", >+ "+[j2]", " [k]", " [l]" }; >+ addLineDelimiters(lines1); >+ Hunk hunk1 = (Hunk) PatchBuilder.createHunk(1, lines1); >+ >+ IHunk[] hunks = new IHunk[] { hunk1, hunk0 }; >+ >+ IFilePatch2 filePatch = PatchBuilder.createFilePatch(new Path(""), >+ IFilePatch2.DATE_UNKNOWN, new Path(""), >+ IFilePatch2.DATE_UNKNOWN, hunks); >+ >+ assertEquals(2, filePatch.getHunks().length); >+ assertEquals(hunk0, filePatch.getHunks()[0]); >+ assertEquals(hunk1, filePatch.getHunks()[1]); >+ >+ FileDiffWrapper wrapper = new FileDiffWrapper((FileDiff) filePatch); >+ IFilePatchResult result = wrapper.apply(contextStorage, >+ new PatchConfiguration(), new NullProgressMonitor()); >+ >+ InputStream actual = result.getPatchedContents(); >+ >+ LineReader lr = new LineReader(PatchUtils >+ .getReader("exp_createFilePatch.txt")); >+ List inLines = lr.readLines(); >+ String expected = LineReader.createString(false, inLines); >+ >+ assertEquals(expected, PatchUtils.asString(actual)); >+ } >+ >+ public void testCreateHunk0() throws CoreException { >+ IStorage patch = new StringStorage("patch_createHunk0.txt"); >+ IFilePatch[] filePatches = ApplyPatchOperation.parsePatch(patch); >+ assertEquals(1, filePatches.length); >+ assertEquals(1, filePatches[0].getHunks().length); >+ >+ String[] lines = new String[] { "+[a1]", "+[a2]", "+[a3]", " [a]", >+ " [b]", "-[c]", " [d]", " [e]", " [f]" }; >+ addLineDelimiters(lines); >+ Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); >+ >+ assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); >+ } >+ >+ public void testCreateHunk1() throws CoreException { >+ IStorage patch = new StringStorage("patch_createHunk1.txt"); >+ IFilePatch[] filePatches = ApplyPatchOperation.parsePatch(patch); >+ assertEquals(1, filePatches.length); >+ assertEquals(1, filePatches[0].getHunks().length); >+ >+ String[] lines = new String[] { " [a]", " [b]", "-[c]", " [d]", "-[e]", >+ " [f]", " [g]", " [h]", "+[h1]", " [i]", " [j]", "+[j1]", >+ "+[j2]", " [k]", " [l]", " [m]" }; >+ addLineDelimiters(lines); >+ Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); >+ >+ assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); >+ } >+ >+ public void testCreateHunk2() throws CoreException { >+ IStorage patch = new StringStorage("patch_createHunk2.txt"); >+ IFilePatch[] filePatches = ApplyPatchOperation.parsePatch(patch); >+ assertEquals(1, filePatches.length); >+ assertEquals(1, filePatches[0].getHunks().length); >+ >+ String[] lines = new String[] { "+[aa]", "+[bb]", "+[cc]" }; >+ addLineDelimiters(lines); >+ Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); >+ >+ assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); >+ } >+ >+ public void testCreateHunk3() throws CoreException { >+ IStorage patch = new StringStorage("patch_createHunk3.txt"); >+ IFilePatch[] filePatches = ApplyPatchOperation.parsePatch(patch); >+ assertEquals(1, filePatches.length); >+ assertEquals(1, filePatches[0].getHunks().length); >+ >+ String[] lines = new String[] { "-[aa]", "-[bb]", "-[cc]", "-[dd]" }; >+ addLineDelimiters(lines); >+ Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); >+ >+ assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); >+ } >+ >+ private void assertHunkEquals(Hunk h1, Hunk h2) { >+ String[] l1 = h1.getLines(); >+ String[] l2 = h2.getLines(); >+ assertEquals(l1.length, l2.length); >+ for (int i = 0; i < l1.length; i++) { >+ assertFalse(l1[i] == null && l2[i] != null); >+ assertEquals(l1[i], (l2[i])); >+ } >+ assertEquals(h1.getStart(false), h2.getStart(false)); >+ assertEquals(h1.getStart(true), h2.getStart(true)); >+ assertEquals(h1.getLength(false), h2.getLength(false)); >+ assertEquals(h1.getLength(true), h2.getLength(true)); >+ assertEquals(h1.getHunkType(false), h2.getHunkType(false)); >+ assertEquals(h1.getHunkType(true), h2.getHunkType(true)); >+ } >+ >+ private void addLineDelimiters(String[] lines) { >+ for (int i = 0; i < lines.length; i++) { >+ lines[i] = lines[i] + "\r\n"; >+ } >+ } >+ >+} >Index: patchdata/patch_createHunk2.txt >=================================================================== >RCS file: patchdata/patch_createHunk2.txt >diff -N patchdata/patch_createHunk2.txt >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ patchdata/patch_createHunk2.txt 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,12 @@ >+### Eclipse Workspace Patch 1.0 >+#P Bug202944 >+Index: tmp/a.txt >+=================================================================== >+RCS file: tmp/a.txt >+diff -N tmp/a.txt >+--- /dev/null 1 Jan 1970 00:00:00 -0000 >++++ tmp/a.txt 1 Jan 1970 00:00:00 -0000 >+@@ -0,0 +1,3 @@ >++[aa] >++[bb] >++[cc] >Index: patchdata/exp_createFilePatch.txt >=================================================================== >RCS file: patchdata/exp_createFilePatch.txt >diff -N patchdata/exp_createFilePatch.txt >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ patchdata/exp_createFilePatch.txt 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,27 @@ >+[a1] >+[a2] >+[a3] >+[a] >+[b] >+[c] >+[f] >+[g] >+[h] >+[h1] >+[i] >+[j] >+[j1] >+[j2] >+[k] >+[l] >+[m] >+[n] >+[o] >+[p] >+[q] >+[r] >+[s] >+[t] >+[u] >+[v] >+[w] >Index: patchdata/exp_modifyHunks.txt >=================================================================== >RCS file: patchdata/exp_modifyHunks.txt >diff -N patchdata/exp_modifyHunks.txt >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ patchdata/exp_modifyHunks.txt 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,28 @@ >+[a] >+[b] >+[c] >+[d] >+[d1] >+[d2] >+[d3] >+[d4] >+[e] >+[f] >+[g] >+[h] >+[i1] >+[j] >+[k] >+[l] >+[m] >+[n] >+[o] >+[p] >+[q] >+[r] >+[s] >+[s1] >+[t] >+[u] >+[v] >+[w] >Index: patchdata/patch_createHunk1.txt >=================================================================== >RCS file: patchdata/patch_createHunk1.txt >diff -N patchdata/patch_createHunk1.txt >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ patchdata/patch_createHunk1.txt 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,26 @@ >+### Eclipse Workspace Patch 1.0 >+#P Bug202944 >+Index: tmp/list.txt >+=================================================================== >+RCS file: /TEST/Bug202944/tmp/list.txt,v >+retrieving revision 1.2 >+diff -u -r1.2 list.txt >+--- tmp/list.txt 20 Feb 2009 11:02:27 -0000 1.2 >++++ tmp/list.txt 20 Feb 2009 11:32:47 -0000 >+@@ -1,13 +1,14 @@ >+ [a] >+ [b] >+-[c] >+ [d] >+-[e] >+ [f] >+ [g] >+ [h] >++[h1] >+ [i] >+ [j] >++[j1] >++[j2] >+ [k] >+ [l] >+ [m] >Index: patchdata/patch_modifyHunks.txt >=================================================================== >RCS file: patchdata/patch_modifyHunks.txt >diff -N patchdata/patch_modifyHunks.txt >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ patchdata/patch_modifyHunks.txt 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,25 @@ >+--- old.txt 2005-05-07 00:16:20.000000000 +0200 >++++ new.txt 2005-05-07 00:16:32.000000000 +0200 >+@@ -3,2 +3,4 @@ >+ [c] >++[c1] >++[c2] >+ [d] >+@@ -8,3 +10,3 @@ >+ [h] >+-[i] >++[i1] >+ [j] >+@@ -14,3 +16,2 @@ >+ [n] >+-[o] >+ [p] >+@@ -19,2 +20,3 @@ >+ [s] >++[s1] >+ [t] >+@@ -29,2 +31,3 @@ >+ [yy] >++[xx] >+ [yy] >+ >\ No newline at end of file >#P org.eclipse.compare.core >Index: src/org/eclipse/compare/internal/core/patch/FileDiffResult.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.compare/plugins/org.eclipse.compare.core/src/org/eclipse/compare/internal/core/patch/FileDiffResult.java,v >retrieving revision 1.3 >diff -u -r1.3 FileDiffResult.java >--- src/org/eclipse/compare/internal/core/patch/FileDiffResult.java 16 Feb 2009 14:08:16 -0000 1.3 >+++ src/org/eclipse/compare/internal/core/patch/FileDiffResult.java 23 Feb 2009 15:45:24 -0000 >@@ -259,9 +259,10 @@ > > public List getFailedHunks() { > List failedHunks = new ArrayList(); >- for (Iterator iterator = fHunkResults.values().iterator(); iterator.hasNext();) { >- HunkResult result = (HunkResult) iterator.next(); >- if (!result.isOK()) >+ IHunk[] hunks = fDiff.getHunks(); >+ for (int i = 0; i < hunks.length; i++) { >+ HunkResult result = (HunkResult) fHunkResults.get(hunks[i]); >+ if (result != null && !result.isOK()) > failedHunks.add(result.getHunk()); > } > return failedHunks; >@@ -280,7 +281,15 @@ > } > > public HunkResult[] getHunkResults() { >- return (HunkResult[]) fHunkResults.values().toArray(new HunkResult[fHunkResults.size()]); >+ List results = new ArrayList(); >+ IHunk[] hunks = fDiff.getHunks(); >+ for (int i = 0; i < hunks.length; i++) { >+ HunkResult result = (HunkResult) fHunkResults.get(hunks[i]); >+ if (result != null) { >+ results.add(result.getHunk()); >+ } >+ } >+ return (HunkResult[]) results.toArray(new HunkResult[results.size()]); > } > > public InputStream getOriginalContents() { >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.4 >diff -u -r1.4 Hunk.java >--- src/org/eclipse/compare/internal/core/patch/Hunk.java 17 Feb 2009 11:46:24 -0000 1.4 >+++ src/org/eclipse/compare/internal/core/patch/Hunk.java 23 Feb 2009 15:45:24 -0000 >@@ -127,7 +127,7 @@ > return sb.toString(); > } > >- int getHunkType(boolean reverse) { >+ public int getHunkType(boolean reverse) { > if (reverse) { > if (hunkType == FileDiff.ADDITION) > return FileDiff.DELETION; >@@ -291,15 +291,23 @@ > return true; > } > >- int getStart(boolean reverse) { >- if (reverse) { >+ public int getStart(boolean after) { >+ if (after) { > return fNewStart; > } > return fOldStart; > } >- >- private int getLength(boolean reverse) { >- if (reverse) { >+ >+ public void setStart(int start, boolean after) { >+ if (after) { >+ fNewStart = start; >+ } else { >+ fOldStart = start; >+ } >+ } >+ >+ public int getLength(boolean after) { >+ if (after) { > return fNewLength; > } > return fOldLength; >Index: src/org/eclipse/compare/patch/PatchBuilder.java >=================================================================== >RCS file: src/org/eclipse/compare/patch/PatchBuilder.java >diff -N src/org/eclipse/compare/patch/PatchBuilder.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/compare/patch/PatchBuilder.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,248 @@ >+/******************************************************************************* >+ * Copyright (c) 2009 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.compare.patch; >+ >+import java.util.Arrays; >+import java.util.Comparator; >+ >+import org.eclipse.compare.internal.core.patch.FileDiff; >+import org.eclipse.compare.internal.core.patch.Hunk; >+import org.eclipse.core.runtime.IPath; >+ >+/** >+ * Builder for creating IFilePatch2 and IHunk objects as well as building >+ * relationship between them. >+ * >+ * @noextend This class is not intended to be subclassed by clients. >+ * @since org.eclipse.compare.core 1.0.0 >+ */ >+public class PatchBuilder { >+ >+ /** >+ * Line prefix used to mark context lines. >+ */ >+ public static final char CONTEXT_PREFIX = ' '; >+ /** >+ * Line prefix used to mark an added lines. >+ */ >+ public static final char ADDITION_PREFIX = '+'; >+ /** >+ * Line prefix used to mark an removed lines. >+ */ >+ public static final char REMOVAL_PREFIX = '-'; >+ >+ /** >+ * Creates an IHunk instance. >+ * >+ * @param start >+ * the start position in the before file >+ * @param lines >+ * content of the hunk. Each line starts with a control >+ * character. Their meaning is as follows: >+ * <ul> >+ * <li> >+ * '+': add the line >+ * <li> >+ * '-': delete the line >+ * <li> >+ * ' ': no change, context line >+ * </ul> >+ * @return IHunk instance >+ */ >+ public static IHunk createHunk(int start, String[] lines) { >+ int type = getHunkType(lines); >+ int oldLength = getHunkLength(lines, true); >+ int newLength = getHunkLength(lines, false); >+ return new Hunk(null, type, start, oldLength, start, newLength, lines); >+ } >+ >+ /** >+ * Creates an IFilePatch2 instance and performs recalculation of all hunks' >+ * after positions. Hunk's after position is position in the file state >+ * after applying a patch. It is affected by all the hunks that are to be >+ * applied before a given one. This recalculation is necessary to keep >+ * IFilePatch2's state coherent. >+ * >+ * @param oldPath >+ * the path of the before state of the file >+ * @param oldDate >+ * the timestamp of the before state of the file, see also >+ * {@link IFilePatch2#DATE_UNKNOWN} >+ * @param newPath >+ * the path of the after state of the file >+ * @param newDate >+ * the timestamp of the after state of the file, see also >+ * {@link IFilePatch2#DATE_UNKNOWN} >+ * @param hunks >+ * a set of hunks to insert into IFilePatch2 >+ * @return IFilePatch2 instance >+ */ >+ public static IFilePatch2 createFilePatch(IPath oldPath, long oldDate, >+ IPath newPath, long newDate, IHunk[] hunks) { >+ reorder(hunks); >+ FileDiff fileDiff = new FileDiff(oldPath, oldDate, newPath, newDate); >+ for (int i = 0; i < hunks.length; i++) { >+ fileDiff.add((Hunk) hunks[i]); >+ } >+ return fileDiff; >+ } >+ >+ /** >+ * Adds IHunks to a given IFilePatch2 and performs recalculation of all >+ * hunks' after positions. Hunk's after position is position in the file >+ * state after applying a patch. It is affected by all the hunks that are to >+ * be applied before a given one. This recalculation is necessary to keep >+ * IFilePatch2's state coherent. >+ * >+ * @param filePatch >+ * a file patch to add hunks to >+ * @param toAdd >+ * a set of IHunks to add >+ * @return newly created file patch with added hunks >+ */ >+ public static IFilePatch2 addHunks(IFilePatch2 filePatch, IHunk[] toAdd) { >+ IHunk[] result = addHunks(filePatch.getHunks(), toAdd); >+ reorder(result); >+ return createFilePatch(filePatch, result); >+ } >+ >+ /** >+ * Removes IHunks from a given IFilePatch2 and performs recalculation of all >+ * hunks' after positions. Hunk's after position is position in the file >+ * state after applying a patch. It is affected by all the hunks that are to >+ * be applied before a given one. This recalculation is necessary to keep >+ * IFilePatch2's state coherent. >+ * >+ * @param filePatch >+ * a file patch to add hunks to >+ * @param toRemove >+ * a set of IHunks to add >+ * @return newly created file patch with removed hunks >+ */ >+ public static IFilePatch2 removeHunks(IFilePatch2 filePatch, >+ IHunk[] toRemove) { >+ IHunk[] result = removeHunks(filePatch.getHunks(), toRemove); >+ reorder(result); >+ return createFilePatch(filePatch, result); >+ } >+ >+ private static IFilePatch2 createFilePatch(IFilePatch2 filePatch, >+ IHunk[] hunks) { >+ PatchConfiguration config = new PatchConfiguration(); >+ IPath beforePath = filePatch.getTargetPath(config); >+ config.setReversed(true); >+ IPath afterPath = filePatch.getTargetPath(config); >+ return createFilePatch(beforePath, filePatch.getBeforeDate(), >+ afterPath, filePatch.getAfterDate(), hunks); >+ } >+ >+ private static int getHunkType(String[] lines) { >+ boolean hasContextLines = checkForPrefix(CONTEXT_PREFIX, lines); >+ if (!hasContextLines) { >+ boolean hasLineAdditions = checkForPrefix(ADDITION_PREFIX, lines); >+ boolean hasLineDeletions = checkForPrefix(REMOVAL_PREFIX, lines); >+ if (hasLineAdditions && !hasLineDeletions) { >+ return FileDiff.ADDITION; >+ } else if (!hasLineAdditions && hasLineDeletions) { >+ return FileDiff.DELETION; >+ } >+ } >+ return FileDiff.CHANGE; >+ } >+ >+ private static int getHunkLength(String[] lines, boolean old) { >+ int length = 0; >+ for (int i = 0; i < lines.length; i++) { >+ if (lines[i].length() > 0) { >+ switch (lines[i].charAt(0)) { >+ case ' ': >+ length++; >+ break; >+ case '+': >+ if (!old) { >+ length++; >+ } >+ break; >+ case '-': >+ if (old) { >+ length++; >+ } >+ break; >+ default: >+ throw new IllegalArgumentException(""); >+ } >+ } >+ } >+ return length; >+ } >+ >+ private static boolean checkForPrefix(char prefix, String[] lines) { >+ for (int i = 0; i < lines.length; i++) { >+ if (lines[i].length() > 0) { >+ if (lines[i].charAt(0) == prefix) { >+ return true; >+ } >+ } >+ } >+ return false; >+ } >+ >+ private static IHunk[] addHunks(IHunk[] hunks, IHunk[] toAdd) { >+ IHunk[] ret = new IHunk[hunks.length + toAdd.length]; >+ System.arraycopy(hunks, 0, ret, 0, hunks.length); >+ System.arraycopy(toAdd, 0, ret, hunks.length, toAdd.length); >+ return ret; >+ } >+ >+ private static IHunk[] removeHunks(IHunk[] hunks, IHunk[] toRemove) { >+ int removed = 0; >+ for (int i = 0; i < hunks.length; i++) { >+ for (int j = 0; j < toRemove.length; j++) { >+ if (toRemove[j] == hunks[i]) { >+ hunks[i] = null; >+ removed++; >+ } >+ } >+ } >+ IHunk[] ret = new IHunk[hunks.length - removed]; >+ for (int i = 0, j = 0; i < hunks.length; i++) { >+ if (hunks[i] != null) { >+ ret[j++] = hunks[i]; >+ } >+ } >+ return ret; >+ } >+ >+ private static void reorder(IHunk[] hunks) { >+ Arrays.sort(hunks, new HunkComparator()); >+ int shift = 0; >+ for (int i = 0; i < hunks.length; i++) { >+ Hunk hunk = (Hunk) hunks[i]; >+ int start = hunk.getStart(false) + shift; >+ hunk.setStart(start, true); >+ shift += hunk.getLength(true) - hunk.getLength(false); >+ } >+ } >+ >+ static class HunkComparator implements Comparator { >+ public int compare(Object arg0, Object arg1) { >+ if ((arg0 != null && arg0 instanceof Hunk) >+ && (arg1 != null && arg1 instanceof Hunk)) { >+ Hunk hunk0 = (Hunk) arg0; >+ Hunk hunk1 = (Hunk) arg1; >+ int shift = hunk0.getStart(true) - hunk1.getStart(true); >+ return shift; >+ } >+ return 0; >+ } >+ } >+ >+} >\ No newline at end of file
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Flags:
pawel.pogorzelski1
:
iplog+
Actions:
View
|
Diff
Attachments on
bug 183226
:
125904
|
125905
|
126312
|
126453
| 126468