Skip to content

Add "pre-event" hooks#1917

Open
KirkEasterson wants to merge 1 commit into
NeogitOrg:masterfrom
KirkEasterson:kirk/hooks
Open

Add "pre-event" hooks#1917
KirkEasterson wants to merge 1 commit into
NeogitOrg: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.13 (nightly)
  • 0.12
  • 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
    • PreBranchCheckoustate:open t
    • 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 3 times, most recently from b310c20 to adab5b1 Compare April 2, 2026 13:33
@KirkEasterson KirkEasterson force-pushed the kirk/hooks branch 2 times, most recently from e248100 to f5b32b1 Compare May 16, 2026 19:28
@KirkEasterson
Copy link
Copy Markdown
Author

Hi! I apologize for taking so long to sort out the merge conflict. I've updated the branch to fix those conflicts, but also removed the commits for the remaining events. This should make reviewing easier for you. Sorry for going overboard. I made a branch on my fork with those commits if you wish to revisit them (kirk/all-hooks).

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