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
This commit is contained in:
Stefan Lay 2013-01-09 14:02:51 +01:00 committed by Gerrit Code Review @ Eclipse.org
parent 1ca7c581a3
commit 215a74eceb
2 changed files with 61 additions and 6 deletions

View File

@ -185,4 +185,46 @@ public void testIntoSymbolicRefHeadPointingToMaster() throws IOException {
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);
}
}

View File

@ -48,6 +48,7 @@
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 @@ else if (ref.getName().equals(ref.getObjectId().getName()))
*/
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,