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): put back blocks Husky wipes on npm install

Erenfd190f2e7f44
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 20b5950..ba712eb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
+- With Husky, cogit's hook blocks no longer vanish for good after `npm install` (Husky rewrites `.husky/_`): the next chat message puts them back. `cogit hooks uninstall` is remembered, so blocks you removed on purpose stay out.
- A session the side model left untitled takes its first thread's title (or its first message's) at session end, instead of showing only its id on the map and in `cogit log`; `cogit log` also stops printing such an id twice.
## [1.0.1] - 2026-11-06
diff --git a/docs/guide/teams-and-git.md b/docs/guide/teams-and-git.md
index 199974e..6b3ebab 100644
--- a/docs/guide/teams-and-git.md
+++ b/docs/guide/teams-and-git.md
@@ -87,6 +87,8 @@ they had, so the repository's size does not shrink. `cogit doctor` reminds you u
setups work). The block never fails a commit or a push. It sits right under the first line, so
an early `exit` in your own lines cannot skip it, and your hook's exit status stays the hook's.
A hook written in another language (Python, Node) is left alone, and `cogit doctor` says so.
+Husky rewrites its hooks folder on every `npm install`; cogit puts its block back on your next
+chat message. After `cogit hooks uninstall` it does not, until `cogit hooks install`.
- **`prepare-commit-msg`** adds a `Thought: h<id>` trailer naming the message the commit came
from: the newest message of the current session. It is skipped for merges and squashes, while
diff --git a/packages/cli/src/git-hooks.ts b/packages/cli/src/git-hooks.ts
index a131234..cf59c28 100644
--- a/packages/cli/src/git-hooks.ts
+++ b/packages/cli/src/git-hooks.ts
@@ -1,6 +1,6 @@
-import { realpathSync } from "node:fs";
+import { existsSync, realpathSync } from "node:fs";
import { chmod, mkdir, readFile, rm, stat } from "node:fs/promises";
-import { join } from "node:path";
+import { dirname, join } from "node:path";
import { findGitRoot, gitHooksDir, resolveCommit } from "./git.js";
import { spawnDetachedProcess } from "./hooks/run.js";
import { pushIsDryRun } from "./push-command.js";
@@ -199,6 +199,7 @@ export function isGitHookEvent(event: string): event is GitHookEvent {
/** Installs the cogit block into every hook git would run; returns the events added. */
export async function installGitHooks(root: string): Promise<GitHookEvent[]> {
+ await rm(projectPaths(root).hooksOff, { force: true });
const installed: GitHookEvent[] = [];
for (const event of GIT_HOOK_EVENTS) {
if (await installGitHook(root, event)) installed.push(event);
@@ -218,8 +219,11 @@ export async function refreshGitHooks(root: string): Promise<GitHookEvent[]> {
const refreshed: GitHookEvent[] = [];
const hasBlock = async (event: string): Promise<boolean> =>
((await readFile(join(dir, event), "utf8").catch(() => undefined)) ?? "").includes(BLOCK_START);
- // A hook this version added reaches a clone that already has cogit's others.
- const installed = await hasBlock("prepare-commit-msg");
+ // Missing blocks come back unless the user took them out with `hooks uninstall`:
+ // Husky rewrites its hooks folder on every `npm install`, and a hook this version
+ // added must reach a clone that already has cogit's others.
+ const installed =
+ !existsSync(projectPaths(root).hooksOff) || (await hasBlock("prepare-commit-msg"));
for (const event of GIT_HOOK_EVENTS) {
if (!installed && !(await hasBlock(event))) continue;
if (await installGitHook(root, event)) refreshed.push(event);
@@ -239,7 +243,14 @@ export async function uninstallGitHooks(root: string): Promise<GitHookEvent[]> {
const dir = await gitHooksDir(gitRoot);
if (dir === undefined) return [];
await removeHookBlocks(dir, RETIRED_GIT_HOOK_EVENTS);
- return removeHookBlocks(dir, GIT_HOOK_EVENTS);
+ const removed = await removeHookBlocks(dir, GIT_HOOK_EVENTS);
+ // Remembered, so the refresh on the next chat message does not put them back.
+ const paths = projectPaths(root);
+ if (existsSync(paths.project)) {
+ await mkdir(dirname(paths.hooksOff), { recursive: true });
+ await atomicWrite(paths.hooksOff, `${new Date().toISOString()}\n`);
+ }
+ return removed;
}
/** Takes cogit's block out of these hooks; returns the ones that had one. */
diff --git a/packages/cli/src/store/paths.ts b/packages/cli/src/store/paths.ts
index c68bfcd..ad8dace 100644
--- a/packages/cli/src/store/paths.ts
+++ b/packages/cli/src/store/paths.ts
@@ -42,6 +42,8 @@ export function projectPaths(root: string) {
detachedLog: join(base, "state", "detached.log"),
/** What the record refs last looked like, and what waits to be sent; local. */
recordSync: join(base, "state", "record-sync.json"),
+ /** Present after `cogit hooks uninstall`: the refresh then adds no block; local. */
+ hooksOff: join(base, "state", "hooks-off"),
/** Commits waiting for their message's record before a Shape; local. */
shapeQueue: join(base, "state", "shape-queue"),
locks: join(base, "locks"),
diff --git a/packages/cli/test/session-git.test.ts b/packages/cli/test/session-git.test.ts
index 515bf27..2467671 100644
--- a/packages/cli/test/session-git.test.ts
+++ b/packages/cli/test/session-git.test.ts
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
+import { existsSync } from "node:fs";
import { chmod, mkdir, readFile, symlink, writeFile } from "node:fs/promises";
import { userInfo } from "node:os";
import { join } from "node:path";
@@ -252,22 +253,32 @@ describe("ignore files and git hooks", () => {
expect(stderr).toBe("");
});
- it("refreshes only the hooks that already carry a cogit block", async () => {
+ it("puts back blocks a tool wiped, but none after hooks uninstall", async () => {
const hooks = join(root, ".git", "hooks");
await writeFile(
join(hooks, "post-commit"),
"#!/bin/sh\n# >>> cogit\ncogit hook post-commit || true\n# <<< cogit\n",
);
+ // Like Husky after `npm install`: the hooks are there, cogit's block is not.
await writeFile(join(hooks, "prepare-commit-msg"), "#!/bin/sh\necho mine\n");
- expect(await refreshGitHooks(root)).toEqual(["post-commit"]);
- expect(await readFile(join(hooks, "post-commit"), "utf8")).toContain(
- hookBlock("post-commit", root),
- );
+ expect(await refreshGitHooks(root)).toEqual(["prepare-commit-msg", "post-commit", "pre-push"]);
expect(await readFile(join(hooks, "prepare-commit-msg"), "utf8")).toBe(
- "#!/bin/sh\necho mine\n",
+ `#!/bin/sh\n${hookBlock("prepare-commit-msg", root)}\necho mine\n`,
);
expect(await refreshGitHooks(root)).toEqual([]);
expect(await refreshGitHooks(await noGitDir())).toEqual([]);
+
+ // The user took them out on purpose: the refresh leaves them out.
+ const io = fakeIo(env);
+ await hooksUninstall(root, io);
+ expect(existsSync(projectPaths(root).hooksOff)).toBe(true);
+ expect(await refreshGitHooks(root)).toEqual([]);
+ expect(await readFile(join(hooks, "prepare-commit-msg"), "utf8")).toBe(
+ "#!/bin/sh\necho mine\n",
+ );
+ // Installing again clears that, so a later wipe is mended again.
+ await hooksInstall(root, io);
+ expect(existsSync(projectPaths(root).hooksOff)).toBe(false);
});
it("adds pre-push to an installed clone and retires an old pre-commit block", async () => {