-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathPatch.cs
208 lines (178 loc) · 6.92 KB
/
Patch.cs
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
/// <summary>
/// Holds the patch between two trees.
/// <para>The individual patches for each file can be accessed through the indexer of this class.</para>
/// <para>Building a patch is an expensive operation. If you only need to know which files have been added,
/// deleted, modified, ..., then consider using a simpler <see cref="TreeChanges"/>.</para>
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Patch : IEnumerable<PatchEntryChanges>, IDiffResult
{
private readonly StringBuilder fullPatchBuilder = new StringBuilder();
private readonly IDictionary<FilePath, PatchEntryChanges> changes = new Dictionary<FilePath, PatchEntryChanges>();
private int linesAdded;
private int linesDeleted;
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected Patch()
{ }
internal unsafe Patch(DiffHandle diff)
{
using (diff)
{
int count = Proxy.git_diff_num_deltas(diff);
for (int i = 0; i < count; i++)
{
using (var patch = Proxy.git_patch_from_diff(diff, i))
{
var delta = Proxy.git_diff_get_delta(diff, i);
AddFileChange(delta);
Proxy.git_patch_print(patch, PrintCallBack);
}
}
}
}
private unsafe void AddFileChange(git_diff_delta* delta)
{
var treeEntryChanges = new TreeEntryChanges(delta);
changes.Add(treeEntryChanges.Path, new PatchEntryChanges(delta->flags.HasFlag(GitDiffFlags.GIT_DIFF_FLAG_BINARY), treeEntryChanges));
}
private unsafe int PrintCallBack(git_diff_delta* delta, GitDiffHunk hunk, GitDiffLine line, IntPtr payload)
{
string patchPart = LaxUtf8Marshaler.FromNative(line.content, (int)line.contentLen);
// Deleted files mean no "new file" path
var pathPtr = delta->new_file.Path != null
? delta->new_file.Path
: delta->old_file.Path;
var filePath = LaxFilePathMarshaler.FromNative(pathPtr);
PatchEntryChanges currentChange = this[filePath];
string prefix = string.Empty;
switch (line.lineOrigin)
{
case GitDiffLineOrigin.GIT_DIFF_LINE_CONTEXT:
prefix = " ";
break;
case GitDiffLineOrigin.GIT_DIFF_LINE_ADDITION:
linesAdded++;
currentChange.LinesAdded++;
currentChange.AddedLines.Add(new Line(line.NewLineNo, patchPart));
prefix = "+";
break;
case GitDiffLineOrigin.GIT_DIFF_LINE_DELETION:
linesDeleted++;
currentChange.LinesDeleted++;
currentChange.DeletedLines.Add(new Line(line.OldLineNo, patchPart));
prefix = "-";
break;
}
string formattedOutput = string.Concat(prefix, patchPart);
fullPatchBuilder.Append(formattedOutput);
currentChange.AppendToPatch(formattedOutput);
return 0;
}
#region IEnumerable<PatchEntryChanges> Members
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
public virtual IEnumerator<PatchEntryChanges> GetEnumerator()
{
return changes.Values.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
/// <summary>
/// Gets the <see cref="ContentChanges"/> corresponding to the specified <paramref name="path"/>.
/// </summary>
public virtual PatchEntryChanges this[string path]
{
get { return this[(FilePath)path]; }
}
private PatchEntryChanges this[FilePath path]
{
get
{
PatchEntryChanges entryChanges;
if (changes.TryGetValue(path, out entryChanges))
{
return entryChanges;
}
return null;
}
}
/// <summary>
/// The total number of lines added in this diff.
/// </summary>
public virtual int LinesAdded
{
get { return linesAdded; }
}
/// <summary>
/// The total number of lines deleted in this diff.
/// </summary>
public virtual int LinesDeleted
{
get { return linesDeleted; }
}
/// <summary>
/// The full patch file of this diff.
/// </summary>
public virtual string Content
{
get { return fullPatchBuilder.ToString(); }
}
/// <summary>
/// Implicit operator for string conversion.
/// </summary>
/// <param name="patch"><see cref="Patch"/>.</param>
/// <returns>The patch content as string.</returns>
public static implicit operator string(Patch patch)
{
return patch.fullPatchBuilder.ToString();
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"+{0} -{1}",
linesAdded,
linesDeleted);
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
// This doesn't do anything yet because it loads everything
// eagerly and disposes of the diff handle in the constructor.
}
}
}