Browse Source

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
stable-2.3
Stefan Lay 12 years ago committed by Gerrit Code Review @ Eclipse.org
parent
commit
215a74eceb
  1. 42
      org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergeMessageFormatterTest.java
  2. 25
      org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeMessageFormatter.java

42
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);
}
}

25
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<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,

Loading…
Cancel
Save