@@ -91,13 +91,6 @@ private static IDictionary<DiffTargets, Func<Repository, TreeComparisonHandleRet
91
91
} ;
92
92
}
93
93
94
- private static readonly IDictionary < Type , Func < DiffSafeHandle , object > > ChangesBuilders = new Dictionary < Type , Func < DiffSafeHandle , object > >
95
- {
96
- { typeof ( Patch ) , diff => new Patch ( diff ) } ,
97
- { typeof ( TreeChanges ) , diff => new TreeChanges ( diff ) } ,
98
- { typeof ( PatchStats ) , diff => new PatchStats ( diff ) } ,
99
- } ;
100
-
101
94
/// <summary>
102
95
/// Show changes between two <see cref="Blob"/>s.
103
96
/// </summary>
@@ -126,17 +119,8 @@ public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob, CompareOptions
126
119
/// <param name="compareOptions">Additional options to define patch generation behavior.</param>
127
120
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
128
121
public virtual T Compare < T > ( Tree oldTree , Tree newTree , IEnumerable < string > paths = null , ExplicitPathsOptions explicitPathsOptions = null ,
129
- CompareOptions compareOptions = null ) where T : class
122
+ CompareOptions compareOptions = null ) where T : class , IDiffResult < T > , new ( )
130
123
{
131
- Func < DiffSafeHandle , object > builder ;
132
-
133
- if ( ! ChangesBuilders . TryGetValue ( typeof ( T ) , out builder ) )
134
- {
135
- throw new LibGit2SharpException ( string . Format ( CultureInfo . InvariantCulture ,
136
- "Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'." , typeof ( T ) ,
137
- typeof ( TreeChanges ) , typeof ( Patch ) ) ) ;
138
- }
139
-
140
124
var comparer = TreeToTree ( repo ) ;
141
125
ObjectId oldTreeId = oldTree != null ? oldTree . Id : null ;
142
126
ObjectId newTreeId = newTree != null ? newTree . Id : null ;
@@ -153,10 +137,10 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
153
137
}
154
138
}
155
139
156
- using ( DiffSafeHandle diff = BuildDiffList ( oldTreeId , newTreeId , comparer ,
157
- diffOptions , paths , explicitPathsOptions , compareOptions ) )
140
+ using ( var diff = new DiffSafeHandleProxy ( BuildDiffList ( oldTreeId , newTreeId , comparer ,
141
+ diffOptions , paths , explicitPathsOptions , compareOptions ) ) )
158
142
{
159
- return ( T ) builder ( diff ) ;
143
+ return new T ( ) . FromNative ( diff ) ;
160
144
}
161
145
}
162
146
@@ -179,17 +163,8 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
179
163
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
180
164
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
181
165
public virtual T Compare < T > ( Tree oldTree , DiffTargets diffTargets , IEnumerable < string > paths = null ,
182
- ExplicitPathsOptions explicitPathsOptions = null , CompareOptions compareOptions = null ) where T : class
166
+ ExplicitPathsOptions explicitPathsOptions = null , CompareOptions compareOptions = null ) where T : class , IDiffResult < T > , new ( )
183
167
{
184
- Func < DiffSafeHandle , object > builder ;
185
-
186
- if ( ! ChangesBuilders . TryGetValue ( typeof ( T ) , out builder ) )
187
- {
188
- throw new LibGit2SharpException ( string . Format ( CultureInfo . InvariantCulture ,
189
- "Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'." , typeof ( T ) ,
190
- typeof ( TreeChanges ) , typeof ( Patch ) ) ) ;
191
- }
192
-
193
168
var comparer = HandleRetrieverDispatcher [ diffTargets ] ( repo ) ;
194
169
ObjectId oldTreeId = oldTree != null ? oldTree . Id : null ;
195
170
@@ -208,10 +183,10 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
208
183
}
209
184
}
210
185
211
- using ( DiffSafeHandle diff = BuildDiffList ( oldTreeId , null , comparer ,
212
- diffOptions , paths , explicitPathsOptions , compareOptions ) )
186
+ using ( var diff = new DiffSafeHandleProxy ( BuildDiffList ( oldTreeId , null , comparer ,
187
+ diffOptions , paths , explicitPathsOptions , compareOptions ) ) )
213
188
{
214
- return ( T ) builder ( diff ) ;
189
+ return new T ( ) . FromNative ( diff ) ;
215
190
}
216
191
}
217
192
@@ -233,23 +208,14 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
233
208
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
234
209
/// <returns>A <typeparamref name="T"/> containing the changes between the working directory and the index.</returns>
235
210
public virtual T Compare < T > ( IEnumerable < string > paths = null , bool includeUntracked = false , ExplicitPathsOptions explicitPathsOptions = null ,
236
- CompareOptions compareOptions = null ) where T : class
211
+ CompareOptions compareOptions = null ) where T : class , IDiffResult < T > , new ( )
237
212
{
238
213
return Compare < T > ( includeUntracked ? DiffModifiers . IncludeUntracked : DiffModifiers . None , paths , explicitPathsOptions , compareOptions ) ;
239
214
}
240
215
241
216
internal virtual T Compare < T > ( DiffModifiers diffOptions , IEnumerable < string > paths = null ,
242
- ExplicitPathsOptions explicitPathsOptions = null , CompareOptions compareOptions = null ) where T : class
217
+ ExplicitPathsOptions explicitPathsOptions = null , CompareOptions compareOptions = null ) where T : class , IDiffResult < T > , new ( )
243
218
{
244
- Func < DiffSafeHandle , object > builder ;
245
-
246
- if ( ! ChangesBuilders . TryGetValue ( typeof ( T ) , out builder ) )
247
- {
248
- throw new LibGit2SharpException ( string . Format ( CultureInfo . InvariantCulture ,
249
- "Unexpected type '{0}' passed to Compare. Supported values are either '{1}' or '{2}'." , typeof ( T ) ,
250
- typeof ( TreeChanges ) , typeof ( Patch ) ) ) ;
251
- }
252
-
253
219
var comparer = WorkdirToIndex ( repo ) ;
254
220
255
221
if ( explicitPathsOptions != null )
@@ -263,10 +229,10 @@ internal virtual T Compare<T>(DiffModifiers diffOptions, IEnumerable<string> pat
263
229
}
264
230
}
265
231
266
- using ( DiffSafeHandle diff = BuildDiffList ( null , null , comparer ,
267
- diffOptions , paths , explicitPathsOptions , compareOptions ) )
232
+ using ( var diff = new DiffSafeHandleProxy ( BuildDiffList ( null , null , comparer ,
233
+ diffOptions , paths , explicitPathsOptions , compareOptions ) ) )
268
234
{
269
- return ( T ) builder ( diff ) ;
235
+ return new T ( ) . FromNative ( diff ) ;
270
236
}
271
237
}
272
238
0 commit comments