Skip to content
Browse files

Add support of the Mixed mode to Repository.Reset()

  • Loading branch information...
1 parent 6faeb4a commit d4b3d9d089504e5173bfbdaa78dd3f9dc3af813a @nulltoken nulltoken committed Jan 6, 2012
Showing with 52 additions and 0 deletions.
  1. +27 −0 LibGit2Sharp.Tests/ResetFixture.cs
  2. +3 −0 LibGit2Sharp/Core/NativeMethods.cs
  3. +10 −0 LibGit2Sharp/Index.cs
  4. +12 −0 LibGit2Sharp/Repository.cs
View
27 LibGit2Sharp.Tests/ResetFixture.cs
@@ -127,5 +127,32 @@ private static void FeedTheRepository(Repository repo)
repo.Index.RetrieveStatus().IsDirty.ShouldBeFalse();
}
+
+ [Test]
+ public void MixedResetRefreshesTheIndex()
+ {
+ SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
+ string dir = Repository.Init(scd.DirectoryPath);
+
+ using (var repo = new Repository(dir))
+ {
+ FeedTheRepository(repo);
+
+ Tag tag = repo.Tags["mytag"];
+
+ repo.Reset(ResetOptions.Mixed, tag.CanonicalName);
+
+ repo.Index.RetrieveStatus("a.txt").ShouldEqual(FileStatus.Modified);
+ }
+ }
+
+ [Test]
+ public void MixedResetInABareRepositoryThrows()
+ {
+ using (var repo = new Repository(BareTestRepoPath))
+ {
+ Assert.Throws<LibGit2Exception>(() => repo.Reset(ResetOptions.Mixed, repo.Head.Tip.Sha));
+ }
+ }
}
}
View
3 LibGit2Sharp/Core/NativeMethods.cs
@@ -156,6 +156,9 @@ static NativeMethods()
public static extern IntPtr git_index_get(IndexSafeHandle index, uint n);
[DllImport(libgit2)]
+ public static extern int git_index_read_tree(IndexSafeHandle index, IntPtr tree);
+
+ [DllImport(libgit2)]
public static extern int git_index_remove(IndexSafeHandle index, int n);
[DllImport(libgit2)]
View
10 LibGit2Sharp/Index.cs
@@ -523,5 +523,15 @@ public RepositoryStatus RetrieveStatus()
{
return new RepositoryStatus(repo);
}
+
+ internal void ReplaceContentWithTree(Tree tree)
+ {
+ using (var nativeTree = new ObjectSafeWrapper(tree.Id, repo))
+ {
+ int res = NativeMethods.git_index_read_tree(Handle, nativeTree.ObjectPtr);
+ Ensure.Success(res);
+ UpdatePhysicalIndex();
+ }
+ }
}
}
View
12 LibGit2Sharp/Repository.cs
@@ -305,6 +305,11 @@ public void Reset(ResetOptions resetOptions, string shaOrReferenceName)
{
Ensure.ArgumentNotNullOrEmptyString(shaOrReferenceName, "shaOrReferenceName");
+ if (resetOptions.Has(ResetOptions.Mixed) && Info.IsBare)
+ {
+ throw new LibGit2Exception("Mixed reset is not allowed in a bare repository");
+ }
+
GitObject commit = Lookup(shaOrReferenceName, GitObjectType.Any, LookUpOptions.ThrowWhenNoGitObjectHasBeenFound | LookUpOptions.DereferenceResultToCommit | LookUpOptions.ThrowWhenCanNotBeDereferencedToACommit);
//TODO: Check for unmerged entries
@@ -317,6 +322,13 @@ public void Reset(ResetOptions resetOptions, string shaOrReferenceName)
return;
}
+ Index.ReplaceContentWithTree(((Commit)commit).Tree);
+
+ if (resetOptions == ResetOptions.Mixed)
+ {
+ return;
+ }
+
throw new NotImplementedException();
}
}

0 comments on commit d4b3d9d

Please sign in to comment.
Something went wrong with that request. Please try again.