Skip to content

Commit 34a27ba

Browse files
committed
Implement git clean
This implements git clean and exposes the ability to remove untracked files from the working directory. If / when git clean functionality is exposed directly by libgit2, we can update this to use that entry point. Currently, we call git_checkout_index with a flags indicating that untracked entries should be removed. This does not support other flags exposed by core Git, such as removing ignored files.
1 parent dc6f52a commit 34a27ba

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@ public void ReadIndexWithBadParamsFails()
8282
}
8383
}
8484

85+
[Fact]
86+
public void CanCleanWorkingDirectory()
87+
{
88+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
89+
using (var repo = new Repository(path.RepositoryPath))
90+
{
91+
// Verify that there are the expected number of entries and untracked files
92+
Assert.Equal(6, repo.Index.RetrieveStatus().Count());
93+
Assert.Equal(1, repo.Index.RetrieveStatus().Untracked.Count());
94+
95+
repo.Index.CleanWorkingDirectory();
96+
97+
// Verify that there are the expected number of entries and 0 untracked files
98+
Assert.Equal(5, repo.Index.RetrieveStatus().Count());
99+
Assert.Equal(0, repo.Index.RetrieveStatus().Untracked.Count());
100+
}
101+
}
102+
85103
[Theory]
86104
[InlineData("1/branch_file.txt", FileStatus.Unaltered, true, FileStatus.Unaltered, true, 0)]
87105
[InlineData("README", FileStatus.Unaltered, true, FileStatus.Unaltered, true, 0)]

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ internal static extern int git_checkout_head(
157157
RepositorySafeHandle repo,
158158
GitCheckoutOpts opts);
159159

160+
[DllImport(libgit2)]
161+
internal static extern int git_checkout_index(
162+
RepositorySafeHandle repo,
163+
GitCheckoutOpts opts);
164+
160165
[DllImport(libgit2)]
161166
internal static extern int git_clone(
162167
out RepositorySafeHandle repo,

LibGit2Sharp/Core/Proxy.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ public static void git_checkout_head(RepositorySafeHandle repo, GitCheckoutOpts
188188
}
189189
}
190190

191+
public static void git_checkout_index(RepositorySafeHandle repo, GitCheckoutOpts opts)
192+
{
193+
using (ThreadAffinity())
194+
{
195+
int res = NativeMethods.git_checkout_index(repo, opts);
196+
Ensure.Success(res);
197+
}
198+
}
199+
191200
#endregion
192201

193202
#region git_clone_

LibGit2Sharp/Index.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,19 @@ public virtual void Remove(IEnumerable<string> paths)
338338
UpdatePhysicalIndex();
339339
}
340340

341+
/// <summary>
342+
/// Clean the working tree by removing files that are not under version control.
343+
/// </summary>
344+
public virtual void CleanWorkingDirectory()
345+
{
346+
GitCheckoutOpts options = new GitCheckoutOpts
347+
{
348+
checkout_strategy = CheckoutStrategy.GIT_CHECKOUT_REMOVE_UNTRACKED,
349+
};
350+
351+
Proxy.git_checkout_index(this.repo.Handle, options);
352+
}
353+
341354
private IEnumerable<KeyValuePair<string, FileStatus>> PrepareBatch(IEnumerable<string> paths)
342355
{
343356
Ensure.ArgumentNotNull(paths, "paths");

0 commit comments

Comments
 (0)