|
| 1 | +using System.Linq; |
| 2 | +using System.Text; |
| 3 | +using LibGit2Sharp.Tests.TestHelpers; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace LibGit2Sharp.Tests |
| 7 | +{ |
| 8 | + public class DiffFixture : BaseFixture |
| 9 | + { |
| 10 | + //TODO Test binary files (do we have hunks/line callbacks) |
| 11 | + //TODO What does content contain when dealing with a Binary file? |
| 12 | + //TODO When does it make sense to expose the Binary property? |
| 13 | + //TODO The PrintCallBack lacks some context (GitDiffDelta) |
| 14 | + |
| 15 | + [Fact] |
| 16 | + public void ComparingATreeAgainstItselfReturnsNoDifference() |
| 17 | + { |
| 18 | + using (var repo = new Repository(StandardTestRepoPath)) |
| 19 | + { |
| 20 | + Tree tree = repo.Head.Tip.Tree; |
| 21 | + |
| 22 | + TreeChanges changes = repo.Diff.Compare(tree, tree); |
| 23 | + |
| 24 | + Assert.Empty(changes); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void RetrievingANonExistentFileChangeReturnsNull() |
| 30 | + { |
| 31 | + using (var repo = new Repository(StandardTestRepoPath)) |
| 32 | + { |
| 33 | + Tree tree = repo.Head.Tip.Tree; |
| 34 | + |
| 35 | + TreeChanges changes = repo.Diff.Compare(tree, tree); |
| 36 | + |
| 37 | + Assert.Null(changes["batman"]); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /* |
| 42 | + * $ git diff --stat HEAD^..HEAD |
| 43 | + * 1.txt | 1 + |
| 44 | + * 1 file changed, 1 insertion(+) |
| 45 | + */ |
| 46 | + [Fact] |
| 47 | + public void CanCompareACommitTreeAgainstItsParent() |
| 48 | + { |
| 49 | + using (var repo = new Repository(StandardTestRepoPath)) |
| 50 | + { |
| 51 | + Tree commitTree = repo.Head.Tip.Tree; |
| 52 | + Tree parentCommitTree = repo.Head.Tip.Parents.Single().Tree; |
| 53 | + |
| 54 | + TreeChanges changes = repo.Diff.Compare(parentCommitTree, commitTree); |
| 55 | + |
| 56 | + Assert.Equal(1, changes.Count()); |
| 57 | + Assert.Equal(1, changes.Added.Count()); |
| 58 | + |
| 59 | + TreeEntryChanges treeEntryChanges = changes["1.txt"]; |
| 60 | + |
| 61 | + Assert.Equal("1.txt", treeEntryChanges.Path); |
| 62 | + Assert.Equal(ChangeKind.Added, treeEntryChanges.Status); |
| 63 | + // Also in Added collection |
| 64 | + Assert.Equal(treeEntryChanges, changes.Added.Single()); |
| 65 | + Assert.Equal(1, treeEntryChanges.LinesAdded); |
| 66 | + |
| 67 | + Assert.Equal(Mode.Nonexistent, treeEntryChanges.OldMode); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /* |
| 72 | + * $ git diff --stat origin/test..HEAD |
| 73 | + * 1.txt | 1 + |
| 74 | + * 1/branch_file.txt | 1 + |
| 75 | + * README | 1 + |
| 76 | + * branch_file.txt | 1 + |
| 77 | + * deleted_staged_file.txt | 1 + |
| 78 | + * deleted_unstaged_file.txt | 1 + |
| 79 | + * modified_staged_file.txt | 1 + |
| 80 | + * modified_unstaged_file.txt | 1 + |
| 81 | + * new.txt | 1 + |
| 82 | + * readme.txt | 2 -- |
| 83 | + * 10 files changed, 9 insertions(+), 2 deletions(-) |
| 84 | + */ |
| 85 | + [Fact] |
| 86 | + public void CanCompareACommitTreeAgainstATreeWithNoCommonAncestor() |
| 87 | + { |
| 88 | + using (var repo = new Repository(StandardTestRepoPath)) |
| 89 | + { |
| 90 | + Tree commitTree = repo.Head.Tip.Tree; |
| 91 | + Tree commitTreeWithDifferentAncestor = repo.Branches["refs/remotes/origin/test"].Tip.Tree; |
| 92 | + |
| 93 | + TreeChanges changes = repo.Diff.Compare(commitTreeWithDifferentAncestor, commitTree); |
| 94 | + |
| 95 | + Assert.Equal(10, changes.Count()); |
| 96 | + Assert.Equal(9, changes.Added.Count()); |
| 97 | + Assert.Equal(1, changes.Deleted.Count()); |
| 98 | + |
| 99 | + Assert.Equal("readme.txt", changes.Deleted.Single().Path); |
| 100 | + Assert.Equal(new[] { "1.txt", "1/branch_file.txt", "README", "branch_file.txt", "deleted_staged_file.txt", "deleted_unstaged_file.txt", "modified_staged_file.txt", "modified_unstaged_file.txt", "new.txt" }, |
| 101 | + changes.Added.Select(x => x.Path)); |
| 102 | + |
| 103 | + Assert.Equal(9, changes.LinesAdded); |
| 104 | + Assert.Equal(2, changes.LinesDeleted); |
| 105 | + Assert.Equal(2, changes["readme.txt"].LinesDeleted); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /* |
| 110 | + * $ git diff -M f8d44d7..4be51d6 |
| 111 | + * diff --git a/my-name-does-not-feel-right.txt b/super-file.txt |
| 112 | + * similarity index 82% |
| 113 | + * rename from my-name-does-not-feel-right.txt |
| 114 | + * rename to super-file.txt |
| 115 | + * index e8953ab..16bdf1d 100644 |
| 116 | + * --- a/my-name-does-not-feel-right.txt |
| 117 | + * +++ b/super-file.txt |
| 118 | + * @@ -2,3 +2,4 @@ That's a terrible name! |
| 119 | + * I don't like it. |
| 120 | + * People look down at me and laugh. :-( |
| 121 | + * Really!!!! |
| 122 | + * +Yeah! Better! |
| 123 | + * |
| 124 | + * $ git diff -M --shortstat f8d44d7..4be51d6 |
| 125 | + * 1 file changed, 1 insertion(+) |
| 126 | + */ |
| 127 | + [Fact(Skip = "Not implemented in libgit2 yet.")] |
| 128 | + public void CanDetectTheRenamingOfAModifiedFile() |
| 129 | + { |
| 130 | + using (var repo = new Repository(StandardTestRepoPath)) |
| 131 | + { |
| 132 | + Tree rootCommitTree = repo.Lookup<Commit>("f8d44d7").Tree; |
| 133 | + Tree commitTreeWithRenamedFile = repo.Lookup<Commit>("4be51d6").Tree; |
| 134 | + |
| 135 | + TreeChanges changes = repo.Diff.Compare(rootCommitTree, commitTreeWithRenamedFile); |
| 136 | + |
| 137 | + Assert.Equal(1, changes.Count()); |
| 138 | + Assert.Equal("super-file.txt", changes["super-file.txt"].Path); |
| 139 | + Assert.Equal("my-name-does-not-feel-right.txt", changes["super-file.txt"].OldPath); |
| 140 | + //Assert.Equal(1, changes.FilesRenamed.Count()); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /* |
| 145 | + * $ git diff f8d44d7..ec9e401 |
| 146 | + * diff --git a/numbers.txt b/numbers.txt |
| 147 | + * index 7909961..4625a36 100644 |
| 148 | + * --- a/numbers.txt |
| 149 | + * +++ b/numbers.txt |
| 150 | + * @@ -8,8 +8,9 @@ |
| 151 | + * 8 |
| 152 | + * 9 |
| 153 | + * 10 |
| 154 | + * -12 |
| 155 | + * +11 |
| 156 | + * 12 |
| 157 | + * 13 |
| 158 | + * 14 |
| 159 | + * 15 |
| 160 | + * +16 |
| 161 | + * \ No newline at end of file |
| 162 | + * |
| 163 | + * $ git diff --shortstat f8d44d7..ec9e401 |
| 164 | + * 1 file changed, 2 insertions(+), 1 deletion(-) |
| 165 | + */ |
| 166 | + [Fact] |
| 167 | + public void CanCompareTwoVersionsOfAFileWithATrailingNewlineDeletion() |
| 168 | + { |
| 169 | + using (var repo = new Repository(StandardTestRepoPath)) |
| 170 | + { |
| 171 | + Tree rootCommitTree = repo.Lookup<Commit>("f8d44d7").Tree; |
| 172 | + Tree commitTreeWithUpdatedFile = repo.Lookup<Commit>("ec9e401").Tree; |
| 173 | + |
| 174 | + TreeChanges changes = repo.Diff.Compare(rootCommitTree, commitTreeWithUpdatedFile); |
| 175 | + |
| 176 | + Assert.Equal(1, changes.Count()); |
| 177 | + Assert.Equal(1, changes.Modified.Count()); |
| 178 | + |
| 179 | + TreeEntryChanges treeEntryChanges = changes.Modified.Single(); |
| 180 | + |
| 181 | + Assert.Equal(2, treeEntryChanges.LinesAdded); |
| 182 | + Assert.Equal(1, treeEntryChanges.LinesDeleted); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + /* |
| 187 | + * $ git diff --inter-hunk-context=2 f8d44d7..7252fe2 |
| 188 | + * diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt |
| 189 | + * deleted file mode 100644 |
| 190 | + * index e8953ab..0000000 |
| 191 | + * --- a/my-name-does-not-feel-right.txt |
| 192 | + * +++ /dev/null |
| 193 | + * @@ -1,4 +0,0 @@ |
| 194 | + * -That's a terrible name! |
| 195 | + * -I don't like it. |
| 196 | + * -People look down at me and laugh. :-( |
| 197 | + * -Really!!!! |
| 198 | + * diff --git a/numbers.txt b/numbers.txt |
| 199 | + * index 7909961..4e935b7 100644 |
| 200 | + * --- a/numbers.txt |
| 201 | + * +++ b/numbers.txt |
| 202 | + * @@ -1,4 +1,5 @@ |
| 203 | + * 1 |
| 204 | + * +2 |
| 205 | + * 3 |
| 206 | + * 4 |
| 207 | + * 5 |
| 208 | + * @@ -8,8 +9,9 @@ |
| 209 | + * 8 |
| 210 | + * 9 |
| 211 | + * 10 |
| 212 | + * -12 |
| 213 | + * +11 |
| 214 | + * 12 |
| 215 | + * 13 |
| 216 | + * 14 |
| 217 | + * 15 |
| 218 | + * +16 |
| 219 | + * diff --git a/super-file.txt b/super-file.txt |
| 220 | + * new file mode 100644 |
| 221 | + * index 0000000..16bdf1d |
| 222 | + * --- /dev/null |
| 223 | + * +++ b/super-file.txt |
| 224 | + * @@ -0,0 +1,5 @@ |
| 225 | + * +That's a terrible name! |
| 226 | + * +I don't like it. |
| 227 | + * +People look down at me and laugh. :-( |
| 228 | + * +Really!!!! |
| 229 | + * +Yeah! Better! |
| 230 | + * |
| 231 | + * $ git diff --stat f8d44d7..7252fe2 |
| 232 | + * my-name-does-not-feel-right.txt | 4 ---- |
| 233 | + * numbers.txt | 4 +++- |
| 234 | + * super-file.txt | 5 +++++ |
| 235 | + * 3 files changed, 8 insertions(+), 5 deletions(-) |
| 236 | + */ |
| 237 | + [Fact] |
| 238 | + public void CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks() |
| 239 | + { |
| 240 | + using (var repo = new Repository(StandardTestRepoPath)) |
| 241 | + { |
| 242 | + Tree rootCommitTree = repo.Lookup<Commit>("f8d44d7").Tree; |
| 243 | + Tree mergedCommitTree = repo.Lookup<Commit>("7252fe2").Tree; |
| 244 | + |
| 245 | + TreeChanges changes = repo.Diff.Compare(rootCommitTree, mergedCommitTree); |
| 246 | + |
| 247 | + Assert.Equal(3, changes.Count()); |
| 248 | + Assert.Equal(1, changes.Modified.Count()); |
| 249 | + Assert.Equal(1, changes.Deleted.Count()); |
| 250 | + Assert.Equal(1, changes.Added.Count()); |
| 251 | + |
| 252 | + TreeEntryChanges treeEntryChanges = changes["numbers.txt"]; |
| 253 | + |
| 254 | + Assert.Equal(3, treeEntryChanges.LinesAdded); |
| 255 | + Assert.Equal(1, treeEntryChanges.LinesDeleted); |
| 256 | + |
| 257 | + Assert.Equal(Mode.Nonexistent, changes["my-name-does-not-feel-right.txt"].Mode); |
| 258 | + |
| 259 | + var expected = new StringBuilder() |
| 260 | + .Append("diff --git a/numbers.txt b/numbers.txt\n") |
| 261 | + .Append("index 7909961..4e935b7 100644\n") |
| 262 | + .Append("--- a/numbers.txt\n") |
| 263 | + .Append("+++ b/numbers.txt\n") |
| 264 | + .Append("@@ -1,4 +1,5 @@\n") |
| 265 | + .Append(" 1\n") |
| 266 | + .Append("+2\n") |
| 267 | + .Append(" 3\n") |
| 268 | + .Append(" 4
10000
\n") |
| 269 | + .Append(" 5\n") |
| 270 | + .Append("@@ -8,8 +9,9 @@\n") |
| 271 | + .Append(" 8\n") |
| 272 | + .Append(" 9\n") |
| 273 | + .Append(" 10\n") |
| 274 | + .Append("-12\n") |
| 275 | + .Append("+11\n") |
| 276 | + .Append(" 12\n") |
| 277 | + .Append(" 13\n") |
| 278 | + .Append(" 14\n") |
| 279 | + .Append(" 15\n") |
| 280 | + .Append("+16\n"); |
| 281 | + |
| 282 | + Assert.Equal(expected.ToString(), treeEntryChanges.Patch); |
| 283 | + |
| 284 | + expected = new StringBuilder() |
| 285 | + .Append("diff --git a/my-name-does-not-feel-right.txt b/my-name-does-not-feel-right.txt\n") |
| 286 | + .Append("deleted file mode 100644\n") |
| 287 | + .Append("index e8953ab..0000000\n") |
| 288 | + .Append("--- a/my-name-does-not-feel-right.txt\n") |
| 289 | + .Append("+++ /dev/null\n") |
| 290 | + .Append("@@ -1,4 +0,0 @@\n") |
| 291 | + .Append("-That's a terrible name!\n") |
| 292 | + .Append("-I don't like it.\n") |
| 293 | + .Append("-People look down at me and laugh. :-(\n") |
| 294 | + .Append("-Really!!!!\n") |
| 295 | + .Append("diff --git a/numbers.txt b/numbers.txt\n") |
| 296 | + .Append("index 7909961..4e935b7 100644\n") |
| 297 | + .Append("--- a/numbers.txt\n") |
| 298 | + .Append("+++ b/numbers.txt\n") |
| 299 | + .Append("@@ -1,4 +1,5 @@\n") |
| 300 | + .Append(" 1\n") |
| 301 | + .Append("+2\n") |
| 302 | + .Append(" 3\n") |
| 303 | + .Append(" 4\n") |
| 304 | + .Append(" 5\n") |
| 305 | + .Append("@@ -8,8 +9,9 @@\n") |
| 306 | + .Append(" 8\n") |
| 307 | + .Append(" 9\n") |
| 308 | + .Append(" 10\n") |
| 309 | + .Append(<
10000
span class=pl-s>"-12\n") |
| 310 | + .Append("+11\n") |
| 311 | + .Append(" 12\n") |
| 312 | + .Append(" 13\n") |
| 313 | + .Append(" 14\n") |
| 314 | + .Append(" 15\n") |
| 315 | + .Append("+16\n") |
| 316 | + .Append("diff --git a/super-file.txt b/super-file.txt\n") |
| 317 | + .Append("new file mode 100644\n") |
| 318 | + .Append("index 0000000..16bdf1d\n") |
| 319 | + .Append("--- /dev/null\n") |
| 320 | + .Append("+++ b/super-file.txt\n") |
| 321 | + .Append("@@ -0,0 +1,5 @@\n") |
| 322 | + .Append("+That's a terrible name!\n") |
| 323 | + .Append("+I don't like it.\n") |
| 324 | + .Append("+People look down at me and laugh. :-(\n") |
| 325 | + .Append("+Really!!!!\n") |
| 326 | + .Append("+Yeah! Better!\n"); |
| 327 | + |
| 328 | + // TODO: uncomment the line below when https://github.com/libgit2/libgit2/pull/643 is merged into development branch. |
| 329 | + //Assert.Equal(expected.ToString(), changes.Patch); |
| 330 | + } |
| 331 | + } |
| 332 | + } |
| 333 | +} |
0 commit comments