Skip to content

Commit e335cd0

Browse files
committed
Expose method to clear the index
1 parent c9157e4 commit e335cd0

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,5 +340,31 @@ public void CanResetIndexWithUnmergedEntriesFromTree()
340340
Assert.Equal(FileStatus.Modified, repo.Index.RetrieveStatus(testFile));
341341
}
342342
}
343+
344+
[Fact]
345+
public void CanClearTheIndex()
346+
{
347+
string path = CloneStandardTestRepo();
348+
const string testFile = "1.txt";
349+
350+
// It is sufficient to check just one of the stage area changes, such as the modified file,
351+
// to verify that the index has indeed been read from the tree.
352+
using (var repo = new Repository(path))
353+
{
354+
Assert.Equal(FileStatus.Unaltered, repo.Index.RetrieveStatus(testFile));
355+
Assert.NotEqual(0, repo.Index.Count);
356+
357+
repo.Index.Clear();
358+
Assert.Equal(0, repo.Index.Count);
359+
360+
Assert.Equal(FileStatus.Removed | FileStatus.Untracked, repo.Index.RetrieveStatus(testFile));
361+
}
362+
363+
// Check that the index was persisted to disk.
364+
using (var repo = new Repository(path))
365+
{
366+
Assert.Equal(FileStatus.Removed | FileStatus.Untracked, repo.Index.RetrieveStatus(testFile));
367+
}
368+
}
343369
}
344370
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,9 @@ internal static extern IndexReucEntrySafeHandle git_index_reuc_get_bypath(
612612
[DllImport(libgit2)]
613613
internal static extern int git_index_read_tree(IndexSafeHandle index, GitObjectSafeHandle tree);
614614

615+
[DllImport(libgit2)]
616+
internal static extern int git_index_clear(IndexSafeHandle index);
617+
615618
[DllImport(libgit2)]
616619
internal static extern int git_merge_base_many(
617620
out GitOid mergeBase,

LibGit2Sharp/Core/Proxy.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,15 @@ public static void git_index_read_fromtree(Index index, GitObjectSafeHandle tree
993993
}
994994
}
995995

996+
public static void git_index_clear(Index index)
997+
{
998+
using (ThreadAffinity())
999+
{
1000+
int res = NativeMethods.git_index_clear(index.Handle);
1001+
Ensure.ZeroResult(res);
1002+
}
1003+
}
1004+
9961005
#endregion
9971006

9981007
#region git_merge_

LibGit2Sharp/Index.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,19 @@ public virtual void Reset(Tree source)
475475
UpdatePhysicalIndex();
476476
}
477477

478+
/// <summary>
479+
/// Clears all entries the index. This is semantically equivalent to
480+
/// creating an empty tree object and resetting the index to that tree.
481+
/// <para>
482+
/// This overwrites all existing state in the staging area.
483+
/// </para>
484+
/// </summary>
485+
public virtual void Clear()
486+
{
487+
Proxy.git_index_clear(this);
488+
UpdatePhysicalIndex();
489+
}
490+
478491
private IDictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>> PrepareBatch(IEnumerable<string> leftPaths, IEnumerable<string> rightPaths)
479492
{
480493
IDictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>> dic = new Dictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>>();

0 commit comments

Comments
 (0)