Skip to content

Commit 3a54043

Browse files
sunshinecogitster
authored andcommitted
worktree: delete .git/worktrees if empty after 'remove'
For cleanliness, "git worktree prune" deletes the .git/worktrees directory if it is empty after pruning is complete. For consistency, make "git worktree remove <path>" likewise delete .git/worktrees if it is empty after the removal. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f414310 commit 3a54043

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

builtin/worktree.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ static int delete_git_dir(const char *id)
6262
return ret;
6363
}
6464

65+
static void delete_worktrees_dir_if_empty(void)
66+
{
67+
rmdir(git_path("worktrees")); /* ignore failed removal */
68+
}
69+
6570
static int prune_worktree(const char *id, struct strbuf *reason)
6671
{
6772
struct stat st;
@@ -149,7 +154,7 @@ static void prune_worktrees(void)
149154
}
150155
closedir(dir);
151156
if (!show_only)
152-
rmdir(git_path("worktrees"));
157+
delete_worktrees_dir_if_empty();
153158
strbuf_release(&reason);
154159
}
155160

@@ -918,6 +923,7 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
918923
* from here.
919924
*/
920925
ret |= delete_git_dir(wt->id);
926+
delete_worktrees_dir_if_empty();
921927

922928
free_worktrees(worktrees);
923929
return ret;

t/t2028-worktree-move.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,16 @@ test_expect_success 'remove locked worktree (force)' '
173173
git worktree remove --force --force gumby
174174
'
175175

176+
test_expect_success 'remove cleans up .git/worktrees when empty' '
177+
git init moog &&
178+
(
179+
cd moog &&
180+
test_commit bim &&
181+
git worktree add --detach goom &&
182+
test_path_exists .git/worktrees &&
183+
git worktree remove goom &&
184+
test_path_is_missing .git/worktrees
185+
)
186+
'
187+
176188
test_done

0 commit comments

Comments
 (0)