diff --git a/internal/gitwt/gitwt_remove.go b/internal/gitwt/gitwt_remove.go index a86565a..9d63305 100644 --- a/internal/gitwt/gitwt_remove.go +++ b/internal/gitwt/gitwt_remove.go @@ -123,6 +123,9 @@ func (x *removeCommandOptions) removeWorktree(command *cobra.Command, name strin if _, err := repository.git(removeArguments...); err != nil { return err } + if err := removeEmptyParents(worktree.Path, worktreeRoot(repository.WorkTree)); err != nil { + return err + } branchExists, err := repository.branchStillExists(worktree.BranchReference) if err != nil { diff --git a/internal/gitwt/gitwt_test.go b/internal/gitwt/gitwt_test.go index 63423da..93aea96 100644 --- a/internal/gitwt/gitwt_test.go +++ b/internal/gitwt/gitwt_test.go @@ -412,6 +412,23 @@ func TestCreateFailsWhenDirectoryExists(t *testing.T) { } } +func TestRemoveRemovesEmptyParentDirectories(t *testing.T) { + const branchName = "feature/nested" + + testRepository := newTestRepository(t) + testRepository.runGitWT(t, "create", branchName) + testRepository.mergeWorktreeBranch(t, branchName) + + result := testRepository.runGitWT(t, "remove", branchName) + if result.err != nil { + t.Fatalf("remove failed: %v\n%s", result.err, result.stderr) + } + + testRepository.assertPathMissing(t, filepath.Join(testRepository.rootPath, "feature", "nested")) + testRepository.assertPathMissing(t, filepath.Join(testRepository.rootPath, "feature")) + testRepository.assertPathPresent(t, testRepository.rootPath) +} + func TestRemoveFailsWhenDirtyWithoutForce(t *testing.T) { const branchName = "feature/dirty" const dirtyFileName = "dirty.txt"