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): require the Assisted-by trailer on commits
Elian Doran1a54d2b54bbe
diff --git a/.claude/hooks/check-commit-attribution.mjs b/.claude/hooks/check-commit-attribution.mjsnew file mode 100644index 0000000000..afac52b595--- /dev/null+++ b/.claude/hooks/check-commit-attribution.mjs@@ -0,0 +1,72 @@+// A `PreToolUse` hook (see `.claude/settings.json`): blocks a `git commit` or `git commit-tree`+// whose message lacks the `Assisted-by` trailer that CLAUDE.md requires, naming the model.+import { readFileSync } from "node:fs";+import { resolve } from "node:path";+import { fileURLToPath } from "node:url";++/** `git` at the start of a command, with its global options, running `commit` or `commit-tree`. */+const COMMIT = /(?:^|[;&|(\n]\s*)git(?:\s+-[cC]\s+\S+|\s+--\S+)*\s+commit(?:-tree)?(?=\s|$)/g;+const QUOTED = /"[^"]*"|'[^']*'/g;+const MESSAGE_FLAG = /\s(?:-[a-zA-Z]*[mF]|--message|--file)(?=[\s"'=]|$)/;+const FILE_FLAG = /\s(?:-F\s*|--file[=\s]\s*)("[^"]+"|'[^']+'|[^\s;&|]+)/g;+const TRAILER = /Assisted-by: Claude Code \([^()<>\n"']+\)/;+const CO_AUTHOR = /Co-Authored-By: Claude/i;++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 "+ + "`Co-Authored-By` (CLAUDE.md, \"Commit messages\"). Add the trailer and commit again.";++/** 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()) {+ const reason = checkCommit(command.slice(start, starts[index + 1]), cwd);+ if (reason) {+ return reason;+ }+ }+ return null;+}++function checkCommit(args, cwd) {+ if (!MESSAGE_FLAG.test(args)) {+ // No message of its own, as with `--amend --no-edit`.+ return null;+ }++ const messages = [ args ];+ for (const [ , quoted ] of args.matchAll(FILE_FLAG)) {+ const path = quoted.replace(/^["']|["']$/g, "");+ if (path !== "-") {+ messages.push(readMessage(path, cwd) ?? "");+ }+ }++ const hasTrailer = messages.some((text) => TRAILER.test(text));+ const hasCoAuthor = messages.some((text) => CO_AUTHOR.test(text));+ return hasTrailer && !hasCoAuthor ? null : EXPECTED;+}++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");+ } catch {+ return null;+ }+}++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());+ if (reason) {+ process.stderr.write(`${reason}\n`);+ process.exit(2);+ }+}diff --git a/.claude/hooks/check-commit-attribution.test.mjs b/.claude/hooks/check-commit-attribution.test.mjsnew file mode 100644index 0000000000..8f6ff13a1f--- /dev/null+++ b/.claude/hooks/check-commit-attribution.test.mjs@@ -0,0 +1,81 @@+// 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 { tmpdir } from "node:os";+import { join } from "node:path";+import { describe, it } from "node:test";+import { fileURLToPath } from "node:url";++import { checkCommand } from "./check-commit-attribution.mjs";++const TRAILER = "Assisted-by: Claude Code (Claude Opus 5.5)";+const dir = mkdtempSync(join(tmpdir(), "commit-attribution-"));++function message(name, text) {+ writeFileSync(join(dir, name), text);+ return name;+}++describe("checkCommand", () => {+ it("passes commands that do not commit, or commit without a message of their own", () => {+ for (const command of [+ "git status --short",+ "grep -rn \"git commit -m wip\" docs",+ "grep -E \"^# pass|git commit -m wip\" out.txt",+ "git log --oneline -5",+ "git commit --amend --no-edit",+ "git commit-graph write"+ ]) {+ assert.equal(checkCommand(command, dir), null, command);+ }+ });++ it("passes a message carrying the trailer, inline or in a file", () => {+ const good = message("good.txt", `fix(x): y\n\nBody.\n\n${TRAILER}\n`);+ for (const command of [+ `git commit -F ${good} -- a.ts`,+ `git add a.ts && git -C . commit --file="${good}"`,+ `git commit -m "fix(x): y" -m "${TRAILER}"`,+ `git commit -F - <<'EOF'\nfix(x): y\n\n${TRAILER}\nEOF`,+ `git commit-tree HEAD^{tree} -p HEAD -F ${good}`+ ]) {+ assert.equal(checkCommand(command, dir), null, 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");+ const coAuthor = message("co-author.txt",+ `fix(x): y\n\n${TRAILER}\nCo-Authored-By: Claude Opus 5.5 <[email protected]>\n`);+ for (const command of [+ `git commit -F ${none} -- a.ts`,+ `git commit -F ${placeholder}`,+ `git commit -F ${coAuthor}`,+ "git commit -am \"fix(x): y\"",+ "git commit -m \"fix(x): y\" -m \"Assisted-by: Claude Code\"",+ "cd sub; git commit --message=wip",+ "git commit -F missing.txt",+ `git commit -m a -m "${TRAILER}"; git commit -m b`+ ]) {+ assert.match(checkCommand(command, dir) ?? "", /Assisted-by: Claude Code \(/, command);+ }+ });+});++describe("the hook", () => {+ const script = fileURLToPath(new URL("./check-commit-attribution.mjs", import.meta.url));+ const run = (command) => spawnSync(process.execPath, [ script ], {+ input: JSON.stringify({ tool_name: "Bash", tool_input: { command }, cwd: dir }),+ encoding: "utf8"+ });++ it("exits with 2 and explains a blocked commit, and with 0 otherwise", () => {+ const blocked = run("git commit -m wip");+ assert.equal(blocked.status, 2);+ assert.match(blocked.stderr, /Assisted-by/);++ assert.equal(run("git status").status, 0);+ });+});diff --git a/.claude/settings.json b/.claude/settings.jsonnew file mode 100644index 0000000000..1035223b93--- /dev/null+++ b/.claude/settings.json@@ -0,0 +1,20 @@+{+ "$schema": "https://json.schemastore.org/claude-code-settings.json",+ "attribution": {+ "commit": "Assisted-by: Claude Code",+ "pr": ""+ },+ "hooks": {+ "PreToolUse": [+ {+ "matcher": "Bash|PowerShell",+ "hooks": [+ {+ "type": "command",+ "command": "node \"$CLAUDE_PROJECT_DIR/.claude/hooks/check-commit-attribution.mjs\""+ }+ ]+ }+ ]+ }+}diff --git a/.gitignore b/.gitignoreindex bf1a1c63ac..a3adf6b810 100644--- a/.gitignore+++ b/.gitignore@@ -60,8 +60,10 @@ scripts/translation/.language*.json# Capacitor regenerates this on every `cap sync` and produces different# output on Windows vs macOS, causing constant churn — keep it local.apps/mobile/ios/App/CapApp-SPM/Package.swift-# Ignore local Claude Code settings, but keep shared project skills.+# Ignore local Claude Code settings, but keep shared project settings and skills..claude/*+!.claude/settings.json+!.claude/hooks/!.claude/skills/.pnpm-storeapps/edit-docs/output.zip