I just got this during a rebase: ❯ git rebase main Link this commit to Agent session…
Private research annotation
Sign in to add a private research label or note.
- User
I just got this during a rebase:
❯ git rebase main Link this commit to Agent session context? [Y/n] n Link this commit to Agent session context? [Y/n] n Rebasing (23/25)
I think we should somehow prevent this from happening. Basically I think the rebase had commits without a trailer, but since the session was still active the hooks tried to link them. This shouldn't be a thing.
Assistant
You're right, that's annoying behavior. The prepare-commit-msg hook shouldn't prompt during rebase operations - those commits already have their history and shouldn't be linked to the current session.
Let me look at how the hook currently works and how we can detect rebase operations.
Assistant
Let me check how git signals rebase state - it uses special directories in
.git/.Assistant
Now I understand the issue. During
git rebase, git calls prepare-commit-msg for each commit being replayed, but it doesn't pass a special "rebase" source - it just calls it normally (often with empty source or "message"). The way to detect rebase is to check for git's rebase state directories.Let me add detection for rebase (and cherry-pick/revert while we're at it) at the start of
PrepareCommitMsg:Assistant
Let me check what helper functions exist in the paths package for getting the git directory:
Assistant
Now I'll implement the fix. I need to add a function to detect rebase/cherry-pick/revert state and check it early in
PrepareCommitMsg:Assistant
Now I need to add the missing imports (
os/execandpath/filepath):Assistant
Let me run the tests to make sure everything compiles and works:
Assistant
I need to use
exec.CommandContext- let me fix that:Assistant
All tests pass. Let me show you the fix:
Assistant
Done. The fix adds detection for git "sequence operations" (rebase, cherry-pick, revert) at the start of
PrepareCommitMsg. It checks for:.git/rebase-merge/- merge-based rebases (git rebase -i,git rebase --merge).git/rebase-apply/- apply-based rebases (git rebase --apply).git/CHERRY_PICK_HEAD- during cherry-pick.git/REVERT_HEAD- during revert
When any of these are detected, the hook returns immediately without prompting. The git source parameter doesn't reliably indicate rebase (git just calls the hook normally), so checking for these state files/directories is the standard way to detect these operations.
- User
can we add tests for this?
Assistant
Now I'll add tests for
isGitSequenceOperation()to hooks_test.go:Assistant
Let me run the tests to verify they pass:
Assistant
All tests pass. Let me run the full test suite to make sure nothing else broke:
Assistant
Done. Added 6 tests covering:
- NoOperation - Returns false in a clean repo
- RebaseMerge - Detects
.git/rebase-merge/(interactive/merge rebases) - RebaseApply - Detects
.git/rebase-apply/(apply-based rebases) - CherryPick - Detects
.git/CHERRY_PICK_HEAD - Revert - Detects
.git/REVERT_HEAD - Worktree - Verifies detection works correctly in worktrees where the git directory is in a different location
- User
The function should use the existing GetGitDir() helper instead of duplicating the git rev-parse --git-dir logic. GetGitDir() properly handles relative paths by making them absolute, which is more robust and consistent with the rest of the codebase. Replace lines 148-154 with a call to GetGitDir().
Assistant
Now I'll update
isGitSequenceOperation()to use the existingGetGitDir()helper:Assistant
Let me check if the imports are still needed elsewhere in the file:
Assistant
Done. The function now uses
GetGitDir()which properly handles relative paths by making them absolute. The implementation is cleaner and consistent with the rest of the codebase.- User
/exit exit