Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tests and added explicit setting of added and deleted lines in patch …
…because linecallback isn't called in this case
  • Loading branch information
Stijn-Rutten committed May 27, 2020
commit 37fa58584a2495a46f461f363a4ff22d824f9471
24 changes: 22 additions & 2 deletions LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using LibGit2Sharp.Tests.TestHelpers;
using System.IO;
using System.Linq;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

namespace LibGit2Sharp.Tests
Expand All @@ -17,7 +17,7 @@ private static void SetUpSimpleDiffContext(IRepository repo)

File.AppendAllText(fullpath, "world\n");

Commands.Stage(repo,fullpath);
Commands.Stage(repo, fullpath);

File.AppendAllText(fullpath, "!!!\n");
}
Expand Down Expand Up @@ -509,5 +509,25 @@ public void CanCompareANullTreeAgainstTheWorkdirAndTheIndex()
}
}
}

[Fact]
public void CompareSetsCorrectAddedAndDeletedLines()
{
string repoPath = InitNewRepository();

using (var repo = new Repository(repoPath))
{
SetUpSimpleDiffContext(repo);

using (var changes = repo.Diff.Compare<Patch>(repo.Head.Tip.Tree,
DiffTargets.WorkingDirectory | DiffTargets.Index))
{
foreach (var entry in changes)
{
Assert.Equal(2, entry.AddedLines.Count());
}
}
}
}
}
}
67 changes: 52 additions & 15 deletions LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using LibGit2Sharp.Tests.TestHelpers;
using System;
using System.IO;
using System.Linq;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;

namespace LibGit2Sharp.Tests
{
Expand All @@ -20,7 +19,7 @@ public void ComparingATreeAgainstItselfReturnsNoDifference()
{
Tree tree = repo.Head.Tip.Tree;

using(var changes = repo.Diff.Compare<TreeChanges>(tree, tree))
using (var changes = repo.Diff.Compare<TreeChanges>(tree, tree))
{
Assert.Empty(changes);
}
Expand Down Expand Up @@ -112,13 +111,13 @@ public void CanDetectABinaryChange()

File.AppendAllText(filepath, "abcdef");

using(var patch = repo.Diff.Compare<Patch>(commit.Tree, DiffTargets.WorkingDirectory, new[] { filename }))
using (var patch = repo.Diff.Compare<Patch>(commit.Tree, DiffTargets.WorkingDirectory, new[] { filename }))
Assert.True(patch[filename].IsBinaryComparison);

Commands.Stage(repo, filename);
var commit2 = repo.Commit("Update binary file", Constants.Signature, Constants.Signature);

using(var patch2 = repo.Diff.Compare<Patch>(commit.Tree, commit2.Tree, new[] { filename }))
using (var patch2 = repo.Diff.Compare<Patch>(commit.Tree, commit2.Tree, new[] { filename }))
Assert.True(patch2[filename].IsBinaryComparison);
}
}
Expand All @@ -138,13 +137,13 @@ public void CanDetectABinaryDeletion()

File.Delete(filepath);

using(var patch = repo.Diff.Compare<Patch>(commit.Tree, DiffTargets.WorkingDirectory, new [] {filename}))
using (var patch = repo.Diff.Compare<Patch>(commit.Tree, DiffTargets.WorkingDirectory, new[] { filename }))
Assert.True(patch[filename].IsBinaryComparison);

Commands.Remove(repo, filename);
var commit2 = repo.Commit("Delete binary file", Constants.Signature, Constants.Signature);

using(var patch2 = repo.Diff.Compare<Patch>(commit.Tree, commit2.Tree, new[] { filename }))
using (var patch2 = repo.Diff.Compare<Patch>(commit.Tree, commit2.Tree, new[] { filename }))
Assert.True(patch2[filename].IsBinaryComparison);
}
}
Expand Down Expand Up @@ -704,7 +703,7 @@ public void CanIncludeUnmodifiedEntriesWhenEnabled()
Touch(repo.Info.WorkingDirectory, "a.txt", "abc\ndef\n");
Touch(repo.Info.WorkingDirectory, "b.txt", "abc\ndef\n");

Commands.Stage(repo, new[] {"a.txt", "b.txt"});
Commands.Stage(repo, new[] { "a.txt", "b.txt" });
Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature);

