### Eclipse Workspace Patch 1.0 #P org.eclipse.compare Index: compare/org/eclipse/compare/internal/patch/Utilities.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/patch/Utilities.java,v retrieving revision 1.3 diff -u -r1.3 Utilities.java --- compare/org/eclipse/compare/internal/patch/Utilities.java 4 Mar 2009 09:31:22 -0000 1.3 +++ compare/org/eclipse/compare/internal/patch/Utilities.java 23 Aug 2010 20:17:15 -0000 @@ -67,6 +67,13 @@ } return true; } + public String getCharset() throws CoreException { + if (storage instanceof IEncodedStorage) { + IEncodedStorage es = (IEncodedStorage) storage; + return es.getCharset(); + } + return super.getCharset(); + } }; } #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.10 diff -u -r1.10 FileDiffResult.java --- src/org/eclipse/compare/internal/core/patch/FileDiffResult.java 26 May 2010 08:43:22 -0000 1.10 +++ src/org/eclipse/compare/internal/core/patch/FileDiffResult.java 23 Aug 2010 20:17:15 -0000 @@ -25,6 +25,7 @@ import org.eclipse.compare.patch.IHunk; import org.eclipse.compare.patch.PatchConfiguration; import org.eclipse.compare.patch.ReaderCreator; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; @@ -70,7 +71,15 @@ this.fMatches= false; this.fDiffProblem= false; boolean create= false; - this.charset = Utilities.getCharset(content); + if (content != null) { + try { + this.charset = content.getCharset(); + } catch (CoreException e) { + // Log and continue + ComparePlugin.log(e); + } + } + //If this diff is an addition, make sure that it doesn't already exist boolean exists = targetExists(content); if (this.fDiff.getDiffType(getConfiguration().isReversed()) == FilePatch2.ADDITION) { Index: src/org/eclipse/compare/patch/ReaderCreator.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.compare/plugins/org.eclipse.compare.core/src/org/eclipse/compare/patch/ReaderCreator.java,v retrieving revision 1.3 diff -u -r1.3 ReaderCreator.java --- src/org/eclipse/compare/patch/ReaderCreator.java 2 Mar 2009 13:47:14 -0000 1.3 +++ src/org/eclipse/compare/patch/ReaderCreator.java 23 Aug 2010 20:17:15 -0000 @@ -39,4 +39,15 @@ public boolean canCreateReader() { return true; } + + /** + * Return the charset of the underlying data or null if the + * charset could not be determined. + * @return the charset of the underlying data or null + * @throws CoreException + */ + public String getCharset() throws CoreException { + return null; + } + } #P org.eclipse.compare.tests Index: src/org/eclipse/compare/tests/FileDiffResultTest.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.compare.tests/src/org/eclipse/compare/tests/FileDiffResultTest.java,v retrieving revision 1.8 diff -u -r1.8 FileDiffResultTest.java --- src/org/eclipse/compare/tests/FileDiffResultTest.java 4 Mar 2009 15:45:35 -0000 1.8 +++ src/org/eclipse/compare/tests/FileDiffResultTest.java 23 Aug 2010 20:17:16 -0000 @@ -25,6 +25,7 @@ import org.eclipse.compare.patch.ApplyPatchOperation; import org.eclipse.compare.patch.IFilePatch; import org.eclipse.compare.patch.IFilePatchResult; +import org.eclipse.compare.patch.IHunk; import org.eclipse.compare.patch.PatchConfiguration; import org.eclipse.compare.patch.WorkspacePatcherUI; import org.eclipse.core.resources.IFile; @@ -187,6 +188,67 @@ } } + + /** + * Tests applying a patch which creates a new file in a project. The file + * already exists in the project, but has different contents. + * + * @throws CoreException + */ + public void testEncoding() + throws CoreException { + IProject project = createProject("FileDiffResultTest", + new String[] { NEW_FILENAME }); + + IFile newFile = project + .getFile(NEW_FILENAME); + newFile.setContents( + new ByteArrayInputStream("I'm a different content".getBytes()), + IResource.NONE, null); + String charset = "ISO-8859-1"; + newFile.setCharset(charset, null); + + try { + // create the patch file + IFile file = project.getFile(PATCH_FILE); + file.create(new ByteArrayInputStream(createPatchAddingFile(project, + NEW_FILENAME, false).getBytes()), true, null); + + assertTrue(newFile.exists()); + + file.setCharset(charset, null); + + IFilePatch[] filePatch = ApplyPatchOperation.parsePatch(file); + assertNotNull(filePatch); + assertEquals(1, filePatch.length); + + IFilePatchResult filePatchResult = filePatch[0].apply(newFile, patchConfiguration, + nullProgressMonitor); + assertFalse(filePatchResult.hasMatches()); + assertEquals(1, filePatchResult.getRejects().length); + assertEquals(charset, filePatchResult.getCharset()); + IHunk[] hunks = filePatch[0].getHunks(); + for (int i = 0; i < hunks.length; i++) { + IHunk hunk = hunks[i]; + assertEquals(charset, hunk.getCharset()); + } + + assertNotNull(filePatchResult.getOriginalContents()); + assertNotNull(filePatchResult.getPatchedContents()); + + assertEquals(new FileInputStream(newFile + .getLocation().toFile()), filePatchResult + .getOriginalContents()); + assertEquals("I'm a different content", + getStringFromStream(filePatchResult.getOriginalContents())); + assertEquals(filePatchResult.getOriginalContents(), filePatchResult + .getPatchedContents()); + + } catch (IOException e) { + fail(); + } + + } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=185379 public void testFileDiffResultWithNullPath() {