Skip to content

feat(hooks): run post-checkout, post-merge and post-rewrite hooks - #87

Merged
cheat2001 merged 50 commits into
cheat2001:mainfrom
ChrisLauinger77:feat/post-hooks
Nov 12, 2026
Merged

cheat2001 merged 50 commits into
cheat2001:mainfrom
ChrisLauinger77:feat/post-hooks

Conversation

@ChrisLauinger77

@ChrisLauinger77 ChrisLauinger77 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

What & why

Third and last part of #82. libgit2 never runs hooks, so until now every checkout, merge, pull, rebase and amend in AngKorGit skipped post-checkout, post-merge and post-rewrite, which tools like Husky, lefthook and git-lfs rely on (reinstalling dependencies after a checkout, re-mapping notes after a rebase, and so on). This PR runs them after every operation that runs them in git, with the same arguments and stdin. It reuses the runner from #84 (git hook run, PATH from ai_cli::search_path, proc::hidden, 600 s timeout that stops the whole process tree) and the stdin path from #85.

As in git, a post-hook never blocks or undoes anything. A non-zero exit becomes a warning with the hook name, exit code and output, never an error.

It also implements the group pull rule approved in #82 (#82 (comment)), so it touches #69's group pull.

Closes #82. Refs #69.

Which operations run which hook

I probed every row against git 2.56 with logging hooks rather than taking it from git help hooks, because the docs are incomplete. For example, git restore runs post-checkout too.

