forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDiffBlobToBlobFixture.cs
211 lines (176 loc) · 6.76 KB
/
DiffBlobToBlobFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System.IO;
using System.Linq;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
namespace LibGit2Sharp.Tests
{
public class DiffBlobToBlobFixture : BaseFixture
{
[Fact]
public void ComparingABlobAgainstItselfReturnsNoDifference()
{
var path = SandboxStandardTestRepoGitDir();
using (var repo = new Repository(path))
{
var blob = repo.Lookup<Blob>("7909961");
ContentChanges changes = repo.Diff.Compare(blob, blob);
Assert.Equal(0, changes.LinesAdded);
Assert.Equal(0, changes.LinesDeleted);
Assert.Equal(string.Empty, changes.Patch);
}
}
[Fact]
public void CanCompareTwoVersionsOfABlobWithADiffOfTwoHunks()
{
var path = SandboxStandardTestRepoGitDir();
using (var repo = new Repository(path))
{
var oldblob = repo.Lookup<Blob>("7909961");
var newblob = repo.Lookup<Blob>("4e935b7");
ContentChanges changes = repo.Diff.Compare(oldblob, newblob);
Assert.False(changes.IsBinaryComparison);
Assert.Equal(3, changes.LinesAdded);
Assert.Equal(1, changes.LinesDeleted);
var expected = new StringBuilder()
.Append("@@ -1,4 +1,5 @@\n")
.Append(" 1\n")
.Append("+2\n")
.Append(" 3\n")
.Append(" 4\n")
.Append(" 5\n")
.Append("@@ -8,8 +9,9 @@\n")
.Append(" 8\n")
.Append(" 9\n")
.Append(" 10\n")
.Append("-12\n")
.Append("+11\n")
.Append(" 12\n")
.Append(" 13\n")
.Append(" 14\n")
.Append(" 15\n")
.Append("+16\n");
Assert.Equal(expected.ToString(), changes.Patch);
}
}
Blob CreateBinaryBlob(IRepository repo)
{
string fullpath = Path.Combine(repo.Info.WorkingDirectory, "binary.bin");
File.WriteAllBytes(fullpath, new byte[] { 17, 16, 0, 4, 65 });
return repo.ObjectDatabase.CreateBlob(fullpath);
}
[Fact]
public void CanCompareATextualBlobAgainstABinaryBlob()
{
string path = SandboxStandardTestRepo();
using (var repo = new Repository(path))
{
Blob binBlob = CreateBinaryBlob(repo);
var blob = repo.Lookup<Blob>("7909961");
ContentChanges changes = repo.Diff.Compare(blob, binBlob);
Assert.True(changes.IsBinaryComparison);
Assert.Equal(0, changes.LinesAdded);
Assert.Equal(0, changes.LinesDeleted);
}
}
[Fact]
public void CanCompareABlobAgainstANullBlob()
{
var path = SandboxStandardTestRepoGitDir();
using (var repo = new Repository(path))
{
var blob = repo.Lookup<Blob>("7909961");
ContentChanges changes = repo.Diff.Compare(null, blob);
Assert.NotEqual(0, changes.LinesAdded);
Assert.Equal(0, changes.LinesDeleted);
Assert.NotEqual(string.Empty, changes.Patch);
changes = repo.Diff.Compare(blob, null);
Assert.Equal(0, changes.LinesAdded);
Assert.NotEqual(0, changes.LinesDeleted);
Assert.NotEqual(string.Empty, changes.Patch);
}
}
[Fact]
public void ComparingTwoNullBlobsReturnsAnEmptyContentChanges()
{
var path = SandboxStandardTestRepoGitDir();
using (var repo = new Repository(path))
{
ContentChanges changes = repo.Diff.Compare((Blob)null, (Blob)null);
Assert.False(changes.IsBinaryComparison);
Assert.Equal(0, changes.LinesAdded);
Assert.Equal(0, changes.LinesDeleted);
}
}
[Fact]
public void ComparingBlobsWithNoSpacesAndIndentHeuristicOptionMakesADifference()
{
var path = SandboxStandardTestRepoGitDir();
using (var repo = new Repository(path))
{
// Based on test diff indent heuristic from:
// https://github.com/git/git/blob/433860f3d0beb0c6f205290bd16cda413148f098/t/t4061-diff-indent.sh#L17
var oldContent =
@" 1
2
a
b
3
4";
var newContent =
@" 1
2
a
b
a
b
3
4";
var oldBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(Encoding.UTF8.GetBytes(oldContent)));
var newBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(Encoding.UTF8.GetBytes(newContent)));
var noIndentHeuristicOption = new CompareOptions { IndentHeuristic = false };
var indentHeuristicOption = new CompareOptions { IndentHeuristic = true };
ContentChanges changes0 = repo.Diff.Compare(oldBlob, newBlob, noIndentHeuristicOption);
ContentChanges changes1 = repo.Diff.Compare(oldBlob, newBlob, indentHeuristicOption);
Assert.NotEqual(changes0.Patch, changes1.Patch);
Assert.Equal(CanonicalChangedLines(changes0), CanonicalChangedLines(changes1));
}
}
[Fact]
public void ComparingBlobsWithNoSpacesIndentHeuristicOptionMakesNoDifference()
{
var path = SandboxStandardTestRepoGitDir();
using (var repo = new Repository(path))
{
var oldContent =
@" 1
2
a
b
3
4";
var newContent =
@" 1
2
a
b
a
b
3
4";
var oldBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(Encoding.UTF8.GetBytes(oldContent)));
var newBlob = repo.ObjectDatabase.CreateBlob(new MemoryStream(Encoding.UTF8.GetBytes(newContent)));
var noIndentHeuristicOption = new CompareOptions { IndentHeuristic = false };
var indentHeuristicOption = new CompareOptions { IndentHeuristic = true };
ContentChanges changes0 = repo.Diff.Compare(oldBlob, newBlob, noIndentHeuristicOption);
ContentChanges changes1 = repo.Diff.Compare(oldBlob, newBlob, indentHeuristicOption);
Assert.Equal(changes0.Patch, changes1.Patch);
}
}
static string CanonicalChangedLines(ContentChanges changes)
{
// Create an ordered representation of lines that have been added or removed
return string.Join("\n", changes.Patch.Split('\n').Where(l => l.StartsWith("+") || l.StartsWith("-")).OrderBy(l => l));
}
}
}