|
1 |
| -using System.Linq; |
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
2 | 5 | using LibGit2Sharp.Tests.TestHelpers;
|
3 | 6 | using Xunit;
|
4 | 7 | using Xunit.Extensions;
|
@@ -341,6 +344,122 @@ public void CanNonFastForwardMergeCommit(bool fromDetachedHead, FastForwardStrat
|
341 | 344 | }
|
342 | 345 | }
|
343 | 346 |
|
| 347 | + [Fact] |
| 348 | + public void MergeReportsCheckoutProgress() |
| 349 | + { |
| 350 | + string repoPath = CloneMergeTestRepo(); |
| 351 | + using (var repo = new Repository(repoPath)) |
| 352 | + { |
| 353 | + Commit commitToMerge = repo.Branches["normal_merge"].Tip; |
| 354 | + |
| 355 | + bool wasCalled = false; |
| 356 | + |
| 357 | + MergeOptions options = new MergeOptions() |
| 358 | + { |
| 359 | + OnCheckoutProgress = (path, completed, total) => wasCalled = true, |
| 360 | + }; |
| 361 | + |
| 362 | + MergeResult result = repo.Merge(commitToMerge, Constants.Signature, options); |
| 363 | + |
| 364 | + Assert.True(wasCalled); |
| 365 | + } |
| 366 | + } |
| 367 | + |
| 368 | + [Fact] |
| 369 | + public void MergeReportsCheckoutNotifications() |
| 370 | + { |
| 371 | + string repoPath = CloneMergeTestRepo(); |
| 372 | + using (var repo = new Repository(repoPath)) |
| 373 | + { |
| 374 | + Commit commitToMerge = repo.Branches["normal_merge"].Tip; |
| 375 | + |
| 376 | + bool wasCalled = false; |
| 377 | + CheckoutNotifyFlags actualNotifyFlags = CheckoutNotifyFlags.None; |
| 378 | + |
| 379 | + MergeOptions options = new MergeOptions() |
| 380 | + { |
| 381 | + OnCheckoutNotify = (path, notificationType) => { wasCalled = true; actualNotifyFlags = notificationType; return true; }, |
| 382 | + CheckoutNotifyFlags = CheckoutNotifyFlags.Updated, |
| 383 | + }; |
| 384 | + |
| 385 | + MergeResult result = repo.Merge(commitToMerge, Constants.Signature, options); |
| 386 | + |
| 387 | + Assert.True(wasCalled); |
| 388 | + Assert.Equal(CheckoutNotifyFlags.Updated, actualNotifyFlags); |
| 389 | + } |
| 390 | + } |
| 391 | + |
| 392 | + [Fact] |
| 393 | + public void FastForwardMergeReportsCheckoutProgress() |
| 394 | + { |
| 395 | + string repoPath = CloneMergeTestRepo(); |
| 396 | + using (var repo = new Repository(repoPath)) |
| 397 | + { |
| 398 | + Commit commitToMerge = repo.Branches["fast_forward"].Tip; |
| 399 | + |
| 400 | + bool wasCalled = false; |
| 401 | + |
| 402 | + MergeOptions options = new MergeOptions() |
| 403 | + { |
| 404 | + OnCheckoutProgress = (path, completed, total) => wasCalled = true, |
| 405 | + }; |
| 406 | + |
| 407 | + MergeResult result = repo.Merge(commitToMerge, Constants.Signature, options); |
| 408 | + |
| 409 | + Assert.True(wasCalled); |
| 410 | + } |
| 411 | + } |
| 412 | + |
| 413 | + [Fact] |
| 414 | + public void FastForwardMergeReportsCheckoutNotifications() |
| 415 | + { |
| 416 | + string repoPath = CloneMergeTestRepo(); |
| 417 | + using (var repo = new Repository(repoPath)) |
| 418 | + { |
| 419 | + Commit commitToMerge = repo.Branches["fast_forward"].Tip; |
| 420 | + |
| 421 | + bool wasCalled = false; |
| 422 | + CheckoutNotifyFlags actualNotifyFlags = CheckoutNotifyFlags.None; |
| 423 | + |
| 424 | + MergeOptions options = new MergeOptions() |
| 425 | + { |
| 426 | + OnCheckoutNotify = (path, notificationType) => { wasCalled = true; actualNotifyFlags = notificationType; return true; }, |
| 427 | + CheckoutNotifyFlags = CheckoutNotifyFlags.Updated, |
| 428 | + }; |
| 429 | + |
| 430 | + MergeResult result = repo.Merge(commitToMerge, Constants.Signature, options); |
| 431 | + |
| 432 | + Assert.True(wasCalled); |
| 433 | + Assert.Equal(CheckoutNotifyFlags.Updated, actualNotifyFlags); |
| 434 | + } |
| 435 | + } |
| 436 | + |
| 437 | + [Fact] |
| 438 | + public void MergeCanDetectRenames() |
| 439 | + { |
| 440 | + // The environment is set up such that: |
| 441 | + // file b.txt is edited in the "rename" branch and |
| 442 | + // edited and renamed in the "rename_source" branch. |
| 443 | + // The edits are automergable. |
| 444 | + // We can rename "rename_source" into "rename" |
| 445 | + // if rename detection is enabled, |
| 446 | + // but the merge will fail with conflicts if this |
| 447 | + // change is not detected as a rename. |
| 448 | + |
| 449 | + string repoPath = CloneMergeTestRepo(); |
| 450 | + using (var repo = new Repository(repoPath)) |
| 451 | + { |
| 452 | + Branch currentBranch = repo.Checkout("rename_source"); |
| 453 | + Assert.NotNull(currentBranch); |
| 454 | + |
| 455 | + Branch branchToMerge = repo.Branches["rename"]; |
| 456 | + |
| 457 | + MergeResult result = repo.Merge(branchToMerge, Constants.Signature); |
| 458 | + |
| 459 | + Assert.Equal(MergeStatus.NonFastForward, result.Status); |
| 460 | + } |
| 461 | + } |
| 462 | + |
344 | 463 | [Fact]
|
345 | 464 | public void FastForwardNonFastForwardableMergeThrows()
|
346 | 465 | {
|
@@ -422,6 +541,112 @@ public void CanMergeCommittish(string committish, FastForwardStrategy strategy,
|
422 | 541 | }
|
423 | 542 | }
|
424 | 543 |
|
| 544 | + [Theory] |
| 545 | + [InlineData(CheckoutFileConflictStrategy.Ours)] |
| 546 | + [InlineData(CheckoutFileConflictStrategy.Theirs)] |
| 547 | + public void CanSpecifyConflictFileStrategy(CheckoutFileConflictStrategy conflictStrategy) |
| 548 | + { |
| 549 | + const string conflictFile = "a.txt"; |
| 550 | + const string conflictBranchName = "conflicts"; |
| 551 | + |
| 552 | + string path = CloneMergeTestRepo(); |
| 553 | + using (var repo = new Repository(path)) |
| 554 | + { |
| 555 | + Branch branch = repo.Branches[conflictBranchName]; |
| 556 | + Assert.NotNull(branch); |
| 557 | + |
| 558 | + MergeOptions mergeOptions = new MergeOptions() |
| 559 | + { |
| 560 | + FileConflictStrategy = conflictStrategy, |
| 561 | + }; |
| 562 | + |
| 563 | + MergeResult result = repo.Merge(branch, Constants.Signature, mergeOptions); |
| 564 | + Assert.Equal(MergeStatus.Conflicts, result.Status); |
| 565 | + |
| 566 | + // Get the information on the conflict. |
| 567 | + Conflict conflict = repo.Index.Conflicts[conflictFile]; |
| 568 | + |
| 569 | + Assert.NotNull(conflict); |
| 570 | + Assert.NotNull(conflict.Theirs); |
| 571 | + Assert.NotNull(conflict.Ours); |
| 572 | + |
| 573 | + // Get the blob containing the expected content. |
| 574 | + Blob expectedBlob = null; |
| 575 | + switch(conflictStrategy) |
| 576 | + { |
| 577 | + case CheckoutFileConflictStrategy.Theirs: |
| 578 | + expectedBlob = repo.Lookup<Blob>(conflict.Theirs.Id); |
| 579 | + break; |
| 580 | + case CheckoutFileConflictStrategy.Ours: |
| 581 | + expectedBlob = repo.Lookup<Blob>(conflict.Ours.Id); |
| 582 | + break; |
| 583 | + default: |
| 584 | + throw new Exception("Unexpected FileConflictStrategy"); |
| 585 | + } |
| 586 | + |
| 587 | + Assert.NotNull(expectedBlob); |
| 588 | + |
| 589 | + // Check the content of the file on disk matches what is expected. |
| 590 | + string expectedContent = expectedBlob.GetContentText(new FilteringOptions(conflictFile)); |
| 591 | + Assert.Equal(expectedContent, File.ReadAllText(Path.Combine(repo.Info.WorkingDirectory, conflictFile))); |
| 592 | + } |
| 593 | + } |
| 594 | + |
| 595 | + [Theory] |
| 596 | + [InlineData(MergeFileFavor.Ours)] |
| 597 | + [InlineData(MergeFileFavor.Theirs)] |
| 598 | + public void MergeCanSpecifyMergeFileFavorOption(MergeFileFavor fileFavorFlag) |
| 599 | + { |
| 600 | + const string conflictFile = "a.txt"; |
| 601 | + const string conflictBranchName = "conflicts"; |
| 602 | + |
| 603 | + string path = CloneMergeTestRepo(); |
| 604 | + using (var repo = InitIsolatedRepository(path)) |
| 605 | + { |
| 606 | + Branch branch = repo.Branches[conflictBranchName]; |
| 607 | + Assert.NotNull(branch); |
| 608 | + |
| 609 | + var status = repo.Index.RetrieveStatus(); |
| 610 | + MergeOptions mergeOptions = new MergeOptions() |
| 611 | + { |
| 612 | + MergeFileFavor = fileFavorFlag, |
| 613 | + }; |
| 614 | + |
| 615 | + MergeResult result = repo.Merge(branch, Constants.Signature, mergeOptions); |
| 616 | + |
| 617 | + Assert.Equal(MergeStatus.NonFastForward, result.Status); |
| 618 | + |
| 619 | + // Verify that the index and working directory are clean |
| 620 | + Assert.True(repo.Index.IsFullyMerged); |
| 621 | + Assert.False(repo.Index.RetrieveStatus().IsDirty); |
| 622 | + |
| 623 | + // Get the blob containing the expected content. |
| 624 | + Blob expectedBlob = null; |
| 625 | + switch (fileFavorFlag) |
| 626 | + { |
| 627 | + case MergeFileFavor.Theirs: |
| 628 | + expectedBlob = repo.Lookup<Blob>("3dd9738af654bbf1c363f6c3bbc323bacdefa179"); |
| 629 | + break; |
| 630 | + case MergeFileFavor.Ours: |
| 631 | + expectedBlob = repo.Lookup<Blob>("610b16886ca829cebd2767d9196f3c4378fe60b5"); |
| 632 | + break; |
| 633 | + default: |
| 634 | + throw new Exception("Unexpected MergeFileFavor"); |
| 635 | + } |
| 636 | + |
| 637 | + Assert.NotNull(expectedBlob); |
| 638 | + |
| 639 | + // Verify the index has the expected contents |
| 640 | + IndexEntry entry = repo.Index[conflictFile]; |
| 641 | + Assert.NotNull(entry); |
| 642 | + Assert.Equal(expectedBlob.Id, entry.Id); |
| 643 | + |
| 644 | + // Verify the content of the file on disk matches what is expected. |
| 645 | + string expectedContent = expectedBlob.GetContentText(new FilteringOptions(conflictFile)); |
| 646 | + Assert.Equal(expectedContent, File.ReadAllText(Path.Combine(repo.Info.WorkingDirectory, conflictFile))); |
| 647 | + } |
| 648 | + } |
| 649 | + |
425 | 650 | [Theory]
|
426 | 651 | [InlineData("refs/heads/normal_merge", FastForwardStrategy.Default, MergeStatus.NonFastForward)]
|
427 | 652 | [InlineData("fast_forward", FastForwardStrategy.Default, MergeStatus.FastForward)]
|
|
0 commit comments