@@ -102,6 +102,21 @@ private static IDictionary<DiffTargets, Func<Repository, TreeComparisonHandleRet
102102 { typeof ( PatchStats ) , diff => new PatchStats ( diff ) } ,
103103 } ;
104104
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+
105120 /// <summary>
106121 /// Show changes between two <see cref="Blob"/>s.
107122 /// </summary>
@@ -134,7 +149,7 @@ public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob, CompareOptions
134149 /// <param name="oldTree">The <see cref="Tree"/> you want to compare from.</param>
135150 /// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
136151 /// <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
138153 {
139154 return Compare < T > ( oldTree , newTree , null , null , null ) ;
140155 }
@@ -146,7 +161,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree) where T : class
146161 /// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
147162 /// <param name="paths">The list of paths (either files or directories) that should be compared.</param>
148163 /// <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
150165 {
151166 return Compare < T > ( oldTree , newTree , paths , null , null ) ;
152167 }
@@ -163,7 +178,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
163178 /// </param>
164179 /// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
165180 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
167182 {
168183 return Compare < T > ( oldTree , newTree , paths , explicitPathsOptions , null ) ;
169184 }
@@ -176,7 +191,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
176191 /// <param name="paths">The list of paths (either files or directories) that should be compared.</param>
177192 /// <param name="compareOptions">Additional options to define patch generation behavior.</param>
178193 /// <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
180195 {
181196 return Compare < T > ( oldTree , newTree , paths , null , compareOptions ) ;
182197 }
@@ -188,7 +203,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
188203 /// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
189204 /// <param name="compareOptions">Additional options to define patch generation behavior.</param>
190205 /// <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
192207 {
193208 return Compare < T > ( oldTree , newTree , null , null , compareOptions ) ;
194209 }
@@ -206,19 +221,8 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, CompareOptions compareOp
206221 /// <param name="compareOptions">Additional options to define patch generation behavior.</param>
207222 /// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
208223 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
210225 {
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-
222226 var comparer = TreeToTree ( repo ) ;
223227 ObjectId oldTreeId = oldTree != null ? oldTree . Id : null ;
224228 ObjectId newTreeId = newTree != null ? newTree . Id : null ;
@@ -236,7 +240,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
236240
237241 using ( DiffSafeHandle diff = BuildDiffList ( oldTreeId , newTreeId , comparer , diffOptions , paths , explicitPathsOptions , compareOptions ) )
238242 {
239- return ( T ) builder ( diff ) ;
243+ return BuildDiffResult < T > ( diff ) ;
240244 }
241245 }
242246
@@ -252,7 +256,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
252256 /// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
253257 /// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
254258 /// <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
256260 {
257261 return Compare < T > ( oldTree , diffTargets , null , null , null ) ;
258262 }
@@ -270,7 +274,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets) where T : cla
270274 /// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
271275 /// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
272276 /// <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
274278 {
275279 return Compare < T > ( oldTree , diffTargets , paths , null , null ) ;
276280 }
@@ -293,7 +297,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
293297 /// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
294298 /// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
295299 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
297301 {
298302 return Compare < T > ( oldTree , diffTargets , paths , explicitPathsOptions , null ) ;
299303 }
@@ -317,19 +321,8 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
317321 /// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
318322 /// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
319323 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
321325 {
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-
333326 var comparer = HandleRetrieverDispatcher [ diffTargets ] ( repo ) ;
334327 ObjectId oldTreeId = oldTree != null ? oldTree . Id : null ;
335328
@@ -349,7 +342,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
349342
350343 using ( DiffSafeHandle diff = BuildDiffList ( oldTreeId , null , comparer , diffOptions , paths , explicitPathsOptions , compareOptions ) )
351344 {
352- return ( T ) builder ( diff ) ;
345+ return BuildDiffResult < T > ( diff ) ;
353346 }
354347 }
355348
@@ -363,7 +356,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
363356 /// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
364357 /// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
365358 /// <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
367360 {
368361 return Compare < T > ( DiffModifiers . None ) ;
369362 }
@@ -379,7 +372,7 @@ public virtual T Compare<T>() where T : class
379372 /// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
380373 /// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
381374 /// <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
383376 {
384377 return Compare < T > ( DiffModifiers . None , paths ) ;
385378 }
@@ -396,7 +389,7 @@ public virtual T Compare<T>(IEnumerable<string> paths) where T : class
396389 /// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
397390 /// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
398391 /// <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
400393 {
401394 return Compare < T > ( includeUntracked ? DiffModifiers . IncludeUntracked : DiffModifiers . None , paths ) ;
402395 }
@@ -417,7 +410,7 @@ public virtual T Compare<T>(IEnumerable<string> paths, bool includeUntracked) wh
417410 /// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
418411 /// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
419412 /// <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
421414 {
422415 return Compare < T > ( includeUntracked ? DiffModifiers . IncludeUntracked : DiffModifiers . None , paths , explicitPathsOptions ) ;
423416 }
@@ -443,7 +436,7 @@ public virtual T Compare<T>(
443436 IEnumerable < string > paths ,
444437 bool includeUntracked ,
445438 ExplicitPathsOptions explicitPathsOptions ,
446- CompareOptions compareOptions ) where T : class
439+ CompareOptions compareOptions ) where T : class , IDiffResult
447440 {
448441 return Compare < T > ( includeUntracked ? DiffModifiers . IncludeUntracked : DiffModifiers . None , paths , explicitPathsOptions , compareOptions ) ;
449442 }
@@ -452,19 +445,8 @@ internal virtual T Compare<T>(
452445 DiffModifiers diffOptions ,
453446 IEnumerable < string > paths = null ,
454447 ExplicitPathsOptions explicitPathsOptions = null ,
455- CompareOptions compareOptions = null ) where T : class
448+ CompareOptions compareOptions = null ) where T : class , IDiffResult
456449 {
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-
468450 var comparer = WorkdirToIndex ( repo ) ;
469451
470452 if ( explicitPathsOptions != null )
@@ -479,7 +461,7 @@ internal virtual T Compare<T>(
479461
480462 using ( DiffSafeHandle diff = BuildDiffList ( null , null , comparer , diffOptions , paths , explicitPathsOptions , compareOptions ) )
481463 {
482- return ( T ) builder ( diff ) ;
464+ return BuildDiffResult < T > ( diff ) ;
483465 }
484466 }
485467
0 commit comments