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);
+ }
}
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;
/**
*/
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,