Skip to content

Build your personal knowledge base with Trilium Notes

chore(claude): stop the commit hook doubling the trailer

Elian Doran199b23dc81f9
diff --git a/.claude/hooks/check-commit-attribution.mjs b/.claude/hooks/check-commit-attribution.mjs
index 70950fa514..8c7176a71d 100644
--- a/.claude/hooks/check-commit-attribution.mjs
+++ b/.claude/hooks/check-commit-attribution.mjs
@@ -1,7 +1,8 @@
// A `PreToolUse` hook (see `.claude/settings.json`) for the `Assisted-by` trailer that CLAUDE.md
// requires on a `git commit` or `git commit-tree`, naming the model. A commit without it gets the
// trailer added through `--trailer`, with the model read from the transcript. A commit crediting
-// Claude with `Co-Authored-By`, or one the hook cannot fix, is blocked.
+// Claude with `Co-Authored-By`, one whose message file the hook cannot read, or one the hook cannot
+// fix, is blocked.
//
// Dry run, which prints what the hook does with a command and runs nothing:
// node .claude/hooks/check-commit-attribution.mjs --dry-run [--model <id>] [--cwd <dir>] -- <command>
@@ -28,6 +29,9 @@ const VARIABLE = /\$\{(\w+)\}|\$(\w+)/g;
const GIT_DIR = /\s-C\s+("[^"]*"|'[^']*'|\S+)/g;
const SEPARATOR = /&&|\|\|?|;|\n/;
const HEREDOC = /<<-?\s*(["']?)(\w+)\1/;
+/** A `cat` line, which writes a heredoc to a file with `cat > file <<EOF` or `cat <<EOF > file`. */
+const CAT_LINE = /(?:^|[;&|(\n]\s*)cat\b([^\n]*)\n/g;
+const REDIRECT = /(?<!>)>(?!>)\s*("[^"]*"|'[^']*'|[^\s;&|<>]+)/;
/** A model id such as `claude-opus-5-5`, `claude-fable-5-1[1m]` or `claude-haiku-4-5-20251001`. */
const MODEL_ID = /^claude-([a-z]+)-(\d+)(?:-(\d{1,2}))?(?:-\d{8})?(?:\[[^\]]*\])?$/;
/** How much of the end of the transcript is searched for the model of the last reply. */
@@ -53,9 +57,10 @@ export function checkCommand(command, cwd, modelName = null) {
const start = match.index;
const commandEnd = start + match[0].length;
const variables = readAssignments(command.slice(0, start));
+ const written = readHeredocFiles(command.slice(0, start), cwd, variables);
const gitCwd = readGitDirectory(command.slice(start, commandEnd), cwd, variables);
const end = findCommitEnd(command, unquoted, commandEnd);
- const verdict = checkCommit(command.slice(start, end), gitCwd, variables);
+ const verdict = checkCommit(command.slice(start, end), gitCwd, variables, written);
if (verdict.action === "pass") {
continue;
}
@@ -154,11 +159,12 @@ function findCommitEnd(command, unquoted, from) {
/**
* `{ action: "pass" }` for a commit that carries the trailer or has no message of its own,
- * `{ action: "block", reason }` for one crediting Claude as co-author, and otherwise
- * `{ action: "fix", reason, ifExists }`, where `ifExists` is the `trailer.ifexists` mode that
- * adds the trailer without repeating it.
+ * `{ action: "block", reason }` for one crediting Claude as co-author or whose message file cannot
+ * be read, and otherwise `{ action: "fix", reason, ifExists }`, where `ifExists` is the
+ * `trailer.ifexists` mode that adds the trailer without repeating it. `written` maps the absolute
+ * paths of the files the command writes from a heredoc before the commit to their contents.
*/
-function checkCommit(args, cwd, variables) {
+function checkCommit(args, cwd, variables, written) {
if (!MESSAGE_FLAG.test(args)) {
// No message of its own, as with `--amend --no-edit`.
return { action: "pass" };
@@ -171,7 +177,7 @@ function checkCommit(args, cwd, variables) {
if (path === "-") {
continue;
}
- const text = readMessage(path, cwd);
+ const text = written.get(resolve(cwd, toNativePath(path))) ?? readMessage(path, cwd);
if (text === null) {
unreadablePath ??= path;
} else {
@@ -185,17 +191,44 @@ function checkCommit(args, cwd, variables) {
if (messages.some((text) => TRAILER.test(text))) {
return { action: "pass" };
}
+ // An unread file can end with a Claude line that `addIfDifferent` keeps beside the new one.
+ if (unreadablePath) {
+ return { action: "block", reason: unreadable(unreadablePath) };
+ }
// `replace` swaps the `Assisted-by` line nearest the end, so it applies only when that line
- // is a Claude one, such as `Assisted-by: Claude Opus 5.5`; `addIfDifferent` leaves a matching
- // line written to an unread file.
+ // is a Claude one, such as `Assisted-by: Claude Opus 5.5`.
const lastTrailer = messages.flatMap((text) => text.match(ANY_TRAILER) ?? []).at(-1);
return {
action: "fix",
- reason: unreadablePath ? unreadable(unreadablePath) : EXPECTED,
+ reason: EXPECTED,
ifExists: lastTrailer && CLAUDE_TRAILER.test(lastTrailer) ? "replace" : "addIfDifferent"
};
}
+/**
+ * The files `command` writes from a heredoc through `cat`, as a map from each absolute path to
+ * the heredoc's body.
+ */
+function readHeredocFiles(command, cwd, variables) {
+ const written = new Map();
+ for (const match of command.matchAll(CAT_LINE)) {
+ const heredoc = HEREDOC.exec(match[1]);
+ const target = REDIRECT.exec(match[1].replace(HEREDOC, ""));
+ if (!heredoc || !target) {
+ continue;
+ }
+ const bodyStart = match.index + match[0].length;
+ const delimiter = new RegExp(`^[ \\t]*${heredoc[2]}[ \\t]*$`, "m");
+ const end = delimiter.exec(command.slice(bodyStart));
+ if (!end) {
+ continue;
+ }
+ const path = resolve(cwd, toNativePath(unquote(target[1], variables)));
+ written.set(path, command.slice(bodyStart, bodyStart + end.index));
+ }
+ return written;
+}
+
/**
* The variables the shell has set by the end of `command`, from its assignments, each expanded
* with the ones before it as the shell does.
diff --git a/.claude/hooks/check-commit-attribution.test.mjs b/.claude/hooks/check-commit-attribution.test.mjs
index 0cc2529a48..285f562471 100644
--- a/.claude/hooks/check-commit-attribution.test.mjs
+++ b/.claude/hooks/check-commit-attribution.test.mjs
@@ -103,17 +103,21 @@ ${TRAILER}
for (const command of [
"git commit -F missing.txt",
"git commit -F \"$UNSET_COMMIT_ATTRIBUTION_DIR/msg.txt\"",
- `cat > new.txt <<'EOF'
-fix(x): y
-
-${TRAILER}
-EOF
-git commit -F new.txt`
+ `printf 'fix(x): y\\n\\n${TRAILER}\\n' > printed.txt && git commit -F printed.txt`
]) {
assert.match(blockReason(command), /Could not read the commit message file/, command);
}
});
+ it("reads a message file the same command writes from a heredoc", () => {
+ for (const command of [
+ `cat > new.txt <<'EOF'\nfix(x): y\n\n${TRAILER}\nEOF\ngit commit -F new.txt`,
+ `S=sub; cat <<"EOF" > "$S/new.txt"\nfix(x): y\n\n${TRAILER}\nEOF\ngit commit -F "$S/new.txt"`
+ ]) {
+ assert.deepEqual(checkCommand(command, dir), PASS, command);
+ }
+ });
+
it("blocks a message without the trailer, with a placeholder model, or crediting Claude as co-author", () => {
const none = message("none.txt", "fix(x): y\n");
const placeholder = message("placeholder.txt", "fix(x): y\n\nAssisted-by: Claude Code (<model>)\n");
@@ -137,12 +141,12 @@ describe("checkCommand with a model", () => {
const MODEL = "Claude Opus 5.5";
const ADD = `--trailer "${TRAILER}"`;
- it("adds the trailer to a commit without it, or whose message file does not exist yet", () => {
+ it("adds the trailer to a commit without it, or whose message file a heredoc writes", () => {
message("none.txt", "fix(x): y\n");
for (const [ command, expected ] of [
[ "git commit -m wip", `git -c trailer.ifexists=addIfDifferent commit ${ADD} -m wip` ],
- [ "git -C sub commit -F none.txt -- a.ts",
- `git -c trailer.ifexists=addIfDifferent -C sub commit ${ADD} -F none.txt -- a.ts` ],
+ [ "git -C sub commit -F ../none.txt -- a.ts",
+ `git -c trailer.ifexists=addIfDifferent -C sub commit ${ADD} -F ../none.txt -- a.ts` ],
[ `cd ${dir} && git commit -F none.txt && git push`,
`cd ${dir} && git -c trailer.ifexists=addIfDifferent commit ${ADD} -F none.txt && git push` ],
[ "cat > new.txt <<'EOF'\nfix(x): y\nEOF\ngit commit -F new.txt",
@@ -152,6 +156,11 @@ describe("checkCommand with a model", () => {
}
});
+ it("blocks a commit whose message file it cannot read, rather than risk a second trailer", () => {
+ assert.match(blockReason("printf 'fix(x): y\\n' > printed.txt && git commit -F printed.txt", MODEL),
+ /Could not read the commit message file/);
+ });
+
it("replaces a malformed Claude line only when it is the last `Assisted-by` line", () => {
const bare = message("bare.txt", "fix(x): y\n\nAssisted-by: DeepSeek V4\nAssisted-by: Claude Code\n");
const notLast = message("not-last.txt", "fix(x): y\n\nAssisted-by: Claude Code\nAssisted-by: DeepSeek V4\n");
@@ -161,6 +170,8 @@ describe("checkCommand with a model", () => {
assert.equal(modeOf("git commit -m \"fix(x): y\" -m \"Assisted-by: Claude Opus 5.5\""), "replace");
assert.equal(modeOf("git commit -m \"fix(x): y\" -m \"Assisted-by: Claude Code <[email protected]>\""), "replace");
assert.equal(modeOf(`git commit -F ${notLast}`), "addIfDifferent");
+ assert.equal(modeOf("cat > new.txt <<'EOF'\nfix(x): y\n\nAssisted-by: Claude Code\nEOF\ngit commit -F new.txt"),
+ "replace");
});
it("rewrites each commit that needs it and leaves the others as they are", () => {