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