From 215a74ecebedba54c326f4dc51edb3795e807869 Mon Sep 17 00:00:00 2001 From: Stefan Lay Date: Wed, 9 Jan 2013 14:02:51 +0100 Subject: Add conflicts message before footer In case of a conflict during cherry-pick or revert the commit message was amended after the footer. This made the footer invalid. Many users do not understand that they have to edit the commit message in order to make it valid again. Change-Id: I7e7fae125129e2a0d8950510550acda766531835 Bug: 367416 --- .../jgit/merge/MergeMessageFormatterTest.java | 42 ++++++++++++++++++++++ .../eclipse/jgit/merge/MergeMessageFormatter.java | 25 +++++++++---- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergeMessageFormatterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergeMessageFormatterTest.java index 73fb838f0b..88ace76eb7 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergeMessageFormatterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergeMessageFormatterTest.java @@ -185,4 +185,46 @@ public class MergeMessageFormatterTest extends SampleDataRepositoryTestCase { String message = formatter.format(Arrays.asList(a), head); assertEquals("Merge branch 'a'", message); } + + @Test + public void testFormatWithConflictsNoFooter() { + String originalMessage = "Header Line\n\nCommit body\n"; + String message = formatter.formatWithConflicts(originalMessage, + Arrays.asList(new String[] { "path1" })); + assertEquals("Header Line\n\nCommit body\n\nConflicts:\n\tpath1\n", + message); + } + + @Test + public void testFormatWithConflictsNoFooterNoLineBreak() { + String originalMessage = "Header Line\n\nCommit body"; + String message = formatter.formatWithConflicts(originalMessage, + Arrays.asList(new String[] { "path1" })); + assertEquals("Header Line\n\nCommit body\n\nConflicts:\n\tpath1\n", + message); + } + + @Test + public void testFormatWithConflictsWithFooters() { + String originalMessage = "Header Line\n\nCommit body\n\nChangeId:" + + " I123456789123456789123456789123456789\nBug:1234567\n"; + String message = formatter.formatWithConflicts(originalMessage, + Arrays.asList(new String[] { "path1" })); + assertEquals( + "Header Line\n\nCommit body\n\nConflicts:\n\tpath1\n\n" + + "ChangeId: I123456789123456789123456789123456789\nBug:1234567\n", + message); + } + + @Test + public void testFormatWithConflictsWithFooterlikeLineInBody() { + String originalMessage = "Header Line\n\nCommit body\nBug:1234567\nMore Body\n\nChangeId:" + + " I123456789123456789123456789123456789\nBug:1234567\n"; + String message = formatter.formatWithConflicts(originalMessage, + Arrays.asList(new String[] { "path1" })); + assertEquals( + "Header Line\n\nCommit body\nBug:1234567\nMore Body\n\nConflicts:\n\tpath1\n\n" + + "ChangeId: I123456789123456789123456789123456789\nBug:1234567\n", + message); + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeMessageFormatter.java b/org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeMessageFormatter.java index 29fa8d65ad..d94e7280c1 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeMessageFormatter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeMessageFormatter.java @@ -48,6 +48,7 @@ import java.util.List; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.util.ChangeIdUtil; import org.eclipse.jgit.util.StringUtils; /** @@ -133,14 +134,26 @@ public class MergeMessageFormatter { */ public String formatWithConflicts(String message, List conflictingPaths) { - StringBuilder sb = new StringBuilder(message); - if (!message.endsWith("\n") && message.length() != 0) //$NON-NLS-1$ - sb.append("\n"); //$NON-NLS-1$ - sb.append("\n"); //$NON-NLS-1$ - sb.append("Conflicts:\n"); + StringBuilder sb = new StringBuilder(); + String[] lines = message.split("\n"); //$NON-NLS-1$ + int firstFooterLine = ChangeIdUtil.indexOfFirstFooterLine(lines); + for (int i = 0; i < firstFooterLine; i++) + sb.append(lines[i]).append('\n'); + if (firstFooterLine == lines.length && message.length() != 0) + sb.append('\n'); + addConflictsMessage(conflictingPaths, sb); + if (firstFooterLine < lines.length) + sb.append('\n'); + for (int i = firstFooterLine; i < lines.length; i++) + sb.append(lines[i]).append('\n'); + return sb.toString(); + } + + private static void addConflictsMessage(List conflictingPaths, + StringBuilder sb) { + sb.append("Conflicts:\n"); //$NON-NLS-1$ for (String conflictingPath : conflictingPaths) sb.append('\t').append(conflictingPath).append('\n'); - return sb.toString(); } private static String joinNames(List names, String singular, -- cgit v1.2.3