Skip to content

feat: add preRemoveCmd lifecycle hook#50

Merged
jackiotyu merged 2 commits intojackiotyu:mainfrom
danielrose7:feat/pre-remove-cmd
Apr 14, 2026
Merged

feat: add preRemoveCmd lifecycle hook#50
jackiotyu merged 2 commits intojackiotyu:mainfrom
danielrose7:feat/pre-remove-cmd

Conversation

@danielrose7
Copy link
Copy Markdown
Contributor

Summary

Adds a git-worktree-manager.preRemoveCmd setting that mirrors the existing postCreateCmd, but runs before a worktree is removed. If the command fails or is cancelled, removal is aborted.

Motivation

When several worktrees run at once, they'd clash in the database. We give each worktree its own Postgres schema to keep them separate. Dropping the worktree without first dropping its schema leaves junk behind. A pre-remove hook lets a cleanup script run before the folder is deleted, and cancels the removal if cleanup fails.

Behavior

  • Setting key: git-worktree-manager.preRemoveCmd (string, default "")
  • Runs inside the worktree directory (cwd = worktreePath)
  • Same variables as postCreateCmd:
    • $BASE_PATH — main repo path
    • $WORKTREE_PATH — worktree path being removed
  • On failure or cancellation: an error is shown via the existing Alert.showErrorMessage path and git worktree remove is not called — no partial state.
  • Implementation mirrors postCreateWorktree.ts (same exec + AbortController + progress wrapper) for consistency.

Files changed

  • src/core/util/preRemoveWorktree.ts — new (mirrors postCreateWorktree.ts)
  • src/core/command/removeWorktreeCmd.ts — invokes hook before removeWorktree()
  • src/core/config/setting.ts — typed config accessor
  • package.json — config schema entry
  • package.nls.json, package.nls.zh-cn.json, package.nls.zh-tw.json, package.nls.ja.json — description strings (happy to have native speakers polish zh/ja — I matched the existing style)
  • README.md, README.zh-CN.md — docs entry next to postCreateCmd

Design notes

  • postRemoveCmd is intentionally not added. Once the worktree directory is gone, a post-remove hook has no natural cwd and its variables refer to a deleted path. Happy to add it later with cwd = mainFolder if there's demand.
  • On pre-remove failure the command aborts outright (no "Remove anyway" prompt), matching the existing error-path UX. Open to changing if preferred.

Test plan

  • pnpm run check-types passes
  • pnpm run lint passes
  • Manual: with preRemoveCmd unset, removal behaves exactly as before
  • Manual: with preRemoveCmd = "echo hi > /tmp/pre.log", file is created with cwd = worktree and removal proceeds
  • Manual: with preRemoveCmd = "exit 1", removal is aborted and error is shown
  • Manual: $WORKTREE_PATH / $BASE_PATH substitute correctly

Mirrors the postCreateCmd pattern but runs before worktree removal.
The command executes inside the worktree directory and supports the
same $BASE_PATH / $WORKTREE_PATH variables. If it fails or is
cancelled, removal is aborted so users can react to cleanup errors
(for example, tearing down a per-worktree database).
@jackiotyu
Copy link
Copy Markdown
Owner

@danielrose7 Hi, thanks for the PR—nice work! It's just missing some translations. Could you please add those? I'll merge it right after you're done.

Comment thread src/core/util/preRemoveWorktree.ts
Comment thread src/core/util/preRemoveWorktree.ts
Add zh-cn, zh-tw, and ja translations for the three new
vscode.l10n.t() strings in preRemoveWorktree.ts.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@danielrose7
Copy link
Copy Markdown
Contributor Author

@jackiotyu Added translations for all three new vscode.l10n.t() strings in preRemoveWorktree.ts across all locale files (zh-cn, zh-tw, ja):

  • Running pre-remove command...
  • Pre-remove command was cancelled
  • Pre-remove command failed: {0}

These mirror the existing Running post-create command... pattern. Ready for re-review!

@danielrose7 danielrose7 requested a review from jackiotyu April 14, 2026 16:55
Copy link
Copy Markdown
Owner

@jackiotyu jackiotyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jackiotyu jackiotyu merged commit dbf8155 into jackiotyu:main Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants