Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Stocker2014-11-08 07:08:26 +0000
committerMatthias Sohn2014-11-23 01:13:12 +0000
commit494065b97c2398566898728f6dffecf429977b6a (patch)
treee66d48a118f31886edc394f57c61175db7949303 /org.eclipse.egit.core/src/org/eclipse/egit/core/op/CreatePatchOperation.java
parent48fd04618e1866775186cbf29bfc55b7c3bdcf10 (diff)
downloadegit-494065b97c2398566898728f6dffecf429977b6a.tar.gz
egit-494065b97c2398566898728f6dffecf429977b6a.tar.xz
egit-494065b97c2398566898728f6dffecf429977b6a.zip
Fix missing trailing newline with "Create Patch"
The resulting patches didn't work with "git apply" because the trailing newline was missing. Adding it makes it work, and it also still works with Eclipse's "Apply Patch". Just removing the line with "trim newline" at the end wasn't enough, because the ByteArrayOutputStream was only overriding one of the write methods (not the one with just one char). So the last newline was never added to the StringBuilder. Replace it with a more straightforward implementation. Bug: 406134 Change-Id: If736ab3bc5972908a464212a29de8311b32e2718 Signed-off-by: Robin Stocker <robin@nibor.org> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.egit.core/src/org/eclipse/egit/core/op/CreatePatchOperation.java')
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/op/CreatePatchOperation.java38
1 files changed, 15 insertions, 23 deletions
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/CreatePatchOperation.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/CreatePatchOperation.java
index e9df15feea..53b6043542 100644
--- a/org.eclipse.egit.core/src/org/eclipse/egit/core/op/CreatePatchOperation.java
+++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/op/CreatePatchOperation.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010, 2012 SAP AG and others.
+ * Copyright (c) 2010, 2014 SAP AG and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -56,6 +56,7 @@ import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
+import org.eclipse.jgit.util.RawParseUtils;
import org.eclipse.osgi.util.NLS;
/**
@@ -169,23 +170,8 @@ public class CreatePatchOperation implements IEGitOperation {
gitMonitor = new EclipseGitProgressTransformer(monitor);
final StringBuilder sb = new StringBuilder();
- final DiffFormatter diffFmt = new DiffFormatter(
- new ByteArrayOutputStream() {
-
- @Override
- public synchronized void write(byte[] b, int off, int len) {
- super.write(b, off, len);
- try {
- if (currentEncoding == null)
- sb.append(toString("UTF-8")); //$NON-NLS-1$
- else
- sb.append(toString(currentEncoding));
- } catch (UnsupportedEncodingException e) {
- sb.append(toString());
- }
- reset();
- }
- }) {
+ final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ final DiffFormatter diffFmt = new DiffFormatter(outputStream) {
private IProject project;
@Override
@@ -235,21 +221,27 @@ public class CreatePatchOperation implements IEGitOperation {
currentEncoding = CompareCoreUtils.getResourceEncoding(repository, path);
diffFmt.format(ent);
}
- } else
+ } else {
diffFmt.format(
new DirCacheIterator(repository.readDirCache()),
new FileTreeIterator(repository));
+ }
+ diffFmt.flush();
} catch (IOException e) {
Activator.logError(CoreText.CreatePatchOperation_patchFileCouldNotBeWritten, e);
}
+ try {
+ String encoding = currentEncoding != null ? currentEncoding
+ : RawParseUtils.UTF8_CHARSET.name();
+ sb.append(outputStream.toString(encoding));
+ } catch (UnsupportedEncodingException e) {
+ sb.append(outputStream.toString());
+ }
+
if (DiffHeaderFormat.WORKSPACE == headerFormat)
updateWorkspacePatchPrefixes(sb, diffFmt);
- // trim newline
- if (sb.charAt(sb.length() - 1) == '\n')
- sb.setLength(sb.length() - 1);
-
patchContent = sb.toString();
}

Back to the top