Skip to content

Commit afbc000

Browse files
jamillnulltoken
authored andcommitted
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 ceef2f7 commit afbc000

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

LibGit2Sharp.Tests/CleanFixture.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Linq;
2+
using LibGit2Sharp.Tests.TestHelpers;
3+
using Xunit;
4+
5+
namespace LibGit2Sharp.Tests
6+
{
7+
public class CleanFixture : BaseFixture
8+
{
9+
[Fact]
10+
public void CanCleanWorkingDirectory()
11+
{
12+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
13+
using (var repo = new Repository(path.RepositoryPath))
14+
{
15+
// Verify that there are the expected number of entries and untracked files
16+
Assert.Equal(6, repo.Index.RetrieveStatus().Count());
17+
Assert.Equal(1, repo.Index.RetrieveStatus().Untracked.Count());
18+
19+
repo.RemoveUntrackedFiles();
20+
21+
// Verify that there are the expected number of entries and 0 untracked files
22+
Assert.Equal(5, repo.Index.RetrieveStatus().Count());
23+
Assert.Equal(0, repo.Index.RetrieveStatus().Untracked.Count());
24+
}
25+
}
26+
27+
[Fact]
28+
public void CannotCleanABareRepository()
29+
{
30+
using (var repo = new Repository(BareTestRepoPath))
31+
{
32+
Assert.Throws<BareRepositoryException>(() => repo.RemoveUntrackedFiles());
33+
}
34+
}
35+
}
36+
}

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
</ItemGroup>
5959
<ItemGroup>
6060
<Compile Include="CheckoutFixture.cs" />
61+
<Compile Include="CleanFixture.cs" />
6162
<Compile Include="MetaFixture.cs" />
6263
<Compile Include="MockedRepositoryFixture.cs" />
6364
<Compile Include="ConfigurationFixture.cs" />

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/Repository.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,18 @@ private IEnumerable<Commit> RetrieveParentsOfTheCommitBeingCreated(bool amendPre
616616
return new[] { Head.Tip };
617617
}
618618

619+
/// <summary>
620+
/// Clean the working tree by removing files that are not under version control.
621+
/// </summary>
622+
public virtual void RemoveUntrackedFiles()
623+
{
624+
var options = new GitCheckoutOpts
625+
{
626+
checkout_strategy = CheckoutStrategy.GIT_CHECKOUT_REMOVE_UNTRACKED,
627+
};
628+
629+
Proxy.git_checkout_index(Handle, options);
630+
}
619631

620632
internal T RegisterForCleanup<T>(T disposable) where T : IDisposable
621633
{

0 commit comments

Comments
 (0)