|
| 1 | +using System.IO; |
| 2 | +using System.Linq; |
| 3 | +using LibGit2Sharp.Tests.TestHelpers; |
| 4 | +using Xunit; |
| 5 | +using Xunit.Extensions; |
| 6 | +using System; |
| 7 | + |
| 8 | +namespace LibGit2Sharp.Tests |
| 9 | +{ |
| 10 | + public class CherryPickFixture : BaseFixture |
| 11 | + { |
| 12 | + [Theory] |
| 13 | + [InlineData(true)] |
| 14 | + [InlineData(false)] |
| 15 | + public void CanCherryPick(bool fromDetachedHead) |
| 16 | + { |
| 17 | + string path = CloneMergeTestRepo(); |
| 18 | + using (var repo = new Repository(path)) |
| 19 | + { |
| 20 | + if (fromDetachedHead) |
| 21 | + { |
| 22 | + repo.Checkout(repo.Head.Tip.Id.Sha); |
| 23 | + } |
| 24 | + |
| 25 | + Commit commitToMerge = repo.Branches["fast_forward"].Tip; |
| 26 | + |
| 27 | + CherryPickResult result = repo.CherryPick(commitToMerge, Constants.Signature); |
| 28 | + |
| 29 | + Assert.Equal(CherryPickStatus.CherryPicked, result.Status); |
| 30 | + Assert.Equal(cherryPickedCommitId, result.Commit.Id.Sha); |
| 31 | + Assert.False(repo.Index.RetrieveStatus().Any()); |
| 32 | + Assert.Equal(fromDetachedHead, repo.Info.IsHeadDetached); |
| 33 | + Assert.Equal(commitToMerge.Author, result.Commit.Author); |
| 34 | + Assert.Equal(Constants.Signature, result.Commit.Committer); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void CherryPickWithConflictDoesNotCommit() |
| 40 | + { |
| 41 | + const string firstBranchFileName = "first branch file.txt"; |
| 42 | + const string secondBranchFileName = "second branch file.txt"; |
| 43 | + const string sharedBranchFileName = "first+second branch file.txt"; |
| 44 | + |
| 45 | + string path = CloneStandardTestRepo(); |
| 46 | + using (var repo = new Repository(path)) |
| 47 | + { |
| 48 | + var firstBranch = repo.CreateBranch("FirstBranch"); |
| 49 | + firstBranch.Checkout(); |
| 50 | + |
| 51 | + // Commit with ONE new file to both first & second branch (SecondBranch is created on this commit). |
| 52 | + AddFileCommitToRepo(repo, sharedBranchFileName); |
| 53 | + |
| 54 | + var secondBranch = repo.CreateBranch("SecondBranch"); |
| 55 | + // Commit with ONE new file to first branch (FirstBranch moves forward as it is checked out, SecondBranch stays back one). |
| 56 | + AddFileCommitToRepo(repo, firstBranchFileName); |
| 57 | + AddFileCommitToRepo(repo, sharedBranchFileName, "The first branches comment"); // Change file in first branch |
| 58 | + |
| 59 | + secondBranch.Checkout(); |
| 60 | + // Commit with ONE new file to second branch (FirstBranch and SecondBranch now point to separate commits that both have the same parent commit). |
| 61 | + AddFileCommitToRepo(repo, secondBranchFileName); |
| 62 | + AddFileCommitToRepo(repo, sharedBranchFileName, "The second branches comment"); // Change file in second branch |
| 63 | + |
| 64 | + CherryPickResult cherryPickResult = repo.CherryPick(repo.Branches["FirstBranch"].Tip, Constants.Signature); |
| 65 | + |
| 66 | + Assert.Equal(CherryPickStatus.Conflicts, cherryPickResult.Status); |
| 67 | + |
| 68 | + Assert.Null(cherryPickResult.Commit); |
| 69 | + Assert.Equal(1, repo.Index.Conflicts.Count()); |
| 70 | + |
| 71 | + var conflict = repo.Index.Conflicts.First(); |
| 72 | + var changes = repo.Diff.Compare(repo.Lookup<Blob>(conflict.Theirs.Id), repo.Lookup<Blob>(conflict.Ours.Id)); |
| 73 | + |
| 74 | + Assert.False(changes.IsBinaryComparison); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + [Theory] |
| 79 | + [InlineData(CheckoutFileConflictStrategy.Ours)] |
| 80 | + [InlineData(CheckoutFileConflictStrategy.Theirs)] |
| 81 | + public void CanSpecifyConflictFileStrategy(CheckoutFileConflictStrategy conflictStrategy) |
| 82 | + { |
| 83 | + const string conflictFile = "a.txt"; |
| 84 | + const string conflictBranchName = "conflicts"; |
| 85 | + |
| 86 | + string path = CloneMergeTestRepo(); |
| 87 | + using (var repo = new Repository(path)) |
| 88 | + { |
| 89 | + Branch branch = repo.Branches[conflictBranchName]; |
| 90 | + Assert.NotNull(branch); |
| 91 | + |
| 92 | + CherryPickOptions cherryPickOptions = new CherryPickOptions() |
| 93 | + { |
| 94 | + FileConflictStrategy = conflictStrategy, |
| 95 | + }; |
| 96 | + |
| 97 | + CherryPickResult result = repo.CherryPick(branch.Tip, Constants.Signature, cherryPickOptions); |
| 98 | + Assert.Equal(CherryPickStatus.Conflicts, result.Status); |
| 99 | + |
| 100 | + // Get the information on the conflict. |
| 101 | + Conflict conflict = repo.Index.Conflicts[conflictFile]; |
| 102 | + |
| 103 | + Assert.NotNull(conflict); |
| 104 | + Assert.NotNull(conflict.Theirs); |
| 105 | + Assert.NotNull(conflict.Ours); |
| 106 | + |
| 107 | + // Get the blob containing the expected content. |
| 108 | + Blob expectedBlob = null; |
| 109 | + switch (conflictStrategy) |
| 110 | + { |
| 111 | + case CheckoutFileConflictStrategy.Theirs: |
| 112 | + expectedBlob = repo.Lookup<Blob>(conflict.Theirs.Id); |
| 113 | + break; |
| 114 | + case CheckoutFileConflictStrategy.Ours: |
| 115 | + expectedBlob = repo.Lookup<Blob>(conflict.Ours.Id); |
| 116 | + break; |
| 117 | + default: |
| 118 | + throw new Exception("Unexpected FileConflictStrategy"); |
| 119 | + } |
| 120 | + |
| 121 | + Assert.NotNull(expectedBlob); |
| 122 | + |
| 123 | + // Check the content of the file on disk matches what is expected. |
| 124 | + string expectedContent = expectedBlob.GetContentText(new FilteringOptions(conflictFile)); |
| 125 | + Assert.Equal(expectedContent, File.ReadAllText(Path.Combine(repo.Info.WorkingDirectory, conflictFile))); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private Commit AddFileCommitToRepo(IRepository repository, string filename, string content = null) |
| 130 | + { |
| 131 | + Touch(repository.Info.WorkingDirectory, filename, content); |
| 132 | + |
| 133 | + repository.Index.Stage(filename); |
| 134 | + |
| 135 | + return repository.Commit("New commit", Constants.Signature, Constants.Signature); |
| 136 | + } |
| 137 | + |
| 138 | + // Commit IDs of the checked in merge_testrepo |
| 139 | + private const string cherryPickedCommitId = "74b37f366b6e1c682c1c9fe0c6b006cbe909cf91"; |
| 140 | + } |
| 141 | +} |
0 commit comments