Skip to content

Commit

Permalink
Add specific explicit interfaces to not have to modify the test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Therzok committed Aug 24, 2015
1 parent 95e0982 commit 5242d4d
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 126 deletions.
16 changes: 15 additions & 1 deletion LibGit2Sharp.Tests/MetaFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MetaFixture
{
private static readonly HashSet<Type> explicitOnlyInterfaces = new HashSet<Type>
{
typeof(IBelongToARepository), typeof(IDiffResult<>),
typeof(IBelongToARepository), typeof(IDiffResult),
};

[Fact]
Expand Down Expand Up @@ -401,6 +401,20 @@ where method.IsDefined(typeof(ExtensionAttribute), false)
select method;
return query;
}

[Fact]
public void AllIDiffResultsAreInChangesBuilder()
{
var diff = typeof(Diff).GetField("ChangesBuilders", BindingFlags.NonPublic | BindingFlags.Static);
var changesBuilders = (System.Collections.IDictionary)diff.GetValue(null);

IEnumerable<Type> diffResults = typeof(Diff).Assembly.GetExportedTypes()
.Where(type => type.GetInterface("IDiffResult") != null);

var nonBuilderTypes = diffResults.Where(diffResult => !changesBuilders.Contains(diffResult));
Assert.False(nonBuilderTypes.Any(), "Classes which implement IDiffResult but are not registered under ChangesBuilders in Diff:" + Environment.NewLine +
string.Join(Environment.NewLine, nonBuilderTypes.Select(type => type.FullName)));
}
}

internal static class TypeExtensions
Expand Down
71 changes: 42 additions & 29 deletions LibGit2Sharp/Diff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ internal Diff(Repository repo)
};
}

private static readonly IDictionary<Type, Func<DiffSafeHandle, object>> ChangesBuilders = new Dictionary<Type, Func<DiffSafeHandle, object>>
{
{ typeof(Patch), diff => new Patch(diff) },
{ typeof(TreeChanges), diff => new TreeChanges(diff) },
{ typeof(PatchStats), diff => new PatchStats(diff) },
};


private static T BuildDiffResult<T>(DiffSafeHandle diff) where T : class, IDiffResult
{
Func<DiffSafeHandle, object> builder;

if (!ChangesBuilders.TryGetValue(typeof(T), out builder))
{
throw new LibGit2SharpException(CultureInfo.InvariantCulture,
"User-defined types passed to Compare are not supported. Supported values are: {1}",
typeof(T),
string.Join(", ", ChangesBuilders.Keys.Select(x => x.Name)));
}

return (T)builder(diff);
}

/// <summary>
/// Show changes between two <see cref="Blob"/>s.
/// </summary>
Expand Down Expand Up @@ -127,7 +150,7 @@ public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob, CompareOptions
/// <param name="oldTree">The <see cref="Tree"/> you want to compare from.</param>
/// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
public virtual T Compare<T>(Tree oldTree, Tree newTree) where T : class, IDiffResult<T>, new()
public virtual T Compare<T>(Tree oldTree, Tree newTree) where T : class, IDiffResult
{
return Compare<T>(oldTree, newTree, null, null, null);
}
Expand All @@ -139,7 +162,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree) where T : class, IDiffRe
/// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
/// <param name="paths">The list of paths (either files or directories) that should be compared.</param>
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> paths) where T : class, IDiffResult<T>, new()
public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> paths) where T : class, IDiffResult
{
return Compare<T>(oldTree, newTree, paths, null, null);
}
Expand All @@ -156,7 +179,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
/// </param>
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> paths,
ExplicitPathsOptions explicitPathsOptions) where T : class, IDiffResult<T>, new()
ExplicitPathsOptions explicitPathsOptions) where T : class, IDiffResult
{
return Compare<T>(oldTree, newTree, paths, explicitPathsOptions, null);
}
Expand All @@ -169,7 +192,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
/// <param name="paths">The list of paths (either files or directories) that should be compared.</param>
/// <param name="compareOptions">Additional options to define patch generation behavior.</param>
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> paths, CompareOptions compareOptions) where T : class, IDiffResult<T>, new()
public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> paths, CompareOptions compareOptions) where T : class, IDiffResult
{
return Compare<T>(oldTree, newTree, paths, null, compareOptions);
}
Expand All @@ -181,7 +204,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> path
/// <param name="newTree">The <see cref="Tree"/> you want to compare to.</param>
/// <param name="compareOptions">Additional options to define patch generation behavior.</param>
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
public virtual T Compare<T>(Tree oldTree, Tree newTree, CompareOptions compareOptions) where T : class, IDiffResult<T>, new()
public virtual T Compare<T>(Tree oldTree, Tree newTree, CompareOptions compareOptions) where T : class, IDiffResult
{
return Compare<T>(oldTree, newTree, null, null, compareOptions);
}
Expand All @@ -199,9 +222,8 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, CompareOptions compareOp
/// <param name="compareOptions">Additional options to define patch generation behavior.</param>
/// <returns>A <see cref="TreeChanges"/> containing the changes between the <paramref name="oldTree"/> and the <paramref name="newTree"/>.</returns>
public virtual T Compare<T>(Tree oldTree, Tree newTree, IEnumerable<string> paths, ExplicitPathsOptions explicitPathsOptions,
CompareOptions compareOptions) where T : class, IDiffResult<T>, new()
CompareOptions compareOptions) where T : class, IDiffResult
{

var comparer = TreeToTree(repo);
ObjectId oldTreeId = oldTree != null ? oldTree.Id : null;
ObjectId newTreeId = newTree != null ? newTree.Id : null;
Expand All @@ -219,10 +241,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, CompareOptions compareOp

using (DiffSafeHandle diff = BuildDiffList(oldTreeId, newTreeId, comparer, diffOptions, paths, explicitPathsOptions, compareOptions))
{
using (var proxy = new DiffSafeHandleProxy(diff))
{
return new T().FromNative(proxy);
}
return BuildDiffResult<T>(diff);
}
}

