10000 Merge pull request #1790 from Stijn-Rutten/1789_addedLines_and_delete… · dotdevelop/libgit2sharp@dbb17e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit dbb17e7

Browse files
authored
Merge pull request libgit2#1790 from Stijn-Rutten/1789_addedLines_and_deletedLines_in_ContentChanges
added lines and deleted lines in content changes
2 parents 7fc4be5 + 34a65c3 commit dbb17e7

File tree

6 files changed

+149
-15
lines changed

6 files changed

+149
-15
lines changed

LibGit2Sharp.Tests/DiffBlobToBlobFixture.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,40 @@ public void ComparingBlobsWithNoSpacesIndentHeuristicOptionMakesNoDifference()
202202
}
203203
}
204204

205+
[Fact]
206+
public void DiffSetsTheAddedAndDeletedLinesCorrectly()
207+
{
208+
var path = SandboxStandardTestRepoGitDir();
209+
210+
using (var repo = new Repository(path))
211+
{
212+
var oldContent =
213+
@"1
214+
2
215+
3
216+
4";
217+
218+
var newContent =
219+
@"1
220+
2
221+
3
222+
5";
223+
var oldBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(Encoding.UTF8.GetBytes(oldContent)));
224+
var newBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(Encoding.UTF8.GetBytes(newContent)));
225+
226+
ContentChanges changes = repo.Diff.Compare(oldBlob, newBlob);
227+
228+
Assert.Single(changes.AddedLines);
229+
Assert.Single(changes.DeletedLines);
230+
231+
Assert.Equal("4", changes.DeletedLines.First().Content);
232+
Assert.Equal("5", changes.AddedLines.First().Content);
233+
234+
Assert.Equal(4, changes.DeletedLines.First().LineNumber);
235+
Assert.Equal(4, changes.AddedLines.First().LineNumber);
236+
}
237+
}
238+
205239
static string CanonicalChangedLines(ContentChanges changes)
206240
{
207241
// Create an ordered representation of lines that have been added or removed

LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private static void SetUpSimpleDiffContext(IRepository repo)
1717

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

20-
Commands.Stage(repo,fullpath);
20+
Commands.Stage(repo, fullpath);
2121

2222
File.AppendAllText(fullpath, "!!!\n");
2323
}
@@ -509,5 +509,25 @@ public void CanCompareANullTreeAgainstTheWorkdirAndTheIndex()
509509
}
510510
}
511511
}
512+
513+
[Fact]
514+
public void CompareSetsCorrectAddedAndDeletedLines()
515+
{
516+
string repoPath = InitNewRepository();
517+
518+
using (var repo = new Repository(repoPath))
519+
{
520+
SetUpSimpleDiffContext(repo);
521+
522+
using (var changes = repo.Diff.Compare<Patch>(repo.Head.Tip.Tree,
523+
DiffTargets.WorkingDirectory | DiffTargets.Index))
524+
{
525+
foreach (var entry in changes)
526+
{
527+
Assert.Equal(2, entry.AddedLines.Count());
528+
}
529+
}
530+
}
531+
}
512532
}
513533
}

LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Text;
55
using LibGit2Sharp.Tests.TestHelpers;
66
using Xunit;
7-
using Xunit.Extensions;
87

