Skip to content

Commit d2f7616

Browse files
committed
Delegate repo.Reset() to libgit2
1 parent 041a4d3 commit d2f7616

File tree

4 files changed

+26
-37
lines changed

4 files changed

+26
-37
lines changed

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,12 @@ internal static extern int git_repository_set_workdir(
672672
[return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(FilePathMarshaler))]
673673
internal static extern FilePath git_repository_workdir(RepositorySafeHandle repository);
674674

675+
[DllImport(libgit2)]
676+
internal static extern int git_reset(
677+
RepositorySafeHandle repo,
678+
GitObjectSafeHandle target,
679+
ResetOptions reset_type);
680+
675681
[DllImport(libgit2)]
676682
internal static extern int git_revparse_single(
677683
out GitObjectSafeHandle obj,

LibGit2Sharp/Core/Proxy.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,23 @@ public static FilePath git_repository_workdir(RepositorySafeHandle repo)
12741274

12751275
#endregion
12761276

1277+
#region git_reset_
1278+
1279+
public static void git_reset(
1280+
RepositorySafeHandle repo,
1281+
ObjectId commitishId,
1282+
ResetOptions resetKind)
1283+
{
1284+
using (ThreadAffinity())
1285+
using (var osw = new ObjectSafeWrapper(commitishId, repo))
1286+
{
1287+
int res = NativeMethods.git_reset(repo, osw.ObjectPtr, resetKind);
1288+
Ensure.Success(res);
1289+
}
1290+
}
1291+
1292+
#endregion
1293+
12771294
#region git_revparse_
12781295

12791296
public static GitObjectSafeHandle git_revparse_single(RepositorySafeHandle repo, string objectish)

LibGit2Sharp/Repository.cs

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -429,18 +429,6 @@ public Branch Checkout(Branch branch)
429429
return branch;
430430
}
431431

432-
private void CheckoutTreeForce(ObjectId treeId)
433-
{
434-
var opts = new GitCheckoutOpts
435-
{
436-
checkout_strategy = CheckoutStrategy.GIT_CHECKOUT_CREATE_MISSING |
437-
CheckoutStrategy.GIT_CHECKOUT_OVERWRITE_MODIFIED |
438-
CheckoutStrategy.GIT_CHECKOUT_REMOVE_UNTRACKED
439-
};
440-
GitIndexerStats stats = new GitIndexerStats();
441-
Proxy.git_checkout_tree(handle, treeId, opts, ref stats);
442-
}
443-
444432
/// <summary>
445433
/// Sets the current <see cref = "Head" /> to the specified commit and optionally resets the <see cref = "Index" /> and
446434
/// the content of the working tree to match.
@@ -451,31 +439,9 @@ public void Reset(ResetOptions resetOptions, string commitish = "HEAD")
451439
{
452440
Ensure.ArgumentNotNullOrEmptyString(commitish, "commitish");
453441

454-
if (resetOptions.Has(ResetOptions.Mixed) && Info.IsBare)
455-
{
456-
throw new BareRepositoryException("Mixed reset is not allowed in a bare repository");
457-
}
458-
459-
Commit commit = LookupCommit(commitish);
460-
461-
//TODO: Check for unmerged entries
462-
463-
string refToUpdate = Info.IsHeadDetached ? "HEAD" : Head.CanonicalName;
464-
Refs.UpdateTarget(refToUpdate, commit.Sha);
465-
466-
if (resetOptions == ResetOptions.Soft)
467-
{
468-
return;
469-
}
470-
471-
Index.ReplaceContentWithTree(commit.Tree);
472-
473-
if (resetOptions == ResetOptions.Mixed)
474-
{
475-
return;
476-
}
442+
GitObject obj = Lookup(commitish, GitObjectType.Any, LookUpOptions.ThrowWhenNoGitObjectHasBeenFound);
477443

478-
CheckoutTreeForce(commit.Tree.Id);
444+
Proxy.git_reset(handle, obj.Id, resetOptions);
479445
}
480446

481447
/// <summary>

LibGit2Sharp/ResetOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public enum ResetOptions
88
/// <summary>
99
/// Moves the branch pointed to by HEAD to the specified commit object.
1010
/// </summary>
11-
Soft,
11+
Soft = 1,
1212

1313
/// <summary>
1414
/// Moves the branch pointed to by HEAD to the specified commit object and resets the index

0 commit comments

Comments
 (0)