-
Notifications
You must be signed in to change notification settings - Fork 926
Expand file tree
/
Copy pathDiff.cs
More file actions
328 lines (275 loc) · 13.8 KB
/
Diff.cs
File metadata and controls
328 lines (275 loc) · 13.8 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;
using LibGit2Sharp.Core.Handles;
using Environment = System.Environment;
namespace LibGit2Sharp
{
/// <summary>
/// Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, or changes between two files on disk.
/// <para>
/// Copied and renamed files currently cannot be detected, as the feature is not supported by libgit2 yet.
/// These files will be shown as a pair of Deleted/Added files.</para>
/// </summary>
public class Diff
{
private readonly Repository repo;
private static GitDiffOptions BuildOptions(DiffOptions diffOptions, FilePath[] filePaths = null, MatchedPathsAggregator matchedPathsAggregator = null)
{
var options = new GitDiffOptions();
options.Flags |= GitDiffOptionFlags.GIT_DIFF_INCLUDE_TYPECHANGE;
options.ContextLines = 3;
if (diffOptions.HasFlag(DiffOptions.IncludeUntracked))
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_INCLUDE_UNTRACKED |
GitDiffOptionFlags.GIT_DIFF_RECURSE_UNTRACKED_DIRS |
GitDiffOptionFlags.GIT_DIFF_INCLUDE_UNTRACKED_CONTENT;
}
if (diffOptions.HasFlag(DiffOptions.IncludeIgnored))
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_INCLUDE_IGNORED;
}
if (diffOptions.HasFlag(DiffOptions.IncludeUnmodified))
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_INCLUDE_UNMODIFIED;
}
if (diffOptions.HasFlag(DiffOptions.DisablePathspecMatch))
{
options.Flags |= GitDiffOptionFlags.GIT_DIFF_DISABLE_PATHSPEC_MATCH;
}
if (matchedPathsAggregator != null)
{
options.NotifyCallback = matchedPathsAggregator.OnGitDiffNotify;
}
if (filePaths == null)
{
return options;
}
options.PathSpec = GitStrArrayIn.BuildFrom(filePaths);
return options;
}
private static FilePath[] ToFilePaths(Repository repo, IEnumerable<string> paths)
{
if (paths == null)
{
return null;
}
var filePaths = new List<FilePath>();
foreach (string path in paths)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException("At least one provided path is either null or empty.", "paths");
}
filePaths.Add(repo.BuildRelativePathFrom(path));
}
if (filePaths.Count == 0)
{
throw new ArgumentException("No path has been provided.", "paths");
}
return filePaths.ToArray();
}
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected Diff()
{ }
internal Diff(Repository repo)
{
this.repo = repo;
}
/// <summary>
/// Show changes between two <see cref = "Tree"/>s.
/// </summary>
/// <param name = "oldTree">The <see cref = "Tree"/> you want to compare from.</param>
/// <param name = "newTree">The <see cref = "Tree"/> you want to compare to.</param>
/// <param name = "paths">The list of paths (either files or directories) that should be compared.</param>
/// <param name = "explicitPathsOptions">
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
/// Use these options to determine how unmatched explicit paths should be handled.
/// </param>
/// <returns>A <see cref = "TreeChanges"/> containing the changes between the <paramref name = "oldTree"/> and the <paramref name = "newTree"/>.</returns>
public virtual TreeChanges Compare(Tree oldTree, Tree newTree, IEnumerable<string> paths = null, ExplicitPathsOptions explicitPathsOptions = null)
{
var comparer = TreeToTree(repo);
ObjectId oldTreeId = oldTree != null ? oldTree.Id : null;
ObjectId newTreeId = newTree != null ? newTree.Id : null;
var diffOptions = DiffOptions.None;
if (explicitPathsOptions != null)
{
diffOptions |= DiffOptions.DisablePathspecMatch;
if (explicitPathsOptions.ShouldFailOnUnmatchedPath ||
explicitPathsOptions.OnUnmatchedPath != null)
{
diffOptions |= DiffOptions.IncludeUnmodified;
}
}
return BuildTreeChangesFromComparer(oldTreeId, newTreeId, comparer, diffOptions, paths, explicitPathsOptions);
}
/// <summary>
/// Show changes between two <see cref = "Blob"/>s.
/// </summary>
/// <param name = "oldBlob">The <see cref = "Blob"/> you want to compare from.</param>
/// <param name = "newBlob">The <see cref = "Blob"/> you want to compare to.</param>
/// <returns>A <see cref = "ContentChanges"/> containing the changes between the <paramref name = "oldBlob"/> and the <paramref name = "newBlob"/>.</returns>
public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob)
{
using (GitDiffOptions options = BuildOptions(DiffOptions.None))
{
return new ContentChanges(repo, oldBlob, newBlob, options);
}
}
private readonly IDictionary<DiffTargets, Func<Repository, TreeComparisonHandleRetriever>> handleRetrieverDispatcher = BuildHandleRetrieverDispatcher();
private static IDictionary<DiffTargets, Func<Repository, TreeComparisonHandleRetriever>> BuildHandleRetrieverDispatcher()
{
return new Dictionary<DiffTargets, Func<Repository, TreeComparisonHandleRetriever>>
{
{ DiffTargets.Index, IndexToTree },
{ DiffTargets.WorkingDirectory, WorkdirToTree },
{ DiffTargets.Index | DiffTargets.WorkingDirectory, WorkdirAndIndexToTree },
};
}
/// <summary>
/// Show changes between a <see cref = "Tree"/> and the Index, the Working Directory, or both.
/// </summary>
/// <param name = "oldTree">The <see cref = "Tree"/> to compare from.</param>
/// <param name = "diffTargets">The targets to compare to.</param>
/// <param name = "paths">The list of paths (either files or directories) that should be compared.</param>
/// <param name = "explicitPathsOptions">
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
/// Use these options to determine how unmatched explicit paths should be handled.
/// </param>
/// <returns>A <see cref = "TreeChanges"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
public virtual TreeChanges Compare(Tree oldTree, DiffTargets diffTargets, IEnumerable<string> paths = null, ExplicitPathsOptions explicitPathsOptions = null)
{
var comparer = handleRetrieverDispatcher[diffTargets](repo);
ObjectId oldTreeId = oldTree != null ? oldTree.Id : null;
DiffOptions diffOptions = diffTargets.HasFlag(DiffTargets.WorkingDirectory) ?
DiffOptions.IncludeUntracked : DiffOptions.None;
if (explicitPathsOptions != null)
{
diffOptions |= DiffOptions.DisablePathspecMatch;
if (explicitPathsOptions.ShouldFailOnUnmatchedPath ||
explicitPathsOptions.OnUnmatchedPath != null)
{
diffOptions |= DiffOptions.IncludeUnmodified;
}
}
return BuildTreeChangesFromComparer(oldTreeId, null, comparer, diffOptions, paths, explicitPathsOptions);
}
/// <summary>
/// Show changes between the working directory and the index.
/// </summary>
/// <param name = "paths">The list of paths (either files or directories) that should be compared.</param>
/// <param name = "includeUntracked">If true, include untracked files from the working dir as additions. Otherwise ignore them.</param>
/// <param name = "explicitPathsOptions">
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
/// Use these options to determine how unmatched explicit paths should be handled.
/// </param>
/// <returns>A <see cref = "TreeChanges"/> containing the changes between the working directory and the index.</returns>
public virtual TreeChanges Compare(IEnumerable<string> paths = null, bool includeUntracked = false, ExplicitPathsOptions explicitPathsOptions = null)
{
return Compare(includeUntracked ? DiffOptions.IncludeUntracked : DiffOptions.None, paths, explicitPathsOptions);
}
internal virtual TreeChanges Compare(DiffOptions diffOptions, IEnumerable<string> paths = null,
ExplicitPathsOptions explicitPathsOptions = null)
{
var comparer = WorkdirToIndex(repo);
if (explicitPathsOptions != null)
{
diffOptions |= DiffOptions.DisablePathspecMatch;
if (explicitPathsOptions.ShouldFailOnUnmatchedPath ||
explicitPathsOptions.OnUnmatchedPath != null)
{
diffOptions |= DiffOptions.IncludeUnmodified;
}
}
return BuildTreeChangesFromComparer(null, null, comparer, diffOptions, paths, explicitPathsOptions);
}
private delegate DiffListSafeHandle TreeComparisonHandleRetriever(ObjectId oldTreeId, ObjectId newTreeId, GitDiffOptions options);
private static TreeComparisonHandleRetriever TreeToTree(Repository repo)
{
return (oh, nh, o) => Proxy.git_diff_tree_to_tree(repo.Handle, oh, nh, o);
}
private static TreeComparisonHandleRetriever WorkdirToIndex(Repository repo)
{
return (oh, nh, o) => Proxy.git_diff_index_to_workdir(repo.Handle, repo.Index.Handle, o);
}
private static TreeComparisonHandleRetriever WorkdirToTree(Repository repo)
{
return (oh, nh, o) => Proxy.git_diff_tree_to_workdir(repo.Handle, oh, o);
}
private static TreeComparisonHandleRetriever WorkdirAndIndexToTree(Repository repo)
{
TreeComparisonHandleRetriever comparisonHandleRetriever = (oh, nh, o) =>
{
DiffListSafeHandle diff = null, diff2 = null;
try
{
diff = Proxy.git_diff_tree_to_index(repo.Handle, repo.Index.Handle, oh, o);
diff2 = Proxy.git_diff_index_to_workdir(repo.Handle, repo.Index.Handle, o);
Proxy.git_diff_merge(diff, diff2);
}
catch
{
diff.SafeDispose();
throw;
}
finally
{
diff2.SafeDispose();
}
return diff;
};
return comparisonHandleRetriever;
}
private static TreeComparisonHandleRetriever IndexToTree(Repository repo)
{
return (oh, nh, o) => Proxy.git_diff_tree_to_index(repo.Handle, repo.Index.Handle, oh, o);
}
private TreeChanges BuildTreeChangesFromComparer(
ObjectId oldTreeId, ObjectId newTreeId, TreeComparisonHandleRetriever comparisonHandleRetriever,
DiffOptions diffOptions, IEnumerable<string> paths = null, ExplicitPathsOptions explicitPathsOptions = null)
{
var matchedPaths = new MatchedPathsAggregator();
var filePaths = ToFilePaths(repo, paths);
using (GitDiffOptions options = BuildOptions(diffOptions, filePaths, matchedPaths))
using (DiffListSafeHandle diffList = comparisonHandleRetriever(oldTreeId, newTreeId, options))
{
if (explicitPathsOptions != null)
{
DispatchUnmatchedPaths(explicitPathsOptions, filePaths, matchedPaths);
}
return new TreeChanges(diffList);
}
}
private static void DispatchUnmatchedPaths(ExplicitPathsOptions explicitPathsOptions,
IEnumerable<FilePath> filePaths,
IEnumerable<FilePath> matchedPaths)
{
List<FilePath> unmatchedPaths = (filePaths != null ?
filePaths.Except(matchedPaths) : Enumerable.Empty<FilePath>()).ToList();
if (!unmatchedPaths.Any())
{
return;
}
if (explicitPathsOptions.OnUnmatchedPath != null)
{
unmatchedPaths.ForEach(filePath => explicitPathsOptions.OnUnmatchedPath(filePath.Native));
}
if (explicitPathsOptions.ShouldFailOnUnmatchedPath)
{
throw new UnmatchedPathException(BuildUnmatchedPathsMessage(unmatchedPaths));
}
}
private static string BuildUnmatchedPathsMessage(List<FilePath> unmatchedPaths)
{
var message = new StringBuilder("There were some unmatched paths:" + Environment.NewLine);
unmatchedPaths.ForEach(filePath => message.AppendFormat("- {0}{1}", filePath.Native, Environment.NewLine));
return message.ToString();
}
}
}