|
1 |
| -using System.Linq; |
| 1 | +using System; |
| 2 | +using System.Linq; |
2 | 3 | using LibGit2Sharp.Tests.TestHelpers;
|
3 | 4 | using Xunit;
|
4 | 5 |
|
@@ -80,5 +81,192 @@ public void CanRetrieveTheBranchBeingMerged()
|
80 | 81 | Assert.Null(mergedHeads[1].Tip);
|
81 | 82 | }
|
82 | 83 | }
|
| 84 | + |
| 85 | + [Fact] |
| 86 | + public void CanMergeRepoNonFastForward() |
| 87 | + { |
| 88 | + const string firstBranchFileName = "first branch file.txt"; |
| 89 | + const string secondBranchFileName = "second branch file.txt"; |
| 90 | + const string sharedBranchFileName = "first+second branch file.txt"; |
| 91 | + |
| 92 | + string path = CloneStandardTestRepo(); |
| 93 | + |
| 94 | + using (var repo = new Repository(path)) |
| 95 | + { |
| 96 | + var firstBranch = repo.CreateBranch("FirstBranch"); |
| 97 | + firstBranch.Checkout(); |
| 98 | + var originalTreeCount = firstBranch.Tip.Tree.Count; |
| 99 | + |
| 100 | + // Commit with ONE new file to both first & second branch (SecondBranch is created on this commit). |
| 101 | + AddFileCommitToRepo(repo, sharedBranchFileName); |
| 102 | + |
| 103 | + var secondBranch = repo.CreateBranch("SecondBranch"); |
| 104 | + // Commit with ONE new file to first branch (FirstBranch moves forward as it is checked out, SecondBranch stays back one). |
| 105 | + AddFileCommitToRepo(repo, firstBranchFileName); |
| 106 | + |
| 107 | + secondBranch.Checkout(); |
| 108 | + |
| 109 | + // Commit with ONE new file to second branch (FirstBranch and SecondBranch now point to separate commits that both have the same parent commit). |
| 110 | + AddFileCommitToRepo(repo, secondBranchFileName); |
| 111 | + |
| 112 | + MergeResult mergeResult = repo.Merge(repo.Branches["FirstBranch"].Tip, Constants.Signature); |
| 113 | + |
| 114 | + Assert.Equal(MergeStatus.NonFastForward, mergeResult.Status); |
| 115 | + |
| 116 | + Assert.Equal(repo.Head.Tip, mergeResult.Commit); |
| 117 | + Assert.Equal(originalTreeCount + 3, mergeResult.Commit.Tree.Count); // Expecting original tree count plussed by the 3 added files. |
| 118 | + Assert.Equal(2, mergeResult.Commit.Parents.Count()); // Merge commit should have 2 parents |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + [Fact] |
| 123 | + public void IsUpToDateMerge() |
| 124 | + { |
| 125 | + const string sharedBranchFileName = "first+second branch file.txt"; |
| 126 | + |
| 127 | + string path = CloneStandardTestRepo(); |
| 128 | + using (var repo = new Repository(path)) |
| 129 | + { |
| 130 | + var firstBranch = repo.CreateBranch("FirstBranch"); |
| 131 | + firstBranch.Checkout(); |
| 132 | + |
| 133 | + // Commit with ONE new file to both first & second branch (SecondBranch is created on this commit). |
| 134 | + AddFileCommitToRepo(repo, sharedBranchFileName); |
| 135 | + |
| 136 | + var secondBranch = repo.CreateBranch("SecondBranch"); |
| 137 | + |
| 138 | + secondBranch.Checkout(); |
| 139 | + |
| 140 | + MergeResult mergeResult = repo.Merge(repo.Branches["FirstBranch"].Tip, Constants.Signature); |
| 141 | + |
| 142 | + Assert.Equal(MergeStatus.UpToDate, mergeResult.Status); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + [Fact] |
| 147 | + public void CanFastForwardRepos() |
| 148 | + { |
| 149 | + const string firstBranchFileName = "first branch file.txt"; |
| 150 | + const string sharedBranchFileName = "first+second branch file.txt"; |
| 151 | + |
| 152 | + string path = CloneStandardTestRepo(); |
| 153 | + using (var repo = new Repository(path)) |
| 154 | + { |
| 155 | + // Reset the index and the working tree. |
| 156 | + repo.Reset(ResetMode.Hard); |
| 157 | + |
| 158 | + // Clean the working directory. |
| 159 | + repo.RemoveUntrackedFiles(); |
| 160 | + |
| 161 | + var firstBranch = repo.CreateBranch("FirstBranch"); |
| 162 | + firstBranch.Checkout(); |
| 163 | + |
| 164 | + // Commit with ONE new file to both first & second branch (SecondBranch is created on this commit). |
| 165 | + AddFileCommitToRepo(repo, sharedBranchFileName); |
| 166 | + |
| 167 | + var secondBranch = repo.CreateBranch("SecondBranch"); |
| 168 | + |
| 169 | + // Commit with ONE new file to first branch (FirstBranch moves forward as it is checked out, SecondBranch stays back one). |
| 170 | + AddFileCommitToRepo(repo, firstBranchFileName); |
| 171 | + |
| 172 | + secondBranch.Checkout(); |
| 173 | + |
| 174 | + MergeResult mergeResult = repo.Merge(repo.Branches["FirstBranch"].Tip, Constants.Signature); |
| 175 | + |
| 176 | + Assert.Equal(MergeStatus.FastForward, mergeResult.Status); |
| 177 | + Assert.Equal(repo.Branches["FirstBranch"].Tip, mergeResult.Commit); |
| 178 | + Assert.Equal(repo.Branches["FirstBranch"].Tip, repo.Head.Tip); |
| 179 | + Assert.Equal(0, repo.Index.RetrieveStatus().Count()); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + [Fact] |
| 184 | + public void ConflictingMergeRepos() |
| 185 | + { |
| 186 | + const string firstBranchFileName = "first branch file.txt"; |
| 187 | + const string secondBranchFileName = "second branch file.txt"; |
| 188 | + const string sharedBranchFileName = "first+second branch file.txt"; |
| 189 | + |
| 190 | + string path = CloneStandardTestRepo(); |
| 191 | + using (var repo = new Repository(path)) |
| 192 | + { |
| 193 | + var firstBranch = repo.CreateBranch("FirstBranch"); |
| 194 | + firstBranch.Checkout(); |
| 195 | + |
| 196 | + // Commit with ONE new file to both first & second branch (SecondBranch is created on this commit). |
| 197 | + AddFileCommitToRepo(repo, sharedBranchFileName); |
| 198 | + |
| 199 | + var secondBranch = repo.CreateBranch("SecondBranch"); |
| 200 | + // Commit with ONE new file to first branch (FirstBranch moves forward as it is checked out, SecondBranch stays back one). |
| 201 | + AddFileCommitToRepo(repo, firstBranchFileName); |
| 202 | + AddFileCommitToRepo(repo, sharedBranchFileName, "The first branches comment"); // Change file in first branch |
| 203 | + |
| 204 | + secondBranch.Checkout(); |
| 205 | + // Commit with ONE new file to second branch (FirstBranch and SecondBranch now point to separate commits that both have the same parent commit). |
| 206 | + AddFileCommitToRepo(repo, secondBranchFileName); |
| 207 | + AddFileCommitToRepo(repo, sharedBranchFileName, "The second branches comment"); // Change file in second branch |
| 208 | + |
| 209 | + MergeResult mergeResult = repo.Merge(repo.Branches["FirstBranch"].Tip, Constants.Signature); |
| 210 | + |
| 211 | + Assert.Equal(MergeStatus.Conflicts, mergeResult.Status); |
| 212 | + |
| 213 | + Assert.Null(mergeResult.Commit); |
| 214 | + Assert.Equal(1, repo.Index.Conflicts.Count()); |
| 215 | + |
| 216 | + var conflict = repo.Index.Conflicts.First(); |
| 217 | + var changes = repo.Diff.Compare(repo.Lookup<Blob>(conflict.Theirs.Id), repo.Lookup<Blob>(conflict.Ours.Id)); |
| 218 | + |
| 219 | + Assert.False(changes.IsBinaryComparison); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + [Fact] |
| 224 | + public void ConflictingMergeReposBinary() |
| 225 | + { |
| 226 | + const string firstBranchFileName = "first branch file.bin"; |
| 227 | + const string secondBranchFileName = "second branch file.bin"; |
| 228 | + const string sharedBranchFileName = "first+second branch file.bin"; |
| 229 | + |
| 230 | + string path = CloneStandardTestRepo(); |
| 231 | + using (var repo = new Repository(path)) |
| 232 | + { |
| 233 | + var firstBranch = repo.CreateBranch("FirstBranch"); |
| 234 | + firstBranch.Checkout(); |
| 235 | + |
| 236 | + // Commit with ONE new file to both first & second branch (SecondBranch is created on this commit). |
| 237 | + AddFileCommitToRepo(repo, sharedBranchFileName); |
| 238 | + |
| 239 | + var secondBranch = repo.CreateBranch("SecondBranch"); |
| 240 | + // Commit with ONE new file to first branch (FirstBranch moves forward as it is checked out, SecondBranch stays back one). |
| 241 | + AddFileCommitToRepo(repo, firstBranchFileName); |
| 242 | + AddFileCommitToRepo(repo, sharedBranchFileName, "\0The first branches comment\0"); // Change file in first branch |
| 243 | + |
| 244 | + secondBranch.Checkout(); |
| 245 | + // Commit with ONE new file to second branch (FirstBranch and SecondBranch now point to separate commits that both have the same parent commit). |
| 246 | + AddFileCommitToRepo(repo, secondBranchFileName); |
| 247 | + AddFileCommitToRepo(repo, sharedBranchFileName, "\0The second branches comment\0"); // Change file in second branch |
| 248 | + |
| 249 | + MergeResult mergeResult = repo.Merge(repo.Branches["FirstBranch"].Tip, Constants.Signature); |
| 250 | + |
| 251 | + Assert.Equal(MergeStatus.Conflicts, mergeResult.Status); |
| 252 | + |
| 253 | + Assert.Equal(1, repo.Index.Conflicts.Count()); |
| 254 | + |
| 255 | + Conflict conflict = repo.Index.Conflicts.First(); |
| 256 | + |
| 257 | + var changes = repo.Diff.Compare(repo.Lookup<Blob>(conflict.Theirs.Id), repo.Lookup<Blob>(conflict.Ours.Id)); |
| 258 | + |
| 259 | + Assert.True(changes.IsBinaryComparison); |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + private Commit AddFileCommitToRepo(IRepository repository, string filename, string content = null) |
| 264 | + { |
| 265 | + Touch(repository.Info.WorkingDirectory, filename, content); |
| 266 | + |
| 267 | + repository.Index.Stage(filename); |
| 268 | + |
| 269 | + return repository.Commit("New commit", Constants.Signature, Constants.Signature); |
| 270 | + } |
83 | 271 | }
|
84 | 272 | }
|
0 commit comments