~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Merge/MergeFormatter.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit'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
using System.Collections.Generic;
 
45
using NGit.Diff;
 
46
using NGit.Merge;
 
47
using Sharpen;
 
48
 
 
49
namespace NGit.Merge
 
50
{
 
51
        /// <summary>A class to convert merge results into a Git conformant textual presentation
 
52
        ///     </summary>
 
53
        public class MergeFormatter
 
54
        {
 
55
                /// <summary>
 
56
                /// Formats the results of a merge of
 
57
                /// <see cref="NGit.Diff.RawText">NGit.Diff.RawText</see>
 
58
                /// objects in a Git
 
59
                /// conformant way. This method also assumes that the
 
60
                /// <see cref="NGit.Diff.RawText">NGit.Diff.RawText</see>
 
61
                /// objects
 
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.
 
65
                /// </summary>
 
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 "<&lt;&lt;&lt;&lt;&lt;&lt; " or ">&gt;&gt;&gt;&gt;&gt;&gt; "
 
71
                /// conflict markers. The names for the sequences are given in
 
72
                /// this list
 
73
                /// </param>
 
74
                /// <param name="charsetName">
 
75
                /// the name of the characterSet used when writing conflict
 
76
                /// metadata
 
77
                /// </param>
 
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)
 
81
                {
 
82
                        string lastConflictingName = null;
 
83
                        // is set to non-null whenever we are
 
84
                        // in a conflict
 
85
                        bool threeWayMerge = (res.GetSequences().Count == 3);
 
86
                        foreach (MergeChunk chunk in res)
 
87
                        {
 
88
                                RawText seq = res.GetSequences()[chunk.GetSequenceIndex()];
 
89
                                if (lastConflictingName != null && chunk.GetConflictState() != MergeChunk.ConflictState
 
90
                                        .NEXT_CONFLICTING_RANGE)
 
91
                                {
 
92
                                        // found the end of an conflict
 
93
                                        @out.Write(Sharpen.Runtime.GetBytesForString((">>>>>>> " + lastConflictingName + 
 
94
                                                "\n"), charsetName));
 
95
                                        lastConflictingName = null;
 
96
                                }
 
97
                                if (chunk.GetConflictState() == MergeChunk.ConflictState.FIRST_CONFLICTING_RANGE)
 
98
                                {
 
99
                                        // found the start of an conflict
 
100
                                        @out.Write(Sharpen.Runtime.GetBytesForString(("<<<<<<< " + seqName[chunk.GetSequenceIndex
 
101
                                                ()] + "\n"), charsetName));
 
102
                                        lastConflictingName = seqName[chunk.GetSequenceIndex()];
 
103
                                }
 
104
                                else
 
105
                                {
 
106
                                        if (chunk.GetConflictState() == MergeChunk.ConflictState.NEXT_CONFLICTING_RANGE)
 
107
                                        {
 
108
                                                // found another conflicting chunk
 
109
                                                lastConflictingName = seqName[chunk.GetSequenceIndex()];
 
110
                                                @out.Write(Sharpen.Runtime.GetBytesForString((threeWayMerge ? "=======\n" : "======= "
 
111
                                                         + lastConflictingName + "\n"), charsetName));
 
112
                                        }
 
113
                                }
 
114
                                // the lines with conflict-metadata are written. Now write the chunk
 
115
                                for (int i = chunk.GetBegin(); i < chunk.GetEnd(); i++)
 
116
                                {
 
117
                                        seq.WriteLine(@out, i);
 
118
                                        @out.Write('\n');
 
119
                                }
 
120
                        }
 
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)
 
124
                        {
 
125
                                @out.Write(Sharpen.Runtime.GetBytesForString((">>>>>>> " + lastConflictingName + 
 
126
                                        "\n"), charsetName));
 
127
                        }
 
128
                }
 
129
 
 
130
                /// <summary>
 
131
                /// Formats the results of a merge of exactly two
 
132
                /// <see cref="NGit.Diff.RawText">NGit.Diff.RawText</see>
 
133
                /// objects in
 
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
 
137
                /// </summary>
 
138
                /// <param name="out">
 
139
                /// the
 
140
                /// <see cref="Sharpen.OutputStream">Sharpen.OutputStream</see>
 
141
                /// where to write the textual
 
142
                /// presentation
 
143
                /// </param>
 
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
 
150
                /// metadata
 
151
                /// </param>
 
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)
 
155
                {
 
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);
 
161
                }
 
162
        }
 
163
}