8000 Tests and added explicit setting of added and deleted lines in patch … · wtf3505-git/libgit2sharp@37fa585 · GitHub
[go: up one dir, main page]

Skip to content

Commit 37fa585

Browse files
committed
Tests and added explicit setting of added and deleted lines in patch because linecallback isn't called in this case
1 parent 9733e74 commit 37fa585

File tree

3 files changed

+79
-22
lines changed

3 files changed

+79
-22
lines changed

LibGit2Sharp.Tests/DiffTreeToTargetFixture.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
using LibGit2Sharp.Tests.TestHelpers;
12
using System.IO;
23
using System.Linq;
34
using System.Text;
4-
using LibGit2Sharp.Tests.TestHelpers;
55
using Xunit;
66

77
namespace LibGit2Sharp.Tests
@@ -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: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
using System;
1+
using LibGit2Sharp.Tests.TestHelpers;
2+
using System;
23
using System.IO;
34
using System.Linq;
45
using System.Text;
5-
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/Patch.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
using LibGit2Sharp.Core;
2+
using LibGit2Sharp.Core.Handles;
13
using System;
24
using System.Collections;
35
using System.Collections.Generic;
46
using System.Diagnostics;
57
using System.Globalization;
68
using System.Text;
7-
using LibGit2Sharp.Core;
8-
using LibGit2Sharp.Core.Handles;
99

1010
namespace LibGit2Sharp
1111
{
@@ -70,8 +70,8 @@ private unsafe int PrintCallBack(git_diff_delta* delta, GitDiffHunk hunk, GitDif
7070

7171
string decodedContent = LaxUtf8Marshaler.FromNative(line.content, (int)line.contentLen);
7272

73-
currentChange.AddedLines = new List<Line>();
74-
currentChange.DeletedLines = new List<Line>();
73+
currentChange.AddedLines = currentChange.AddedLines ?? new List<Line>();
74+
currentChange.DeletedLines = currentChange.DeletedLines ?? new List<Line>();
7575

7676
switch (line.lineOrigin)
7777
{
@@ -175,7 +175,7 @@ public virtual string Content
175175
/// </summary>
176176
/// <param name="patch"><see cref="Patch"/>.</param>
177177
/// <returns>The patch content as string.</returns>
178-
public static implicit operator string (Patch patch)
178+
public static implicit operator string(Patch patch)
179179
{
180180
return patch.fullPatchBuilder.ToString();
181181
}

0 commit comments

Comments
 (0)
0