Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(claude): check only the commit's own message in the hook
Elian Doranc3639a1d4840
diff --git a/.claude/hooks/check-commit-attribution.mjs b/.claude/hooks/check-commit-attribution.mjsindex 795fb65095..090989533c 100644--- a/.claude/hooks/check-commit-attribution.mjs+++ b/.claude/hooks/check-commit-attribution.mjs@@ -14,6 +14,9 @@ const CO_AUTHOR = /Co-Authored-By: Claude/i;/** `NAME=value` at the start of a command, as `S="…" && git commit -F "$S/msg.txt"` sets. */const ASSIGNMENT = /(?:^|[;&|(\n]\s*)(?:export\s+)?([A-Za-z_]\w*)=("[^"]*"|'[^']*'|[^\s;&|]*)/g;const VARIABLE = /\$\{(\w+)\}|\$(\w+)/g;+const GIT_DIR = /\s-C\s+("[^"]*"|'[^']*'|\S+)/g;+const SEPARATOR = /&&|\|\|?|;|\n/;+const HEREDOC = /<<-?\s*(["']?)(\w+)\1/;const EXPECTED = "Commit messages written with Claude Code end with a trailer naming the model, "+ "e.g. `Assisted-by: Claude Code (Claude Opus 5.5)`, and never credit it with "@@ -21,13 +24,16 @@ const EXPECTED = "Commit messages written with Claude Code end with a trailer na/** Returns why `command` must not run, or `null` when it is not a commit with a bad message. */export function checkCommand(command, cwd) {- // Quoted text is blanked out, so `git commit` inside an argument such as a search pattern is- // not taken for a commit.- const unquoted = command.replace(QUOTED, (quoted) => " ".repeat(quoted.length));- const starts = [ ...unquoted.matchAll(COMMIT) ].map((match) => match.index);- for (const [ index, start ] of starts.entries()) {+ // Quoted text is masked, so `git commit` or a separator inside an argument such as a search+ // pattern is not taken for one, while a quoted `git -C` path still reads as a word.+ const unquoted = command.replace(QUOTED, (quoted) => "_".repeat(quoted.length));+ for (const match of unquoted.matchAll(COMMIT)) {+ const start = match.index;+ const commandEnd = start + match[0].length;const variables = readAssignments(command.slice(0, start));- const reason = checkCommit(command.slice(start, starts[index + 1]), cwd, variables);+ const gitCwd = readGitDirectory(command.slice(start, commandEnd), cwd, variables);+ const end = findCommitEnd(command, unquoted, commandEnd);+ const reason = checkCommit(command.slice(start, end), gitCwd, variables);if (reason) {return reason;}@@ -35,6 +41,33 @@ export function checkCommand(command, cwd) {return null;}+/** The directory git runs in after the `-C` options of `gitCommand`, each relative to the last. */+function readGitDirectory(gitCommand, cwd, variables) {+ let directory = cwd;+ for (const [ , word ] of gitCommand.matchAll(GIT_DIR)) {+ directory = resolve(directory, toNativePath(unquote(word, variables)));+ }+ return directory;+}++/**+ * Where the commit starting before `from` ends: at the next separator, or after the end line of a+ * heredoc it reads its message from.+ */+function findCommitEnd(command, unquoted, from) {+ const separator = unquoted.slice(from).search(SEPARATOR);+ if (separator === -1) {+ return command.length;+ }+ const heredoc = HEREDOC.exec(command.slice(from, from + separator));+ if (!heredoc) {+ return from + separator;+ }+ const delimiter = new RegExp(`\\n\\s*${heredoc[2]}\\s*(?:\\n|$)`);+ const body = delimiter.exec(command.slice(from + separator));+ return body ? from + separator + body.index + body[0].length : command.length;+}+function checkCommit(args, cwd, variables) {if (!MESSAGE_FLAG.test(args)) {// No message of its own, as with `--amend --no-edit`.@@ -89,17 +122,18 @@ function unreadable(path) {}function readMessage(path, cwd) {- // Git Bash passes paths such as `/c/Users/...`, which Node on Windows reads as `C:/Users/...`.- const nativePath = process.platform === "win32"- ? path.replace(/^\/([a-zA-Z])\//, "$1:/")- : path;try {- return readFileSync(resolve(cwd, nativePath), "utf8");+ return readFileSync(resolve(cwd, toNativePath(path)), "utf8");} catch {return null;}}+/** Git Bash passes paths such as `/c/Users/...`, which Node on Windows reads as `C:/Users/...`. */+function toNativePath(path) {+ return process.platform === "win32" ? path.replace(/^\/([a-zA-Z])\//, "$1:/") : path;+}+if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {const input = JSON.parse(readFileSync(0, "utf8"));const reason = checkCommand(input.tool_input?.command ?? "", input.cwd ?? process.cwd());diff --git a/.claude/hooks/check-commit-attribution.test.mjs b/.claude/hooks/check-commit-attribution.test.mjsindex b80337faad..d2c57f81fa 100644--- a/.claude/hooks/check-commit-attribution.test.mjs+++ b/.claude/hooks/check-commit-attribution.test.mjs@@ -1,7 +1,7 @@// Run with `node --test .claude/hooks/check-commit-attribution.test.mjs`.import assert from "node:assert/strict";import { spawnSync } from "node:child_process";-import { mkdtempSync, writeFileSync } from "node:fs";+import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { describe, it } from "node:test";@@ -60,6 +60,37 @@ ${TRAILER}}});+ it("reads a message file relative to the directories `git -C` names", () => {+ mkdirSync(join(dir, "sub", "deeper"), { recursive: true });+ message(join("sub", "in-sub.txt"), `fix(x): y\n\n${TRAILER}\n`);+ message(join("sub", "deeper", "in-deeper.txt"), `fix(x): y\n\n${TRAILER}\n`);+ for (const command of [+ "git -C sub commit -F in-sub.txt",+ "git -C sub -C deeper commit --file=in-deeper.txt",+ `git -C sub -C "${join(dir, "sub", "deeper")}" commit -F in-deeper.txt`+ ]) {+ assert.equal(checkCommand(command, dir), null, command);+ }+ });++ it("reads the message from the commit alone, not from the commands after it", () => {+ for (const command of [+ `git commit -m "fix(x): y" -m "${TRAILER}" && echo "Co-Authored-By: Claude"`,+ `git commit -F - <<'EOF'\nfix(x): y\n\n${TRAILER}\nEOF\n`+ + "git log -1 | grep \"Co-Authored-By: Claude\""+ ]) {+ assert.equal(checkCommand(command, dir), null, command);+ }+ for (const command of [+ `git commit -m wip && echo "${TRAILER}"`,+ `git commit -m wip; echo "${TRAILER}"`,+ `git commit -m wip | tee log.txt "${TRAILER}"`,+ `git commit -m wip\necho "${TRAILER}"`+ ]) {+ assert.match(checkCommand(command, dir) ?? "", /Assisted-by: Claude Code \(/, command);+ }+ });+it("says so when it cannot read the message file, as one written in the same command", () => {for (const command of ["git commit -F missing.txt",