Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make repo.Reset() accept ResetOptions.Hard

  • Loading branch information...
commit 0580f3688eaf128c484675eb67c8880a1d45c9f8 1 parent 815086d
@nulltoken nulltoken authored
View
34 LibGit2Sharp.Tests/ResetHeadFixture.cs
@@ -1,5 +1,6 @@
using System;
using System.IO;
+using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;
@@ -152,5 +153,38 @@ public void MixedResetInABareRepositoryThrows()
Assert.Throws<BareRepositoryException>(() => repo.Reset(ResetOptions.Mixed));
}
}
+
+ [Fact]
+ public void HardResetInABareRepositoryThrows()
+ {
+ using (var repo = new Repository(BareTestRepoPath))
+ {
+ Assert.Throws<BareRepositoryException>(() => repo.Reset(ResetOptions.Hard));
+ }
+ }
+
+ [Fact]
+ public void HardResetUpdatesTheContentOfTheWorkingDirectory()
+ {
+ var clone = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
+
+ using (var repo = new Repository(clone.DirectoryPath))
+ {
+ var names = new DirectoryInfo(repo.Info.WorkingDirectory).GetFileSystemInfos().Select(fsi => fsi.Name).ToList();
+ names.Sort(StringComparer.Ordinal);
+
+ File.Delete(Path.Combine(repo.Info.WorkingDirectory, "README"));
+ File.WriteAllText(Path.Combine(repo.Info.WorkingDirectory, "WillBeRemoved.txt"), "content\n");
+
+ Assert.True(names.Count > 4);
+
+ repo.Reset(ResetOptions.Hard, "HEAD~3");
+
+ names = new DirectoryInfo(repo.Info.WorkingDirectory).GetFileSystemInfos().Select(fsi => fsi.Name).ToList();
+ names.Sort(StringComparer.Ordinal);
+
+ Assert.Equal(new[] { ".git", "README", "branch_file.txt", "new.txt" }, names);
+ }
+ }
}
}
View
2  LibGit2Sharp/LibGit2Sharp.csproj
@@ -68,6 +68,8 @@
<Compile Include="Configuration.cs" />
<Compile Include="ConfigurationEntry.cs" />
<Compile Include="ContentChanges.cs" />
+ <Compile Include="Core\GitCheckoutOpts.cs" />
+ <Compile Include="Core\GitIndexerStats.cs" />
<Compile Include="Core\Proxy.cs" />
<Compile Include="ReferenceCollectionExtensions.cs" />
<Compile Include="TagCollectionExtensions.cs" />
View
16 LibGit2Sharp/Repository.cs
@@ -429,6 +429,18 @@ public Branch Checkout(Branch branch)
return branch;
}
+ private void CheckoutTreeForce(ObjectId treeId)
+ {
+ var opts = new GitCheckoutOpts
+ {
+ checkout_strategy = CheckoutStrategy.GIT_CHECKOUT_CREATE_MISSING |
+ CheckoutStrategy.GIT_CHECKOUT_OVERWRITE_MODIFIED |
+ CheckoutStrategy.GIT_CHECKOUT_REMOVE_UNTRACKED
+ };
+
+ Proxy.git_checkout_tree(handle, treeId, opts, null);
+ }
+
/// <summary>
/// Sets the current <see cref = "Head" /> to the specified commit and optionally resets the <see cref = "Index" /> and
/// the content of the working tree to match.
@@ -437,7 +449,7 @@ public Branch Checkout(Branch branch)
/// <param name = "commitish">A revparse spec for the target commit object.</param>
public void Reset(ResetOptions resetOptions, string commitish = "HEAD")
{
- Ensure.ArgumentNotNullOrEmptyString(commitish, "commitOrBranchSpec");
+ Ensure.ArgumentNotNullOrEmptyString(commitish, "commitish");
if (resetOptions.Has(ResetOptions.Mixed) && Info.IsBare)
{
@@ -463,7 +475,7 @@ public void Reset(ResetOptions resetOptions, string commitish = "HEAD")
return;
}
- throw new NotImplementedException();
+ CheckoutTreeForce(commit.Tree.Id);
}
/// <summary>
View
7 LibGit2Sharp/ResetOptions.cs
@@ -15,5 +15,12 @@ public enum ResetOptions
/// to the tree recorded by the commit.
/// </summary>
Mixed,
+
+ /// <summary>
+ /// Moves the branch pointed to by HEAD to the specified commit object, resets the index
+ /// to the tree recorded by the commit and updates the working directory to match the content
+ /// of the index.
+ /// </summary>
+ Hard,
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.