Skip to content

Commit 82e42a0

Browse files
committed
Issue-1471 Added the ability to prune already deleted repositories. Fixed test data to correctly reference the worktree's working directories
1 parent b895106 commit 82e42a0

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../../worktrees/i-do-numbers/.git
1+
../../../../worktrees/i-do-numbers/.git
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../../worktrees/logo/.git
1+
../../../../worktrees/logo/.git

LibGit2Sharp.Tests/WorktreeFixture.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,29 @@ public void CanPruneUnlockedWorktree()
154154
}
155155
}
156156

157+
[Fact]
158+
public void CanPruneDeletedWorktree()
159+
{
160+
var repoPath = SandboxWorktreeTestRepo();
161+
using (var repo = new Repository(repoPath))
162+
{
163+
Assert.Equal(2, repo.Worktrees.Count());
164+
var repoPath2 = repo.Info.Path;
165+
var repoWd = repo.Info.WorkingDirectory;
166+
// unlocked
167+
var worktreeUnlocked = repo.Worktrees["i-do-numbers"];
168+
Assert.Equal("i-do-numbers", worktreeUnlocked.Name);
169+
Assert.False(worktreeUnlocked.IsLocked);
170+
var info = worktreeUnlocked.WorktreeRepository.Info;
171+
172+
Directory.Delete(info.WorkingDirectory, true);
173+
174+
Assert.True(repo.Worktrees.Prune(worktreeUnlocked));
175+
176+
Assert.Single(repo.Worktrees);
177+
}
178+
}
179+
157180
[Fact]
158181
public void CanNotPruneLockedWorktree()
159182
{

LibGit2Sharp/WorktreeCollection.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -131,29 +131,19 @@ public virtual bool Prune(Worktree worktree, bool ifLocked)
131131
{
132132
using (var handle = worktree.GetWorktreeHandle())
133133
{
134-
using (var repository = worktree.WorktreeRepository)
134+
git_worktree_prune_options options = new git_worktree_prune_options
135+
{
136+
version = 1,
137+
// default
138+
flags = GitWorktreePruneOptionFlags.GIT_WORKTREE_PRUNE_WORKING_TREE | GitWorktreePruneOptionFlags.GIT_WORKTREE_PRUNE_VALID
139+
};
140+
141+
if (ifLocked)
135142
{
136-
string wd = repository.Info.WorkingDirectory;
137-
138-
if (!Directory.Exists(wd))
139-
{
140-
return false;
141-
}
142-
143-
git_worktree_prune_options options = new git_worktree_prune_options
144-
{
145-
version = 1,
146-
// default
147-
flags = GitWorktreePruneOptionFlags.GIT_WORKTREE_PRUNE_WORKING_TREE | GitWorktreePruneOptionFlags.GIT_WORKTREE_PRUNE_VALID
148-
};
149-
150-
if (ifLocked)
151-
{
152-
options.flags |= GitWorktreePruneOptionFlags.GIT_WORKTREE_PRUNE_LOCKED;
153-
}
154-
155-
return Proxy.git_worktree_prune(handle, options);
143+
options.flags |= GitWorktreePruneOptionFlags.GIT_WORKTREE_PRUNE_LOCKED;
156144
}
145+
146+
return Proxy.git_worktree_prune(handle, options);
157147
}
158148
}
159149

0 commit comments

Comments
 (0)