From 79e11b960d3010aa8e8541cf6d2406b0d6c563a9 Mon Sep 17 00:00:00 2001 From: Stijn Rutten Date: Thu, 30 Apr 2020 09:50:36 +0200 Subject: [PATCH 01/10] Added Line struct and lists for addedlines and deletedlines in content changes --- LibGit2Sharp/ContentChanges.cs | 6 ++++++ LibGit2Sharp/Core/Line.cs | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 LibGit2Sharp/Core/Line.cs diff --git a/LibGit2Sharp/ContentChanges.cs b/LibGit2Sharp/ContentChanges.cs index 221c99efa..c92f0a984 100644 --- a/LibGit2Sharp/ContentChanges.cs +++ b/LibGit2Sharp/ContentChanges.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Text; @@ -51,6 +52,11 @@ internal void AppendToPatch(string patch) /// public virtual int LinesDeleted { get; internal set; } + public IEnumerable AddedLines { get; internal set; } + + public IEnumerable DeletedLines { get; internal set; } + + /// /// The patch corresponding to these changes. /// diff --git a/LibGit2Sharp/Core/Line.cs b/LibGit2Sharp/Core/Line.cs new file mode 100644 index 000000000..4a75289bf --- /dev/null +++ b/LibGit2Sharp/Core/Line.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace LibGit2Sharp.Core +{ + internal struct Line + { + /// + /// Points to the number of the original line in the blob + /// + public int LineNumber { get; } + + /// + /// This content of the line in the original blob + /// + public String Content { get; } + + internal Line(int lineNumber, string content) + { + LineNumber = lineNumber; + Content = content; + } + } +} From 92b9e20edcb35da430ee698b20d3d12ce9ab87be Mon Sep 17 00:00:00 2001 From: Stijn Rutten Date: Thu, 30 Apr 2020 10:30:47 +0200 Subject: [PATCH 02/10] Added Line struct and lists for addedlines and deletedlines in content changes --- LibGit2Sharp/ContentChanges.cs | 9 +++++++-- LibGit2Sharp/Core/Line.cs | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/LibGit2Sharp/ContentChanges.cs b/LibGit2Sharp/ContentChanges.cs index c92f0a984..223fd6375 100644 --- a/LibGit2Sharp/ContentChanges.cs +++ b/LibGit2Sharp/ContentChanges.cs @@ -23,6 +23,9 @@ protected ContentChanges() internal unsafe ContentChanges(Repository repo, Blob oldBlob, Blob newBlob, GitDiffOptions options) { + AddedLines = new List(); + DeletedLines = new List(); + Proxy.git_diff_blobs(repo.Handle, oldBlob != null ? oldBlob.Id : null, newBlob != null ? newBlob.Id : null, @@ -52,9 +55,9 @@ internal void AppendToPatch(string patch) /// public virtual int LinesDeleted { get; internal set; } - public IEnumerable AddedLines { get; internal set; } + public List AddedLines { get; internal set; } - public IEnumerable DeletedLines { get; internal set; } + public List DeletedLines { get; internal set; } /// @@ -101,11 +104,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; diff --git a/LibGit2Sharp/Core/Line.cs b/LibGit2Sharp/Core/Line.cs index 4a75289bf..caa50c17c 100644 --- a/LibGit2Sharp/Core/Line.cs +++ b/LibGit2Sharp/Core/Line.cs @@ -4,7 +4,7 @@ namespace LibGit2Sharp.Core { - internal struct Line + public struct Line { /// /// Points to the number of the original line in the blob From 445bbbe2e317d2cb5aaa2648ca4de5fd69b81ff0 Mon Sep 17 00:00:00 2001 From: Stijn Rutten Date: Thu, 30 Apr 2020 10:57:05 +0200 Subject: [PATCH 03/10] Fixed for tests --- LibGit2Sharp/ContentChanges.cs | 10 ++++++++-- LibGit2Sharp/{Core => }/Line.cs | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) rename LibGit2Sharp/{Core => }/Line.cs (95%) diff --git a/LibGit2Sharp/ContentChanges.cs b/LibGit2Sharp/ContentChanges.cs index 223fd6375..f88a2d493 100644 --- a/LibGit2Sharp/ContentChanges.cs +++ b/LibGit2Sharp/ContentChanges.cs @@ -55,9 +55,15 @@ internal void AppendToPatch(string patch) /// public virtual int LinesDeleted { get; internal set; } - public List AddedLines { get; internal set; } + /// + /// Lis of all lines added. + /// + public virtual List AddedLines { get; internal set; } - public List DeletedLines { get; internal set; } + /// + /// List of all lines deleted. + /// + public virtual List DeletedLines { get; internal set; } /// diff --git a/LibGit2Sharp/Core/Line.cs b/LibGit2Sharp/Line.cs similarity index 95% rename from LibGit2Sharp/Core/Line.cs rename to LibGit2Sharp/Line.cs index caa50c17c..f62abdd99 100644 --- a/LibGit2Sharp/Core/Line.cs +++ b/LibGit2Sharp/Line.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace LibGit2Sharp.Core +namespace LibGit2Sharp { public struct Line { From 70b6bd74d7bf6d403e050a192c01e8b4ab5c7d1f Mon Sep 17 00:00:00 2001 From: Stijn Rutten Date: Thu, 30 Apr 2020 11:01:19 +0200 Subject: [PATCH 04/10] Added missing documentatiion of Line --- LibGit2Sharp/Line.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/LibGit2Sharp/Line.cs b/LibGit2Sharp/Line.cs index f62abdd99..f201aeaff 100644 --- a/LibGit2Sharp/Line.cs +++ b/LibGit2Sharp/Line.cs @@ -4,6 +4,9 @@ namespace LibGit2Sharp { + /// + /// Represents a Line with line number and content. + /// public struct Line { /// From 3c777dc40211f6208edb3d2f3d06ea47ccd455c7 Mon Sep 17 00:00:00 2001 From: Stijn Rutten Date: Thu, 30 Apr 2020 14:03:16 +0200 Subject: [PATCH 05/10] Set deleted and added lines in patch printcallback --- LibGit2Sharp/Patch.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/LibGit2Sharp/Patch.cs b/LibGit2Sharp/Patch.cs index 2cd4d1605..d41741216 100644 --- a/LibGit2Sharp/Patch.cs +++ b/LibGit2Sharp/Patch.cs @@ -68,6 +68,11 @@ private unsafe int PrintCallBack(git_diff_delta* delta, GitDiffHunk hunk, GitDif PatchEntryChanges currentChange = this[filePath]; string prefix = string.Empty; + string decodedContent = LaxUtf8Marshaler.FromNative(line.content, (int)line.contentLen); + + currentChange.AddedLines = new List(); + currentChange.DeletedLines = new List(); + switch (line.lineOrigin) { case GitDiffLineOrigin.GIT_DIFF_LINE_CONTEXT: @@ -77,12 +82,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, decodedContent)); prefix = "+"; break; case GitDiffLineOrigin.GIT_DIFF_LINE_DELETION: linesDeleted++; currentChange.LinesDeleted++; + currentChange.DeletedLines.Add(new Line(line.OldLineNo, decodedContent)); prefix = "-"; break; } From 9733e7442577c725b422cd2f2fbc6f6986df3f0c Mon Sep 17 00:00:00 2001 From: Stijn Rutten Date: Wed, 27 May 2020 20:35:23 +0200 Subject: [PATCH 06/10] Added test for blobToBlob compare --- LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs | 32 +++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs b/LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs index fea0bbb74..d710750e7 100644 --- a/LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs +++ b/LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs @@ -1,7 +1,7 @@ -using System.IO; +using LibGit2Sharp.Tests.TestHelpers; +using System.IO; using System.Linq; using System.Text; -using LibGit2Sharp.Tests.TestHelpers; using Xunit; namespace LibGit2Sharp.Tests @@ -202,6 +202,34 @@ 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); + } + } + static string CanonicalChangedLines(ContentChanges changes) { // Create an ordered representation of lines that have been added or removed From 37fa58584a2495a46f461f363a4ff22d824f9471 Mon Sep 17 00:00:00 2001 From: Stijn Rutten Date: Wed, 27 May 2020 20:48:07 +0200 Subject: [PATCH 07/10] Tests and added explicit setting of added and deleted lines in patch because linecallback isn't called in this case --- LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs | 24 ++++++- LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs | 67 ++++++++++++++----- LibGit2Sharp/Patch.cs | 10 +-- 3 files changed, 79 insertions(+), 22 deletions(-) diff --git a/LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs b/LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs index 2fb359f24..6218dc858 100644 --- a/LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs +++ b/LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs @@ -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 @@ -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"); } @@ -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(repo.Head.Tip.Tree, + DiffTargets.WorkingDirectory | DiffTargets.Index)) + { + foreach (var entry in changes) + { + Assert.Equal(2, entry.AddedLines.Count()); + } + } + } + } } } diff --git a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs index dba762bfe..9137ab138 100644 --- a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs +++ b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs @@ -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 { @@ -20,7 +19,7 @@ public void ComparingATreeAgainstItselfReturnsNoDifference() { Tree tree = repo.Head.Tip.Tree; - using(var changes = repo.Diff.Compare(tree, tree)) + using (var changes = repo.Diff.Compare(tree, tree)) { Assert.Empty(changes); } @@ -112,13 +111,13 @@ public void CanDetectABinaryChange() File.AppendAllText(filepath, "abcdef"); - using(var patch = repo.Diff.Compare(commit.Tree, DiffTargets.WorkingDirectory, new[] { filename })) + using (var patch = repo.Diff.Compare(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(commit.Tree, commit2.Tree, new[] { filename })) + using (var patch2 = repo.Diff.Compare(commit.Tree, commit2.Tree, new[] { filename })) Assert.True(patch2[filename].IsBinaryComparison); } } @@ -138,13 +137,13 @@ public void CanDetectABinaryDeletion() File.Delete(filepath); - using(var patch = repo.Diff.Compare(commit.Tree, DiffTargets.WorkingDirectory, new [] {filename})) + using (var patch = repo.Diff.Compare(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(commit.Tree, commit2.Tree, new[] { filename })) + using (var patch2 = repo.Diff.Compare(commit.Tree, commit2.Tree, new[] { filename })) Assert.True(patch2[filename].IsBinaryComparison); } } @@ -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"); @@ -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"); @@ -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); @@ -1087,7 +1086,7 @@ public void ComparingReliesOnProvidedConfigEntriesIfAny() using (var repo = new Repository(path)) { SetFilemode(repo, true); - using(var changes = repo.Diff.Compare(new[] { file })) + using (var changes = repo.Diff.Compare(new[] { file })) { Assert.Single(changes); @@ -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(repo.Lookup(treeOldOid), repo.Lookup(treeNewOid))) + { + foreach (var entry in changes) + { + Assert.Single(entry.AddedLines); + Assert.Single(entry.DeletedLines); + } + } + } + } + [Fact] public void UsingPatienceAlgorithmCompareOptionProducesPatienceDiff() { diff --git a/LibGit2Sharp/Patch.cs b/LibGit2Sharp/Patch.cs index d41741216..b38349efc 100644 --- a/LibGit2Sharp/Patch.cs +++ b/LibGit2Sharp/Patch.cs @@ -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 { @@ -70,8 +70,8 @@ private unsafe int PrintCallBack(git_diff_delta* delta, GitDiffHunk hunk, GitDif string decodedContent = LaxUtf8Marshaler.FromNative(line.content, (int)line.contentLen); - currentChange.AddedLines = new List(); - currentChange.DeletedLines = new List(); + currentChange.AddedLines = currentChange.AddedLines ?? new List(); + currentChange.DeletedLines = currentChange.DeletedLines ?? new List(); switch (line.lineOrigin) { @@ -175,7 +175,7 @@ public virtual string Content /// /// . /// The patch content as string. - public static implicit operator string (Patch patch) + public static implicit operator string(Patch patch) { return patch.fullPatchBuilder.ToString(); } From 81965391c95d99e2556737ddd9ca59329648ceb8 Mon Sep 17 00:00:00 2001 From: Stijn Rutten Date: Fri, 29 May 2020 14:24:26 +0200 Subject: [PATCH 08/10] Update LibGit2Sharp/Line.cs Co-authored-by: Brandon Ording --- LibGit2Sharp/Line.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LibGit2Sharp/Line.cs b/LibGit2Sharp/Line.cs index f201aeaff..5131259d9 100644 --- a/LibGit2Sharp/Line.cs +++ b/LibGit2Sharp/Line.cs @@ -10,7 +10,7 @@ namespace LibGit2Sharp public struct Line { /// - /// Points to the number of the original line in the blob + /// The line number of the original line in the blob. /// public int LineNumber { get; } From a9d5c3614c4d499627d28fb611fc8d55ef76a8d2 Mon Sep 17 00:00:00 2001 From: Stijn Rutten Date: Fri, 29 May 2020 14:25:13 +0200 Subject: [PATCH 09/10] Apply suggestions from code review Co-authored-by: Brandon Ording --- LibGit2Sharp/ContentChanges.cs | 4 ++-- LibGit2Sharp/Line.cs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/LibGit2Sharp/ContentChanges.cs b/LibGit2Sharp/ContentChanges.cs index f88a2d493..93a30582c 100644 --- a/LibGit2Sharp/ContentChanges.cs +++ b/LibGit2Sharp/ContentChanges.cs @@ -56,12 +56,12 @@ internal void AppendToPatch(string patch) public virtual int LinesDeleted { get; internal set; } /// - /// Lis of all lines added. + /// The list of added lines. /// public virtual List AddedLines { get; internal set; } /// - /// List of all lines deleted. + /// The list of deleted lines. /// public virtual List DeletedLines { get; internal set; } diff --git a/LibGit2Sharp/Line.cs b/LibGit2Sharp/Line.cs index 5131259d9..830247fc3 100644 --- a/LibGit2Sharp/Line.cs +++ b/LibGit2Sharp/Line.cs @@ -5,7 +5,7 @@ namespace LibGit2Sharp { /// - /// Represents a Line with line number and content. + /// Represents a line with line number and content. /// public struct Line { @@ -15,9 +15,9 @@ public struct Line public int LineNumber { get; } /// - /// This content of the line in the original blob + /// The content of the line in the original blob. /// - public String Content { get; } + public string Content { get; } internal Line(int lineNumber, string content) { From c5bd90a3ec68a8fda7ee6e533feb4da0afa2f624 Mon Sep 17 00:00:00 2001 From: Stijn Rutten Date: Thu, 11 Jun 2020 11:10:20 +0200 Subject: [PATCH 10/10] Apply suggestions from code review --- LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs | 10 ++++++++-- LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs | 2 +- LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs | 4 ++-- LibGit2Sharp/ContentChanges.cs | 8 ++------ LibGit2Sharp/Patch.cs | 13 ++++--------- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs b/LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs index d710750e7..046fe5214 100644 --- a/LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs +++ b/LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs @@ -1,7 +1,7 @@ -using LibGit2Sharp.Tests.TestHelpers; -using System.IO; +using System.IO; using System.Linq; using System.Text; +using LibGit2Sharp.Tests.TestHelpers; using Xunit; namespace LibGit2Sharp.Tests @@ -227,6 +227,12 @@ public void DiffSetsTheAddedAndDeletedLinesCorrectly() 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); } } diff --git a/LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs b/LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs index 6218dc858..b712a214b 100644 --- a/LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs +++ b/LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs @@ -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 diff --git a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs index 9137ab138..d03b26e4e 100644 --- a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs +++ b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs @@ -1,8 +1,8 @@ -using LibGit2Sharp.Tests.TestHelpers; -using System; +using System; using System.IO; using System.Linq; using System.Text; +using LibGit2Sharp.Tests.TestHelpers; using Xunit; namespace LibGit2Sharp.Tests diff --git a/LibGit2Sharp/ContentChanges.cs b/LibGit2Sharp/ContentChanges.cs index f88a2d493..25e824b82 100644 --- a/LibGit2Sharp/ContentChanges.cs +++ b/LibGit2Sharp/ContentChanges.cs @@ -23,9 +23,6 @@ protected ContentChanges() internal unsafe ContentChanges(Repository repo, Blob oldBlob, Blob newBlob, GitDiffOptions options) { - AddedLines = new List(); - DeletedLines = new List(); - Proxy.git_diff_blobs(repo.Handle, oldBlob != null ? oldBlob.Id : null, newBlob != null ? newBlob.Id : null, @@ -58,13 +55,12 @@ internal void AppendToPatch(string patch) /// /// Lis of all lines added. /// - public virtual List AddedLines { get; internal set; } + public virtual List AddedLines { get; } = new List(); /// /// List of all lines deleted. /// - public virtual List DeletedLines { get; internal set; } - + public virtual List DeletedLines { get; } = new List(); /// /// The patch corresponding to these changes. diff --git a/LibGit2Sharp/Patch.cs b/LibGit2Sharp/Patch.cs index b38349efc..50157eb32 100644 --- a/LibGit2Sharp/Patch.cs +++ b/LibGit2Sharp/Patch.cs @@ -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 { @@ -68,11 +68,6 @@ private unsafe int PrintCallBack(git_diff_delta* delta, GitDiffHunk hunk, GitDif PatchEntryChanges currentChange = this[filePath]; string prefix = string.Empty; - string decodedContent = LaxUtf8Marshaler.FromNative(line.content, (int)line.contentLen); - - currentChange.AddedLines = currentChange.AddedLines ?? new List(); - currentChange.DeletedLines = currentChange.DeletedLines ?? new List(); - switch (line.lineOrigin) { case GitDiffLineOrigin.GIT_DIFF_LINE_CONTEXT: @@ -82,14 +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, decodedContent)); + 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, decodedContent)); + currentChange.DeletedLines.Add(new Line(line.OldLineNo, patchPart)); prefix = "-"; break; }