8000 Adds Reset(Commit commit, IEnumerable<string> paths = null) to Reposi… · rlazev/libgit2sharp@91aad11 · GitHub
[go: up one dir, main page]

Skip to content

Commit 91aad11

Browse files
committed
Adds Reset(Commit commit, IEnumerable<string> paths = null) to Repository
1 parent 0c290c4 commit 91aad11

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

LibGit2Sharp.Tests/ResetIndexFixture.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void ResetTheIndexWithTheHeadUnstagesEverything()
7676
}
7777

7878
[Fact]
79-
public void CanResetTheIndexToTheContentOfACommit()
79+
public void CanResetTheIndexToTheContentOfACommitWithCommitishAsArgument()
8080
{
8181
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
8282

@@ -95,7 +95,26 @@ public void CanResetTheIndexToTheContentOfACommit()
9595
}
9696

9797
[Fact]
98-
public void CanResetTheIndexToASubsetOfTheContentOfACommit()
98+
public void CanResetTheIndexToTheContentOfACommitWithCommitAsArgument()
99+
{
100+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
101+
102+
using (var repo = new Repository(path.DirectoryPath))
103+
{
104+
repo.Reset(repo.Lookup<Commit>("be3563a"));
105+
106+
RepositoryStatus newStatus = repo.Index.RetrieveStatus();
107+
108+
var expected = new[] { "1.txt", Path.Combine("1", "branch_file.txt"), "deleted_staged_file.txt",
109+
"deleted_unstaged_file.txt", "modified_staged_file.txt", "modified_unstaged_file.txt" };
110+
111+
Assert.Equal(expected.Length, newStatus.Where(IsStaged).Count());
112+
Assert.Equal(expected, newStatus.Removed);
113+
}
114+
}
115+
116+
[Fact]
117+
public void CanResetTheIndexToASubsetOfTheContentOfACommitWithCommitishAsArgument()
99118
{
100119
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
101120

@@ -107,5 +126,19 @@ public void CanResetTheIndexToASubsetOfTheContentOfACommit()
107126
Assert.Equal("fa49b077972391ad58037050f2a75f74e3671e92", repo.Index["new.txt"].Id.Sha);
108127
}
109128
}
129+
130+
[Fact]
131+
public void CanResetTheIndexToASubsetOfTheContentOfACommitWithCommitAsArgument()
132+
{
133+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
134+
135+
using (var repo = new Repository(path.DirectoryPath))
136+
{
137+
repo.Reset(repo.Lookup<Commit>("5b5b025"), new[] { "new.txt" });
138+
139+
Assert.Equal("a8233120f6ad708f843d861ce2b7228ec4e3dec6", repo.Index["README"].Id.Sha);
140+
Assert.Equal("fa49b077972391ad58037050f2a75f74e3671e92", repo.Index["new.txt"].Id.Sha);
141+
}
142+
}
110143
}
111144
}

LibGit2Sharp/IRepository.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ public interface IRepository : IDisposable
133133
/// <param name = "committish">A revparse spec for the target commit object.</param>
134134
void Reset(ResetOptions resetOptions, string committish = "HEAD");
135135

136+
/// <summary>
137+
/// Replaces entries in the <see cref="Repository.Index"/> with entries from the specified commit.
138+
/// </summary>
139+
/// <param name = "commit">The target commit object.</param>
140+
/// <param name = "paths">The list of paths (either files or directories) that should be considered.</param>
141+
void Reset(Commit commit, IEnumerable<string> paths = null);
142+
136143
/// <summary>
137144
/// Replaces entries in the <see cref="Repository.Index"/> with entries from the specified commit.
138145
/// </summary>

LibGit2Sharp/Repository.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,24 @@ public void Reset(ResetOptions resetOptions, string committish = "HEAD")
553553
Reset(resetOptions, commit);
554554
}
555555

556+
/// <summary>
557+
/// Replaces entries in the <see cref="Repository.Index"/> with entries from the specified commit.
558+
/// </summary>
559+
/// <param name = "commit">The target commit object.</param>
560+
/// <param name = "paths">The list of paths (either files or directories) that should be considered.</param>
561+
public void Reset(Commit commit, IEnumerable<string> paths = null)
562+
{
563+
if (Info.IsBare)
564+
{
565+
throw new BareRepositoryException("Reset is not allowed in a bare repository");
566+
}
567+
568+
Ensure.ArgumentNotNull(commit, "commit");
569+
570+
TreeChanges changes = Diff.Compare(commit.Tree, DiffTargets.Index, paths);
571+
Index.Reset(changes);
572+
}
573+
556574
/// <summary>
557575
/// Replaces entries in the <see cref="Index"/> with entries from the specified commit.
558576
/// </summary>
@@ -565,10 +583,10 @@ public void Reset(string committish = "HEAD", IEnumerable<string> paths = null)
565583
throw new BareRepositoryException("Reset is not allowed in a bare repository");
566584
}
567585

568-
Commit commit = LookupCommit(committish);
569-
TreeChanges changes = Diff.Compare(commit.Tree, DiffTargets.Index, paths);
586+
Ensure.ArgumentNotNullOrEmptyString(committish, "committish");
570587

571-
Index.Reset(changes);
588+
Commit commit = LookupCommit(committish);
589+
Reset(commit, paths);
572590
}
573591

574592
/// <summary>

0 commit comments

Comments
 (0)
0