8000 Fix repo.Stage("*") behavior · GiTechLab/libgit2sharp@fed306b · GitHub 10000
[go: up one dir, main page]

Skip to content

Commit fed306b

Browse files
committed
Fix repo.Stage("*") behavior
- Drop unnecessary detection of renames and copies - Always perform removals from the Index before additions
1 parent f4c7bd6 commit fed306b

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,5 +443,39 @@ public void AddingAnEntryToTheIndexFromAUnknwonFileInTheWorkdirThrows()
443443
Assert.Throws<NotFoundException>(() => repo.Index.Add(filePath));
444444
}
445445
}
446+
447+
[Fact]
448+
public void CanMimicGitAddAll()
449+
{
450+
var path = SandboxStandardTestRepoGitDir();
451+
using (var repo = new Repository(path))
452+
{
453+
var before = repo.RetrieveStatus();
454+
Assert.True(before.Any(se => se.State == FileStatus.Untracked));
455+
Assert.True(before.Any(se => se.State == FileStatus.Modified));
456+
Assert.True(before.Any(se => se.State == FileStatus.Missing));
457+
458+
AddSomeCornerCases(repo);
459+
460+
repo.Stage("*");
461+
462+
var after = repo.RetrieveStatus();
463+
Assert.False(after.Any(se => se.State == FileStatus.Untracked));
464+
Assert.False(after.Any(se => se.State == FileStatus.Modified));
465+
Assert.False(after.Any(se => se.State == FileStatus.Missing));
466+
}
467+
}
468+
469+
private static void AddSomeCornerCases(Repository repo)
470+
{
471+
// Turn 1.txt into a directory in the Index
472+
repo.Index.Remove("1.txt");
473+
var blob = repo.Lookup<Blob>("a8233120f6ad708f843d861ce2b7228ec4e3dec6");
474+
repo.Index.Add(blob, "1.txt/Sneaky", Mode.NonExecutableFile);
475+
476+
// Turn README into a symlink
477+
Blob linkContent = OdbHelper.CreateBlob(repo, "1.txt/sneaky");
478+
repo.Index.Add(linkContent, "README", Mode.SymbolicLink);
479+
}
446480
}
447481
}

LibGit2Sharp/Repository.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,28 +1493,41 @@ public void Stage(IEnumerable<string> paths, StageOptions stageOptions)
14931493
diffModifiers |= DiffModifiers.IncludeIgnored;
14941494
}
14951495

1496-
var changes = Diff.Compare<TreeChanges>(diffModifiers, paths, explicitPathsOptions);
1496+
var changes = Diff.Compare<TreeChanges>(diffModifiers, paths, explicitPathsOptions,
1497+
new CompareOptions { Similarity = SimilarityOptions.None });
14971498

1498-
foreach (var treeEntryChanges in changes)
1499+
var unexpectedTypesOfChanges = changes
1500+
.Where(
1501+
tec => tec.Status != ChangeKind.Added &&
1502+
tec.Status != ChangeKind.Modified &&
1503+
tec.Status != ChangeKind.Unmodified &&
1504+
tec.Status != ChangeKind.Deleted).ToList();
1505+
1506+
if (unexpectedTypesOfChanges.Count > 0)
14991507
{
1500-
switch (treeEntryChanges.Status)
1501-
{
1502-
case ChangeKind.Unmodified:
1503-
continue;
1508+
throw new InvalidOperationException(
1509+
string.Format(CultureInfo.InvariantCulture,
1510+
"Entry '{0}' bears an unexpected ChangeKind '{1}'",
1511+
unexpectedTypesOfChanges[0].Path, unexpectedTypesOfChanges[0].Status));
1512+
}
15041513

1505-
case ChangeKind.Deleted:
1506-
RemoveFromIndex(treeEntryChanges.Path);
1507-
continue;
1514+
foreach (TreeEntryChanges treeEntryChanges in changes
1515+
.Where(tec => tec.Status == ChangeKind.Deleted))
1516+
{
1517+
RemoveFromIndex(treeEntryChanges.Path);
1518+
}
15081519

1520+
foreach (TreeEntryChanges treeEntryChanges in changes)
1521+
{
1522+
switch (treeEntryChanges.Status)
1523+
{
15091524
case ChangeKind.Added:
1510-
/* Fall through */
15111525
case ChangeKind.Modified:
15121526
AddToIndex(treeEntryChanges.Path);
1513-
continue;
1527+
break;
15141528

15151529
default:
1516-
throw new InvalidOperationException(
1517-
string.Format(CultureInfo.InvariantCulture, "Entry '{0}' bears an unexpected ChangeKind '{1}'", treeEntryChanges.Path, treeEntryChanges.Status));
1530+
continue;
15181531
}
15191532
}
15201533

0 commit comments

Comments
 (0)
0