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 <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Aug 28, 2018 at 17:20 UTC 3a5404333c6c8b5897e78c9dcb711d623035791b
2 files changed +19 -1
builtin/worktree.c
+7 -1
@@ -62,6 +62,11 @@ static int delete_git_dir(const char *id)
62 return ret;
63 }
64
65 +static void delete_worktrees_dir_if_empty(void)
66 +{
67 + rmdir(git_path("worktrees")); /* ignore failed removal */
68 +}
69 +
70 static int prune_worktree(const char *id, struct strbuf *reason)
71 {
72 struct stat st;
@@ -149,7 +154,7 @@ static void prune_worktrees(void)
154 }
155 closedir(dir);
156 if (!show_only)
152 - rmdir(git_path("worktrees"));
157 + delete_worktrees_dir_if_empty();
158 strbuf_release(&reason);
159 }
160
@@ -918,6 +923,7 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
923 * from here.
924 */
925 ret |= delete_git_dir(wt->id);
926 + delete_worktrees_dir_if_empty();
927
928 free_worktrees(worktrees);
929 return ret;
t/t2028-worktree-move.sh
+12
@@ -173,4 +173,16 @@ test_expect_success 'remove locked worktree (force)' '
173 git worktree remove --force --force gumby
174 '
175
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 +
188 test_done