App operation Closest CLI Hook(s)
Check out a branch / remote branch / detached commit, create + check out, PR checkout git switch, git checkout -B post-checkout <old> <new> 1
Clone, add worktree git clone, git worktree add post-checkout 000… <HEAD> 1 (inside the new worktree; none for an empty repo)
Merge (fast-forward or merge commit), Pull (merge mode, or rebase mode when only behind), sidebar Pull of the HEAD branch git merge, git pull post-merge 0 (not when up to date or conflicted); ORIG_HEAD is set first, as git merge does
Pull with rebase, diverged git pull --rebase post-checkout <orig> <onto> 1, then post-rewrite rebase; no post-merge
Rebase / continue a paused rebase git rebase, --continue post-checkout <orig> <onto> 1 right after onto is checked out (before any commit is replayed), post-rewrite rebase once with every rewritten commit (including the ones from before the pause); nothing when the branch already sits linearly on top of its upstream (git's "up to date")
Interactive rebase, reword of an earlier commit git rebase -i post-checkout <orig> <result> 1, post-rewrite rebase (squash/fixup map to the combined commit, dropped commits left out)
Amend, reword HEAD git commit --amend post-rewrite amend, stdin <old> <new>
Discard (unstaged, staged, all, or several selected files), apply files from a stash git restore [-SW] [--source=…] post-checkout <HEAD> <HEAD> 0, once per command (a multi-file discard and Discard all staged changes try every file, report the ones they could not discard, and run the hook once afterwards when any file was restored)
Discard an untracked file, discard a hunk or line git clean, git restore -p none
Stash push/apply/pop, reset, cherry-pick, revert, merge/rebase abort, pull a non-HEAD branch none
Group Pull all (fast-forward only) git pull --ff-only post-merge 0 per repository that moved

Decisions worth a look

  • Skip hooks also skips post-hooks. This differs from git: --no-verify never skips a post-hook (git commit --amend --no-verify still runs post-rewrite). The issue's spec and the approved group pull rule ask for Skip hooks to mean "no hooks". So:

    • the commit box's Skip hooks also skips post-rewrite when amending;
    • the Pull menu gets Pull without hooks;
    • the group menu gets Pull all without hooks.

    Each appears when a matching hook exists, or when the hooks live in the worktree, because a pull can bring in a new hook there that runs right after the fast-forward. "Live in the worktree" means that locating the hook passes through the worktree at any step. The hook path is walked component by component the way the filesystem resolves it: each link is replaced by its target before a following .. applies. A step counts as inside when it sits strictly below the worktree and outside .git, compared by file identity (same-file, already in Cargo.lock) rather than by path text, so letter case and /var vs /private/var can't hide a match. Examples: core.hooksPath=.husky (even when .husky is a tracked link to an empty folder outside, which a pull can replace with a real folder), .git/hooks linked to a folder there, .git/hooks/post-merge -> ../../scripts/post-merge, proxy/../.husky/post-merge with proxy linked into the worktree, or a chain through a link outside, dangling or not. The same applies to Git config: if .git/config (or the global, XDG or system config) includes a file that lives in the worktree, the opt-out shows too. Includes are followed file by file through include.path and includeIf.*.path, because a tracked project.gitconfig can gain core.hooksPath=.husky in the same pull that adds .husky/post-merge. If the walk reaches its limit of 64 files with more still queued, the opt-out shows: an unfinished scan never hides it. The toolbar's Pull menu checks again every time it opens, because the watcher ignores .git/config. Checkout, merge, rebase, discard, clone and worktree have no skip option, which matches git.

  • Git's per-step post-rewrite amend inside rebase -i is not reproduced. Git's sequencer fires it for each squash, fixup and reword step. The app builds the rebase in memory, and a reword has no intermediate commit to report. The single post-rewrite rebase carries every pair.

  • The interactive rebase's post-checkout names the result, not onto. Git passes <orig> <onto> because it checks out onto before replaying. The app's interactive rebase never checks out onto: it builds the chain in memory and moves the worktree once, from orig to the result. So $2 names the commit the worktree was actually checked out to, which is what HEAD is when the hook runs.

  • Skip hooks survives a paused rebase. A rebase started with hooks skipped (Pull without hooks with pull.rebase) writes a marker file into .git/rebase-merge right after the rebase starts, before any commit is replayed, so it is there however the replay stops (a conflict, or an untracked file in the way). Continue rebase reads it. Git and libgit2 ignore the file and delete it with the folder.

  • Two behaviour changes outside hooks, both matching git:

    • Rebasing a branch that already sits linearly on its upstream now reports "Already up to date". libgit2 used to re-create those commits with new commit dates. Uncommitted changes or an operation in progress are refused first, as before. Untracked files inside a submodule don't count as changes, matching libgit2's own rebase check. The interactive rebase and rewording an older commit use each submodule's configured ignore rule instead, so ignore = dirty lets an edited submodule through there. libgit2's status list doesn't apply that rule, so the checks ask each submodule directly.
    • Fast-forward merges and Pull all now write ORIG_HEAD, through libgit2's reference database. That takes ORIG_HEAD.lock and fails rather than overwrite the pointer while another Git process holds the lock.
  • Timeouts are reported as timeouts. A hook that runs past the 10-minute limit is stopped, and the warning says it timed out and includes what it printed so far. It doesn't say the hook never ran.

  • Pull without hooks still asks first. It goes through the same conflict check dialog as the plain Pull when the preview predicts conflicts or blocking edits.

  • Warnings survive a failed operation. A rebase's post-checkout runs before the replay, so the rebase and pull wrappers return their warnings next to the result. A failing hook is still reported when the replay then fails.

  • Warnings travel as a hook-warning event. The hooked commands keep their return types, and one host shows a warning toast with Show output, which opens the hook dialog from feat(commit): run pre-commit and commit-msg hooks on commit and amend #84 without a retry button. The toast appears 600 ms after the event, so the operation's own "done" toast doesn't cover it.

  • The engine functions keep their signatures. core/post_hooks.rs holds one wrapper per command. It runs the hook after submodule::synced, so a hook sees the final state, submodules included. The rebase pairs come from libgit2's rebase-merge/rewritten, read before finish() deletes it.

Group pull and Cancel

post-merge runs one repository at a time, only where the branch fast-forwarded. A failure shows as a warning with its output on that repository's row (the run keeps the Updated status, and the counts line adds "n hook warnings"). The other repositories still update.

The hook runs inside the same locked section as the fast-forward, so the two are one unit. That includes processes the hook started in the background: on macOS and Linux the lock is released only once nothing from the hook's process group is still running, and anything left at the hook deadline is stopped. Windows has no process groups, so there git runs inside a job object: processes it starts join the job, the lock is released once the job holds no running process, and the job is terminated at the deadline. The Windows part is covered by two Windows-only runner tests and the cross-platform group pull test, so the Windows CI job is what checks it.

  • Cancel before the fast-forward means neither runs.
  • Cancel during the hook waits for it, the same way it already waits for the checkout, bounded by the hook timeout.

So there is never a fast-forward whose hook was silently skipped, and never a hook still writing after Cancel was acknowledged. The receipt Cancel returns includes the hook's result. group_pull_matches_toolbar_pull_with_active_hooks now asserts that both pulls run post-merge 0 and neither runs post-checkout.

Not in this PR

  • A libgit2 rebase limitation with submodules, found while testing this. When onto moves a submodule's recorded commit, libgit2's rebase.commit() fails with "unstaged changes exist in workdir", because the replay never updates the submodule folder; git rebase ignores submodules here. A Pull with rebase whose upstream bumped a submodule therefore fails in the app. This already happens on main, independent of this PR. Filed as Pull with rebase fails when the upstream moved a submodule to a new commit #89.

  • A rebase_continue issue found while testing this, unrelated to hooks. When replay stops on an error rather than a conflict (for example an untracked file in the way), libgit2 never wrote the index for that commit. Continue rebase then gets "already applied" from commit() and silently skips the commit, where git rebase --continue would pick it again. Filed as Continue rebase drops the commit a rebase stopped on when replay fails without a conflict #88.

  • post-commit: trivial for commit and amend now that warnings have a path (a present check plus one run after the commit is written). Git also runs it for merge, cherry-pick, revert and rebase commits, which is more work.

  • Template hooks on clone: libgit2's clone ignores init.templateDir, so a fresh clone only gets hooks from a global core.hooksPath.

  • Submodules' own post-checkout: the app's follow-the-recorded-commit step and Update submodule don't run it; git submodule update would.

  • Live output and cancel for a running hook.

Screenshots

Checklist

  • pnpm typecheck && pnpm test pass
  • cargo fmt --check && cargo clippy --all-targets -- -D warnings && cargo test pass
  • New engine functions have integration tests in tests/git_engine.rs (12 new, plus tests/clone_hooks.rs, which needs its own binary because it sets HOME for a global core.hooksPath)
  • Mutating operations go through the undo tracked() wrapper where undoable (no new mutating operations)
  • Colors use design tokens only (no hex in components)
  • CLAUDE.md updated (G55 (8), post_hooks.rs, demo flag, test map)
  • Commits carry no trailers from coding tools

@cheat2001

Copy link
Copy Markdown
Owner

Went through all of it and ran it here. The engine tests, the new e2e tests and the typecheck all pass on my Mac, and the hook arguments match what I get from git. Really thorough work.

Two things before I merge:

While a post-hook runs, nothing on screen says so. A checkout or merge from the sidebar or the graph has no busy state, so a husky post-checkout that runs npm install makes the app look frozen for a minute, and a second click starts another checkout on top of the first. I don't need live output in this PR, just something like a "Running post-checkout…" indicator while it runs.

Pull all waits for anything the post-merge hook leaves running in the background and kills it at the 10 minute limit. A hook that does (npm install &) or starts a watcher would hold each repo for 10 minutes and then lose its process, while the toolbar Pull and git both leave it alone. Could the group wait only for the hook itself, and wait for leftovers only when someone presses Cancel?

Small one: a_post_hook_that_runs_past_its_time_is_a_timeout_warning_with_its_output failed once for me under a full parallel run (the output came back empty because the 1 s timeout fired before the echo). It passed on every rerun, and a 3 s timeout would make it solid.

@ChrisLauinger77

ChrisLauinger77 commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

I think its ready for merge, used it the complete weekend locally on mac and linux. A lot of renovate lock file maintainance and the hooks worked great. I rebase it to get rid of the conflicts

@cheat2001

Copy link
Copy Markdown
Owner

Thanks for living with it all weekend, that's the kind of testing I can't do here. I went through the new commits and built it on my Mac again. All three things I asked for are in. The "Running post-checkout…" toast shows up while a hook runs, Pull all no longer waits for what the hook leaves in the background, and the timeout test has been solid.

One problem on macOS though. After Cancel on Pull all, every repo that ran a post-merge hook gets "Cancel could not confirm that all processes exited. Hook processes may still be running." That includes a hook that only does exit 0. I reproduced it with a plain exit 0 post-merge, group_update_controlled and then cancel_results(), and the warning is there every time. Leftovers::running() returns false on anything that isn't Linux or Windows, so the Mac can never tell that the group is gone and always reports it as unconfirmed. Could you probe the group with killpg(pgid, 0) on macOS and treat ESRCH as "nothing left"? Signal 0 never kills anything, so a reused pid would only make Cancel wait or warn, never hit another process. Most of our users are on a Mac, so as it stands every Cancel looks like something went wrong.

Small one: starting a second checkout while post-checkout runs shows a red "Checkout main failed: Another operation is still running in this repository" toast, and it covers the progress toast. Nothing failed, the app just said no. An info toast like "post-checkout is still running in angkorgit" would read better.

The bigger thing is scope. Since my last review stage.rs gained about 900 lines of backup and rollback for staged discards (renames, symlinks, case-only renames, Windows handle renames), plus around 2,000 lines of tests for it. It's careful work, but it isn't hooks, and discard is something everyone uses every day, so I want to review it on its own. It also moves the user's files into .angkorgit-discard-* folders inside the worktree while it works, and I'd like to think about what happens if the app dies in the middle of that. Can you keep in this PR what post-checkout needs, the batch discard that runs the hook once, and move the backup and rollback hardening into its own PR? Then I can merge the hooks part as soon as the macOS warning is fixed.

Every operation that runs a post-* hook in git now runs it through the
existing hook runner, with git's arguments and stdin: post-checkout after
checkouts, clones, new worktrees, discards and stash file applies,
post-merge after merges and pulls that merged or fast-forwarded, and
post-rewrite after amend, reword, rebase, interactive rebase and pull
with rebase. A failing post-hook is reported as a warning and never
undoes the operation. Pull all runs post-merge per repository that
fast-forwarded, inside the same locked section as the fast-forward, and
Skip hooks, Pull without hooks and Pull all without hooks skip them.

Refs cheat2001#82, cheat2001#69
- Pull without hooks that stops on a rebase conflict stays without hooks:
  the paused rebase remembers the choice in its state folder, so
  Continue rebase no longer runs post-rewrite.
- A rebase runs post-checkout right after it checks out onto, before
  replaying commits, so the hook sees onto and no conflict markers. The
  in-memory interactive rebase reports the commit it actually checks
  out.
- Fast-forward merges and Pull all write ORIG_HEAD before moving HEAD,
  like git merge, so post-merge hooks can diff ORIG_HEAD..HEAD.
- Rebasing a branch that already sits linearly on its upstream is up
  to date: no rewritten commits and no hooks, like git rebase.

Refs cheat2001#82
…ks arrive

- A rebase started without hooks records that choice right after it
  starts, so a replay that stops on an untracked file instead of a
  conflict still continues without post-rewrite.
- Pull without hooks and Pull all without hooks also show when the hook
  folder lives inside the worktree (core.hooksPath=.husky), where a pull
  can add a hook that runs right after the fast-forward. repo_hooks now
  returns the found hooks plus that flag.
- The up-to-date rebase shortcut first refuses uncommitted changes and
  an operation in progress, like libgit2 and git do.

Refs cheat2001#82
… failure

- repo_hooks also reports hooks that live in the worktree through a
  symlink: a hook file linked to a worktree path (dangling until a pull
  adds the target) or a hooks folder that is itself a link into the
  worktree, so Pull without hooks and Pull all without hooks show
  before such a hook can run.
- A rebase or pull with rebase whose post-checkout failed and whose
  replay then fails returns the hook warning next to the error instead
  of dropping it.

Refs cheat2001#82
…he worktree

repo_hooks now follows each hook path one symlink hop at a time and
reports the hooks as living in the worktree as soon as any step lies in
it. This covers a hooks folder that is a tracked link to a folder
outside the worktree, which a pull can replace with a real folder
holding post-merge, and a hook linked through an intermediate link to a
worktree file.

Refs cheat2001#82
…n Pull

- Hook paths are walked component by component the way the OS resolves
  them: a symlink is replaced by its target before a following `..`
  applies, so proxy/../.husky/post-merge with proxy linked into the
  worktree is recognised.
- Whether a step lies in the worktree is decided by file identity
  (same-file handles of each ancestor), so a core.hooksPath spelled in
  other letter case on a case-insensitive disk still counts.
- The toolbar Pull menu checks the hooks again every time it opens,
  because the watcher ignores .git/config and a core.hooksPath changed
  in a terminal left the cached answer stale.

Refs cheat2001#82
… files, lock ORIG_HEAD

- repo_hooks also reports the hooks as changeable by a pull when a Git
  config file the repository reads lives in the worktree, following
  include.path and includeIf paths file by file, so Pull without hooks
  shows before a tracked project config adds core.hooksPath.
- The rebase precondition ignores untracked files inside submodules,
  like libgit2's own rebase check, while still refusing edited ones.
- ORIG_HEAD is written through libgit2's reference database, which takes
  ORIG_HEAD.lock and fails while another git process holds it.

Refs cheat2001#82
…ubmodule ignore rules

- The config include walk answers "hooks may change" when it reaches its
  file limit with files still queued, so a tracked include listed before
  many outside ones can no longer hide Pull without hooks.
- The interactive rebase and the reword of an older commit check
  submodules with each submodule's configured ignore rule again
  (ignore = dirty lets an edited submodule through), while the ordinary
  rebase keeps libgit2's rule of ignoring untracked submodule files.

Refs cheat2001#82
…ontained group hooks

- Discarding several selected files runs one command that restores
  them all and then runs post-checkout once, like git restore a b c.
- Pull without hooks goes through the pull check dialog when the
  preview predicts conflicts or blocking edits, like the plain Pull.
- Pull all waits for processes post-merge left running in its process
  group before it releases the cancel lock, and stops them at the hook
  deadline (unix), so a backgrounded job cannot write after Cancel.
- A hook that runs past its limit is reported as timed out, with the
  output it printed, instead of as a hook that never ran.

Refs cheat2001#82
…ccess

The bulk discard now tries every selected file and returns the files it
left behind and the files that failed with their error, instead of
stopping at the first error and dropping what it had already done.
post-checkout still runs once when anything was restored. The working
copy panel names the files that could not be discarded and no longer
shows the success message after a partial failure.

Refs cheat2001#82
… discards

Group pull waited for the hook's process group on unix only, so on Windows a background child of post-merge could still write after Cancel was acknowledged. The runner now puts git in a job object on Windows, waits until the job holds no process and terminates it at the deadline.

Discard all staged changes returned at the first file it could not delete, after earlier files were already restored, and never ran post-checkout. It now tries every file, returns the per-file results like the bulk discard and runs the hook once when any file came back.
@ChrisLauinger77

ChrisLauinger77 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Fixed the Cancel case. An empty process group now counts as finished on macOS and Linux: killpg(pgid, 0), with ESRCH meaning nothing is left (Linux uses the pidfd when available). An exit 0 hook no longer warns. The warning stays when a hook process is still alive and exit cannot be confirmed; on macOS it appears right away because the app never signals a pgid after the leader is reaped. The trade-off is that a child which leaves the group with setsid is not seen, same as Git; the engine test now documents that it can still write after Cancel.

A second operation while a hook runs now gets an info toast, such as "post-checkout is still running in angkorgit", instead of the red error.

The rollback hardening and its tests moved to the PR. #87 keeps the simple discard path, per-file batch failures and the bulk discard that runs post-checkout once.

Could you rerun your macOS repro?

@cheat2001

Copy link
Copy Markdown
Owner

Reran it on my Mac. The exit 0 Cancel test fails on the old code with the exact warning I saw and passes now, and the info toast reads much better. Thanks.

One thing before I merge: the rebase abort guard and the worktree target lock from #91 are hook fixes, so I'd like them here. On #87 alone, aborting a rebase while post-checkout runs leaves the repo fine but then shows "could not open …/rebase-merge/msgnum for writing" as the error. Once those two commits move over I'll merge.

@ChrisLauinger77

Copy link
Copy Markdown
Contributor Author

Both hook fixes are now in #87: 61d8d3f handles rebase aborts during post-checkout, and f56ecc9 holds the new worktree’s target lock until its hook finishes. Their regression tests came over too; the abort test reproduces the msgnum error on the old code and passes with the guard.

All six CI checks are green, including Rust on Linux, macOS and Windows, and Playwright. The discard hardening stays in #91; I’ll rebase that once #87 is merged.

@cheat2001
cheat2001 merged commit abfebd7 into cheat2001:main Nov 12, 2026
6 checks passed
@ChrisLauinger77
ChrisLauinger77 deleted the feat/post-hooks branch November 12, 2026 07:14
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.

Run local Git hooks (pre-commit, commit-msg, pre-push)

2 participants