diff options
author | Robin Stocker | 2014-11-08 07:08:26 +0000 |
---|---|---|
committer | Matthias Sohn | 2014-11-23 01:13:12 +0000 |
commit | 494065b97c2398566898728f6dffecf429977b6a (patch) | |
tree | e66d48a118f31886edc394f57c61175db7949303 /org.eclipse.egit.core.test | |
parent | 48fd04618e1866775186cbf29bfc55b7c3bdcf10 (diff) | |
download | egit-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.test')
-rw-r--r-- | org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/CreatePatchOperationTest.java | 83 |
1 files changed, 48 insertions, 35 deletions
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/CreatePatchOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/CreatePatchOperationTest.java index 249cd9fccd..31be9fe5b8 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/CreatePatchOperationTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/CreatePatchOperationTest.java @@ -1,13 +1,10 @@ /******************************************************************************* - * Copyright (c) 2011, Tasktop Technologies + * Copyright (c) 2011, 2014 Tasktop Technologies 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: - * Benjamin Muskalla (Tasktop Technologies) - initial implementation *******************************************************************************/ package org.eclipse.egit.core.test.op; @@ -34,35 +31,32 @@ import org.junit.Test; public class CreatePatchOperationTest extends GitTestCase { - private static final String SIMPLE_GIT_PATCH_CONTENT = "From 6dcd097c7d39e9ba0b31a380981d3fb46017d6c2 Sat, 23 Jul 2011 20:33:24 -0330\n" + private static final String SIMPLE_GIT_PATCH_CONTENT = "From ca644a113ac60405e54731330be7879f6a36199c Sat, 23 Jul 2011 20:33:24 -0330\n" + "From: J. Git <j.git@egit.org>\n" + "Date: Sat, 15 Aug 2009 20:12:58 -0330\n" + "Subject: [PATCH] 2nd commit\n" + "\n" + "diff --git a/test-file b/test-file\n" - + "index e69de29..eb5f2c9 100644\n" + + "index e69de29..af28d38 100644\n" + "--- a/test-file\n" + "+++ b/test-file\n" + "@@ -0,0 +1 @@\n" - + "+another line\n" - + "\\ No newline at end of file"; + + "+another line\n"; - private static final String SIMPLE_ONELINE_PATCH_CONTENT = "6dcd097c7d39e9ba0b31a380981d3fb46017d6c2 2nd commit\n" + private static final String SIMPLE_ONELINE_PATCH_CONTENT = "ca644a113ac60405e54731330be7879f6a36199c 2nd commit\n" + "diff --git a/test-file b/test-file\n" - + "index e69de29..eb5f2c9 100644\n" + + "index e69de29..af28d38 100644\n" + "--- a/test-file\n" + "+++ b/test-file\n" + "@@ -0,0 +1 @@\n" - + "+another line\n" - + "\\ No newline at end of file"; + + "+another line\n"; private static final String SIMPLE_PATCH_CONTENT = "diff --git a/test-file b/test-file\n" - + "index e69de29..eb5f2c9 100644\n" + + "index e69de29..af28d38 100644\n" + "--- a/test-file\n" + "+++ b/test-file\n" + "@@ -0,0 +1 @@\n" - + "+another line\n" - + "\\ No newline at end of file"; + + "+another line\n"; private static final String SIMPLE_WORKSPACE_PATCH_CONTENT = "### Eclipse Workspace Patch 1.0\n" + "#P Project-1\n" @@ -73,19 +67,17 @@ public class CreatePatchOperationTest extends GitTestCase { + "+++ /dev/null\n" + "diff --git new-file new-file\n" + "new file mode 100644\n" - + "index 0000000..47d2739\n" + + "index 0000000..b66ba06\n" + "--- /dev/null\n" + "+++ new-file\n" + "@@ -0,0 +1 @@\n" + "+new content\n" - + "\\ No newline at end of file\n" + "diff --git test-file test-file\n" - + "index e69de29..eb5f2c9 100644\n" + + "index e69de29..af28d38 100644\n" + "--- test-file\n" + "+++ test-file\n" + "@@ -0,0 +1 @@\n" - + "+another line\n" - + "\\ No newline at end of file"; + + "+another line\n"; private RevCommit commit; @@ -116,7 +108,7 @@ public class CreatePatchOperationTest extends GitTestCase { @Test public void testSimpleGitPatch() throws Exception { RevCommit secondCommit = testRepository.appendContentAndCommit( - project.getProject(), file, "another line", "2nd commit"); + project.getProject(), file, "another line\n", "2nd commit"); CreatePatchOperation operation = new CreatePatchOperation( testRepository.getRepository(), secondCommit); @@ -142,7 +134,7 @@ public class CreatePatchOperationTest extends GitTestCase { @Test public void testSimplePatch() throws Exception { RevCommit secondCommit = testRepository.appendContentAndCommit( - project.getProject(), file, "another line", "2nd commit"); + project.getProject(), file, "another line\n", "2nd commit"); CreatePatchOperation operation = new CreatePatchOperation( testRepository.getRepository(), secondCommit); @@ -156,13 +148,31 @@ public class CreatePatchOperationTest extends GitTestCase { } @Test - public void testOnelineHeaderPatch() throws Exception { + public void testSimplePatchNoNewlineAtEnd() throws Exception { RevCommit secondCommit = testRepository.appendContentAndCommit( project.getProject(), file, "another line", "2nd commit"); CreatePatchOperation operation = new CreatePatchOperation( testRepository.getRepository(), secondCommit); + operation.setHeaderFormat(DiffHeaderFormat.NONE); + operation.execute(new NullProgressMonitor()); + + String patchContent = operation.getPatchContent(); + assertNotNull(patchContent); + assertPatch(SIMPLE_PATCH_CONTENT.replace("af28d38", "eb5f2c9") + + "\\ No newline at end of file\n", + patchContent); + } + + @Test + public void testOnelineHeaderPatch() throws Exception { + RevCommit secondCommit = testRepository.appendContentAndCommit( + project.getProject(), file, "another line\n", "2nd commit"); + + CreatePatchOperation operation = new CreatePatchOperation( + testRepository.getRepository(), secondCommit); + operation.setHeaderFormat(DiffHeaderFormat.ONELINE); operation.execute(new NullProgressMonitor()); @@ -182,7 +192,7 @@ public class CreatePatchOperationTest extends GitTestCase { assertPatch("5d67e6eaa2464d15c4216a93a0e7180ec905a2bb new file\n" + "diff --git a/test-file b/test-file\n" + "new file mode 100644\nindex 0000000..e69de29\n" - + "--- /dev/null\n" + "+++ b/test-file", patchContent); + + "--- /dev/null\n" + "+++ b/test-file\n", patchContent); } @SuppressWarnings("unused") @@ -207,7 +217,7 @@ public class CreatePatchOperationTest extends GitTestCase { @Test public void testNullMonitor() throws Exception { RevCommit secondCommit = testRepository.appendContentAndCommit( - project.getProject(), file, "another line", "2nd commit"); + project.getProject(), file, "another line\n", "2nd commit"); CreatePatchOperation operation = new CreatePatchOperation( testRepository.getRepository(), secondCommit); operation.execute(null); @@ -216,15 +226,19 @@ public class CreatePatchOperationTest extends GitTestCase { @Test public void testSuggestName() throws Exception { RevCommit aCommit = testRepository.appendContentAndCommit( - project.getProject(), file, "another line", "2nd commit"); + project.getProject(), file, "another line\n", "2nd commit"); assertEquals("2nd-commit.patch", CreatePatchOperation.suggestFileName(aCommit)); aCommit = testRepository.appendContentAndCommit( - project.getProject(), file, "another line", "[findBugs] Change visibility of repositoryFile to package"); +project.getProject(), + file, "another line\n", + "[findBugs] Change visibility of repositoryFile to package"); assertEquals("findBugs-Change-visibility-of-repositoryFile-to-pack.patch", CreatePatchOperation.suggestFileName(aCommit)); aCommit = testRepository.appendContentAndCommit( - project.getProject(), file, "another line", "Add collapse/expand all utility method for tree viewers."); +project.getProject(), + file, "another line\n", + "Add collapse/expand all utility method for tree viewers."); assertEquals("Add-collapse-expand-all-utility-method-for-tree-view.patch", CreatePatchOperation.suggestFileName(aCommit)); } @@ -262,7 +276,7 @@ public class CreatePatchOperationTest extends GitTestCase { public void testUpdateWorkspacePatchPrefixes() throws Exception { // setup workspace File newFile = testRepository.createFile(project.getProject(), "new-file"); - testRepository.appendFileContent(newFile, "new content"); + testRepository.appendFileContent(newFile, "new content\n"); File deletedFile = testRepository.createFile(project.getProject(), "deleted-file"); commit = testRepository.addAndCommit(project.getProject(), deletedFile, "whatever"); @@ -279,12 +293,11 @@ public class CreatePatchOperationTest extends GitTestCase { sb.append("+++ /dev/null").append("\n"); sb.append("diff --git a/new-file b/new-file").append("\n"); sb.append("new file mode 100644").append("\n"); - sb.append("index 0000000..47d2739").append("\n"); + sb.append("index 0000000..b66ba06").append("\n"); sb.append("--- /dev/null").append("\n"); sb.append("+++ b/new-file").append("\n"); sb.append("@@ -0,0 +1 @@").append("\n"); sb.append("+new content").append("\n"); - sb.append("\\ No newline at end of file").append("\n"); sb.append(SIMPLE_PATCH_CONTENT); // update patch @@ -304,9 +317,9 @@ public class CreatePatchOperationTest extends GitTestCase { commit = testRepository.addAndCommit(project.getProject(), deletedFile, "whatever"); FileUtils.delete(deletedFile); - testRepository.appendFileContent(file, "another line"); + testRepository.appendFileContent(file, "another line\n"); File newFile = testRepository.createFile(project.getProject(), "new-file"); - testRepository.appendFileContent(newFile, "new content"); + testRepository.appendFileContent(newFile, "new content\n"); testRepository.untrack(deletedFile); testRepository.track(file); testRepository.track(newFile); @@ -328,9 +341,9 @@ public class CreatePatchOperationTest extends GitTestCase { testRepository.addToIndex(project.getProject().findMember(".classpath")); testRepository.addToIndex(project.getProject().findMember(".project")); testRepository.commit("commit all"); - testRepository.appendFileContent(file, "another line"); + testRepository.appendFileContent(file, "another line\n"); File newFile = testRepository.createFile(project.getProject(), "new-file"); - testRepository.appendFileContent(newFile, "new content"); + testRepository.appendFileContent(newFile, "new content\n"); File deletedFile = testRepository.createFile(project.getProject(), "deleted-file"); commit = testRepository.addAndCommit(project.getProject(), deletedFile, "whatever"); |