File.AppendAllText(Path.Combine(repo.Info.WorkingDirectory, "b.txt"), "ghi\njkl\n");
Expand All @@ -728,12 +727,12 @@ public void CanDetectTheExactRenamingExactCopyingOfNonModifiedAndModifiedFilesWh
var path = Repository.Init(scd.DirectoryPath);
using (var repo = new Repository(path))
{
const string originalPath = "original.txt";
const string renamedPath = "renamed.txt";
const string originalPath = "original.txt";
const string renamedPath = "renamed.txt";
const string originalPath2 = "original2.txt";
const string copiedPath1 = "copied.txt";
const string copiedPath1 = "copied.txt";
const string originalPath3 = "original3.txt";
const string copiedPath2 = "copied2.txt";
const string copiedPath2 = "copied2.txt";

Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\n");
Touch(repo.Info.WorkingDirectory, originalPath2, "1\n2\n3\n4\n");
Expand Down Expand Up @@ -986,7 +985,7 @@ public void CanHandleTwoTreeEntryChangesWithTheSamePathUsingSimilarityNone()
Assert.Single(changes.Deleted);
Assert.Single(changes.TypeChanged);

TreeEntryChanges change = changes.Single(c => c.Path== path);
TreeEntryChanges change = changes.Single(c => c.Path == path);
Assert.Equal(Mode.SymbolicLink, change.OldMode);
Assert.Equal(Mode.NonExecutableFile, change.Mode);
Assert.Equal(ChangeKind.TypeChanged, change.Status);
Expand Down Expand Up @@ -1087,7 +1086,7 @@ public void ComparingReliesOnProvidedConfigEntriesIfAny()
using (var repo = new Repository(path))
{
SetFilemode(repo, true);
using(var changes = repo.Diff.Compare<TreeChanges>(new[] { file }))
using (var changes = repo.Diff.Compare<TreeChanges>(new[] { file }))
{
Assert.Single(changes);

Expand Down Expand Up @@ -1147,6 +1146,44 @@ public void RetrievingDiffChangesMustAlwaysBeCaseSensitive()
}
}

[Fact]
public void RetrievingDiffContainsRightAmountOfAddedAndDeletedLines()
{
ObjectId treeOldOid, treeNewOid;

string repoPath = InitNewRepository();

using (var repo = new Repository(repoPath))
{
Blob oldContent = OdbHelper.CreateBlob(repo, "awesome content\n");
Blob newContent = OdbHelper.CreateBlob(repo, "more awesome content\n");

var td = new TreeDefinition()
.Add("A.TXT", oldContent, Mode.NonExecutableFile)
.Add("a.txt", oldContent, Mode.NonExecutableFile);

treeOldOid = repo.ObjectDatabase.CreateTree(td).Id;

td = new TreeDefinition()
.Add("A.TXT", newContent, Mode.NonExecutableFile)
.Add("a.txt", newContent, Mode.NonExecutableFile);

treeNewOid = repo.ObjectDatabase.CreateTree(td).Id;
}

using (var repo = new Repository(repoPath))
{
using (var changes = repo.Diff.Compare<Patch>(repo.Lookup<Tree>(treeOldOid), repo.Lookup<Tree>(treeNewOid)))
{
foreach (var entry in changes)
{
Assert.Single(entry.AddedLines);
Assert.Single(entry.DeletedLines);
}
}
}
}

[Fact]
public void UsingPatienceAlgorithmCompareOptionProducesPatienceDiff()
{
Expand Down
10 changes: 5 additions & 5 deletions LibGit2Sharp/Patch.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;

namespace LibGit2Sharp
{
Expand Down Expand Up @@ -70,8 +70,8 @@ private unsafe int PrintCallBack(git_diff_delta* delta, GitDiffHunk hunk, GitDif

string decodedContent = LaxUtf8Marshaler.FromNative(line.content, (int)line.contentLen);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this when this appears to be the same as the patchPart declaration on line 59?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should able to be removed. You can just use the existing patchPart instead.


currentChange.AddedLines = new List<Line>();
currentChange.DeletedLines = new List<Line>();
currentChange.AddedLines = currentChange.AddedLines ?? new List<Line>();
currentChange.DeletedLines = currentChange.DeletedLines ?? new List<Line>();

switch (line.lineOrigin)
{
Expand Down Expand Up @@ -175,7 +175,7 @@ public virtual string Content
/// </summary>
/// <param name="patch"><see cref="Patch"/>.</param>
/// <returns>The patch content as string.</returns>
public static implicit operator string (Patch patch)
public static implicit operator string(Patch patch)
{
return patch.fullPatchBuilder.ToString();
}
Expand Down