Skip to content

Commit

Permalink
Initial change to type-safe Diff.Compare
Browse files Browse the repository at this point in the history
Fixes #1176.
  • Loading branch information
Therzok committed Aug 23, 2015
1 parent b279955 commit 16e2dcc
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 119 deletions.
11 changes: 0 additions & 11 deletions LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,17 +1123,6 @@ public void RetrievingDiffChangesMustAlwaysBeCaseSensitive()
}
}

[Fact]
public void CallingCompareWithAnUnsupportedGenericParamThrows()
{
var path = SandboxStandardTestRepoGitDir();
using (var repo = new Repository(path))
{
Assert.Throws<LibGit2SharpException>(() => repo.Diff.Compare<string>(default(Tree), default(Tree)));
Assert.Throws<LibGit2SharpException>(() => repo.Diff.Compare<string>());
}
}

[Fact]
public void UsingPatienceAlgorithmCompareOptionProducesPatienceDiff()
{
Expand Down
86 changes: 28 additions & 58 deletions LibGit2Sharp/Diff.cs

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions LibGit2Sharp/DiffSafeHandleProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using LibGit2Sharp.Core.Handles;

namespace LibGit2Sharp
{
public class DiffSafeHandleProxy : IDisposable
{
internal readonly DiffSafeHandle nativeHandle;

internal DiffSafeHandleProxy(DiffSafeHandle handle)
{
nativeHandle = handle;
}

#region IDisposable implementation

void IDisposable.Dispose()
{
nativeHandle.Dispose();
}

#endregion
}
}

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

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

2 changes: 2 additions & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@
<Compile Include="Core\GitCertificateSsh.cs" />
<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
38 changes: 21 additions & 17 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>
public class Patch : IEnumerable<PatchEntryChanges>, IDiffResult<Patch>
{
private readonly StringBuilder fullPatchBuilder = new StringBuilder();

Expand All @@ -27,24 +27,9 @@ public class Patch : IEnumerable<PatchEntryChanges>
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected Patch()
public 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 @@ -115,6 +100,25 @@ 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
56 changes: 30 additions & 26 deletions LibGit2Sharp/PatchStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,18 @@ namespace LibGit2Sharp
/// <para>The individual patches for each file can be accessed through the indexer of this class.</para>
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class PatchStats : IEnumerable<ContentChangeStats>
public class PatchStats : IEnumerable<ContentChangeStats>, IDiffResult<PatchStats>
{
private readonly IDictionary<FilePath, ContentChangeStats> changes = new Dictionary<FilePath, ContentChangeStats>();
private readonly int totalLinesAdded;
private readonly int totalLinesDeleted;
private int totalLinesAdded;
private int totalLinesDeleted;

/// <summary>
/// For mocking.
/// </summary>
protected PatchStats()
public PatchStats()
{ }

internal PatchStats(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);
var pathPtr = delta.NewFile.Path != IntPtr.Zero ? delta.NewFile.Path : delta.OldFile.Path;
var newFilePath = LaxFilePathMarshaler.FromNative(pathPtr);

var stats = Proxy.git_patch_line_stats(patch);
int added = stats.Item1;
int deleted = stats.Item2;
changes.Add(newFilePath, new ContentChangeStats(added, deleted));
totalLinesAdded += added;
totalLinesDeleted += deleted;
}

}
}

#region IEnumerable<ContentChanges> Members

/// <summary>
Expand All @@ -69,6 +47,32 @@ IEnumerator IEnumerable.GetEnumerator()

#endregion

#region IDiffResult implementation

PatchStats IDiffResult<PatchStats>.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);
var pathPtr = delta.NewFile.Path != IntPtr.Zero ? delta.NewFile.Path : delta.OldFile.Path;
var newFilePath = LaxFilePathMarshaler.FromNative(pathPtr);

var stats = Proxy.git_patch_line_stats(patch);
int added = stats.Item1;
int deleted = stats.Item2;
changes.Add(newFilePath, new ContentChangeStats(added, deleted));
totalLinesAdded += added;
totalLinesDeleted += deleted;
}
}
return this;
}

#endregion

/// <summary>
/// Gets the <see cref="ContentChangeStats"/> corresponding to the specified <paramref name="path"/>.
/// </summary>
Expand Down
18 changes: 11 additions & 7 deletions LibGit2Sharp/TreeChanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace LibGit2Sharp
/// <para>To obtain the actual patch of the diff, use the <see cref="Patch"/> class when calling Compare.</para>.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class TreeChanges : IEnumerable<TreeEntryChanges>
public class TreeChanges : IEnumerable<TreeEntryChanges>, IDiffResult<TreeChanges>
{
private readonly List<TreeEntryChanges> changes = new List<TreeEntryChanges>();
private readonly List<TreeEntryChanges> added = new List<TreeEntryChanges>();
Expand Down Expand Up @@ -47,14 +47,9 @@ public class TreeChanges : IEnumerable<TreeEntryChanges>
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected TreeChanges()
public TreeChanges()
{ }

internal TreeChanges(DiffSafeHandle diff)
{
Proxy.git_diff_foreach(diff, FileCallback, null, null);
}

private int FileCallback(GitDiffDelta delta, float progress, IntPtr payload)
{
AddFileChange(delta);
Expand Down Expand Up @@ -91,6 +86,15 @@ IEnumerator IEnumerable.GetEnumerator()

#endregion

#region IDiffResult implementation

TreeChanges IDiffResult<TreeChanges>.FromNative(DiffSafeHandleProxy diff)
{
Proxy.git_diff_foreach(diff.nativeHandle, FileCallback, null, null);
return this;
}

#endregion

/// <summary>
/// List of <see cref="TreeEntryChanges"/> that have been been added.
Expand Down

0 comments on commit 16e2dcc

Please sign in to comment.