]> source.dussan.org Git - jgit.git/commitdiff
Add conflicts message before footer 30/9530/9
authorStefan Lay <stefan.lay@sap.com>
Wed, 9 Jan 2013 13:02:51 +0000 (14:02 +0100)
committerGerrit Code Review @ Eclipse.org <gerrit@eclipse.org>
Sat, 19 Jan 2013 16:38:33 +0000 (11:38 -0500)
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

org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergeMessageFormatterTest.java
org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeMessageFormatter.java

index 73fb838f0bc186c47f94ce9ee185fd189cdaf6ca..88ace76eb72d1ff7d0f5363c51dbfdfb51e4e954 100644 (file)
@@ -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);
+       }
 }
index 29fa8d65ad62a6aab5ebf67d7e5ffbfed86246e2..d94e7280c151b6bd5b81f6320d57308af6ce3e59 100644 (file)
@@ -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<String> 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<String> 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<String> names, String singular,