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