Skip to content

Commit d4b3d9d

Browse files
committed
Add support of the Mixed mode to Repository.Reset()
1 parent 6faeb4a commit d4b3d9d

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

LibGit2Sharp.Tests/ResetFixture.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,32 @@ private static void FeedTheRepository(Repository repo)
127127

128128
repo.Index.RetrieveStatus().IsDirty.ShouldBeFalse();
129129
}
130+
131+
[Test]
132+
public void MixedResetRefreshesTheIndex()
133+
{
134+
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
135+
string dir = Repository.Init(scd.DirectoryPath);
136+
137+
using (var repo = new Repository(dir))
138+
{
139+
FeedTheRepository(repo);
140+
141+
Tag tag = repo.Tags["mytag"];
142+
143+
repo.Reset(ResetOptions.Mixed, tag.CanonicalName);
144+
145+
repo.Index.RetrieveStatus("a.txt").ShouldEqual(FileStatus.Modified);
146+
}
147+
}
148+
149+
[Test]
150+
public void MixedResetInABareRepositoryThrows()
151+
{
152+
using (var repo = new Repository(BareTestRepoPath))
153+
{
154+
Assert.Throws<LibGit2Exception>(() => repo.Reset(ResetOptions.Mixed, repo.Head.Tip.Sha));
155+
}
156+
}
130157
}
131158
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ public static extern int git_index_find(
155155
[DllImport(libgit2)]
156156
public static extern IntPtr git_index_get(IndexSafeHandle index, uint n);
157157

158+
[DllImport(libgit2)]
159+
public static extern int git_index_read_tree(IndexSafeHandle index, IntPtr tree);
160+
158161
[DllImport(libgit2)]
159162
public static extern int git_index_remove(IndexSafeHandle index, int n);
160163

LibGit2Sharp/Index.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,5 +523,15 @@ public RepositoryStatus RetrieveStatus()
523523
{
524524
return new RepositoryStatus(repo);
525525
}
526+
527+
internal void ReplaceContentWithTree(Tree tree)
528+
{
529+
using (var nativeTree = new ObjectSafeWrapper(tree.Id, repo))
530+
{
531+
int res = NativeMethods.git_index_read_tree(Handle, nativeTree.ObjectPtr);
532+
Ensure.Success(res);
533+
UpdatePhysicalIndex();
534+
}
535+
}
526536
}
527537
}

LibGit2Sharp/Repository.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ public void Reset(ResetOptions resetOptions, string shaOrReferenceName)
305305
{
306306
Ensure.ArgumentNotNullOrEmptyString(shaOrReferenceName, "shaOrReferenceName");
307307

308+
if (resetOptions.Has(ResetOptions.Mixed) && Info.IsBare)
309+
{
310+
throw new LibGit2Exception("Mixed reset is not allowed in a bare repository");
311+
}
312+
308313
GitObject commit = Lookup(shaOrReferenceName, GitObjectType.Any, LookUpOptions.ThrowWhenNoGitObjectHasBeenFound | LookUpOptions.DereferenceResultToCommit | LookUpOptions.ThrowWhenCanNotBeDereferencedToACommit);
309314

310315
//TODO: Check for unmerged entries
@@ -317,6 +322,13 @@ public void Reset(ResetOptions resetOptions, string shaOrReferenceName)
317322
return;
318323
}
319324

325+
Index.ReplaceContentWithTree(((Commit)commit).Tree);
326+
327+
if (resetOptions == ResetOptions.Mixed)
328+
{
329+
return;
330+
}
331+
320332
throw new NotImplementedException();
321333
}
322334
}

0 commit comments

Comments
 (0)