View Javadoc
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  
45  import java.util.ArrayList;
46  import java.util.List;
47  
48  import org.eclipse.jgit.lib.Constants;
49  import org.eclipse.jgit.lib.ObjectId;
50  import org.eclipse.jgit.lib.Ref;
51  import org.eclipse.jgit.lib.Repository;
52  import org.eclipse.jgit.util.ChangeIdUtil;
53  import org.eclipse.jgit.util.StringUtils;
54  
55  /**
56   * Formatter for constructing the commit message for a merge commit.
57   * <p>
58   * The format should be the same as C Git does it, for compatibility.
59   */
60  public class MergeMessageFormatter {
61  	/**
62  	 * Construct the merge commit message.
63  	 *
64  	 * @param refsToMerge
65  	 *            the refs which will be merged
66  	 * @param target
67  	 *            the branch ref which will be merged into
68  	 * @return merge commit message
69  	 */
70  	public String format(List<Ref> refsToMerge, Ref target) {
71  		StringBuilder sb = new StringBuilder();
72  		sb.append("Merge "); //$NON-NLS-1$
73  
74  		List<String> branches = new ArrayList<>();
75  		List<String> remoteBranches = new ArrayList<>();
76  		List<String> tags = new ArrayList<>();
77  		List<String> commits = new ArrayList<>();
78  		List<String> others = new ArrayList<>();
79  		for (Ref ref : refsToMerge) {
80  			if (ref.getName().startsWith(Constants.R_HEADS)) {
81  				branches.add("'" + Repository.shortenRefName(ref.getName()) //$NON-NLS-1$
82  						+ "'"); //$NON-NLS-1$
83  			} else if (ref.getName().startsWith(Constants.R_REMOTES)) {
84  				remoteBranches.add("'" //$NON-NLS-1$
85  						+ Repository.shortenRefName(ref.getName()) + "'"); //$NON-NLS-1$
86  			} else if (ref.getName().startsWith(Constants.R_TAGS)) {
87  				tags.add("'" + Repository.shortenRefName(ref.getName()) + "'"); //$NON-NLS-1$ //$NON-NLS-2$
88  			} else {
89  				ObjectId objectId = ref.getObjectId();
90  				if (objectId != null && ref.getName().equals(objectId.getName())) {
91  					commits.add("'" + ref.getName() + "'"); //$NON-NLS-1$ //$NON-NLS-2$
92  				} else {
93  					others.add(ref.getName());
94  				}
95  			}
96  		}
97  
98  		List<String> listings = new ArrayList<>();
99  
100 		if (!branches.isEmpty())
101 			listings.add(joinNames(branches, "branch", "branches")); //$NON-NLS-1$//$NON-NLS-2$
102 
103 		if (!remoteBranches.isEmpty())
104 			listings.add(joinNames(remoteBranches, "remote-tracking branch", //$NON-NLS-1$
105 					"remote-tracking branches")); //$NON-NLS-1$
106 
107 		if (!tags.isEmpty())
108 			listings.add(joinNames(tags, "tag", "tags")); //$NON-NLS-1$ //$NON-NLS-2$
109 
110 		if (!commits.isEmpty())
111 			listings.add(joinNames(commits, "commit", "commits")); //$NON-NLS-1$ //$NON-NLS-2$
112 
113 		if (!others.isEmpty())
114 			listings.add(StringUtils.join(others, ", ", " and ")); //$NON-NLS-1$ //$NON-NLS-2$
115 
116 		sb.append(StringUtils.join(listings, ", ")); //$NON-NLS-1$
117 
118 		String targetName = target.getLeaf().getName();
119 		if (!targetName.equals(Constants.R_HEADS + Constants.MASTER)) {
120 			String targetShortName = Repository.shortenRefName(targetName);
121 			sb.append(" into " + targetShortName); //$NON-NLS-1$
122 		}
123 
124 		return sb.toString();
125 	}
126 
127 	/**
128 	 * Add section with conflicting paths to merge message.
129 	 *
130 	 * @param message
131 	 *            the original merge message
132 	 * @param conflictingPaths
133 	 *            the paths with conflicts
134 	 * @return merge message with conflicting paths added
135 	 */
136 	public String formatWithConflicts(String message,
137 			List<String> conflictingPaths) {
138 		StringBuilder sb = new StringBuilder();
139 		String[] lines = message.split("\n"); //$NON-NLS-1$
140 		int firstFooterLine = ChangeIdUtil.indexOfFirstFooterLine(lines);
141 		for (int i = 0; i < firstFooterLine; i++)
142 			sb.append(lines[i]).append('\n');
143 		if (firstFooterLine == lines.length && message.length() != 0)
144 			sb.append('\n');
145 		addConflictsMessage(conflictingPaths, sb);
146 		if (firstFooterLine < lines.length)
147 			sb.append('\n');
148 		for (int i = firstFooterLine; i < lines.length; i++)
149 			sb.append(lines[i]).append('\n');
150 		return sb.toString();
151 	}
152 
153 	private static void addConflictsMessage(List<String> conflictingPaths,
154 			StringBuilder sb) {
155 		sb.append("Conflicts:\n"); //$NON-NLS-1$
156 		for (String conflictingPath : conflictingPaths)
157 			sb.append('\t').append(conflictingPath).append('\n');
158 	}
159 
160 	private static String joinNames(List<String> names, String singular,
161 			String plural) {
162 		if (names.size() == 1)
163 			return singular + " " + names.get(0); //$NON-NLS-1$
164 		else
165 			return plural + " " + StringUtils.join(names, ", ", " and "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
166 	}
167 }