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