8000 Initial change to type-safe Diff.Compare · libgit2/libgit2sharp@f07006d · GitHub
[go: up one dir, main page]

Skip to content

Commit f07006d

Browse files
committed
Initial change to type-safe Diff.Compare
Fixes #1176.
1 parent b279955 commit f07006d

File tree

8 files changed

+128
-120
lines changed

8 files changed

+128
-120
lines changed

LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,17 +1123,6 @@ public void RetrievingDiffChangesMustAlwaysBeCaseSensitive()
11231123
}
11241124
}
11251125

1126-
[Fact]
1127-
public void CallingCompareWithAnUnsupportedGenericParamThrows()
1128-
{
1129-
var path = SandboxStandardTestRepoGitDir();
1130-
using (var repo = new Repository(path))
1131-
{
1132-
Assert.Throws<LibGit2SharpException>(() => repo.Diff.Compare<string>(default(Tree), default(Tree)));
1133-
Assert.Throws<LibGit2SharpException>(() => repo.Diff.Compare<string>());
1134-
}
1135-
}
1136-
11371126
[Fact]
11381127
public void UsingPatienceAlgorithmCompareOptionProducesPatienceDiff()
11391128
{

LibGit2Sharp/Diff.cs

Lines changed: 29 additions & 59 deletions
Large diffs are not rendered by default.

LibGit2Sharp/DiffSafeHandleProxy.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using LibGit2Sharp.Core.Handles;
3+
4+
namespace LibGit2Sharp
5+
{
6+
public class DiffSafeHandleProxy : IDisposable
7+
{
8+
internal readonly DiffSafeHandle nativeHandle;
9+
10+
internal DiffSafeHandleProxy(DiffSafeHandle handle)
11+
{
12+
nativeHandle = handle;
13+
}
14+
15+
#region IDisposable implementation
16+
17+
void IDisposable.Dispose()
18+
{
19+
nativeHandle.Dispose();
20+
}
21+
22+
#endregion
23+
}
24+
}
25+

LibGit2Sharp/IDiffResult.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace LibGit2Sharp
4+
{
5+
public interface IDiffResult<T> where T:class
6+
{
7+
T FromNative(DiffSafeHandleProxy diff);
8+
}
9+
}
10+

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@
380380
<Compile Include="Core\GitCertificateSsh.cs" />
381381
<Compile Include="Core\GitCertificateSshType.cs" />
382382
<Compile Include="CertificateSsh.cs" />
383+
<Compile Include="IDiffResult.cs" />
384+
<Compile Include="DiffSafeHandleProxy.cs" />
383385
</ItemGroup>
384386
<ItemGroup>
385387
<CodeAnalysisDictionary Include="CustomDictionary.xml" />

LibGit2Sharp/Patch.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace LibGit2Sharp
1616
/// deleted, modified, ..., then consider using a simpler <see cref="TreeChanges"/>.</para>
1717
/// </summary>
1818
[DebuggerDisplay("{DebuggerDisplay,nq}")]
19-
public class Patch : IEnumerable<PatchEntryChanges>
19+
public class Patch : IEnumerable<PatchEntryChanges>, IDiffResult<Patch>
2020
{
2121
private readonly StringBuilder fullPatchBuilder = new StringBuilder();
2222

@@ -27,24 +27,9 @@ public class Patch : IEnumerable<PatchEntryChanges>
2727
/// <summary>
2828
/// Needed for mocking purposes.
2929
/// </summary>
30-
protected Patch()
30+
public Patch()
3131
{ }
3232

33-
internal Patch(DiffSafeHandle diff)
34-
{
35-
int count = Proxy.git_diff_num_deltas(diff);
36-
for (int i = 0; i < count; i++)
37-
{
38-
using (var patch = Proxy.git_patch_from_diff(diff, i))
39-
{
40-
var delta = Proxy.git_diff_get_delta(diff, i);
41-
AddFileChange(delta);
42-
Proxy.git_patch_print(patch, PrintCallBack);
43-
}
44-
45-
}
46-
}
47-
4833
private void AddFileChange(GitDiffDelta delta)
4934
{
5035
var treeEntryChanges = new TreeEntryChanges(delta);
@@ -115,6 +100,25 @@ IEnumerator IEnumerable.GetEnumerator()
115100

116101
#endregion
117102

103+
#region IDiffResult implementation
104+
105+
Patch IDiffResult<Patch>.FromNative(DiffSafeHandleProxy diff)
106+
{
107+
int count = Proxy.git_diff_num_deltas(diff.nativeHandle);
108+
for (int i = 0; i < count; i++)
109+
{
110+
using (var patch = Proxy.git_patch_from_diff(diff.nativeHandle, i))
111+
{
112+
var delta = Proxy.git_diff_get_delta(diff.nativeHandle, i);
113+
AddFileChange(delta);
114+
Proxy.git_patch_print(patch, PrintCallBack);
115+
}
116+
}
117+
return this;
118+
}
119+
120+
#endregion
121+
118122
/// <summary>
119123
/// Gets the <see cref="ContentChanges"/> corresponding to the specified <paramref name="path"/>.
120124
/// </summary>

LibGit2Sharp/PatchStats.cs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,18 @@ namespace LibGit2Sharp
1313
/// <para>The individual patches for each file can be accessed through the indexer of this class.</para>
1414
/// </summary>
1515
[DebuggerDisplay("{DebuggerDisplay,nq}")]
16-
public class PatchStats : IEnumerable<ContentChangeStats>
16+
public class PatchStats : IEnumerable<ContentChangeStats>, IDiffResult<PatchStats>
1717
{
1818
private readonly IDictionary<FilePath, ContentChangeStats> changes = new Dictionary<FilePath, ContentChangeStats>();
19-
private readonly int totalLinesAdded;
20-
private readonly int totalLinesDeleted;
19+
private int totalLinesAdded;
20+
private int totalLinesDeleted;
2121

2222
/// <summary>
2323
/// For mocking.
2424
/// </summary>
25-
protected PatchStats()
25+
public PatchStats()
2626
{ }
2727

28-
internal PatchStats(DiffSafeHandle diff)
29-
{
30-
int count = Proxy.git_diff_num_deltas(diff);
31-
for (int i = 0; i < count; i++)
32-
{
33-
using (var patch = Proxy.git_patch_from_diff(diff, i))
34-
{
35-
var delta = Proxy.git_diff_get_delta(diff, i);
36-
var pathPtr = delta.NewFile.Path != IntPtr.Zero ? delta.NewFile.Path : delta.OldFile.Path;
37-
var newFilePath = LaxFilePathMarshaler.FromNative(pathPtr);
38-
39-
var stats = Proxy.git_patch_line_stats(patch);
40-
int added = stats.Item1;
41-
int deleted = stats.Item2;
42-
changes.Add(newFilePath, new ContentChangeStats(added, deleted));
43-
totalLinesAdded += added;
44-
totalLinesDeleted += deleted;
45-
}
46-
47-
}
48-
}
49-
5028
#region IEnumerable<ContentChanges> Members
5129

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

7048
#endregion
7149

50+
#region IDiffResult implementation
51+
52+
PatchStats IDiffResult<PatchStats>.FromNative(DiffSafeHandleProxy diff)
53+
{
54+
int count = Proxy.git_diff_num_deltas(diff.nativeHandle);
55+
for (int i = 0; i < count; i++)
56+
{
57+
using (var patch = Proxy.git_patch_from_diff(diff.nativeHandle, i))
58+
{
59+
var delta = Proxy.git_diff_get_delta(diff.nativeHandle, i);
60+
var pathPtr = delta.NewFile.Path != IntPtr.Zero ? delta.NewFile.Path : delta.OldFile.Path;
61+
var newFilePath = LaxFilePathMarshaler.FromNative(pathPtr);
62+
63+
var stats = Proxy.git_patch_line_stats(patch);
64+
int added = stats.Item1;
65+
int deleted = stats.Item2;
66+
changes.Add(newFilePath, new ContentChangeStats(added, deleted));
67+
totalLinesAdded += added;
68+
totalLinesDeleted += deleted;
69+
}
70+
}
71+
return this;
72+
}
73+
74+
#endregion
75+
7276
/// <summary>
7377
/// Gets the <see cref="ContentChangeStats"/> corresponding to the specified <paramref name="path"/>.
7478
/// </summary>

LibGit2Sharp/TreeChanges.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace LibGit2Sharp
1515
/// <para>To obtain the actual patch of the diff, use the <see cref="Patch"/> class when calling Compare.</para>.
1616
/// </summary>
1717
[DebuggerDisplay("{DebuggerDisplay,nq}")]
18-
public class TreeChanges : IEnumerable<TreeEntryChanges>
18+
public class TreeChanges : IEnumerable<TreeEntryChanges>, IDiffResult<TreeChanges>
1919
{
2020
private readonly List<TreeEntryChanges> changes = new List<TreeEntryChanges>();
2121
private readonly List<TreeEntryChanges> added = new List<TreeEntryChanges>();
@@ -47,14 +47,9 @@ private static IDictionary<ChangeKind, Action<TreeChanges, TreeEntryChanges>> Bu
4747
/// <summary>
4848
/// Needed for mocking purposes.
4949
/// </summary>
50-
protected TreeChanges()
50+
public TreeChanges()
5151
{ }
5252

53-
internal TreeChanges(DiffSafeHandle diff)
54-
{
55-
Proxy.git_diff_foreach(diff, FileCallback, null, null);
56-
}
57-
5853
private int FileCallback(GitDiffDelta delta, float progress, IntPtr payload)
5954
{
6055
AddFileChange(delta);
@@ -91,6 +86,15 @@ IEnumerator IEnumerable.GetEnumerator()
9186

9287
#endregion
9388

89+
#region IDiffResult implementation
90+
91+
TreeChanges IDiffResult<TreeChanges>.FromNative(DiffSafeHandleProxy diff)
92+
{
93+
Proxy.git_diff_foreach(diff.nativeHandle, FileCallback, null, null);
94+
return this;
95+
}
96+
97+
#endregion
9498

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

0 commit comments

Comments
 (0)
0