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;
54
/// <summary>A file in the Git "diff --cc" or "diff --combined" format.</summary>
56
/// A file in the Git "diff --cc" or "diff --combined" format.
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.
62
public class CombinedFileHeader : FileHeader
64
private static readonly byte[] MODE = Constants.EncodeASCII("mode ");
66
private AbbreviatedObjectId[] oldIds;
68
private FileMode[] oldModes;
70
internal CombinedFileHeader(byte[] b, int offset) : base(b, offset)
74
public override IList<HunkHeader> GetHunks()
76
return base.GetHunks();
79
/// <returns>number of ancestor revisions mentioned in this diff.</returns>
80
internal override int GetParentCount()
85
/// <returns>get the file mode of the first parent.</returns>
86
public override FileMode GetOldMode()
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)
96
return oldModes[nthParent];
99
/// <returns>get the object id of the first parent.</returns>
100
public override AbbreviatedObjectId GetOldId()
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)
110
return oldIds[nthParent];
113
public override string GetScriptText(Encoding ocs, Encoding ncs)
115
Encoding[] cs = new Encoding[GetParentCount() + 1];
116
Arrays.Fill(cs, ocs);
117
cs[GetParentCount()] = ncs;
118
return GetScriptText(cs);
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
127
/// <see cref="GetParentCount()">GetParentCount()</see>
129
/// representing the old revision character sets and the new
130
/// revision character set.
132
/// <returns>the patch script, as a Unicode string.</returns>
133
internal override string GetScriptText(Encoding[] charsetGuess)
135
return base.GetScriptText(charsetGuess);
138
internal override int ParseGitHeaders(int ptr, int end)
142
int eol = RawParseUtils.NextLF(buf, ptr);
143
if (IsHunkHdr(buf, ptr, end) >= 1)
145
// First hunk header; break out and parse them later.
150
if (RawParseUtils.Match(buf, ptr, OLD_NAME) >= 0)
152
ParseOldName(ptr, eol);
156
if (RawParseUtils.Match(buf, ptr, NEW_NAME) >= 0)
158
ParseNewName(ptr, eol);
162
if (RawParseUtils.Match(buf, ptr, INDEX) >= 0)
164
ParseIndexLine(ptr + INDEX.Length, eol);
168
if (RawParseUtils.Match(buf, ptr, MODE) >= 0)
170
ParseModeLine(ptr + MODE.Length, eol);
174
if (RawParseUtils.Match(buf, ptr, NEW_FILE_MODE) >= 0)
176
ParseNewFileMode(ptr, eol);
180
if (RawParseUtils.Match(buf, ptr, DELETED_FILE_MODE) >= 0)
182
ParseDeletedFileMode(ptr + DELETED_FILE_MODE.Length, eol);
186
// Probably an empty patch (stat dirty).
200
internal override void ParseIndexLine(int ptr, int eol)
202
// "index $asha1,$bsha1..$csha1"
204
IList<AbbreviatedObjectId> ids = new AList<AbbreviatedObjectId>();
207
int comma = RawParseUtils.NextLF(buf, ptr, ',');
212
ids.AddItem(AbbreviatedObjectId.FromString(buf, ptr, comma - 1));
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];
223
internal override void ParseNewFileMode(int ptr, int eol)
225
for (int i = 0; i < oldModes.Length; i++)
227
oldModes[i] = FileMode.MISSING;
229
base.ParseNewFileMode(ptr, eol);
232
internal override HunkHeader NewHunkHeader(int offset)
234
return new CombinedHunkHeader(this, offset);
237
private void ParseModeLine(int ptr, int eol)
239
// "mode $amode,$bmode..$cmode"
244
int comma = RawParseUtils.NextLF(buf, ptr, ',');
249
oldModes[n++] = ParseFileMode(ptr, comma);
252
int dot2 = RawParseUtils.NextLF(buf, ptr, '.');
253
oldModes[n] = ParseFileMode(ptr, dot2);
254
newMode = ParseFileMode(dot2 + 1, eol);
257
private void ParseDeletedFileMode(int ptr, int eol)
259
// "deleted file mode $amode,$bmode"
261
changeType = DiffEntry.ChangeType.DELETE;
265
int comma = RawParseUtils.NextLF(buf, ptr, ',');
270
oldModes[n++] = ParseFileMode(ptr, comma);
273
oldModes[n] = ParseFileMode(ptr, eol);
274
newMode = FileMode.MISSING;