1 /*
2 * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
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
44 package org.eclipse.jgit.merge;
45
46 import java.io.IOException;
47 import java.io.OutputStream;
48 import java.nio.charset.Charset;
49 import java.util.ArrayList;
50 import java.util.List;
51
52 import org.eclipse.jgit.diff.RawText;
53
54 /**
55 * A class to convert merge results into a Git conformant textual presentation
56 */
57 public class MergeFormatter {
58 /**
59 * Formats the results of a merge of {@link org.eclipse.jgit.diff.RawText}
60 * objects in a Git conformant way. This method also assumes that the
61 * {@link org.eclipse.jgit.diff.RawText} objects being merged are line
62 * oriented files which use LF as delimiter. This method will also use LF to
63 * separate chunks and conflict metadata, therefore it fits only to texts
64 * that are LF-separated lines.
65 *
66 * @param out
67 * the output stream where to write the textual presentation
68 * @param res
69 * the merge result which should be presented
70 * @param seqName
71 * When a conflict is reported each conflicting range will get a
72 * name. This name is following the "<<<<<<<
73 * " or ">>>>>>> " conflict markers. The
74 * names for the sequences are given in this list
75 * @param charsetName
76 * the name of the character set used when writing conflict
77 * metadata
78 * @throws java.io.IOException
79 * @deprecated Use
80 * {@link #formatMerge(OutputStream, MergeResult, List, Charset)}
81 * instead.
82 */
83 @Deprecated
84 public void formatMerge(OutputStream out, MergeResult<RawText> res,
85 List<String> seqName, String charsetName) throws IOException {
86 formatMerge(out, res, seqName, Charset.forName(charsetName));
87 }
88
89 /**
90 * Formats the results of a merge of {@link org.eclipse.jgit.diff.RawText}
91 * objects in a Git conformant way. This method also assumes that the
92 * {@link org.eclipse.jgit.diff.RawText} objects being merged are line
93 * oriented files which use LF as delimiter. This method will also use LF to
94 * separate chunks and conflict metadata, therefore it fits only to texts
95 * that are LF-separated lines.
96 *
97 * @param out
98 * the output stream where to write the textual presentation
99 * @param res
100 * the merge result which should be presented
101 * @param seqName
102 * When a conflict is reported each conflicting range will get a
103 * name. This name is following the "<<<<<<<
104 * " or ">>>>>>> " conflict markers. The
105 * names for the sequences are given in this list
106 * @param charset
107 * the character set used when writing conflict metadata
108 * @throws java.io.IOException
109 * @since 5.2
110 */
111 public void formatMerge(OutputStream out, MergeResult<RawText> res,
112 List<String> seqName, Charset charset) throws IOException {
113 new MergeFormatterPass(out, res, seqName, charset).formatMerge();
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 charsetName
135 * the name of the character set used when writing conflict
136 * metadata
137 * @throws java.io.IOException
138 * @deprecated use
139 * {@link #formatMerge(OutputStream, MergeResult, String, String, String, Charset)}
140 * instead.
141 */
142 @Deprecated
143 public void formatMerge(OutputStream out, MergeResult res, String baseName,
144 String oursName, String theirsName, String charsetName) throws IOException {
145 formatMerge(out, res, baseName, oursName, theirsName,
146 Charset.forName(charsetName));
147 }
148
149 /**
150 * Formats the results of a merge of exactly two
151 * {@link org.eclipse.jgit.diff.RawText} objects in a Git conformant way.
152 * This convenience method accepts the names for the three sequences (base
153 * and the two merged sequences) as explicit parameters and doesn't require
154 * the caller to specify a List
155 *
156 * @param out
157 * the {@link java.io.OutputStream} where to write the textual
158 * presentation
159 * @param res
160 * the merge result which should be presented
161 * @param baseName
162 * the name ranges from the base should get
163 * @param oursName
164 * the name ranges from ours should get
165 * @param theirsName
166 * the name ranges from theirs should get
167 * @param charset
168 * the character set used when writing conflict metadata
169 * @throws java.io.IOException
170 * @since 5.2
171 */
172 @SuppressWarnings("unchecked")
173 public void formatMerge(OutputStream out, MergeResult res, String baseName,
174 String oursName, String theirsName, Charset charset)
175 throws IOException {
176 List<String> names = new ArrayList<>(3);
177 names.add(baseName);
178 names.add(oursName);
179 names.add(theirsName);
180 formatMerge(out, res, names, charset);
181 }
182 }