diff --git a/LibGit2Sharp.Tests/IndexFixture.cs b/LibGit2Sharp.Tests/IndexFixture.cs
index 1b401bc7a..3f5d5c41b 100644
--- a/LibGit2Sharp.Tests/IndexFixture.cs
+++ b/LibGit2Sharp.Tests/IndexFixture.cs
@@ -340,5 +340,31 @@ public void CanResetIndexWithUnmergedEntriesFromTree()
Assert.Equal(FileStatus.Modified, repo.Index.RetrieveStatus(testFile));
}
}
+
+ [Fact]
+ public void CanClearTheIndex()
+ {
+ string path = CloneStandardTestRepo();
+ const string testFile = "1.txt";
+
+ // It is sufficient to check just one of the stage area changes, such as the modified file,
+ // to verify that the index has indeed been read from the tree.
+ using (var repo = new Repository(path))
+ {
+ Assert.Equal(FileStatus.Unaltered, repo.Index.RetrieveStatus(testFile));
+ Assert.NotEqual(0, repo.Index.Count);
+
+ repo.Index.Clear();
+ Assert.Equal(0, repo.Index.Count);
+
+ Assert.Equal(FileStatus.Removed | FileStatus.Untracked, repo.Index.RetrieveStatus(testFile));
+ }
+
+ // Check that the index was persisted to disk.
+ using (var repo = new Repository(path))
+ {
+ Assert.Equal(FileStatus.Removed | FileStatus.Untracked, repo.Index.RetrieveStatus(testFile));
+ }
+ }
}
}
diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs
index 826c94635..24f704fec 100644
--- a/LibGit2Sharp/Core/NativeMethods.cs
+++ b/LibGit2Sharp/Core/NativeMethods.cs
@@ -612,6 +612,9 @@ internal static extern IndexReucEntrySafeHandle git_index_reuc_get_bypath(
[DllImport(libgit2)]
internal static extern int git_index_read_tree(IndexSafeHandle index, GitObjectSafeHandle tree);
+ [DllImport(libgit2)]
+ internal static extern int git_index_clear(IndexSafeHandle index);
+
[DllImport(libgit2)]
internal static extern int git_merge_base_many(
out GitOid mergeBase,
diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs
index 8040594ea..40b757330 100644
--- a/LibGit2Sharp/Core/Proxy.cs
+++ b/LibGit2Sharp/Core/Proxy.cs
@@ -993,6 +993,15 @@ public static void git_index_read_fromtree(Index index, GitObjectSafeHandle tree
}
}
+ public static void git_index_clear(Index index)
+ {
+ using (ThreadAffinity())
+ {
+ int res = NativeMethods.git_index_clear(index.Handle);
+ Ensure.ZeroResult(res);
+ }
+ }
+
#endregion
#region git_merge_
diff --git a/LibGit2Sharp/Index.cs b/LibGit2Sharp/Index.cs
index aacdef59d..89bd9b156 100644
--- a/LibGit2Sharp/Index.cs
+++ b/LibGit2Sharp/Index.cs
@@ -475,6 +475,19 @@ public virtual void Reset(Tree source)
UpdatePhysicalIndex();
}
+ ///
+ /// Clears all entries the index. This is semantically equivalent to
+ /// creating an empty tree object and resetting the index to that tree.
+ ///
+ /// This overwrites all existing state in the staging area.
+ ///
+ ///
+ public virtual void Clear()
+ {
+ Proxy.git_index_clear(this);
+ UpdatePhysicalIndex();
+ }
+
private IDictionary, Tuple> PrepareBatch(IEnumerable leftPaths, IEnumerable rightPaths)
{
IDictionary, Tuple> dic = new Dictionary, Tuple>();