|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Globalization; |
3 | 4 | using System.IO; |
4 | 5 | using System.Linq; |
5 | 6 | using LibGit2Sharp.Tests.TestHelpers; |
@@ -443,5 +444,54 @@ public void AddingAnEntryToTheIndexFromAUnknwonFileInTheWorkdirThrows() |
443 | 444 | Assert.Throws<LibGit2SharpException>(() => repo.Index.Add(filePath)); |
444 | 445 | } |
445 | 446 | } |
| 447 | + |
| 448 | + [Fact] |
| 449 | + public void CanMimicGitAddAll() |
| 450 | + { |
| 451 | + var path = SandboxStandardTestRepoGitDir(); |
| 452 | + using (var repo = new Repository(path)) |
| 453 | + { |
| 454 | + var before = repo.RetrieveStatus(); |
| 455 | + Assert.True(before.Any(se => se.State == FileStatus.Untracked)); |
| 456 | + Assert.True(before.Any(se => se.State == FileStatus.Modified)); |
| 457 | + Assert.True(before.Any(se => se.State == FileStatus.Missing)); |
| 458 | + |
| 459 | + StageAllAdditionsRemovalsAndModifications(repo); |
| 460 | + |
| 461 | + var after = repo.RetrieveStatus(); |
| 462 | + Assert.False(after.Any(se => se.State == FileStatus.Untracked)); |
| 463 | + Assert.False(after.Any(se => se.State == FileStatus.Modified)); |
| 464 | + Assert.False(after.Any(se => se.State == FileStatus.Missing)); |
| 465 | + } |
| 466 | + } |
| 467 | + |
| 468 | + private void StageAllAdditionsRemovalsAndModifications(IRepository repo) |
| 469 | + { |
| 470 | + var changes = repo.Diff.Compare<TreeChanges>(null, true, |
| 471 | + compareOptions: new CompareOptions { Similarity = SimilarityOptions.None }); |
| 472 | + |
| 473 | + foreach (TreeEntryChanges treeEntryChanges in changes) |
| 474 | + { |
| 475 | + switch (treeEntryChanges.Status) |
| 476 | + { |
| 477 | + case ChangeKind.Unmodified: |
| 478 | + continue; |
| 479 | + |
| 480 | + case ChangeKind.Added: |
| 481 | + case ChangeKind.Modified: |
| 482 | + repo.Index.Add(treeEntryChanges.Path); |
| 483 | + continue; |
| 484 | + |
| 485 | + case ChangeKind.Deleted: |
| 486 | + repo.Index.Remove(treeEntryChanges.Path); |
| 487 | + continue; |
| 488 | + |
| 489 | + default: |
| 490 | + throw new InvalidOperationException( |
| 491 | + string.Format(CultureInfo.InvariantCulture, "Entry '{0}' bears an unexpected ChangeKind '{1}'", |
| 492 | + treeEntryChanges.Path, treeEntryChanges.Status)); |
| 493 | + } |
| 494 | + } |
| 495 | + } |
446 | 496 | } |
447 | 497 | } |
0 commit comments