View Javadoc
1   /*
2    * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com> 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  
11  package org.eclipse.jgit.merge;
12  
13  import java.io.IOException;
14  import java.io.OutputStream;
15  import java.nio.charset.Charset;
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import org.eclipse.jgit.diff.RawText;
20  
21  /**
22   * A class to convert merge results into a Git conformant textual presentation
23   */
24  public class MergeFormatter {
25  	/**
26  	 * Formats the results of a merge of {@link org.eclipse.jgit.diff.RawText}
27  	 * objects in a Git conformant way. This method also assumes that the
28  	 * {@link org.eclipse.jgit.diff.RawText} objects being merged are line
29  	 * oriented files which use LF as delimiter. This method will also use LF to
30  	 * separate chunks and conflict metadata, therefore it fits only to texts
31  	 * that are LF-separated lines.
32  	 *
33  	 * @param out
34  	 *            the output stream where to write the textual presentation
35  	 * @param res
36  	 *            the merge result which should be presented
37  	 * @param seqName
38  	 *            When a conflict is reported each conflicting range will get a
39  	 *            name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
40  	 *            " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
41  	 *            names for the sequences are given in this list
42  	 * @param charsetName
43  	 *            the name of the character set used when writing conflict
44  	 *            metadata
45  	 * @throws java.io.IOException
46  	 * @deprecated Use
47  	 *             {@link #formatMerge(OutputStream, MergeResult, List, Charset)}
48  	 *             instead.
49  	 */
50  	@Deprecated
51  	public void formatMerge(OutputStream out, MergeResult<RawText> res,
52  			List<String> seqName, String charsetName) throws IOException {
53  		formatMerge(out, res, seqName, Charset.forName(charsetName));
54  	}
55  
56  	/**
57  	 * Formats the results of a merge of {@link org.eclipse.jgit.diff.RawText}
58  	 * objects in a Git conformant way. This method also assumes that the
59  	 * {@link org.eclipse.jgit.diff.RawText} objects being merged are line
60  	 * oriented files which use LF as delimiter. This method will also use LF to
61  	 * separate chunks and conflict metadata, therefore it fits only to texts
62  	 * that are LF-separated lines.
63  	 *
64  	 * @param out
65  	 *            the output stream where to write the textual presentation
66  	 * @param res
67  	 *            the merge result which should be presented
68  	 * @param seqName
69  	 *            When a conflict is reported each conflicting range will get a
70  	 *            name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
71  	 *            " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
72  	 *            names for the sequences are given in this list
73  	 * @param charset
74  	 *            the character set used when writing conflict metadata
75  	 * @throws java.io.IOException
76  	 * @since 5.2
77  	 */
78  	public void formatMerge(OutputStream out, MergeResult<RawText> res,
79  			List<String> seqName, Charset charset) throws IOException {
80  		new MergeFormatterPass(out, res, seqName, charset).formatMerge();
81  	}
82  
83  	/**
84  	 * Formats the results of a merge of exactly two
85  	 * {@link org.eclipse.jgit.diff.RawText} objects in a Git conformant way.
86  	 * This convenience method accepts the names for the three sequences (base
87  	 * and the two merged sequences) as explicit parameters and doesn't require
88  	 * the caller to specify a List
89  	 *
90  	 * @param out
91  	 *            the {@link java.io.OutputStream} where to write the textual
92  	 *            presentation
93  	 * @param res
94  	 *            the merge result which should be presented
95  	 * @param baseName
96  	 *            the name ranges from the base should get
97  	 * @param oursName
98  	 *            the name ranges from ours should get
99  	 * @param theirsName
100 	 *            the name ranges from theirs should get
101 	 * @param charsetName
102 	 *            the name of the character set used when writing conflict
103 	 *            metadata
104 	 * @throws java.io.IOException
105 	 * @deprecated use
106 	 *             {@link #formatMerge(OutputStream, MergeResult, String, String, String, Charset)}
107 	 *             instead.
108 	 */
109 	@Deprecated
110 	public void formatMerge(OutputStream out, MergeResult res, String baseName,
111 			String oursName, String theirsName, String charsetName) throws IOException {
112 		formatMerge(out, res, baseName, oursName, theirsName,
113 				Charset.forName(charsetName));
114 	}
115 
116 	/**
117 	 * Formats the results of a merge of exactly two
118 	 * {@link org.eclipse.jgit.diff.RawText} objects in a Git conformant way.
119 	 * This convenience method accepts the names for the three sequences (base
120 	 * and the two merged sequences) as explicit parameters and doesn't require
121 	 * the caller to specify a List
122 	 *
123 	 * @param out
124 	 *            the {@link java.io.OutputStream} where to write the textual
125 	 *            presentation
126 	 * @param res
127 	 *            the merge result which should be presented
128 	 * @param baseName
129 	 *            the name ranges from the base should get
130 	 * @param oursName
131 	 *            the name ranges from ours should get
132 	 * @param theirsName
133 	 *            the name ranges from theirs should get
134 	 * @param charset
135 	 *            the character set used when writing conflict metadata
136 	 * @throws java.io.IOException
137 	 * @since 5.2
138 	 */
139 	@SuppressWarnings("unchecked")
140 	public void formatMerge(OutputStream out, MergeResult res, String baseName,
141 			String oursName, String theirsName, Charset charset)
142 			throws IOException {
143 		List<String> names = new ArrayList<>(3);
144 		names.add(baseName);
145 		names.add(oursName);
146 		names.add(theirsName);
147 		formatMerge(out, res, names, charset);
148 	}
149 }