aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeFormatter.java
blob: 079db4a07f1cd81141bb09b72ad3604c5c1bf6ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.merge;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jgit.diff.RawText;

/**
 * A class to convert merge results into a Git conformant textual presentation
 */
public class MergeFormatter {

	/**
	 * Formats the results of a merge of {@link org.eclipse.jgit.diff.RawText}
	 * objects in a Git conformant way. This method also assumes that the
	 * {@link org.eclipse.jgit.diff.RawText} objects being merged are line
	 * oriented files which use LF as delimiter. This method will also use LF to
	 * separate chunks and conflict metadata, therefore it fits only to texts
	 * that are LF-separated lines.
	 *
	 * @param out
	 *            the output stream where to write the textual presentation
	 * @param res
	 *            the merge result which should be presented
	 * @param seqName
	 *            When a conflict is reported each conflicting range will get a
	 *            name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
	 *            " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
	 *            names for the sequences are given in this list
	 * @param charset
	 *            the character set used when writing conflict metadata
	 * @throws java.io.IOException
	 *             if an IO error occurred
	 * @since 5.2
	 */
	public void formatMerge(OutputStream out, MergeResult<RawText> res,
			List<String> seqName, Charset charset) throws IOException {
		new MergeFormatterPass(out, res, seqName, charset).formatMerge();
	}

	/**
	 * Formats the results of a merge of {@link org.eclipse.jgit.diff.RawText}
	 * objects in a Git conformant way using diff3 style. This method also
	 * assumes that the {@link org.eclipse.jgit.diff.RawText} objects being
	 * merged are line oriented files which use LF as delimiter. This method
	 * will also use LF to separate chunks and conflict metadata, therefore it
	 * fits only to texts that are LF-separated lines.
	 *
	 * @param out
	 *            the output stream where to write the textual presentation
	 * @param res
	 *            the merge result which should be presented
	 * @param seqName
	 *            When a conflict is reported each conflicting range will get a
	 *            name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
	 *            ", "|||||||" or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict
	 *            markers. The names for the sequences are given in this list
	 * @param charset
	 *            the character set used when writing conflict metadata
	 * @throws java.io.IOException
	 *            if an IO error occurred
	 * @since 6.7
	 */
	public void formatMergeDiff3(OutputStream out,
			MergeResult<RawText> res, List<String> seqName, Charset charset)
			throws IOException {
		new MergeFormatterPass(out, res, seqName, charset, true).formatMerge();
	}

	/**
	 * Formats the results of a merge of exactly two
	 * {@link org.eclipse.jgit.diff.RawText} objects in a Git conformant way.
	 * This convenience method accepts the names for the three sequences (base
	 * and the two merged sequences) as explicit parameters and doesn't require
	 * the caller to specify a List
	 *
	 * @param out
	 *            the {@link java.io.OutputStream} where to write the textual
	 *            presentation
	 * @param res
	 *            the merge result which should be presented
	 * @param baseName
	 *            the name ranges from the base should get
	 * @param oursName
	 *            the name ranges from ours should get
	 * @param theirsName
	 *            the name ranges from theirs should get
	 * @param charset
	 *            the character set used when writing conflict metadata
	 * @throws java.io.IOException
	 *             if an IO error occurred
	 * @since 5.2
	 */
	@SuppressWarnings("unchecked")
	public void formatMerge(OutputStream out, MergeResult res, String baseName,
			String oursName, String theirsName, Charset charset)
			throws IOException {
		List<String> names = new ArrayList<>(3);
		names.add(baseName);
		names.add(oursName);
		names.add(theirsName);
		formatMerge(out, res, names, charset);
	}

	/**
	 * Formats the results of a merge of three
	 * {@link org.eclipse.jgit.diff.RawText} objects in a Git conformant way,
	 * using diff-3 style. This convenience method accepts the names for the
	 * three sequences (base and the two merged sequences) as explicit
	 * parameters and doesn't require the caller to specify a List
	 *
	 * @param out
	 *            the {@link java.io.OutputStream} where to write the textual
	 *            presentation
	 * @param res
	 *            the merge result which should be presented
	 * @param baseName
	 *            the name ranges from the base should get
	 * @param oursName
	 *            the name ranges from ours should get
	 * @param theirsName
	 *            the name ranges from theirs should get
	 * @param charset
	 *            the character set used when writing conflict metadata
	 * @throws java.io.IOException
	 *            if an IO error occurred
	 * @since 6.7
	 */
	@SuppressWarnings("unchecked")
	public void formatMergeDiff3(OutputStream out,
			MergeResult res, String baseName, String oursName,
			String theirsName, Charset charset) throws IOException {
		List<String> names = new ArrayList<>(3);
		names.add(baseName);
		names.add(oursName);
		names.add(theirsName);
		formatMergeDiff3(out, res, names, charset);
	}
}