Skip to content

Commit

Permalink
Merge pull request #1790 from Stijn-Rutten/1789_addedLines_and_delete…
Browse files Browse the repository at this point in the history
…dLines_in_ContentChanges

added lines and deleted lines in content changes
  • Loading branch information
bording committed Nov 7, 2020
2 parents 7fc4be5 + 34a65c3 commit dbb17e7
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 15 deletions.
34 changes: 34 additions & 0 deletions LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,40 @@ public void ComparingBlobsWithNoSpacesIndentHeuristicOptionMakesNoDifference()
}
}

[Fact]
public void DiffSetsTheAddedAndDeletedLinesCorrectly()
{
var path = SandboxStandardTestRepoGitDir();

using (var repo = new Repository(path))
{
var oldContent =
@"1
2
3
4";

var newContent =
@"1
2
3
5";
var oldBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(Encoding.UTF8.GetBytes(oldContent)));
var newBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(Encoding.UTF8.GetBytes(newContent)));

ContentChanges changes = repo.Diff.Compare(oldBlob, newBlob);

Assert.Single(changes.AddedLines);
Assert.Single(changes.DeletedLines);

Assert.Equal("4", changes.DeletedLines.First().Content);
Assert.Equal("5", changes.AddedLines.First().Content);

Assert.Equal(4, changes.DeletedLines.First().LineNumber);
Assert.Equal(4, changes.AddedLines.First().LineNumber);
}
}

static string CanonicalChangedLines(ContentChanges changes)
{
// Create an ordered representation of lines that have been added or removed
Expand Down
22 changes: 21 additions & 1 deletion LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
}
}
}
63 changes: 50 additions & 13 deletions LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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
13 changes: 13 additions & 0 deletions LibGit2Sharp/ContentChanges.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
Expand Down Expand Up @@ -51,6 +52,16 @@ internal void AppendToPatch(string patch)
/// </summary>
public virtual int LinesDeleted { get; internal set; }

/// <summary>
/// The list of added lines.
/// </summary>
public virtual List<Line> AddedLines { get; } = new List<Line>();

/// <summary>
/// The list of deleted lines.
/// </summary>
public virtual List<Line> DeletedLines { get; } = new List<Line>();

/// <summary>
/// The patch corresponding to these changes.
/// </summary>
Expand Down Expand Up @@ -95,11 +106,13 @@ private unsafe int LineCallback(git_diff_delta* delta, GitDiffHunk hunk, GitDiff
switch (line.lineOrigin)
{
case GitDiffLineOrigin.GIT_DIFF_LINE_ADDITION:
AddedLines.Add(new Line(line.NewLineNo, decodedContent));
LinesAdded++;
prefix = Encoding.ASCII.GetString(new[] { (byte)line.lineOrigin });
break;

case GitDiffLineOrigin.GIT_DIFF_LINE_DELETION:
DeletedLines.Add(new Line(line.OldLineNo, decodedContent));
LinesDeleted++;
prefix = Encoding.ASCII.GetString(new[] { (byte)line.lineOrigin });
break;
Expand Down
28 changes: 28 additions & 0 deletions LibGit2Sharp/Line.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace LibGit2Sharp
{
/// <summary>
/// Represents a line with line number and content.
/// </summary>
public struct Line
{
/// <summary>
/// The line number of the original line in the blob.
/// </summary>
public int LineNumber { get; }

/// <summary>
/// The content of the line in the original blob.
/// </summary>
public string Content { get; }

internal Line(int lineNumber, string content)
{
LineNumber = lineNumber;
Content = content;
}
}
}
4 changes: 3 additions & 1 deletion LibGit2Sharp/Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ private unsafe int PrintCallBack(git_diff_delta* delta, GitDiffHunk hunk, GitDif
case GitDiffLineOrigin.GIT_DIFF_LINE_ADDITION:
linesAdded++;
currentChange.LinesAdded++;
currentChange.AddedLines.Add(new Line(line.NewLineNo, patchPart));
prefix = "+";
break;

case GitDiffLineOrigin.GIT_DIFF_LINE_DELETION:
linesDeleted++;
currentChange.LinesDeleted++;
currentChange.DeletedLines.Add(new Line(line.OldLineNo, patchPart));
prefix = "-";
break;
}
Expand Down Expand Up @@ -168,7 +170,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

0 comments on commit dbb17e7

Please sign in to comment.