~ubuntu-branches/ubuntu/saucy/monodevelop/saucy

« back to all changes in this revision

Viewing changes to external/ngit/NGit/NGit.Patch/CombinedFileHeader.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-05-27 18:08:20 UTC
  • mfrom: (1.8.5) (1.5.8 sid)
  • Revision ID: package-import@ubuntu.com-20120527180820-f1ub6lhg0s50wci1
Tags: 3.0.2+dfsg-3
* [fcecfe7] Fix monodevelop-core-addins.pc.in to point to actual 
  installed location of assemblies.
* [26e1a07] DebSrc 3.0 does not support Quilt's -p parameter, so 
  manually adjust the path in the patch file.

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 System.Text;
 
46
using NGit;
 
47
using NGit.Diff;
 
48
using NGit.Patch;
 
49
using NGit.Util;
 
50
using Sharpen;
 
51
 
 
52
namespace NGit.Patch
 
53
{
 
54
        /// <summary>A file in the Git "diff --cc" or "diff --combined" format.</summary>
 
55
        /// <remarks>
 
56
        /// A file in the Git "diff --cc" or "diff --combined" format.
 
57
        /// <p>
 
58
        /// A combined diff shows an n-way comparison between two or more ancestors and
 
59
        /// the final revision. Its primary function is to perform code reviews on a
 
60
        /// merge which introduces changes not in any ancestor.
 
61
        /// </remarks>
 
62
        public class CombinedFileHeader : FileHeader
 
63
        {
 
64
                private static readonly byte[] MODE = Constants.EncodeASCII("mode ");
 
65
 
 
66
                private AbbreviatedObjectId[] oldIds;
 
67
 
 
68
                private FileMode[] oldModes;
 
69
 
 
70
                internal CombinedFileHeader(byte[] b, int offset) : base(b, offset)
 
71
                {
 
72
                }
 
73
 
 
74
                public override IList<HunkHeader> GetHunks()
 
75
                {
 
76
                        return base.GetHunks();
 
77
                }
 
78
 
 
79
                /// <returns>number of ancestor revisions mentioned in this diff.</returns>
 
80
                internal override int GetParentCount()
 
81
                {
 
82
                        return oldIds.Length;
 
83
                }
 
84
 
 
85
                /// <returns>get the file mode of the first parent.</returns>
 
86
                public override FileMode GetOldMode()
 
87
                {
 
88
                        return GetOldMode(0);
 
89
                }
 
90
 
 
91
                /// <summary>Get the file mode of the nth ancestor</summary>
 
92
                /// <param name="nthParent">the ancestor to get the mode of</param>
 
93
                /// <returns>the mode of the requested ancestor.</returns>
 
94
                public virtual FileMode GetOldMode(int nthParent)
 
95
                {
 
96
                        return oldModes[nthParent];
 
97
                }
 
98
 
 
99
                /// <returns>get the object id of the first parent.</returns>
 
100
                public override AbbreviatedObjectId GetOldId()
 
101
                {
 
102
                        return GetOldId(0);
 
103
                }
 
104
 
 
105
                /// <summary>Get the ObjectId of the nth ancestor</summary>
 
106
                /// <param name="nthParent">the ancestor to get the object id of</param>
 
107
                /// <returns>the id of the requested ancestor.</returns>
 
108
                public virtual AbbreviatedObjectId GetOldId(int nthParent)
 
109
                {
 
110
                        return oldIds[nthParent];
 
111
                }
 
112
 
 
113
                public override string GetScriptText(Encoding ocs, Encoding ncs)
 
114
                {
 
115
                        Encoding[] cs = new Encoding[GetParentCount() + 1];
 
116
                        Arrays.Fill(cs, ocs);
 
117
                        cs[GetParentCount()] = ncs;
 
118
                        return GetScriptText(cs);
 
119
                }
 
120
 
 
121
                /// <summary>Convert the patch script for this file into a string.</summary>
 
122
                /// <remarks>Convert the patch script for this file into a string.</remarks>
 
123
                /// <param name="charsetGuess">
 
124
                /// optional array to suggest the character set to use when
 
125
                /// decoding each file's line. If supplied the array must have a
 
126
                /// length of <code>
 
127
                /// <see cref="GetParentCount()">GetParentCount()</see>
 
128
                /// + 1</code>
 
129
                /// representing the old revision character sets and the new
 
130
                /// revision character set.
 
131
                /// </param>
 
132
                /// <returns>the patch script, as a Unicode string.</returns>
 
133
                internal override string GetScriptText(Encoding[] charsetGuess)
 
134
                {
 
135
                        return base.GetScriptText(charsetGuess);
 
136
                }
 
137
 
 
138
                internal override int ParseGitHeaders(int ptr, int end)
 
139
                {
 
140
                        while (ptr < end)
 
141
                        {
 
142
                                int eol = RawParseUtils.NextLF(buf, ptr);
 
143
                                if (IsHunkHdr(buf, ptr, end) >= 1)
 
144
                                {
 
145
                                        // First hunk header; break out and parse them later.
 
146
                                        break;
 
147
                                }
 
148
                                else
 
149
                                {
 
150
                                        if (RawParseUtils.Match(buf, ptr, OLD_NAME) >= 0)
 
151
                                        {
 
152
                                                ParseOldName(ptr, eol);
 
153
                                        }
 
154
                                        else
 
155
                                        {
 
156
                                                if (RawParseUtils.Match(buf, ptr, NEW_NAME) >= 0)
 
157
                                                {
 
158
                                                        ParseNewName(ptr, eol);
 
159
                                                }
 
160
                                                else
 
161
                                                {
 
162
                                                        if (RawParseUtils.Match(buf, ptr, INDEX) >= 0)
 
163
                                                        {
 
164
                                                                ParseIndexLine(ptr + INDEX.Length, eol);
 
165
                                                        }
 
166
                                                        else
 
167
                                                        {
 
168
                                                                if (RawParseUtils.Match(buf, ptr, MODE) >= 0)
 
169
                                                                {
 
170
                                                                        ParseModeLine(ptr + MODE.Length, eol);
 
171
                                                                }
 
172
                                                                else
 
173
                                                                {
 
174
                                                                        if (RawParseUtils.Match(buf, ptr, NEW_FILE_MODE) >= 0)
 
175
                                                                        {
 
176
                                                                                ParseNewFileMode(ptr, eol);
 
177
                                                                        }
 
178
                                                                        else
 
179
                                                                        {
 
180
                                                                                if (RawParseUtils.Match(buf, ptr, DELETED_FILE_MODE) >= 0)
 
181
                                                                                {
 
182
                                                                                        ParseDeletedFileMode(ptr + DELETED_FILE_MODE.Length, eol);
 
183
                                                                                }
 
184
                                                                                else
 
185
                                                                                {
 
186
                                                                                        // Probably an empty patch (stat dirty).
 
187
                                                                                        break;
 
188
                                                                                }
 
189
                                                                        }
 
190
                                                                }
 
191
                                                        }
 
192
                                                }
 
193
                                        }
 
194
                                }
 
195
                                ptr = eol;
 
196
                        }
 
197
                        return ptr;
 
198
                }
 
199
 
 
200
                internal override void ParseIndexLine(int ptr, int eol)
 
201
                {
 
202
                        // "index $asha1,$bsha1..$csha1"
 
203
                        //
 
204
                        IList<AbbreviatedObjectId> ids = new AList<AbbreviatedObjectId>();
 
205
                        while (ptr < eol)
 
206
                        {
 
207
                                int comma = RawParseUtils.NextLF(buf, ptr, ',');
 
208
                                if (eol <= comma)
 
209
                                {
 
210
                                        break;
 
211
                                }
 
212
                                ids.AddItem(AbbreviatedObjectId.FromString(buf, ptr, comma - 1));
 
213
                                ptr = comma;
 
214
                        }
 
215
                        oldIds = new AbbreviatedObjectId[ids.Count + 1];
 
216
                        Sharpen.Collections.ToArray(ids, oldIds);
 
217
                        int dot2 = RawParseUtils.NextLF(buf, ptr, '.');
 
218
                        oldIds[ids.Count] = AbbreviatedObjectId.FromString(buf, ptr, dot2 - 1);
 
219
                        newId = AbbreviatedObjectId.FromString(buf, dot2 + 1, eol - 1);
 
220
                        oldModes = new FileMode[oldIds.Length];
 
221
                }
 
222
 
 
223
                internal override void ParseNewFileMode(int ptr, int eol)
 
224
                {
 
225
                        for (int i = 0; i < oldModes.Length; i++)
 
226
                        {
 
227
                                oldModes[i] = FileMode.MISSING;
 
228
                        }
 
229
                        base.ParseNewFileMode(ptr, eol);
 
230
                }
 
231
 
 
232
                internal override HunkHeader NewHunkHeader(int offset)
 
233
                {
 
234
                        return new CombinedHunkHeader(this, offset);
 
235
                }
 
236
 
 
237
                private void ParseModeLine(int ptr, int eol)
 
238
                {
 
239
                        // "mode $amode,$bmode..$cmode"
 
240
                        //
 
241
                        int n = 0;
 
242
                        while (ptr < eol)
 
243
                        {
 
244
                                int comma = RawParseUtils.NextLF(buf, ptr, ',');
 
245
                                if (eol <= comma)
 
246
                                {
 
247
                                        break;
 
248
                                }
 
249
                                oldModes[n++] = ParseFileMode(ptr, comma);
 
250
                                ptr = comma;
 
251
                        }
 
252
                        int dot2 = RawParseUtils.NextLF(buf, ptr, '.');
 
253
                        oldModes[n] = ParseFileMode(ptr, dot2);
 
254
                        newMode = ParseFileMode(dot2 + 1, eol);
 
255
                }
 
256
 
 
257
                private void ParseDeletedFileMode(int ptr, int eol)
 
258
                {
 
259
                        // "deleted file mode $amode,$bmode"
 
260
                        //
 
261
                        changeType = DiffEntry.ChangeType.DELETE;
 
262
                        int n = 0;
 
263
                        while (ptr < eol)
 
264
                        {
 
265
                                int comma = RawParseUtils.NextLF(buf, ptr, ',');
 
266
                                if (eol <= comma)
 
267
                                {
 
268
                                        break;
 
269
                                }
 
270
                                oldModes[n++] = ParseFileMode(ptr, comma);
 
271
                                ptr = comma;
 
272
                        }
 
273
                        oldModes[n] = ParseFileMode(ptr, eol);
 
274
                        newMode = FileMode.MISSING;
 
275
                }
 
276
        }
 
277
}