98
namespace LibGit2Sharp.Tests
109
{
@@ -20,7 +19,7 @@ public void ComparingATreeAgainstItselfReturnsNoDifference()
2019
{
2120
Tree tree = repo.Head.Tip.Tree;
2221

23-
using(var changes = repo.Diff.Compare<TreeChanges>(tree, tree))
22+
using (var changes = repo.Diff.Compare<TreeChanges>(tree, tree))
2423
{
2524
Assert.Empty(changes);
2625
}
@@ -112,13 +111,13 @@ public void CanDetectABinaryChange()
112111

113112
File.AppendAllText(filepath, "abcdef");
114113

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

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

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

139138
File.Delete(filepath);
140139

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

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

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

707-
Commands.Stage(repo, new[] {"a.txt", "b.txt"});
706+
Commands.Stage(repo, new[] { "a.txt", "b.txt" });
708707
Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature);
709708

710709
File.AppendAllText(Path.Combine(repo.Info.WorkingDirectory, "b.txt"), "ghi\njkl\n");
@@ -728,12 +727,12 @@ public void CanDetectTheExactRenamingExactCopyingOfNonModifiedAndModifiedFilesWh
728727
var path = Repository.Init(scd.DirectoryPath);
729728
using (var repo = new Repository(path))
730729
{
731-
const string originalPath = "original.txt";
732-
const string renamedPath = "renamed.txt";
730+
const string originalPath = "original.txt";
731+
const string renamedPath = "renamed.txt";
733732
const string originalPath2 = "original2.txt";
734-
const string copiedPath1 = "copied.txt";
733+
const string copiedPath1 = "copied.txt";
735734
const string originalPath3 = "original3.txt";
736-
const string copiedPath2 = "copied2.txt";
735+
const string copiedPath2 = "copied2.txt";
737736

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

989-
TreeEntryChanges change = changes.Single(c => c.Path== path);
988+
TreeEntryChanges change = changes.Single(c => c.Path == path);
990989
Assert.Equal(Mode.SymbolicLink, change.OldMode);
991990
Assert.Equal(Mode.NonExecutableFile, change.Mode);
992991
Assert.Equal(ChangeKind.TypeChanged, change.Status);
@@ -1087,7 +1086,7 @@ public void ComparingReliesOnProvidedConfigEntriesIfAny()
10871086
using (var repo = new Repository(path))
10881087
{
10891088
SetFilemode(repo, true);
1090-
using(var changes = repo.Diff.Compare<TreeChanges>(new[] { file }))
1089+
using (var changes = repo.Diff.Compare<TreeChanges>(new[] { file }))
10911090
{
10921091
Assert.Single(changes);
10931092

@@ -1147,6 +1146,44 @@ public void RetrievingDiffChangesMustAlwaysBeCaseSensitive()
11471146
}
11481147
}
11491148

1149+
[Fact]
1150+
public void RetrievingDiffContainsRightAmountOfAddedAndDeletedLines()
1151+
{
1152+
ObjectId treeOldOid, treeNewOid;
1153+
1154+
string repoPath = InitNewRepository();
1155+
1156+
using (var repo = new Repository(repoPath))
1157+
{
1158+
Blob oldContent = OdbHelper.CreateBlob(repo, "awesome content\n");
1159+
Blob newContent = OdbHelper.CreateBlob(repo, "more awesome content\n");
1160+
1161+
var td = new TreeDefinition()
1162+
.Add("A.TXT", oldContent, Mode.NonExecutableFile)
1163+
.Add("a.txt", oldContent, Mode.NonExecutableFile);
1164+
1165+
treeOldOid = repo.ObjectDatabase.CreateTree(td).Id;
1166+
1167+
td = new TreeDefinition()
1168+
.Add("A.TXT", newContent, Mode.NonExecutableFile)
1169+
.Add("a.txt", newContent, Mode.NonExecutableFile);
1170+
1171+
treeNewOid = repo.ObjectDatabase.CreateTree(td).Id;
1172+
}
1173+
1174+
using (var repo = new Repository(repoPath))
1175+
{
1176+
using (var changes = repo.Diff.Compare<Patch>(repo.Lookup<Tree>(treeOldOid), repo.Lookup<Tree>(treeNewOid)))
1177+
{
1178+
foreach (var entry in changes)
1179+
{
1180+
Assert.Single(entry.AddedLines);
1181+
Assert.Single(entry.DeletedLines);
1182+
}
1183+
}
1184+
}
1185+
}
1186+
11501187
[Fact]
11511188
public void UsingPatienceAlgorithmCompareOptionProducesPatienceDiff()
11521189
{

LibGit2Sharp/ContentChanges.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Globalization;
45
using System.Text;
@@ -51,6 +52,16 @@ internal void AppendToPatch(string patch)
5152
/// </summary>
5253
public virtual int LinesDeleted { get; internal set; }
5354

55+
/// <summary>
56+
/// The list of added lines.
57+
/// </summary>
58+
public virtual List<Line> AddedLines { get; } = new List<Line>();
59+
60+
/// <summary>
61+
/// The list of deleted lines.
62+
/// </summary>
63+
public virtual List<Line> DeletedLines { get; } = new List<Line>();
64+
5465
/// <summary>
5566
/// The patch corresponding to these changes.
5667
/// </summary>
@@ -95,11 +106,13 @@ private unsafe int LineCallback(git_diff_delta* delta, GitDiffHunk hunk, GitDiff
95106
switch (line.lineOrigin)
96107
{
97108
case GitDiffLineOrigin.GIT_DIFF_LINE_ADDITION:
109+
AddedLines.Add(new Line(line.NewLineNo, decodedContent));
98110
LinesAdded++;
99111
prefix = Encoding.ASCII.GetString(new[] { (byte)line.lineOrigin });
100112
break;
101113

102114
case GitDiffLineOrigin.GIT_DIFF_LINE_DELETION:
115+
DeletedLines.Add(new Line(line.OldLineNo, decodedContent));
103116
LinesDeleted++;
104117
prefix = Encoding.ASCII.GetString(new[] { (byte)line.lineOrigin });
105118
break;

LibGit2Sharp/Line.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LibGit2Sharp
6+
{
7+
/// <summary>
8+
/// Represents a line with line number and content.
9+
/// </summary>
10+
public struct Line
11+
{
12+
/// <summary>
13+
/// The line number of the original line in the blob.
14+
/// </summary>
15+
public int LineNumber { get; }
16+
17+
/// <summary>
18+
/// The content of the line in the original blob.
19+
/// </summary>
20+
public string Content { get; }
21+
22+
internal Line(int lineNumber, string content)
23+
{
24+
LineNumber = lineNumber;
25+
Content = content;
26+
}
27+
}
28+
}

LibGit2Sharp/Patch.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,14 @@ private unsafe int PrintCallBack(git_diff_delta* delta, GitDiffHunk hunk, GitDif
7777
case GitDiffLineOrigin.GIT_DIFF_LINE_ADDITION:
7878
linesAdded++;
7979
currentChange.LinesAdded++;
80+
currentChange.AddedLines.Add(new Line(line.NewLineNo, patchPart));
8081
prefix = "+";
8182
break;
8283

8384
case GitDiffLineOrigin.GIT_DIFF_LINE_DELETION:
8485
linesDeleted++;
8586
currentChange.LinesDeleted++;
87+
currentChange.DeletedLines.Add(new Line(line.OldLineNo, patchPart));
8688
prefix = "-";
8789
break;
8890
}
@@ -168,7 +170,7 @@ public virtual string Content
168170
/// </summary>
169171
/// <param name="patch"><see cref="Patch"/>.</param>
170172
/// <returns>The patch content as string.</returns>
171-
public static implicit operator string (Patch patch)
173+
public static implicit operator string(Patch patch)
172174
{
173175
return patch.fullPatchBuilder.ToString();
174176
}

0 commit comments

Comments
 (0)
0