Expand All @@ -238,7 +257,7 @@ public virtual T Compare<T>(Tree oldTree, Tree newTree, CompareOptions compareOp
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets) where T : class, IDiffResult<T>, new()
public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets) where T : class, IDiffResult
{
return Compare<T>(oldTree, diffTargets, null, null, null);
}
Expand All @@ -256,7 +275,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets) where T : cla
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<string> paths) where T : class, IDiffResult<T>, new()
public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<string> paths) where T : class, IDiffResult
{
return Compare<T>(oldTree, diffTargets, paths, null, null);
}
Expand All @@ -279,7 +298,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<string> paths,
ExplicitPathsOptions explicitPathsOptions) where T : class, IDiffResult<T>, new()
ExplicitPathsOptions explicitPathsOptions) where T : class, IDiffResult
{
return Compare<T>(oldTree, diffTargets, paths, explicitPathsOptions, null);
}
Expand All @@ -303,7 +322,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
/// <returns>A <typeparamref name="T"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<string> paths,
ExplicitPathsOptions explicitPathsOptions, CompareOptions compareOptions) where T : class, IDiffResult<T>, new()
ExplicitPathsOptions explicitPathsOptions, CompareOptions compareOptions) where T : class, IDiffResult
{
var comparer = HandleRetrieverDispatcher[diffTargets](repo);
ObjectId oldTreeId = oldTree != null ? oldTree.Id : null;
Expand All @@ -324,10 +343,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s

using (DiffSafeHandle diff = BuildDiffList(oldTreeId, null, comparer, diffOptions, paths, explicitPathsOptions, compareOptions))
{
using (var proxy = new DiffSafeHandleProxy(diff))
{
return new T().FromNative(proxy);
}
return BuildDiffResult<T>(diff);
}
}

