Repository navigation
Expand file tree
/
Copy pathCheckoutFixture.cs
More file actions
357 lines (286 loc) · 13.7 KB
/
Copy pathCheckoutFixture.cs
File metadata and controls
357 lines (286 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using System;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;
using System.IO;
namespace LibGit2Sharp.Tests
{
public class CheckoutFixture : BaseFixture
{
private static readonly string originalFilePath = "a.txt";
private static readonly string originalFileContent = "Hello";
private static readonly string otherBranchName = "other";
[Theory]
[InlineData("i-do-numbers")]
[InlineData("diff-test-cases")]
public void CanCheckoutAnExistingBranch(string branchName)
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(path.RepositoryPath))
{
Branch master = repo.Branches["master"];
Assert.True(master.IsCurrentRepositoryHead);
// Set the working directory to the current head
ResetAndCleanWorkingDirectory(repo);
Assert.False(repo.Index.RetrieveStatus().IsDirty);
Branch branch = repo.Branches[branchName];
Assert.NotNull(branch);
Branch test = repo.Checkout(branch);
Assert.False(repo.Info.IsHeadDetached);
Assert.False(test.IsRemote);
Assert.True(test.IsCurrentRepositoryHead);
Assert.Equal(repo.Head, test);
Assert.False(master.IsCurrentRepositoryHead);
// Working directory should not be dirty
Assert.False(repo.Index.RetrieveStatus().IsDirty);
}
}
[Theory]
[InlineData("i-do-numbers")]
[InlineData("diff-test-cases")]
public void CanCheckoutAnExistingBranchByName(string branchName)
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(path.RepositoryPath))
{
Branch master = repo.Branches["master"];
Assert.True(master.IsCurrentRepositoryHead);
// Set the working directory to the current head
ResetAndCleanWorkingDirectory(repo);
Assert.False(repo.Index.RetrieveStatus().IsDirty);
Branch test = repo.Checkout(branchName);
Assert.False(repo.Info.IsHeadDetached);
Assert.False(test.IsRemote);
Assert.True(test.IsCurrentRepositoryHead);
Assert.Equal(repo.Head, test);
Assert.False(master.IsCurrentRepositoryHead);
// Working directory should not be dirty
Assert.False(repo.Index.RetrieveStatus().IsDirty);
}
}
[Theory]
[InlineData("6dcf9bf")]
[InlineData("refs/tags/lw")]
public void CanCheckoutAnArbitraryCommit(string commitPointer)
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(path.RepositoryPath))
{
Branch master = repo.Branches["master"];
Assert.True(master.IsCurrentRepositoryHead);
// Set the working directory to the current head
ResetAndCleanWorkingDirectory(repo);
Assert.False(repo.Index.RetrieveStatus().IsDirty);
Branch detachedHead = repo.Checkout(commitPointer);
Assert.Equal(repo.Head, detachedHead);
Assert.Equal(repo.Lookup(commitPointer).Sha, detachedHead.Tip.Sha);
Assert.True(repo.Head.IsCurrentRepositoryHead);
Assert.True(repo.Info.IsHeadDetached);
Assert.False(repo.Index.RetrieveStatus().IsDirty);
Assert.True(detachedHead.IsCurrentRepositoryHead);
Assert.False(detachedHead.IsRemote);
Assert.Equal(detachedHead.Name, detachedHead.CanonicalName);
Assert.Equal("(no branch)", detachedHead.CanonicalName);
Assert.False(master.IsCurrentRepositoryHead);
}
}
[Fact]
public void CheckoutAddsMissingFilesInWorkingDirectory()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
using (var repo = Repository.Init(scd.DirectoryPath))
{
PopulateBasicRepository(repo);
// Remove the file in master branch
// Verify it exists after checking out otherBranch.
string fileFullPath = Path.Combine(repo.Info.WorkingDirectory, originalFilePath);
repo.Index.Remove(fileFullPath);
repo.Commit("2nd commit", Constants.Signature, Constants.Signature);
// Checkout other_branch
Branch otherBranch = repo.Branches[otherBranchName];
Assert.NotNull(otherBranch);
otherBranch.Checkout();
// Verify working directory is updated
Assert.False(repo.Index.RetrieveStatus().IsDirty);
Assert.Equal(originalFileContent, File.ReadAllText(fileFullPath));
}
}
[Fact]
public void CheckoutRemovesExtraFilesInWorkingDirectory()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
using (var repo = Repository.Init(scd.DirectoryPath))
{
PopulateBasicRepository(repo);
// Add extra file in master branch
// Verify it is removed after checking out otherBranch.
string newFileFullPath = Path.Combine(repo.Info.WorkingDirectory, "b.txt");
File.WriteAllText(newFileFullPath, "hello from master branch!\n");
repo.Index.Stage(newFileFullPath);
repo.Commit("2nd commit", Constants.Signature, Constants.Signature);
// Checkout other_branch
Branch otherBranch = repo.Branches[otherBranchName];
Assert.NotNull(otherBranch);
otherBranch.Checkout();
// Verify working directory is updated
Assert.False(repo.Index.RetrieveStatus().IsDirty);
Assert.False(File.Exists(newFileFullPath));
}
}
[Fact]
public void CheckoutUpdatesModifiedFilesInWorkingDirectory()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
using (var repo = Repository.Init(scd.DirectoryPath))
{
PopulateBasicRepository(repo);
// Modify file in master branch.
// Verify contents match initial commit after checking out other branch.
string fullPath = Path.Combine(repo.Info.WorkingDirectory, originalFilePath);
File.WriteAllText(fullPath, "Update : hello from master branch!\n");
repo.Index.Stage(fullPath);
repo.Commit("2nd commit", Constants.Signature, Constants.Signature);
// Checkout other_branch
Branch otherBranch = repo.Branches[otherBranchName];
Assert.NotNull(otherBranch);
otherBranch.Checkout();
// Verify working directory is updated
Assert.False(repo.Index.RetrieveStatus().IsDirty);
Assert.Equal(originalFileContent, File.ReadAllText(fullPath));
}
}
[Fact]
public void CanForcefullyCheckoutWithStagedChanges()
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(path.RepositoryPath))
{
string fileFullPath = Path.Combine(repo.Info.WorkingDirectory, originalFilePath);
Branch master = repo.Branches["master"];
Assert.True(master.IsCurrentRepositoryHead);
// Set the working directory to the current head
ResetAndCleanWorkingDirectory(repo);
Assert.False(repo.Index.RetrieveStatus().IsDirty);
// Add local change
string fullPath = Path.Combine(repo.Info.WorkingDirectory, fileFullPath);
File.WriteAllText(fullPath, originalFileContent);
repo.Index.Stage(fullPath);
// Verify working directory is now dirty
Assert.True(repo.Index.RetrieveStatus().IsDirty);
// And that the new file exists
Assert.True(File.Exists(fileFullPath));
// Checkout with the force option
Branch targetBranch = repo.Branches["i-do-numbers"];
targetBranch.Checkout(CheckoutOptions.Force, null);
// Assert that target branch is checked out
Assert.True(targetBranch.IsCurrentRepositoryHead);
// And that staged change (add) is no longer preset
Assert.False(File.Exists(fileFullPath));
}
}
[Fact]
public void CheckingOutWithMergeConflictsThrows()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
using (var repo = Repository.Init(scd.DirectoryPath))
{
string fullPath = Path.Combine(repo.Info.WorkingDirectory, "a.txt");
File.WriteAllText(fullPath, "Hello\n");
repo.Index.Stage(fullPath);
repo.Commit("Initial commit", Constants.Signature, Constants.Signature);
// Create 2nd branch
repo.CreateBranch("branch2");
// Update file in main
File.WriteAllText(fullPath, "Hello from master!\n");
repo.Index.Stage(fullPath);
repo.Commit("2nd commit", Constants.Signature, Constants.Signature);
// Checkout branch2
repo.Checkout("branch2");
File.WriteAllText(fullPath, "Hello From branch2!\n");
// Assert that checking out master throws
// when there are unstaged commits
Assert.Throws<MergeConflictException>(() => repo.Checkout("master"));
// And when there are staged commits
repo.Index.Stage(fullPath);
Assert.Throws<MergeConflictException>(() => repo.Checkout("master"));
}
}
[Fact]
public void CheckingOutInABareRepoThrows()
{
using (var repo = new Repository(BareTestRepoPath))
{
Assert.Throws<InvalidOperationException>(() => repo.Checkout(repo.Branches["refs/heads/test"]));
Assert.Throws<InvalidOperationException>(() => repo.Checkout("refs/heads/test"));
}
}
[Fact]
public void CheckingOutANonExistingBranchThrows()
{
using (var repo = new Repository(StandardTestRepoWorkingDirPath))
{
Assert.Throws<LibGit2SharpException>(() => repo.Checkout("i-do-not-exist"));
}
}
[Fact]
public void CheckingOutABranchWithBadParamsThrows()
{
using (var repo = new Repository(StandardTestRepoWorkingDirPath))
{
Assert.Throws<ArgumentException>(() => repo.Checkout(string.Empty));
Assert.Throws<ArgumentNullException>(() => repo.Checkout(default(Branch)));
Assert.Throws<ArgumentNullException>(() => repo.Checkout(default(string)));
}
}
[Fact]
public void CheckingOutThroughBranchCallsCheckoutProgress()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
using (var repo = Repository.Init(scd.DirectoryPath))
{
PopulateBasicRepository(repo);
bool wasCalled = false;
Branch branch = repo.Branches[otherBranchName];
branch.Checkout(CheckoutOptions.None, (path, completed, total) => wasCalled = true);
Assert.True(wasCalled);
}
}
[Fact]
public void CheckingOutThroughRepositoryCallsCheckoutProgress()
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
using (var repo = Repository.Init(scd.DirectoryPath))
{
PopulateBasicRepository(repo);
bool wasCalled = false;
repo.Checkout(otherBranchName, CheckoutOptions.None, (path, completed, total) => wasCalled = true);
Assert.True(wasCalled);
}
}
/// <summary>
/// Helper method to populate a simple repository with
/// a single file and two branches.
/// </summary>
/// <param name="repo">Repository to populate</param>
private void PopulateBasicRepository(Repository repo)
{
string fullPathFileA = Path.Combine(repo.Info.WorkingDirectory, "a.txt");
File.WriteAllText(fullPathFileA, originalFileContent);
repo.Index.Stage(fullPathFileA);
repo.Commit("Initial commit", Constants.Signature, Constants.Signature);
repo.CreateBranch(otherBranchName);
}
/// <summary>
/// Reset and clean current working directory. This will ensure that the current
/// working directory matches the current Head commit.
/// </summary>
/// <param name="repo">Repository whose current working directory should be operated on.</param>
private void ResetAndCleanWorkingDirectory(Repository repo)
{
// Reset the index and the working tree.
repo.Reset(ResetOptions.Hard);
// Clean the working directory.
repo.RemoveUntrackedFiles();
}
}
}