@@ -95,6 +95,28 @@ private static IDictionary<DiffTargets, Func<Repository, TreeComparisonHandleRet
95
95
} ;
96
96
}
97
97
98
+ private static readonly IDictionary < Type , Func < DiffSafeHandle , object > > ChangesBuilders = new Dictionary < Type , Func < DiffSafeHandle , object > >
99
+ {
100
+ { typeof ( Patch ) , diff => new Patch ( diff ) } ,
101
+ { typeof ( TreeChanges ) , diff => new TreeChanges ( diff ) } ,
102
+ { typeof ( PatchStats ) , diff => new PatchStats ( diff ) } ,
103
+ } ;
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
+
98
120
/// <summary>
99
121
/// Show changes between two <see cref="Blob"/>s.
100
122
/// </summary>
@@ -127,7 +149,7 @@ public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob, CompareOptions
127
149
/// <param name="oldTree">The <see cref="Tree"/> you want to compare from.</param>
128
150
/// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
129
151
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
130
- public virtual T Compare < T > ( Tree oldTree , Tree newTree ) where T : class , IDiffResult < T > , new ( )
152
+ public virtual T Compare < T > ( Tree oldTree , Tree newTree ) where T : class , IDiffResult
131
153
{
132
154
return Compare < T > ( oldTree , newTree , null , null , null ) ;
133
155
}
@@ -139,7 +161,7 @@ public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob, CompareOptions
139
161
/// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
140
162
/// <param name="paths">The list of paths (either files or directories) that should be compared.</param>
141
163
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
142
- public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths ) where T : class , IDiffResult < T > , new ( )
164
+ public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths ) where T : class , IDiffResult
143
165
{
144
166
return Compare < T > ( oldTree , newTree , paths , null , null ) ;
145
167
}
@@ -156,7 +178,7 @@ public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob, CompareOptions
156
178
/// </param>
157
179
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
158
180
public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths ,
159
- ExplicitPathsOptions explicitPathsOptions ) where T : class , IDiffResult < T > , new ( )
181
+ ExplicitPathsOptions explicitPathsOptions ) where T : class , IDiffResult
160
182
{
161
183
return Compare < T > ( oldTree , newTree , paths , explicitPathsOptions , null ) ;
162
184
}
@@ -169,7 +191,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
169
191
/// <param name="paths">The list of paths (either files or directories) that should be compared.</param>
170
192
/// <param name="compareOptions">Additional options to define patch generation behavior.</param>
171
193
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
172
- public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths , CompareOptions compareOptions ) where T : class , IDiffResult < T > , new ( )
194
+ public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths , CompareOptions compareOptions ) where T : class , IDiffResult
173
195
{
174
196
return Compare < T > ( oldTree , newTree , paths , null , compareOptions ) ;
175
197
}
@@ -181,7 +203,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
181
203
/// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
182
204
/// <param name="compareOptions">Additional options to define patch generation behavior.</param>
183
205
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
184
- public virtual T Compare < T > ( Tree oldTree , Tree newTree , CompareOptions compareOptions ) where T : class , IDiffResult < T > , new ( )
206
+ public virtual T Compare < T > ( Tree oldTree , Tree newTree , CompareOptions compareOptions ) where T : class , IDiffResult
185
207
{
186
208
return Compare < T > ( oldTree , newTree , null , null , compareOptions ) ;
187
209
}
@@ -199,9 +221,8 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
199
221
/// <param name="compareOptions">Additional options to define patch generation behavior.</param>
200
222
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
201
223
public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths , ExplicitPathsOptions explicitPathsOptions ,
202
- CompareOptions compareOptions ) where T : class , IDiffResult < T > , new ( )
224
+ CompareOptions compareOptions ) where T : class , IDiffResult
203
225
{
204
-
205
226
var comparer = TreeToTree ( repo ) ;
206
227
ObjectId oldTreeId = oldTree != null ? oldTree . Id : null ;
207
228
ObjectId newTreeId = newTree != null ? newTree . Id : null ;
@@ -219,10 +240,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
219
240
220
241
using ( DiffSafeHandle diff = BuildDiffList ( oldTreeId , newTreeId , comparer , diffOptions , paths , explicitPathsOptions , compareOptions ) )
221
242
{
222
- using ( var proxy = new DiffSafeHandleProxy ( diff ) )
223
- {
224
- return new T ( ) . FromNative ( proxy ) ;
225
- }
243
+ return BuildDiffResult < T > ( diff ) ;
226
244
}
227
245
}
228
246
@@ -238,7 +256,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
238
256
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
239
257
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
240
258
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
241
- public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets ) where T : class , IDiffResult < T > , new ( )
259
+ public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets ) where T : class , IDiffResult
242
260
{
243
261
return Compare < T > ( oldTree , diffTargets , null , null , null ) ;
244
262
}
@@ -256,7 +274,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
256
274
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
257
275
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
258
276
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
259
- public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets , IEnumerable < string > paths ) where T : class , IDiffResult < T > , new ( )
277
+ public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets , IEnumerable < string > paths ) where T : class , IDiffResult
260
278
{
261
279
return Compare < T > ( oldTree , diffTargets , paths , null , null ) ;
262
280
}
@@ -279,7 +297,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
279
297
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
280
298
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
281
299
public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets , IEnumerable < string > paths ,
282
- ExplicitPathsOptions explicitPathsOptions ) where T : class , IDiffResult < T > , new ( )
300
+ ExplicitPathsOptions explicitPathsOptions ) where T : class , IDiffResult
283
301
{
284
302
return Compare < T > ( oldTree , diffTargets , paths , explicitPathsOptions , null ) ;
285
303
}
@@ -303,7 +321,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
303
321
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
304
322
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
305
323
public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets , IEnumerable < string > paths ,
306
- ExplicitPathsOptions explicitPathsOptions , CompareOptions compareOptions ) where T : class , IDiffResult < T > , new ( )
324
+ ExplicitPathsOptions explicitPathsOptions , CompareOptions compareOptions ) where T : class , IDiffResult
307
325
{
308
326
var comparer = HandleRetrieverDispatcher [ diffTargets ] ( repo ) ;
309
327
ObjectId oldTreeId = oldTree != null ? oldTree . Id : null ;
@@ -324,10 +342,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
324
342
325
343
using ( DiffSafeHandle diff = BuildDiffList ( oldTreeId , null , comparer , diffOptions , paths , explicitPathsOptions , compareOptions ) )
326
344
{
327
- using ( var proxy = new DiffSafeHandleProxy ( diff ) )
328
- {
329
- return new T ( ) . FromNative ( proxy ) ;
330
- }
345
+ return BuildDiffResult < T > ( diff ) ;
331
346
}
332
347
}
333
348
@@ -341,7 +356,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
341
356
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
342
357
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
343
358
/// <returns>A <typeparamref name="T"/> containing the changes between the working directory and the index.</returns>
344
- public virtual T Compare < T > ( ) where T : class , IDiffResult < T > , new ( )
359
+ public virtual T Compare < T > ( ) where T : class , IDiffResult
345
360
{
346
361
return Compare < T > ( DiffModifiers . None ) ;
347
362
}
@@ -357,7 +372,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
357
372
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
358
373
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
359
374
/// <returns>A <typeparamref name="T"/> containing the changes between the working directory and the index.</returns>
360
- public virtual T Compare < T > ( IEnumerable < string > paths ) where T : class , IDiffResult < T > , new ( )
375
+ public virtual T Compare < T > ( IEnumerable < string > paths ) where T : class , IDiffResult
361
376
{
362
377
return Compare < T > ( DiffModifiers . None , paths ) ;
363
378
}
@@ -374,7 +389,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
374
389
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
375
390
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
376
391
/// <returns>A <typeparamref name="T"/> containing the changes between the working directory and the index.</returns>
377
- public virtual T Compare < T > ( IEnumerable < string > paths , bool includeUntracked ) where T : class , IDiffResult < T > , new ( )
392
+ public virtual T Compare < T > ( IEnumerable < string > paths , bool includeUntracked ) where T : class , IDiffResult
378
393
{
379
394
return Compare < T > ( includeUntracked ? DiffModifiers . IncludeUntracked : DiffModifiers . None , paths ) ;
380
395
}
@@ -395,7 +410,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
395
410
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
396
411
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
397
412
/// <returns>A <typeparamref name="T"/> containing the changes between the working directory and the index.</returns>
398
- public virtual T Compare < T > ( IEnumerable < string > paths , bool includeUntracked , ExplicitPathsOptions explicitPathsOptions ) where T : class , IDiffResult < T > , new ( )
413
+ public virtual T Compare < T > ( IEnumerable < string > paths , bool includeUntracked , ExplicitPathsOptions explicitPathsOptions ) where T : class , IDiffResult
399
414
{
400
415
return Compare < T > ( includeUntracked ? DiffModifiers . IncludeUntracked : DiffModifiers . None , paths , explicitPathsOptions ) ;
401
416
}
@@ -421,7 +436,7 @@ public virtual T Compare<T>(
421
436
IEnumerable < string > paths ,
422
437
bool includeUntracked ,
423
438
ExplicitPathsOptions explicitPathsOptions ,
424
- CompareOptions compareOptions ) where T : class , IDiffResult < T > , new ( )
439
+ CompareOptions compareOptions ) where T : class , IDiffResult
425
440
{
426
441
return Compare < T > ( includeUntracked ? DiffModifiers . IncludeUntracked : DiffModifiers . None , paths , explicitPathsOptions , compareOptions ) ;
427
442
}
@@ -430,7 +445,7 @@ internal virtual T Compare<T>(
430
445
DiffModifiers diffOptions ,
431
446
IEnumerable < string > paths = null ,
432
447
ExplicitPathsOptions explicitPathsOptions = null ,
433
- CompareOptions compareOptions = null ) where T : class , IDiffResult < T > , new ( )
448
+ CompareOptions compareOptions = null ) where T : class , IDiffResult
434
449
{
435
450
var comparer = WorkdirToIndex ( repo ) ;
436
451
@@ -446,10 +461,7 @@ internal virtual T Compare<T>(
446
461
447
462
using ( DiffSafeHandle diff = BuildDiffList ( null , null , comparer , diffOptions , paths , explicitPathsOptions , compareOptions ) )
448
463
{
449
- using ( var proxy = new DiffSafeHandleProxy ( diff ) )
450
- {
451
- return new T ( ) . FromNative ( proxy ) ;
452
- }
464
+ return BuildDiffResult < T > ( diff ) ;
453
465
}
454
466
}
455
467
0 commit comments