@@ -102,6 +102,21 @@ private static IDictionary<DiffTargets, Func<Repository, TreeComparisonHandleRet
102
102
{ typeof ( PatchStats ) , diff => new PatchStats ( diff ) } ,
103
103
} ;
104
104
105
+
106
+ private static T BuildDiffResult < T > ( DiffSafeHandle diff ) where T : class , IDiffResult
107
+ {
108
+ Func < DiffSafeHandle , object > builder ;
109
+
110
+ if ( ! ChangesBuilders . TryGetValue ( typeof ( T ) , out builder ) )
111
+ {
112
+ throw new LibGit2SharpException ( CultureInfo . InvariantCulture ,
113
+ "User-defined types passed to Compare are not supported. Supported values are: {0}" ,
114
+ string . Join ( ", " , ChangesBuilders . Keys . Select ( x => x . Name ) ) ) ;
115
+ }
116
+
117
+ return ( T ) builder ( diff ) ;
118
+ }
119
+
105
120
/// <summary>
106
121
/// Show changes between two <see cref="Blob"/>s.
107
122
/// </summary>
@@ -134,7 +149,7 @@ public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob, CompareOptions
134
149
/// <param name="oldTree">The <see cref="Tree"/> you want to compare from.</param>
135
150
/// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
136
151
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
137
- public virtual T Compare < T > ( Tree oldTree , Tree newTree ) where T : class
152
+ public virtual T Compare < T > ( Tree oldTree , Tree newTree ) where T : class , IDiffResult
138
153
{
139
154
return Compare < T > ( oldTree , newTree , null , null , null ) ;
140
155
}
@@ -146,7 +161,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree) where T : class
146
161
/// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
147
162
/// <param name="paths">The list of paths (either files or directories) that should be compared.</param>
148
163
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
149
- public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths ) where T : class
164
+ public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths ) where T : class , IDiffResult
150
165
{
151
166
return Compare < T > ( oldTree , newTree , paths , null , null ) ;
152
167
}
@@ -163,7 +178,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
163
178
/// </param>
164
179
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
165
180
public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths ,
166
- ExplicitPathsOptions explicitPathsOptions ) where T : class
181
+ ExplicitPathsOptions explicitPathsOptions ) where T : class , IDiffResult
167
182
{
168
183
return Compare < T > ( oldTree , newTree , paths , explicitPathsOptions , null ) ;
169
184
}
@@ -176,7 +191,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
176
191
/// <param name="paths">The list of paths (either files or directories) that should be compared.</param>
177
192
/// <param name="compareOptions">Additional options to define patch generation behavior.</param>
178
193
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
179
- public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths , CompareOptions compareOptions ) where T : class
194
+ public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths , CompareOptions compareOptions ) where T : class , IDiffResult
180
195
{
181
196
return Compare < T > ( oldTree , newTree , paths , null , compareOptions ) ;
182
197
}
@@ -188,7 +203,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
188
203
/// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
189
204
/// <param name="compareOptions">Additional options to define patch generation behavior.</param>
190
205
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
191
- public virtual T Compare < T > ( Tree oldTree , Tree newTree , CompareOptions compareOptions ) where T : class
206
+ public virtual T Compare < T > ( Tree oldTree , Tree newTree , CompareOptions compareOptions ) where T : class , IDiffResult
192
207
{
193
208
return Compare < T > ( oldTree , newTree , null , null , compareOptions ) ;
194
209
}
@@ -206,19 +221,8 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, CompareOptions compareOp
206
221
/// <param name="compareOptions">Additional options to define patch generation behavior.</param>
207
222
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
208
223
public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths , ExplicitPathsOptions explicitPathsOptions ,
209
- CompareOptions compareOptions ) where T : class
224
+ CompareOptions compareOptions ) where T : class , IDiffResult
210
225
{
211
- Func < DiffSafeHandle , object > builder ;
212
-
213
- if ( ! ChangesBuilders . TryGetValue ( typeof ( T ) , out builder ) )
214
- {
215
- throw new LibGit2SharpException ( CultureInfo . InvariantCulture ,
216
- "Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'." ,
217
- typeof ( T ) ,
218
- typeof ( TreeChanges ) ,
219
- typeof ( Patch ) ) ;
220
- }
221
-
222
226
var comparer = TreeToTree ( repo ) ;
223
227
ObjectId oldTreeId = oldTree != null ? oldTree . Id : null ;
224
228
ObjectId newTreeId = newTree != null ? newTree . Id : null ;
@@ -236,7 +240,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
236
240
237
241
using ( DiffSafeHandle diff = BuildDiffList ( oldTreeId , newTreeId , comparer , diffOptions , paths , explicitPathsOptions , compareOptions ) )
238
242
{
239
- return ( T ) builder ( diff ) ;
243
+ return BuildDiffResult < T > ( diff ) ;
240
244
}
241
245
}
242
246
@@ -252,7 +256,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
252
256
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
253
257
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
254
258
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
255
- public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets ) where T : class
259
+ public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets ) where T : class , IDiffResult
256
260
{
257
261
return Compare < T > ( oldTree , diffTargets , null , null , null ) ;
258
262
}
@@ -270,7 +274,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets) where T : cla
270
274
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
271
275
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
272
276
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
273
- public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets , IEnumerable < string > paths ) where T : class
277
+ public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets , IEnumerable < string > paths ) where T : class , IDiffResult
274
278
{
275
279
return Compare < T > ( oldTree , diffTargets , paths , null , null ) ;
276
280
}
@@ -293,7 +297,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
293
297
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
294
298
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
295
299
public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets , IEnumerable < string > paths ,
296
- ExplicitPathsOptions explicitPathsOptions ) where T : class
300
+ ExplicitPathsOptions explicitPathsOptions ) where T : class , IDiffResult
297
301
{
298
302
return Compare < T > ( oldTree , diffTargets , paths , explicitPathsOptions , null ) ;
299
303
}
@@ -317,19 +321,8 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
317
321
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
318
322
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
319
323
public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets , IEnumerable < string > paths ,
320
- ExplicitPathsOptions explicitPathsOptions , CompareOptions compareOptions ) where T : class
324
+ ExplicitPathsOptions explicitPathsOptions , CompareOptions compareOptions ) where T : class , IDiffResult
321
325
{
322
- Func < DiffSafeHandle , object > builder ;
323
-
324
- if ( ! ChangesBuilders . TryGetValue ( typeof ( T ) , out builder ) )
325
- {
326
- throw new LibGit2SharpException ( CultureInfo . InvariantCulture ,
327
- "Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'." ,
328
- typeof ( T ) ,
329
- typeof ( TreeChanges ) ,
330
- typeof ( Patch ) ) ;
331
- }
332
-
333
326
var comparer = HandleRetrieverDispatcher [ diffTargets ] ( repo ) ;
334
327
ObjectId oldTreeId = oldTree != null ? oldTree . Id : null ;
335
328
@@ -349,7 +342,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
349
342
350
343
using ( DiffSafeHandle diff = BuildDiffList ( oldTreeId , null , comparer , diffOptions , paths , explicitPathsOptions , compareOptions ) )
351
344
{
352
- return ( T ) builder ( diff ) ;
345
+ return BuildDiffResult < T > ( diff ) ;
353
346
}
354
347
}
355
348
@@ -363,7 +356,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
363
356
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
364
357
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
365
358
/// <returns>A <typeparamref name="T"/> containing the changes between the working directory and the index.</returns>
366
- public virtual T Compare < T > ( ) where T : class
359
+ public virtual T Compare < T > ( ) where T : class , IDiffResult
367
360
{
368
361
return Compare < T > ( DiffModifiers . None ) ;
369
362
}
@@ -379,7 +372,7 @@ public virtual T Compare<T>() where T : class
379
372
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
380
373
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
381
374
/// <returns>A <typeparamref name="T"/> containing the changes between the working directory and the index.</returns>
382
- public virtual T Compare < T > ( IEnumerable < string > paths ) where T : class
375
+ public virtual T Compare < T > ( IEnumerable < string > paths ) where T : class , IDiffResult
383
376
{
384
377
return Compare < T > ( DiffModifiers . None , paths ) ;
385
378
}
@@ -396,7 +389,7 @@ public virtual T Compare<T>(IEnumerable<string> paths) where T : class
396
389
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
397
390
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
398
391
/// <returns>A <typeparamref name="T"/> containing the changes between the working directory and the index.</returns>
399
- public virtual T Compare < T > ( IEnumerable < string > paths , bool includeUntracked ) where T : class
392
+ public virtual T Compare < T > ( IEnumerable < string > paths , bool includeUntracked ) where T : class , IDiffResult
400
393
{
401
394
return Compare < T > ( includeUntracked ? DiffModifiers . IncludeUntracked : DiffModifiers . None , paths ) ;
402
395
}
@@ -417,7 +410,7 @@ public virtual T Compare<T>(IEnumerable<string> paths, bool includeUntracked) wh
417
410
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
418
411
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
419
412
/// <returns>A <typeparamref name="T"/> containing the changes between the working directory and the index.</returns>
420
- public virtual T Compare < T > ( IEnumerable < string > paths , bool includeUntracked , ExplicitPathsOptions explicitPathsOptions ) where T : class
413
+ public virtual T Compare < T > ( IEnumerable < string > paths , bool includeUntracked , ExplicitPathsOptions explicitPathsOptions ) where T : class , IDiffResult
421
414
{
422
415
return Compare < T > ( includeUntracked ? DiffModifiers . IncludeUntracked : DiffModifiers . None , paths , explicitPathsOptions ) ;
423
416
}
@@ -443,7 +436,7 @@ public virtual T Compare<T>(
443
436
IEnumerable < string > paths ,
444
437
bool includeUntracked ,
445
438
ExplicitPathsOptions explicitPathsOptions ,
446
- CompareOptions compareOptions ) where T : class
439
+ CompareOptions compareOptions ) where T : class , IDiffResult
447
440
{
448
441
return Compare < T > ( includeUntracked ? DiffModifiers . IncludeUntracked : DiffModifiers . None , paths , explicitPathsOptions , compareOptions ) ;
449
442
}
@@ -452,19 +445,8 @@ internal virtual T Compare<T>(
452
445
DiffModifiers diffOptions ,
453
446
IEnumerable < string > paths = null ,
454
447
ExplicitPathsOptions explicitPathsOptions = null ,
455
- CompareOptions compareOptions = null ) where T : class
448
+ CompareOptions compareOptions = null ) where T : class , IDiffResult
456
449
{
457
- Func < DiffSafeHandle , object > builder ;
458
-
459
- if ( ! ChangesBuilders . TryGetValue ( typeof ( T ) , out builder ) )
460
- {
461
- throw new LibGit2SharpException ( CultureInfo . InvariantCulture ,
462
- "Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'." ,
463
- typeof ( T ) ,
464
- typeof ( TreeChanges ) ,
465
- typeof ( Patch ) ) ;
466
- }
467
-
468
450
var comparer = WorkdirToIndex ( repo ) ;
469
451
470
452
if ( explicitPathsOptions != null )
@@ -479,7 +461,7 @@ internal virtual T Compare<T>(
479
461
480
462
using ( DiffSafeHandle diff = BuildDiffList ( null , null , comparer , diffOptions , paths , explicitPathsOptions , compareOptions ) )
481
463
{
482
- return ( T ) builder ( diff ) ;
464
+ return BuildDiffResult < T > ( diff ) ;
483
465
}
484
466
}
485
467
0 commit comments