Expand All @@ -341,7 +357,7 @@ public virtual T Compare<T>(Tree oldTree, DiffTargets diffTargets, IEnumerable<s
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
/// <returns>A <typeparamref name="T"/> containing the changes between the working directory and the index.</returns>
public virtual T Compare<T>() where T : class, IDiffResult<T>, new()
public virtual T Compare<T>() where T : class, IDiffResult
{
return Compare<T>(DiffModifiers.None);
}
Expand All @@ -357,7 +373,7 @@ public virtual T Compare<T>() where T : class, IDiffResult<T>, new()
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
/// <returns>A <typeparamref name="T"/> containing the changes between the working directory and the index.</returns>
public virtual T Compare<T>(IEnumerable<string> paths) where T : class, IDiffResult<T>, new()
public virtual T Compare<T>(IEnumerable<string> paths) where T : class, IDiffResult
{
return Compare<T>(DiffModifiers.None, paths);
}
Expand All @@ -374,7 +390,7 @@ public virtual T Compare<T>(IEnumerable<string> paths) where T : class, IDiffRes
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
/// <returns>A <typeparamref name="T"/> containing the changes between the working directory and the index.</returns>
public virtual T Compare<T>(IEnumerable<string> paths, bool includeUntracked) where T : class, IDiffResult<T>, new()
public virtual T Compare<T>(IEnumerable<string> paths, bool includeUntracked) where T : class, IDiffResult
{
return Compare<T>(includeUntracked ? DiffModifiers.IncludeUntracked : DiffModifiers.None, paths);
}
Expand All @@ -395,7 +411,7 @@ public virtual T Compare<T>(IEnumerable<string> paths, bool includeUntracked) wh
/// <typeparam name="T">Can be either a <see cref="TreeChanges"/> if you are only interested in the list of files modified, added, ..., or
/// a <see cref="Patch"/> if you want the actual patch content for the whole diff and for individual files.</typeparam>
/// <returns>A <typeparamref name="T"/> containing the changes between the working directory and the index.</returns>
public virtual T Compare<T>(IEnumerable<string> paths, bool includeUntracked, ExplicitPathsOptions explicitPathsOptions) where T : class, IDiffResult<T>, new()
public virtual T Compare<T>(IEnumerable<string> paths, bool includeUntracked, ExplicitPathsOptions explicitPathsOptions) where T : class, IDiffResult
{
return Compare<T>(includeUntracked ? DiffModifiers.IncludeUntracked : DiffModifiers.None, paths, explicitPathsOptions);
}
Expand All @@ -421,7 +437,7 @@ public virtual T Compare<T>(IEnumerable<string> paths, bool includeUntracked, Ex
IEnumerable<string> paths,
bool includeUntracked,
ExplicitPathsOptions explicitPathsOptions,
CompareOptions compareOptions) where T : class, IDiffResult<T>, new()
CompareOptions compareOptions) where T : class, IDiffResult
{
return Compare<T>(includeUntracked ? DiffModifiers.IncludeUntracked : DiffModifiers.None, paths, explicitPathsOptions, compareOptions);
}
Expand All @@ -430,7 +446,7 @@ public virtual T Compare<T>(IEnumerable<string> paths, bool includeUntracked, Ex
DiffModifiers diffOptions,
IEnumerable<string> paths = null,
ExplicitPathsOptions explicitPathsOptions = null,
CompareOptions compareOptions = null) where T : class, IDiffResult<T>, new()
CompareOptions compareOptions = null) where T : class, IDiffResult
{
var comparer = WorkdirToIndex(repo);

Expand All @@ -446,10 +462,7 @@ public virtual T Compare<T>(IEnumerable<string> paths, bool includeUntracked, Ex

using (DiffSafeHandle diff = BuildDiffList(null, null, comparer, diffOptions, paths, explicitPathsOptions, compareOptions))
{
using (var proxy = new DiffSafeHandleProxy(diff))
{
return new T().FromNative(proxy);
}
return BuildDiffResult<T>(diff);
}
}

Expand Down
25 changes: 0 additions & 25 deletions LibGit2Sharp/DiffSafeHandleProxy.cs

This file was deleted.

10 changes: 3 additions & 7 deletions LibGit2Sharp/IDiffResult.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;

namespace LibGit2Sharp
namespace LibGit2Sharp
{
public interface IDiffResult<T> where T:class
{
T FromNative(DiffSafeHandleProxy diff);
}
public interface IDiffResult
{ }
}

1 change: 0 additions & 1 deletion LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@
<Compile Include="Core\GitCertificateSshType.cs" />
<Compile Include="CertificateSsh.cs" />
<Compile Include="IDiffResult.cs" />
<Compile Include="DiffSafeHandleProxy.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="CustomDictionary.xml" />
Expand Down
37 changes: 16 additions & 21 deletions LibGit2Sharp/Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace LibGit2Sharp
/// deleted, modified, ..., then consider using a simpler <see cref="TreeChanges"/>.</para>
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Patch : IEnumerable<PatchEntryChanges>, IDiffResult<Patch>
public class Patch : IEnumerable<PatchEntryChanges>, IDiffResult
{
private readonly StringBuilder fullPatchBuilder = new StringBuilder();

Expand All @@ -27,9 +27,23 @@ public class Patch : IEnumerable<PatchEntryChanges>, IDiffResult<Patch>
/// <summary>
/// Needed for mocking purposes.
/// </summary>
public Patch()
protected Patch()
{ }

internal Patch(DiffSafeHandle diff)
{
int count = Proxy.git_diff_num_deltas(diff);
for (int i = 0; i < count; i++)
{
using (var patch = Proxy.git_patch_from_diff(diff, i))
{
var delta = Proxy.git_diff_get_delta(diff, i);
AddFileChange(delta);
Proxy.git_patch_print(patch, PrintCallBack);
}
}
}

private void AddFileChange(GitDiffDelta delta)
{
var treeEntryChanges = new TreeEntryChanges(delta);
Expand Down Expand Up @@ -100,25 +114,6 @@ IEnumerator IEnumerable.GetEnumerator()

#endregion

#region IDiffResult implementation

Patch IDiffResult<Patch>.FromNative(DiffSafeHandleProxy diff)
{
int count = Proxy.git_diff_num_deltas(diff.nativeHandle);
for (int i = 0; i < count; i++)
{
using (var patch = Proxy.git_patch_from_diff(diff.nativeHandle, i))
{
var delta = Proxy.git_diff_get_delta(diff.nativeHandle, i);
AddFileChange(delta);
Proxy.git_patch_print(patch, PrintCallBack);
}
}
return this;
}

#endregion

/// <summary>
/// Gets the <see cref="ContentChanges"/> corresponding to the specified <paramref name="path"/>.
/// </summary>
Expand Down

0 comments on commit 5242d4d

Please sign in to comment.