View | Details | Raw Unified | Return to bug 323290 | Differences between
and this patch

Collapse All | Expand All

(-)compare/org/eclipse/compare/internal/patch/Utilities.java (+7 lines)
Lines 67-72 Link Here
67
				}
67
				}
68
				return true;
68
				return true;
69
			}
69
			}
70
            public String getCharset() throws CoreException {
71
                if (storage instanceof IEncodedStorage) {
72
                    IEncodedStorage es = (IEncodedStorage) storage;
73
                    return es.getCharset();
74
                }
75
                return super.getCharset();
76
            }
70
		};
77
		};
71
	}
78
	}
72
79
(-)src/org/eclipse/compare/internal/core/patch/FileDiffResult.java (-1 / +10 lines)
Lines 25-30 Link Here
25
import org.eclipse.compare.patch.IHunk;
25
import org.eclipse.compare.patch.IHunk;
26
import org.eclipse.compare.patch.PatchConfiguration;
26
import org.eclipse.compare.patch.PatchConfiguration;
27
import org.eclipse.compare.patch.ReaderCreator;
27
import org.eclipse.compare.patch.ReaderCreator;
28
import org.eclipse.core.runtime.CoreException;
28
import org.eclipse.core.runtime.IPath;
29
import org.eclipse.core.runtime.IPath;
29
import org.eclipse.core.runtime.IProgressMonitor;
30
import org.eclipse.core.runtime.IProgressMonitor;
30
import org.eclipse.core.runtime.NullProgressMonitor;
31
import org.eclipse.core.runtime.NullProgressMonitor;
Lines 70-76 Link Here
70
		this.fMatches= false;
71
		this.fMatches= false;
71
		this.fDiffProblem= false;
72
		this.fDiffProblem= false;
72
		boolean create= false;
73
		boolean create= false;
73
		this.charset = Utilities.getCharset(content);
74
		if (content != null) {
75
			try {
76
				this.charset = content.getCharset();
77
			} catch (CoreException e) {
78
				// Log and continue
79
				ComparePlugin.log(e);
80
			}
81
		}
82
		
74
		//If this diff is an addition, make sure that it doesn't already exist
83
		//If this diff is an addition, make sure that it doesn't already exist
75
		boolean exists = targetExists(content);
84
		boolean exists = targetExists(content);
76
		if (this.fDiff.getDiffType(getConfiguration().isReversed()) == FilePatch2.ADDITION) {
85
		if (this.fDiff.getDiffType(getConfiguration().isReversed()) == FilePatch2.ADDITION) {
(-)src/org/eclipse/compare/patch/ReaderCreator.java (+11 lines)
Lines 39-42 Link Here
39
	public boolean canCreateReader() {
39
	public boolean canCreateReader() {
40
		return true;
40
		return true;
41
	}
41
	}
42
43
	/**
44
	 * Return the charset of the underlying data or <code>null</code> if the
45
	 * charset could not be determined.
46
	 * @return the charset of the underlying data or <code>null</code>
47
	 * @throws CoreException
48
	 */
49
	public String getCharset() throws CoreException {
50
		return null;
51
	}
52
	
42
}
53
}
(-)src/org/eclipse/compare/tests/FileDiffResultTest.java (+62 lines)
Lines 25-30 Link Here
25
import org.eclipse.compare.patch.ApplyPatchOperation;
25
import org.eclipse.compare.patch.ApplyPatchOperation;
26
import org.eclipse.compare.patch.IFilePatch;
26
import org.eclipse.compare.patch.IFilePatch;
27
import org.eclipse.compare.patch.IFilePatchResult;
27
import org.eclipse.compare.patch.IFilePatchResult;
28
import org.eclipse.compare.patch.IHunk;
28
import org.eclipse.compare.patch.PatchConfiguration;
29
import org.eclipse.compare.patch.PatchConfiguration;
29
import org.eclipse.compare.patch.WorkspacePatcherUI;
30
import org.eclipse.compare.patch.WorkspacePatcherUI;
30
import org.eclipse.core.resources.IFile;
31
import org.eclipse.core.resources.IFile;
Lines 187-192 Link Here
187
		}
188
		}
188
189
189
	}
190
	}
191
	
192
	/**
193
	 * Tests applying a patch which creates a new file in a project. The file
194
	 * already exists in the project, but has different contents.
195
	 * 
196
	 * @throws CoreException
197
	 */
198
	public void testEncoding()
199
			throws CoreException {
200
		IProject project = createProject("FileDiffResultTest",
201
				new String[] { NEW_FILENAME });
202
203
		IFile newFile = project
204
				.getFile(NEW_FILENAME);
205
		newFile.setContents(
206
				new ByteArrayInputStream("I'm a different content".getBytes()),
207
				IResource.NONE, null);
208
		String charset = "ISO-8859-1";
209
		newFile.setCharset(charset, null);
210
211
		try {
212
			// create the patch file
213
			IFile file = project.getFile(PATCH_FILE);
214
			file.create(new ByteArrayInputStream(createPatchAddingFile(project,
215
					NEW_FILENAME, false).getBytes()), true, null);
216
217
			assertTrue(newFile.exists());
218
			
219
			file.setCharset(charset, null);
220
221
			IFilePatch[] filePatch = ApplyPatchOperation.parsePatch(file);
222
			assertNotNull(filePatch);
223
			assertEquals(1, filePatch.length);
224
225
			IFilePatchResult filePatchResult = filePatch[0].apply(newFile, patchConfiguration,
226
					nullProgressMonitor);
227
			assertFalse(filePatchResult.hasMatches());
228
			assertEquals(1, filePatchResult.getRejects().length);
229
			assertEquals(charset, filePatchResult.getCharset());
230
			IHunk[] hunks = filePatch[0].getHunks();
231
			for (int i = 0; i < hunks.length; i++) {
232
				IHunk hunk = hunks[i];
233
				assertEquals(charset, hunk.getCharset());
234
			}
235
236
			assertNotNull(filePatchResult.getOriginalContents());
237
			assertNotNull(filePatchResult.getPatchedContents());
238
239
			assertEquals(new FileInputStream(newFile
240
					.getLocation().toFile()), filePatchResult
241
					.getOriginalContents());
242
			assertEquals("I'm a different content",
243
					getStringFromStream(filePatchResult.getOriginalContents()));
244
			assertEquals(filePatchResult.getOriginalContents(), filePatchResult
245
					.getPatchedContents());
246
247
		} catch (IOException e) {
248
			fail();
249
		}
250
251
	}
190
252
191
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185379
253
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185379
192
	public void testFileDiffResultWithNullPath() {
254
	public void testFileDiffResultWithNullPath() {

Return to bug 323290