2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
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
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
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.
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
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.
44
using System.Collections.Generic;
51
/// <summary>A class to convert merge results into a Git conformant textual presentation
53
public class MergeFormatter
56
/// Formats the results of a merge of
57
/// <see cref="NGit.Diff.RawText">NGit.Diff.RawText</see>
59
/// conformant way. This method also assumes that the
60
/// <see cref="NGit.Diff.RawText">NGit.Diff.RawText</see>
62
/// being merged are line oriented files which use LF as delimiter. This
63
/// method will also use LF to separate chunks and conflict metadata,
64
/// therefore it fits only to texts that are LF-separated lines.
66
/// <param name="out">the outputstream where to write the textual presentation</param>
67
/// <param name="res">the merge result which should be presented</param>
68
/// <param name="seqName">
69
/// When a conflict is reported each conflicting range will get a
70
/// name. This name is following the "<<<<<<< " or ">>>>>>> "
71
/// conflict markers. The names for the sequences are given in
74
/// <param name="charsetName">
75
/// the name of the characterSet used when writing conflict
78
/// <exception cref="System.IO.IOException">System.IO.IOException</exception>
79
public virtual void FormatMerge(OutputStream @out, MergeResult<RawText> res, IList
80
<string> seqName, string charsetName)
82
string lastConflictingName = null;
83
// is set to non-null whenever we are
85
bool threeWayMerge = (res.GetSequences().Count == 3);
86
foreach (MergeChunk chunk in res)
88
RawText seq = res.GetSequences()[chunk.GetSequenceIndex()];
89
if (lastConflictingName != null && chunk.GetConflictState() != MergeChunk.ConflictState
90
.NEXT_CONFLICTING_RANGE)
92
// found the end of an conflict
93
@out.Write(Sharpen.Runtime.GetBytesForString((">>>>>>> " + lastConflictingName +
95
lastConflictingName = null;
97
if (chunk.GetConflictState() == MergeChunk.ConflictState.FIRST_CONFLICTING_RANGE)
99
// found the start of an conflict
100
@out.Write(Sharpen.Runtime.GetBytesForString(("<<<<<<< " + seqName[chunk.GetSequenceIndex
101
()] + "\n"), charsetName));
102
lastConflictingName = seqName[chunk.GetSequenceIndex()];
106
if (chunk.GetConflictState() == MergeChunk.ConflictState.NEXT_CONFLICTING_RANGE)
108
// found another conflicting chunk
109
lastConflictingName = seqName[chunk.GetSequenceIndex()];
110
@out.Write(Sharpen.Runtime.GetBytesForString((threeWayMerge ? "=======\n" : "======= "
111
+ lastConflictingName + "\n"), charsetName));
114
// the lines with conflict-metadata are written. Now write the chunk
115
for (int i = chunk.GetBegin(); i < chunk.GetEnd(); i++)
117
seq.WriteLine(@out, i);
121
// one possible leftover: if the merge result ended with a conflict we
122
// have to close the last conflict here
123
if (lastConflictingName != null)
125
@out.Write(Sharpen.Runtime.GetBytesForString((">>>>>>> " + lastConflictingName +
126
"\n"), charsetName));
131
/// Formats the results of a merge of exactly two
132
/// <see cref="NGit.Diff.RawText">NGit.Diff.RawText</see>
134
/// a Git conformant way. This convenience method accepts the names for the
135
/// three sequences (base and the two merged sequences) as explicit
136
/// parameters and doesn't require the caller to specify a List
138
/// <param name="out">
140
/// <see cref="Sharpen.OutputStream">Sharpen.OutputStream</see>
141
/// where to write the textual
144
/// <param name="res">the merge result which should be presented</param>
145
/// <param name="baseName">the name ranges from the base should get</param>
146
/// <param name="oursName">the name ranges from ours should get</param>
147
/// <param name="theirsName">the name ranges from theirs should get</param>
148
/// <param name="charsetName">
149
/// the name of the characterSet used when writing conflict
152
/// <exception cref="System.IO.IOException">System.IO.IOException</exception>
153
public virtual void FormatMerge(OutputStream @out, MergeResult<RawText> res, string baseName
154
, string oursName, string theirsName, string charsetName)
156
IList<string> names = new AList<string>(3);
157
names.AddItem(baseName);
158
names.AddItem(oursName);
159
names.AddItem(theirsName);
160
FormatMerge(@out, res, names, charsetName);