Skip to content

Add "pre-event" hooks#1917

Open
KirkEasterson wants to merge 21 commits intoNeogitOrg:masterfrom
KirkEasterson:kirk/hooks
Open

Add "pre-event" hooks#1917
KirkEasterson wants to merge 21 commits intoNeogitOrg:masterfrom
KirkEasterson:kirk/hooks

Conversation

@KirkEasterson
Copy link
Copy Markdown

@KirkEasterson KirkEasterson commented Mar 14, 2026

This is a POC for hooks as requested in #1909. This PR will remain a draft during the iteration and planning phase. Once an approach is agreed upon, then this PR will be cleaned up to be a candidate for merging.

I am not fluent in lua, so please be critical with coding standards.

Testing done:
I've run this locally on my own config to solve the problem from the aforementioned issue. I added the following to my neogit config:

    hooks = {
      ["PreBranchCheckout"] = function()
        local neogit = require("neogit")
        local autosession = require("auto-session")

        neogit.close()
        autosession.SaveSession(nil, false)
        neogit.open()
      end,
    },

The function body can be replaced with print("Running PreBranchCheckout") for a simple verification.

Neovim versions tested on:

  • 0.12 (nightly)
  • 0.11
  • 0.10
  • 0.9

OSes tests on:

  • arch linux
  • macos
  • windows

Remaining work:

  • add more hooks, ideally one-to-one for all events
    • PreStatusRefreshed
    • PreCommitComplete
    • PrePushComplete
    • PrePullComplete
    • PreFetchComplete
    • PreBranchCreate
    • PreBranchDelete
    • PreBranchCheckout
    • PreBranchReset
    • PreBranchRename
    • PreRebase
    • PreReset
    • PreTagCreate
    • PreTagDelete
    • PreCherryPick
    • PreMerge
    • PreStash
    • RefsRefreshed
    • DiffLoaded
    • Bisect
    • WorktreeCreate
  • update documentation
  • update tests

@KirkEasterson KirkEasterson changed the title POC for hooks, starting with PreBranchCheckout POC for hooks Mar 14, 2026
@CKolkey
Copy link
Copy Markdown
Member

CKolkey commented Mar 14, 2026

That's about what I had in mind too. I would, though, create a "hook.lua" file with a run_hook("...", {}) function that can wrap the conditional logic in one place. If there's no hook, it's just a no-op.

The second arg can be an optional table of arguments to pass into the hook function.

@KirkEasterson KirkEasterson force-pushed the kirk/hooks branch 4 times, most recently from 4de8583 to 503f93c Compare March 14, 2026 18:55
@KirkEasterson
Copy link
Copy Markdown
Author

KirkEasterson commented Mar 14, 2026

Changes since last push:

  • rebase on latest master
  • create hook.lua file with your recommendations
  • rename NeogitHooks alias to NeogitHook

@KirkEasterson
Copy link
Copy Markdown
Author

KirkEasterson commented Mar 14, 2026

I implemented the remaining events as hooks. The commits can be squashed later. I assumed separate commits would be easier to review.

@KirkEasterson KirkEasterson changed the title POC for hooks Add "pre-event" hooks Mar 14, 2026
Comment thread lua/neogit/config.lua Outdated
@KirkEasterson
Copy link
Copy Markdown
Author

I wasn't certain about where to call some hooks. Some calls can be moved a few lines while others can be moved to different functions.

@KirkEasterson KirkEasterson force-pushed the kirk/hooks branch 4 times, most recently from ceb559c to 1b82ecc Compare March 15, 2026 08:59
@KirkEasterson
Copy link
Copy Markdown
Author

I've updated the PR with documentation.

@KirkEasterson KirkEasterson marked this pull request as ready for review March 15, 2026 09:04
@KirkEasterson KirkEasterson force-pushed the kirk/hooks branch 2 times, most recently from 5590b4f to b310c20 Compare March 28, 2026 07:54
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