Repository navigation
feat(hooks): run post-checkout, post-merge and post-rewrite hooks - #87
Conversation
|
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 Small one: |
|
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 |
a345c88 to
c630e24
Compare
|
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 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 |
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
…s and hook timeouts
…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.
c630e24 to
a3df747
Compare
|
Fixed the Cancel case. An empty process group now counts as finished on macOS and Linux: 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? |
|
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. |
|
Both hook fixes are now in #87: 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. |
7a1f9de to
d2c6bf8
Compare
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-mergeandpost-rewrite, which tools like Husky, lefthook andgit-lfsrely 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 ingit, with the same arguments and stdin. It reuses the runner from #84 (git hook run, PATH fromai_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 restorerunspost-checkouttoo.git switch,git checkout -Bpost-checkout <old> <new> 1git clone,git worktree addpost-checkout 000… <HEAD> 1(inside the new worktree; none for an empty repo)git merge,git pullpost-merge 0(not when up to date or conflicted);ORIG_HEADis set first, asgit mergedoesgit pull --rebasepost-checkout <orig> <onto> 1, thenpost-rewrite rebase; nopost-mergegit rebase,--continuepost-checkout <orig> <onto> 1right after onto is checked out (before any commit is replayed),post-rewrite rebaseonce 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")git rebase -ipost-checkout <orig> <result> 1,post-rewrite rebase(squash/fixup map to the combined commit, dropped commits left out)git commit --amendpost-rewrite amend, stdin<old> <new>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)git clean,git restore -pgit pull --ff-onlypost-merge 0per repository that movedDecisions worth a look
Skip hooks also skips post-hooks. This differs from
git:--no-verifynever skips a post-hook (git commit --amend --no-verifystill runspost-rewrite). The issue's spec and the approved group pull rule ask for Skip hooks to mean "no hooks". So:post-rewritewhen amending;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 inCargo.lock) rather than by path text, so letter case and/varvs/private/varcan't hide a match. Examples:core.hooksPath=.husky(even when.huskyis a tracked link to an empty folder outside, which a pull can replace with a real folder),.git/hookslinked to a folder there,.git/hooks/post-merge -> ../../scripts/post-merge,proxy/../.husky/post-mergewithproxylinked 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 throughinclude.pathandincludeIf.*.path, because a trackedproject.gitconfigcan gaincore.hooksPath=.huskyin 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 matchesgit.Git's per-step
post-rewrite amendinsiderebase -iis 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 singlepost-rewrite rebasecarries every pair.The interactive rebase's
post-checkoutnames 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$2names 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-mergeright 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:
ignorerule instead, soignore = dirtylets an edited submodule through there. libgit2's status list doesn't apply that rule, so the checks ask each submodule directly.ORIG_HEAD, through libgit2's reference database. That takesORIG_HEAD.lockand 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-checkoutruns before the replay, so therebaseandpullwrappers return their warnings next to the result. A failing hook is still reported when the replay then fails.Warnings travel as a
hook-warningevent. 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.rsholds one wrapper per command. It runs the hook aftersubmodule::synced, so a hook sees the final state, submodules included. The rebase pairs come from libgit2'srebase-merge/rewritten, read beforefinish()deletes it.Group pull and Cancel
post-mergeruns 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.
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_hooksnow asserts that both pulls runpost-merge 0and neither runspost-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 rebaseignores submodules here. A Pull with rebase whose upstream bumped a submodule therefore fails in the app. This already happens onmain, independent of this PR. Filed as Pull with rebase fails when the upstream moved a submodule to a new commit #89.A
rebase_continueissue 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" fromcommit()and silently skips the commit, wheregit rebase --continuewould 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 (apresentcheck plus onerunafter 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 globalcore.hooksPath.Submodules' own
post-checkout: the app's follow-the-recorded-commit step and Update submodule don't run it;git submodule updatewould.Live output and cancel for a running hook.
Screenshots
Checklist
pnpm typecheck && pnpm testpasscargo fmt --check && cargo clippy --all-targets -- -D warnings && cargo testpasstests/git_engine.rs(12 new, plustests/clone_hooks.rs, which needs its own binary because it sets HOME for a globalcore.hooksPath)tracked()wrapper where undoable (no new mutating operations)CLAUDE.mdupdated (G55 (8),post_hooks.rs, demo flag, test map)