Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions LibGit2Sharp.Tests/ResetIndexFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,59 @@ public void ResettingTheIndexToASubsetOfTheContentOfACommitWithCommitAsArgumentA
repo.Reset(repo.Lookup<Commit>("5b5b025"), new[] { "new.txt", "non-existent-path-28.txt" }, new ExplicitPathsOptions()));
}
}

[Fact]
public void CanResetTheIndexWhenARenameExists()
{
using (var repo = new Repository(CloneStandardTestRepo()))
{
repo.Index.Move("branch_file.txt", "renamed_branch_file.txt");
repo.Reset(repo.Lookup<Commit>("32eab9c"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small request: could you include a comment about what commit "32eab9c" is? Is this the Tip commit of the standard test repo?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is at present; but I think that future commits to that repo would make that comment outdated quickly. (The other Reset tests use an older commit which I suspect was tip when they were written.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that were to happen, I imagine this test would need to be updated anyways, as you would be reseting the index to a commit other than HEAD, and it would probably have other staged changes at that point?

Anyway, not a big deal. I do think a comment describing what the intent of why you are using that specific commit might help when looking at this code in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW there are many tests that depend on the current repo.Head.Tip, including some Reset() tests. No harm in depending on it here too, IMO.


RepositoryStatus status = repo.Index.RetrieveStatus();
Assert.Equal(0, status.Where(IsStaged).Count());
}
}

[Fact]
public void CanResetSourceOfARenameInIndex()
{
using (var repo = new Repository(CloneStandardTestRepo()))
{
repo.Index.Move("branch_file.txt", "renamed_branch_file.txt");

RepositoryStatus oldStatus = repo.Index.RetrieveStatus();
Assert.Equal(1, oldStatus.RenamedInIndex.Count());
Assert.Equal(FileStatus.Nonexistent, oldStatus["branch_file.txt"].State);
Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State);

repo.Reset(repo.Lookup<Commit>("32eab9c"), new string[] { "branch_file.txt" });

RepositoryStatus newStatus = repo.Index.RetrieveStatus();
Assert.Equal(0, newStatus.RenamedInIndex.Count());
Assert.Equal(FileStatus.Missing, newStatus["branch_file.txt"].State);
Assert.Equal(FileStatus.Added, newStatus["renamed_branch_file.txt"].State);
}
}

[Fact]
public void CanResetTargetOfARenameInIndex()
{
using (var repo = new Repository(CloneStandardTestRepo()))
{
repo.Index.Move("branch_file.txt", "renamed_branch_file.txt");

RepositoryStatus oldStatus = repo.Index.RetrieveStatus();
Assert.Equal(1, oldStatus.RenamedInIndex.Count());
Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State);

repo.Reset(repo.Lookup<Commit>("32eab9c"), new string[] { "renamed_branch_file.txt" });

RepositoryStatus newStatus = repo.Index.RetrieveStatus();
Assert.Equal(0, newStatus.RenamedInIndex.Count());
Assert.Equal(FileStatus.Untracked, newStatus["renamed_branch_file.txt"].State);
Assert.Equal(FileStatus.Removed, newStatus["branch_file.txt"].State);
}
}
}
}
55 changes: 55 additions & 0 deletions LibGit2Sharp.Tests/UnstageFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,60 @@ public void UnstagingFileWithBadParamsThrows()
Assert.Throws<ArgumentException>(() => repo.Index.Unstage(new string[] { null }));
}
}

[Fact]
public void CanUnstageSourceOfARename()
{
using (var repo = new Repository(CloneStandardTestRepo()))
{
repo.Index.Move("branch_file.txt", "renamed_branch_file.txt");

RepositoryStatus oldStatus = repo.Index.RetrieveStatus();
Assert.Equal(1, oldStatus.RenamedInIndex.Count());
Assert.Equal(FileStatus.Nonexistent, oldStatus["branch_file.txt"].State);
Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State);

repo.Index.Unstage(new string[] { "branch_file.txt" });

RepositoryStatus newStatus = repo.Index.RetrieveStatus();
Assert.Equal(0, newStatus.RenamedInIndex.Count());
Assert.Equal(FileStatus.Missing, newStatus["branch_file.txt"].State);
Assert.Equal(FileStatus.Added, newStatus["renamed_branch_file.txt"].State);
}
}

[Fact]
public void CanUnstageTargetOfARename()
{
using (var repo = new Repository(CloneStandardTestRepo()))
{
repo.Index.Move("branch_file.txt", "renamed_branch_file.txt");

RepositoryStatus oldStatus = repo.Index.RetrieveStatus();
Assert.Equal(1, oldStatus.RenamedInIndex.Count());
Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State);

repo.Index.Unstage(new string[] { "renamed_branch_file.txt" });

RepositoryStatus newStatus = repo.Index.RetrieveStatus();
Assert.Equal(0, newStatus.RenamedInIndex.Count());
Assert.Equal(FileStatus.Untracked, newStatus["renamed_branch_file.txt"].State);
Assert.Equal(FileStatus.Removed, newStatus["branch_file.txt"].State);
}
}

[Fact]
public void CanUnstageBothSidesOfARename()
{
using (var repo = new Repository(CloneStandardTestRepo()))
{
repo.Index.Move("branch_file.txt", "renamed_branch_file.txt");
repo.Index.Unstage(new string[] { "branch_file.txt", "renamed_branch_file.txt" });

RepositoryStatus status = repo.Index.RetrieveStatus();
Assert.Equal(FileStatus.Missing, status["branch_file.txt"].State);
Assert.Equal(FileStatus.Untracked, status["renamed_branch_file.txt"].State);
}
}
}
}
2 changes: 1 addition & 1 deletion LibGit2Sharp/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public virtual void Unstage(IEnumerable<string> paths, ExplicitPathsOptions expl

if (repo.Info.IsHeadUnborn)
{
var changes = repo.Diff.Compare<TreeChanges>(null, DiffTargets.Index, paths, explicitPathsOptions);
var changes = repo.Diff.Compare<TreeChanges>(null, DiffTargets.Index, paths, explicitPathsOptions, new CompareOptions { Similarity = SimilarityOptions.None });

Reset(changes);
}
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ public void Reset(Commit commit, IEnumerable<string> paths = null, ExplicitPaths

Ensure.ArgumentNotNull(commit, "commit");

var changes = Diff.Compare<TreeChanges>(commit.Tree, DiffTargets.Index, paths, explicitPathsOptions);
var changes = Diff.Compare<TreeChanges>(commit.Tree, DiffTargets.Index, paths, explicitPathsOptions, new CompareOptions { Similarity = SimilarityOptions.None });
Index.Reset(changes);
}

Expand Down