You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MergeMessageFormatter.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2010-2012, Robin Stocker <robin@nibor.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.merge;
  44. import java.util.ArrayList;
  45. import java.util.List;
  46. import org.eclipse.jgit.lib.Constants;
  47. import org.eclipse.jgit.lib.Ref;
  48. import org.eclipse.jgit.lib.Repository;
  49. import org.eclipse.jgit.util.ChangeIdUtil;
  50. import org.eclipse.jgit.util.StringUtils;
  51. /**
  52. * Formatter for constructing the commit message for a merge commit.
  53. * <p>
  54. * The format should be the same as C Git does it, for compatibility.
  55. */
  56. public class MergeMessageFormatter {
  57. /**
  58. * Construct the merge commit message.
  59. *
  60. * @param refsToMerge
  61. * the refs which will be merged
  62. * @param target
  63. * the branch ref which will be merged into
  64. * @return merge commit message
  65. */
  66. public String format(List<Ref> refsToMerge, Ref target) {
  67. StringBuilder sb = new StringBuilder();
  68. sb.append("Merge "); //$NON-NLS-1$
  69. List<String> branches = new ArrayList<String>();
  70. List<String> remoteBranches = new ArrayList<String>();
  71. List<String> tags = new ArrayList<String>();
  72. List<String> commits = new ArrayList<String>();
  73. List<String> others = new ArrayList<String>();
  74. for (Ref ref : refsToMerge) {
  75. if (ref.getName().startsWith(Constants.R_HEADS))
  76. branches.add("'" + Repository.shortenRefName(ref.getName()) //$NON-NLS-1$
  77. + "'"); //$NON-NLS-1$
  78. else if (ref.getName().startsWith(Constants.R_REMOTES))
  79. remoteBranches.add("'" //$NON-NLS-1$
  80. + Repository.shortenRefName(ref.getName()) + "'"); //$NON-NLS-1$
  81. else if (ref.getName().startsWith(Constants.R_TAGS))
  82. tags.add("'" + Repository.shortenRefName(ref.getName()) + "'"); //$NON-NLS-1$ //$NON-NLS-2$
  83. else if (ref.getName().equals(ref.getObjectId().getName()))
  84. commits.add("'" + ref.getName() + "'"); //$NON-NLS-1$ //$NON-NLS-2$
  85. else
  86. others.add(ref.getName());
  87. }
  88. List<String> listings = new ArrayList<String>();
  89. if (!branches.isEmpty())
  90. listings.add(joinNames(branches, "branch", "branches"));
  91. if (!remoteBranches.isEmpty())
  92. listings.add(joinNames(remoteBranches, "remote-tracking branch",
  93. "remote-tracking branches"));
  94. if (!tags.isEmpty())
  95. listings.add(joinNames(tags, "tag", "tags"));
  96. if (!commits.isEmpty())
  97. listings.add(joinNames(commits, "commit", "commits"));
  98. if (!others.isEmpty())
  99. listings.add(StringUtils.join(others, ", ", " and ")); //$NON-NLS-1$
  100. sb.append(StringUtils.join(listings, ", ")); //$NON-NLS-1$
  101. String targetName = target.getLeaf().getName();
  102. if (!targetName.equals(Constants.R_HEADS + Constants.MASTER)) {
  103. String targetShortName = Repository.shortenRefName(targetName);
  104. sb.append(" into " + targetShortName);
  105. }
  106. return sb.toString();
  107. }
  108. /**
  109. * Add section with conflicting paths to merge message.
  110. *
  111. * @param message
  112. * the original merge message
  113. * @param conflictingPaths
  114. * the paths with conflicts
  115. * @return merge message with conflicting paths added
  116. */
  117. public String formatWithConflicts(String message,
  118. List<String> conflictingPaths) {
  119. StringBuilder sb = new StringBuilder();
  120. String[] lines = message.split("\n"); //$NON-NLS-1$
  121. int firstFooterLine = ChangeIdUtil.indexOfFirstFooterLine(lines);
  122. for (int i = 0; i < firstFooterLine; i++)
  123. sb.append(lines[i]).append('\n');
  124. if (firstFooterLine == lines.length && message.length() != 0)
  125. sb.append('\n');
  126. addConflictsMessage(conflictingPaths, sb);
  127. if (firstFooterLine < lines.length)
  128. sb.append('\n');
  129. for (int i = firstFooterLine; i < lines.length; i++)
  130. sb.append(lines[i]).append('\n');
  131. return sb.toString();
  132. }
  133. private static void addConflictsMessage(List<String> conflictingPaths,
  134. StringBuilder sb) {
  135. sb.append("Conflicts:\n"); //$NON-NLS-1$
  136. for (String conflictingPath : conflictingPaths)
  137. sb.append('\t').append(conflictingPath).append('\n');
  138. }
  139. private static String joinNames(List<String> names, String singular,
  140. String plural) {
  141. if (names.size() == 1)
  142. return singular + " " + names.get(0); //$NON-NLS-1$
  143. else
  144. return plural + " " + StringUtils.join(names, ", ", " and "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  145. }
  146. }