8000 Make Diff.Compare() able to compare a null Tree against the Index and… · ben/libgit2sharp@fdc972b · GitHub
[go: up one dir, main page]

Skip to content

Commit fdc972b

Browse files
committed
Make Diff.Compare() able to compare a null Tree against the Index and Workdir
1 parent 23622fe commit fdc972b

File tree

6 files changed

+109
-11
lines changed

6 files changed

+109
-11
lines changed

LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public void CanCompareASimpleTreeAgainstTheWorkDirAndTheIndex()
9898
}
9999
}
100100

101-
102101
[Fact]
103102
/*
104103
* $ git diff
@@ -335,5 +334,65 @@ public void ComparingATreeInABareRepositoryAgainstTheWorkDirOrTheIndexThrows()
335334
() => repo.Diff.Compare(repo.Head.Tip.Tree, DiffTargets.WorkingDirectory | DiffTargets.Index));
336335
}
337336
}
337+
338+
[Fact]
339+
public void CanCompareANullTreeAgainstTheIndex()
340+
{
341+
var scd = BuildSelfCleaningDirectory();
342+
343+
using (var repo = Repository.Init(scd.RootedDirectoryPath))
344+
{
345+
SetUpSimpleDiffContext(repo);
346+
347+
TreeChanges changes = repo.Diff.Compare(null,
348+
DiffTargets.Index);
349+
350+
Assert.Equal(1, changes.Count());
351+
Assert.Equal(1, changes.Added.Count());
352+
353+
Assert.Equal("file.txt", changes.Added.Single().Path);
354+
Assert.Equal(2, changes.Added.Single().LinesAdded);
355+
}
356+
}
357+
358+
[Fact]
359+
public void CanCompareANullTreeAgainstTheWorkdir()
360+
{
361+
var scd = BuildSelfCleaningDirectory();
362+
363+
using (var repo = Repository.Init(scd.RootedDirectoryPath))
364+
{
365+
SetUpSimpleDiffContext(repo);
366+
367+
TreeChanges changes = repo.Diff.Compare(null,
368+
DiffTargets.WorkingDirectory);
369+
370+
Assert.Equal(1, changes.Count());
371+
Assert.Equal(1, changes.Added.Count());
372+
373+
Assert.Equal("file.txt", changes.Added.Single().Path);
374+
Assert.Equal(3, changes.Added.Single().LinesAdded);
375+
}
376+
}
377+
378+
[Fact]
379+
public void CanCompareANullTreeAgainstTheWorkdirAndTheIndex()
380+
{
381+
var scd = BuildSelfCleaningDirectory();
382+
383+
using (var repo = Repository.Init(scd.RootedDirectoryPath))
384+
{
385+
SetUpSimpleDiffContext(repo);
386+
387+
TreeChanges changes = repo.Diff.Compare(null,
388+
DiffTargets.WorkingDirectory | DiffTargets.Index);
389+
390+
Assert.Equal(1, changes.Count());
391+
Assert.Equal(1, changes.Added.Count());
392+
393+
Assert.Equal("file.txt", changes.Added.Single().Path);
394+
Assert.Equal(3, changes.Added.Single().LinesAdded);
395+
}
396+
}
338397
}
339398
}

LibGit2Sharp/Core/Proxy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ public static DiffListSafeHandle git_diff_index_to_tree(
540540
GitDiffOptions options)
541541
{
542542
using (ThreadAffinity())
543-
using (var osw = new ObjectSafeWrapper(oldTree, repo))
543+
using (var osw = new ObjectSafeWrapper(oldTree, repo, true))
544544
{
545545
DiffListSafeHandle diff;
546546
int res = NativeMethods.git_diff_index_to_tree(out diff, repo, osw.ObjectPtr, index, options);
@@ -612,7 +612,7 @@ public static DiffListSafeHandle git_diff_workdir_to_tree(
612612
GitDiffOptions options)
613613
{
614614
using (ThreadAffinity())
615-
using (var osw = new ObjectSafeWrapper(oldTree, repo))
615+
using (var osw = new ObjectSafeWrapper(oldTree, repo, true))
616616
{
617617
DiffListSafeHandle diff;
618618
int res = NativeMethods.git_diff_workdir_to_tree(out diff, repo, osw.ObjectPtr, options);

LibGit2Sharp/Diff.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ public class Diff
15< F438 /code>15
{
1616
private readonly Repository repo;
1717

18-
private static GitDiffOptions BuildOptions(IEnumerable<string> paths = null)
18+
private GitDiffOptions BuildOptions(DiffOptions diffOptions, IEnumerable<string> paths = null)
1919
{
2020
var options = new GitDiffOptions();
2121

22+
if (diffOptions.Has(DiffOptions.IncludeUntracked))
23+
{
24+
options.Flags |= GitDiffOptionFlags.GIT_DIFF_INCLUDE_UNTRACKED |
25+
GitDiffOptionFlags.GIT_DIFF_RECURSE_UNTRACKED_DIRS |
26+
GitDiffOptionFlags.GIT_DIFF_INCLUDE_UNTRACKED_CONTENT;
27+
}
28+
2229
if (paths == null)
2330
{
2431
return options;
@@ -70,7 +77,7 @@ internal Diff(Repository repo)
7077
/// <returns>A <see cref = "TreeChanges"/> containing the changes between the <paramref name = "oldTree"/> and the <paramref name = "newTree"/>.</returns>
7178
public virtual TreeChanges Compare(Tree oldTree, Tree newTree, IEnumerable<string> paths = null)
7279
{
73-
using(GitDiffOptions options = BuildOptions(paths))
80+
using(GitDiffOptions options = BuildOptions(DiffOptions.None, paths))
7481
using (DiffListSafeHandle diff = BuildDiffListFromTrees(
7582
oldTree != null ? oldTree.Id : null,
7683
newTree != null ? newTree.Id : null,
@@ -93,7 +100,7 @@ private DiffListSafeHandle BuildDiffListFromTrees(ObjectId oldTree, ObjectId new
93100
/// <returns>A <see cref = "ContentChanges"/> containing the changes between the <paramref name = "oldBlob"/> and the <paramref name = "newBlob"/>.</returns>
94101
public virtual ContentChanges Compare(Blob oldBlob, Blob newBlob)
95102
{
96-
using (GitDiffOptions options = BuildOptions())
103+
using (GitDiffOptions options = BuildOptions(DiffOptions.None))
97104
{
98105
return new ContentChanges(repo, oldBlob, newBlob, options);
99106
}
@@ -150,12 +157,15 @@ public virtual TreeChanges Compare(Tree oldTree, DiffTarget diffTarget, IEnumera
150157
/// <returns>A <see cref = "TreeChanges"/> containing the changes between the <see cref="Tree"/> and the selected target.</returns>
151158
public virtual TreeChanges Compare(Tree oldTree, DiffTargets diffTargets, IEnumerable<string> paths = null)
152159
{
153-
Ensure.ArgumentNotNull(oldTree, "oldTree");
154-
155160
var comparer = handleRetrieverDispatcher[diffTargets](repo);
156161

157-
using (GitDiffOptions options = BuildOptions(paths))
158-
using (DiffListSafeHandle dl = BuildDiffListFromTreeAndComparer(oldTree.Id, comparer, options))
162+
DiffOptions diffOptions = diffTargets.Has(DiffTargets.WorkingDirectory) ?
163+
DiffOptions.IncludeUntracked : DiffOptions.None;
164+
165+
using (GitDiffOptions options = BuildOptions(diffOptions, paths))
166+
using (DiffListSafeHandle dl = BuildDiffListFromTreeAndComparer(
167+
oldTree != null ? oldTree.Id : null,
168+
comparer, options))
159169
{
160170
return new TreeChanges(dl);
161171
}
@@ -170,7 +180,7 @@ public virtual TreeChanges Compare(IEnumerable<string> paths = null)
170180
{
171181
var comparer = WorkdirToIndex(repo);
172182

173-
using (GitDiffOptions options = BuildOptions(paths))
183+
using (GitDiffOptions options = BuildOptions(DiffOptions.None, paths))
174184
using (DiffListSafeHandle dl = BuildDiffListFromComparer(null, comparer, options))
175185
{
176186
return new TreeChanges(dl);

LibGit2Sharp/DiffOptions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace LibGit2Sharp
4+
{
5+
/// <summary>
6+
/// Additional behaviors the diffing should take into account
7+
/// when performing the comparison.
8+
/// </summary>
9+
[Flags]
10+
internal enum DiffOptions
11+
{
12+
/// <summary>
13+
/// No special behavior.
14+
/// </summary>
15+
None,
16+
17+
/// <summary>
18+
/// Include untracked files among the files to be processed, when
19+
/// diffing against the working directory.
20+
/// </summary>
21+
IncludeUntracked,
22+
}
23+
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
<Compile Include="Core\LazyGroup.cs" />
8282
<Compile Include="Core\Handles\GitConfigEntryHandle.cs" />
8383
<Compile Include="Core\Proxy.cs" />
84+
<Compile Include="DiffOptions.cs" />
8485
<Compile Include="DiffTargets.cs" />
8586
<Compile Include="Handlers.cs" />
8687
<Compile Include="MergeConflictException.cs" />

LibGit2Sharp/TreeChanges.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ private TreeEntryChanges AddFileChange(GitDiffDelta delta, GitDiffLineOrigin lin
9191
var newOid = new ObjectId(delta.NewFile.Oid);
9292
var oldOid = new ObjectId(delta.OldFile.Oid);
9393

94+
if (delta.Status == ChangeKind.Untracked)
95+
{
96+
delta.Status = ChangeKind.Added;
97+
}
98+
9499
var diffFile = new TreeEntryChanges(newFilePath, newMode, newOid, delta.Status, oldFilePath, oldMode, oldOid, delta.IsBinary());
95100

96101
fileDispatcher[delta.Status](this, diffFile);

0 commit comments

Comments
 (0)
0