Skip to content

armgit / cogit

Public Imported from GitHub
0

Git for the intent behind agent-written code. Records what your coding agent decided and why, and links every commit to it.

fix(hooks): keep the Thought trailer past a user's EXIT trap or exec

Eren0433f4c06e3e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b9ffac2..82a0c07 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
+### Fixed
+- A `prepare-commit-msg` hook of your own that sets its own `trap ... EXIT` or ends with `exec` no longer drops the `Thought:` trailer: cogit now runs your lines as a child and adds the trailer once they finish. Existing hooks update on the next chat message.
## [1.1.0] - 2026-11-06
### Changed
diff --git a/packages/cli/src/git-hooks.ts b/packages/cli/src/git-hooks.ts
index 88af92a..6b1f3e8 100644
--- a/packages/cli/src/git-hooks.ts
+++ b/packages/cli/src/git-hooks.ts
@@ -49,9 +49,11 @@ export function currentCliPath(): string | undefined {
*
* - `pre-push` reads git's stdin itself and hands it back to the lines below,
* so a user hook that reads it still gets it and cogit always sees the refs.
- * - `prepare-commit-msg` runs at exit, after the user's lines: a tool that
- * rewrites the message file (commitizen) keeps the `Thought:` trailer, and the
- * user's exit status stays the hook's.
+ * - `prepare-commit-msg` runs the same file again as a child, which skips the
+ * block, and adds the trailer once the user's lines are done: a tool that
+ * rewrites the message file (commitizen) keeps the `Thought:` trailer, a user
+ * `trap ... EXIT` or a closing `exec` cannot drop it, and the user's exit
+ * status stays the hook's.
*/
export function hookBlock(
event: string,
@@ -73,11 +75,14 @@ export function hookBlock(
if (event === "prepare-commit-msg") {
return [
BLOCK_START,
- "cogit_msg=${1-} cogit_source=${2-}",
- "cogit_thought() {",
+ 'if [ "${COGIT_HOOK_RUN-}" = "$0" ]; then',
+ " unset COGIT_HOOK_RUN",
+ "else",
+ ' COGIT_HOOK_RUN=$0 "$0" "$@"',
+ " cogit_status=$?",
...cogitCall(event, projectRoot, cliPath, "", " "),
- "}",
- `trap 'cogit_status=$?; cogit_thought "$cogit_msg" "$cogit_source"; exit $cogit_status' EXIT`,
+ " exit $cogit_status",
+ "fi",
BLOCK_END,
].join("\n");
}
diff --git a/packages/cli/test/git-hook-block.test.ts b/packages/cli/test/git-hook-block.test.ts
index e65ae67..afef937 100644
--- a/packages/cli/test/git-hook-block.test.ts
+++ b/packages/cli/test/git-hook-block.test.ts
@@ -114,6 +114,23 @@ describe("the cogit block in a user's hook", () => {
expect(message).toContain("Thought: h123456");
});
+ it("adds the Thought trailer past a user's own EXIT trap and a closing exec", async () => {
+ const trapped = join(dir, "trap.log");
+ await hook("prepare-commit-msg", `trap 'echo trap ran > "${trapped}"' EXIT\nexec true\n`);
+ await writeFile(join(root, "d.txt"), "d\n");
+ await git("add", "d.txt");
+ await git("commit", "-qm", "exec");
+ expect((await git("log", "-1", "--format=%B")).stdout).toContain("Thought: h123456");
+ await hook("prepare-commit-msg", `trap 'echo trap ran > "${trapped}"' EXIT\n`);
+ await writeFile(join(root, "e.txt"), "e\n");
+ await git("add", "e.txt");
+ await git("commit", "-qm", "trap");
+ expect((await git("log", "-1", "--format=%B")).stdout).toContain("Thought: h123456");
+ expect(await readFile(trapped, "utf8")).toBe("trap ran\n");
+ // One trailer per commit: the child run of the hook skips the block.
+ expect((await readFile(log, "utf8")).match(/prepare-commit-msg/g)).toHaveLength(2);
+ });
+
it("keeps a failing prepare-commit-msg hook failing", async () => {
await hook("prepare-commit-msg", "exit 3\n");
await writeFile(join(root, "c.txt"), "c\n");