View Javadoc
1   /*
2    * Copyright (C) 2012, IBM Corporation and others. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.merge;
11  
12  import java.util.List;
13  
14  import org.eclipse.jgit.lib.PersonIdent;
15  import org.eclipse.jgit.lib.Ref;
16  import org.eclipse.jgit.revwalk.RevCommit;
17  import org.eclipse.jgit.util.GitDateFormatter;
18  import org.eclipse.jgit.util.GitDateFormatter.Format;
19  
20  /**
21   * Formatter for constructing the commit message for a squashed commit.
22   * <p>
23   * The format should be the same as C Git does it, for compatibility.
24   */
25  public class SquashMessageFormatter {
26  
27  	private GitDateFormatter dateFormatter;
28  
29  	/**
30  	 * Create a new squash message formatter.
31  	 */
32  	public SquashMessageFormatter() {
33  		dateFormatter = new GitDateFormatter(Format.DEFAULT);
34  	}
35  	/**
36  	 * Construct the squashed commit message.
37  	 *
38  	 * @param squashedCommits
39  	 *            the squashed commits
40  	 * @param target
41  	 *            the target branch
42  	 * @return squashed commit message
43  	 */
44  	public String format(List<RevCommit> squashedCommits, Ref target) {
45  		StringBuilder sb = new StringBuilder();
46  		sb.append("Squashed commit of the following:\n"); //$NON-NLS-1$
47  		for (RevCommit c : squashedCommits) {
48  			sb.append("\ncommit "); //$NON-NLS-1$
49  			sb.append(c.getName());
50  			sb.append("\n"); //$NON-NLS-1$
51  			sb.append(toString(c.getAuthorIdent()));
52  			sb.append("\n\t"); //$NON-NLS-1$
53  			sb.append(c.getShortMessage());
54  			sb.append("\n"); //$NON-NLS-1$
55  		}
56  		return sb.toString();
57  	}
58  
59  	private String toString(PersonIdent author) {
60  		final StringBuilder a = new StringBuilder();
61  
62  		a.append("Author: "); //$NON-NLS-1$
63  		a.append(author.getName());
64  		a.append(" <"); //$NON-NLS-1$
65  		a.append(author.getEmailAddress());
66  		a.append(">\n"); //$NON-NLS-1$
67  		a.append("Date:   "); //$NON-NLS-1$
68  		a.append(dateFormatter.formatDate(author));
69  		a.append("\n"); //$NON-NLS-1$
70  
71  		return a.toString();
72  	}
73  }