Skip to content

Commit 6f80497

Browse files
committed
Expose Reset() with checkout options
A hard reset can take a while as it involves a checkout, allow use of progress reporting.
1 parent e6f9e97 commit 6f80497

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

LibGit2Sharp.Tests/ResetHeadFixture.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ public void HardResetInABareRepositoryThrows()
235235
[Fact]
236236
public void HardResetUpdatesTheContentOfTheWorkingDirectory()
237237
{
238+
bool progressCalled = false;
239+
238240
string path = SandboxStandardTestRepo();
239241
using (var repo = new Repository(path))
240242
{
@@ -245,11 +247,16 @@ public void HardResetUpdatesTheContentOfTheWorkingDirectory()
245247

246248
Assert.True(names.Count > 4);
247249

248-
repo.Reset(ResetMode.Hard, "HEAD~3");
250+
var commit = repo.Lookup<Commit>("HEAD~3");
251+
repo.Reset(ResetMode.Hard, commit, new CheckoutOptions()
252+
{
253+
OnCheckoutProgress = (_path, _completed, _total) => { progressCalled = true; },
254+
});
249255

250256
names = new DirectoryInfo(repo.Info.WorkingDirectory).GetFileSystemInfos().Select(fsi => fsi.Name).ToList();
251257
names.Sort(StringComparer.Ordinal);
252258

259+
Assert.Equal(true, progressCalled);
253260
Assert.Equal(new[] { ".git", "README", "WillNotBeRemoved.txt", "branch_file.txt", "new.txt", "new_untracked_file.txt" }, names);
254261
}
255262
}

LibGit2Sharp/IRepository.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ public interface IRepository : IDisposable
165165
/// <param name="commit">The target commit object.</param>
166166
void Reset(ResetMode resetMode, Commit commit);
167167

168+
/// <summary>
169+
/// Sets <see cref="Head"/> to the specified commit and optionally resets the <see cref="Index"/> and
170+
/// the content of the working tree to match.
171+
/// </summary>
172+
/// <param name="resetMode">Flavor of reset operation to perform.</param>
173+
/// <param name="commit">The target commit object.</param>
174+
/// <param name="opts">Collection of parameters controlling checkout behavior.</param>
175+
void Reset(ResetMode resetMode, Commit commit, CheckoutOptions options);
176+
168177
/// <summary>
169178
/// Replaces entries in the <see cref="Repository.Index"/> with entries from the specified commit.
170179
/// </summary>

LibGit2Sharp/Repository.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,24 @@ public void Reset(ResetMode resetMode, Commit commit)
984984
Reset(resetMode, commit, new CheckoutOptions());
985985
}
986986

987+
/// <summary>
988+
/// Sets <see cref="Head"/> to the specified commit and optionally resets the <see cref="Index"/> and
989+
/// the content of the working tree to match.
990+
/// </summary>
991+
/// <param name="resetMode">Flavor of reset operation to perform.</param>
992+
/// <param name="commit">The target commit object.</param>
993+
/// <param name="opts">Collection of parameters controlling checkout behavior.</param>
994+
public void Reset(ResetMode resetMode, Commit commit, CheckoutOptions opts)
995+
{
996+
Ensure.ArgumentNotNull(commit, "commit");
997+
998+
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(opts))
999+
{
1000+
var options = checkoutOptionsWrapper.Options;
1001+
Proxy.git_reset(handle, commit.Id, resetMode, ref options);
1002+
}
1003+
}
1004+
9871005
/// <summary>
9881006
/// Sets the current <see cref="Head"/> to the specified commit and optionally resets the <see cref="Index"/> and
9891007
/// the content of the working tree to match.

0 commit comments

Comments
 (0)