@@ -95,13 +95,6 @@ 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
98
/// <summary>
106
99
/// Show changes between two <see cref="Blob"/>s.
107
100
/// </summary>
@@ -134,7 +127,7 @@ public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob, CompareOptions
134
127
/// <param name="oldTree">The <see cref="Tree"/> you want to compare from.</param>
135
128
/// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
136
129
/// <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
130
+ public virtual T Compare < T > ( Tree oldTree , Tree newTree ) where T : class , IDiffResult < T > , new ( )
138
131
{
139
132
return Compare < T > ( oldTree , newTree , null , null , null ) ;
140
133
}
@@ -146,7 +139,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree) where T : class
146
139
/// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
147
140
/// <param name="paths">The list of paths (either files or directories) that should be compared.</param>
148
141
/// <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
142
+ public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths ) where T : class , IDiffResult < T > , new ( )
150
143
{
151
144
return Compare < T > ( oldTree , newTree , paths , null , null ) ;
152
145
}
@@ -163,7 +156,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
163
156
/// </param>
164
157
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
165
158
public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths ,
166
- ExplicitPathsOptions explicitPathsOptions ) where T : class
159
+ ExplicitPathsOptions explicitPathsOptions ) where T : class , IDiffResult < T > , new ( )
167
160
{
168
161
return Compare < T > ( oldTree , newTree , paths , explicitPathsOptions , null ) ;
169
162
}
@@ -176,7 +169,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
176
169
/// <param name="paths">The list of paths (either files or directories) that should be compared.</param>
177
170
/// <param name="compareOptions">Additional options to define patch generation behavior.</param>
178
171
/// <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
172
+ public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths , CompareOptions compareOptions ) where T : class , IDiffResult < T > , new ( )
180
173
{
181
174
return Compare < T > ( oldTree , newTree , paths , null , compareOptions ) ;
182
175
}
@@ -188,7 +181,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
188
181
/// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
189
182
/// <param name="compareOptions">Additional options to define patch generation behavior.</param>
190
183
/// <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
184
+ public virtual T Compare < T > ( Tree oldTree , Tree newTree , CompareOptions compareOptions ) where T : class , IDiffResult < T > , new ( )
192
185
{
193
186
return Compare < T > ( oldTree , newTree , null , null , compareOptions ) ;
194
187
}
@@ -206,18 +199,8 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, CompareOptions compareOp
206
199
/// <param name="compareOptions">Additional options to define patch generation behavior.</param>
207
200
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
208
201
public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths , ExplicitPathsOptions explicitPathsOptions ,
209
- CompareOptions compareOptions ) where T : class
202
+ CompareOptions compareOptions ) where T : class , IDiffResult < T > , new ( )
210
203
{
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
204
222
205
var comparer = TreeToTree ( repo ) ;
223
206
ObjectId oldTreeId = oldTree != null ? oldTree . Id : null ;
@@ -236,7 +219,10 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
236
219
237
220
using ( DiffSafeHandle diff = BuildDiffList ( oldTreeId , newTreeId , comparer , diffOptions , paths , explicitPathsOptions , compareOptions ) )
238
221
{
239
- return ( T ) builder ( diff ) ;
222
+ using ( var proxy = new DiffSafeHandleProxy ( diff ) )
223
+ {
224
+ return new T ( ) . FromNative ( proxy ) ;
225
+ }
240
226
}
241
227
}
242
228
@@ -252,7 +238,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
252
238
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
253
239
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
254
240
/// <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
241
+ public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets ) where T : class , IDiffResult < T > , new ( )
256
242
{
257
243
return Compare < T > ( oldTree , diffTargets , null , null , null ) ;
258
244
}
@@ -270,7 +256,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets) where T : cla
270
256
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
271
257
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
272
258
/// <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
259
+ public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets , IEnumerable < string > paths ) where T : class , IDiffResult < T > , new ( )
274
260
{
275
261
return Compare < T > ( oldTree , diffTargets , paths , null , null ) ;
276
262
}
@@ -293,7 +279,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
293
279
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
294
280
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
295
281
public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets , IEnumerable < string > paths ,
296
- ExplicitPathsOptions explicitPathsOptions ) where T : class
282
+ ExplicitPathsOptions explicitPathsOptions ) where T : class , IDiffResult < T > , new ( )
297
283
{
298
284
return Compare < T > ( oldTree , diffTargets , paths , explicitPathsOptions , null ) ;
299
285
}
@@ -317,19 +303,8 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
317
303
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
318
304
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
319
305
public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets , IEnumerable < string > paths ,
320
- ExplicitPathsOptions explicitPathsOptions , CompareOptions compareOptions ) where T : class
306
+ ExplicitPathsOptions explicitPathsOptions , CompareOptions compareOptions ) where T : class , IDiffResult < T > , new ( )
321
307
{
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
308
var comparer = HandleRetrieverDispatcher [ diffTargets ] ( repo ) ;
334
309
ObjectId oldTreeId = oldTree != null ? oldTree . Id : null ;
335
310
@@ -349,7 +324,10 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
349
324
350
325
using ( DiffSafeHandle diff = BuildDiffList ( oldTreeId , null , comparer , diffOptions , paths , explicitPathsOptions , compareOptions ) )
351
326
{
352
- return ( T ) builder ( diff ) ;
327
+ using ( var proxy = new DiffSafeHandleProxy ( diff ) )
328
+ {
329
+ return new T ( ) . FromNative ( proxy ) ;
330
+ }
353
331
}
354
332
}
355
333
@@ -363,7 +341,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
363
341
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
364
342
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
365
343
/// <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
344
+ public virtual T Compare < T > ( ) where T : class , IDiffResult < T > , new ( )
367
345
{
368
346
return Compare < T > ( DiffModifiers . None ) ;
369
347
}
@@ -379,7 +357,7 @@ public virtual T Compare<T>() where T : class
379
357
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
380
358
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
381
359
/// <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
360
+ public virtual T Compare < T > ( IEnumerable < string > paths ) where T : class , IDiffResult < T > , new ( )
383
361
{
384
362
return Compare < T > ( DiffModifiers . None , paths ) ;
385
363
}
@@ -396,7 +374,7 @@ public virtual T Compare<T>(IEnumerable<string> paths) where T : class
396
374
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
397
375
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
398
376
/// <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
377
+ public virtual T Compare < T > ( IEnumerable < string > paths , bool includeUntracked ) where T : class , IDiffResult < T > , new ( )
400
378
{
401
379
return Compare < T > ( includeUntracked ? DiffModifiers . IncludeUntracked : DiffModifiers . None , paths ) ;
402
380
}
@@ -417,7 +395,7 @@ public virtual T Compare<T>(IEnumerable<string> paths, bool includeUntracked) wh
417
395
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
418
396
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
419
397
/// <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
398
+ public virtual T Compare < T > ( IEnumerable < string > paths , bool includeUntracked , ExplicitPathsOptions explicitPathsOptions ) where T : class , IDiffResult < T > , new ( )
421
399
{
422
400
return Compare < T > ( includeUntracked ? DiffModifiers . IncludeUntracked : DiffModifiers . None , paths , explicitPathsOptions ) ;
423
401
}
@@ -443,7 +421,7 @@ public virtual T Compare<T>(
443
421
IEnumerable < string > paths ,
444
422
bool includeUntracked ,
445
423
ExplicitPathsOptions explicitPathsOptions ,
446
- CompareOptions compareOptions ) where T : class
424
+ CompareOptions compareOptions ) where T : class , IDiffResult < T > , new ( )
447
425
{
448
426
return Compare < T > ( includeUntracked ? DiffModifiers . IncludeUntracked : DiffModifiers . None , paths , explicitPathsOptions , compareOptions ) ;
449
427
}
@@ -452,19 +430,8 @@ internal virtual T Compare<T>(
452
430
DiffModifiers diffOptions ,
453
431
IEnumerable < string > paths = null ,
454
432
ExplicitPathsOptions explicitPathsOptions = null ,
455
- CompareOptions compareOptions = null ) where T : class
433
+ CompareOptions compareOptions = null ) where T : class , IDiffResult < T > , new ( )
456
434
{
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
435
var comparer = WorkdirToIndex ( repo ) ;
469
436
470
437
if ( expl
10000
icitPathsOptions != null )
@@ -479,7 +446,10 @@ internal virtual T Compare<T>(
479
446
480
447
using ( DiffSafeHandle diff = BuildDiffList ( null , null , comparer , diffOptions , paths , explicitPathsOptions , compareOptions ) )
481
448
{
482
- return ( T ) builder ( diff ) ;
449
+ using ( var proxy = new DiffSafeHandleProxy ( diff ) )
450
+ {
451
+ return new T ( ) . FromNative ( proxy ) ;
452
+ }
483
453
}
484
454
}
485
455
0 commit comments