Skip to content

feat: add on_create, on_delete, and on_switch lifecycle hooks#7

Open
Stan15 wants to merge 3 commits intoafonsofrancof:mainfrom
Stan15:feat/lifecycle-hooks
Open

feat: add on_create, on_delete, and on_switch lifecycle hooks#7
Stan15 wants to merge 3 commits intoafonsofrancof:mainfrom
Stan15:feat/lifecycle-hooks

Conversation

@Stan15
Copy link
Contributor

@Stan15 Stan15 commented Mar 11, 2026

Summary

Add lifecycle hooks: on_create, on_delete, on_switch

Adds optional callback hooks to the setup config that fire after worktree lifecycle events. This is very for automating environment setup that can't be shared across worktrees - e.g. symlinking .env files, running build scripts, e.t.c.

Usage:

require("worktrees").setup({
    on_create = function(path)
        -- called after a worktree is created
    end,
    on_delete = function(path)
        -- called after a worktree is deleted
    end,
    on_switch = function(from_path, to_path)
        -- called after switching worktrees
    end,
})

Details:

  • All hooks are optional (nil by default)
  • on_switch receives the path switched from and the path switched to
  • When a newly created worktree is auto-switched to, on_create fires before on_switch
  • Documented in both README and vimdoc

@afonsofrancof
Copy link
Owner

afonsofrancof commented Mar 12, 2026

Hi!
Thanks for contributing!
I like this idea very much.
One small thing though. I would prefer if "from_path" actually returned nil if the current worktree root isn't found, instead of "(root)".
This signifies that we didn't switch from a worktree, but from somewhere else.
Other than that LGTM.

@Stan15
Copy link
Contributor Author

Stan15 commented Mar 13, 2026

Thanks for the feedback, @afonsofrancof! I can make that change and push out an update in a sec 👍

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