Skip to content

Build your personal knowledge base with Trilium Notes

chore(claude): read commit messages from paths holding variables

Elian Doran13999115536d
diff --git a/.claude/hooks/check-commit-attribution.mjs b/.claude/hooks/check-commit-attribution.mjs
index afac52b595..795fb65095 100644
--- a/.claude/hooks/check-commit-attribution.mjs
+++ b/.claude/hooks/check-commit-attribution.mjs
@@ -11,6 +11,9 @@ 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;
+/** `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 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 "
@@ -23,7 +26,8 @@ export function checkCommand(command, cwd) {
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);
+ const variables = readAssignments(command.slice(0, start));
+ const reason = checkCommit(command.slice(start, starts[index + 1]), cwd, variables);
if (reason) {
return reason;
}
@@ -31,7 +35,7 @@ export function checkCommand(command, cwd) {
return null;
}
-function checkCommit(args, cwd) {
+function checkCommit(args, cwd, variables) {
if (!MESSAGE_FLAG.test(args)) {
// No message of its own, as with `--amend --no-edit`.
return null;
@@ -39,10 +43,15 @@ function checkCommit(args, cwd) {
const messages = [ args ];
for (const [ , quoted ] of args.matchAll(FILE_FLAG)) {
- const path = quoted.replace(/^["']|["']$/g, "");
- if (path !== "-") {
- messages.push(readMessage(path, cwd) ?? "");
+ const path = unquote(quoted, variables);
+ if (path === "-") {
+ continue;
}
+ const text = readMessage(path, cwd);
+ if (text === null) {
+ return unreadable(path);
+ }
+ messages.push(text);
}
const hasTrailer = messages.some((text) => TRAILER.test(text));
@@ -50,6 +59,35 @@ function checkCommit(args, cwd) {
return hasTrailer && !hasCoAuthor ? null : EXPECTED;
}
+/**
+ * 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.
+ */
+function readAssignments(command) {
+ const variables = {};
+ for (const [ , name, value ] of command.matchAll(ASSIGNMENT)) {
+ variables[name] = unquote(value, variables);
+ }
+ return variables;
+}
+
+/** A shell word as the shell reads it: single quotes taken literally, any other expanded. */
+function unquote(word, variables) {
+ if (word.startsWith("'")) {
+ return word.slice(1, -1);
+ }
+ return word.replace(/^"|"$/g, "").replace(VARIABLE, (whole, braced, bare) => {
+ const name = braced ?? bare;
+ return variables[name] ?? process.env[name] ?? whole;
+ });
+}
+
+function unreadable(path) {
+ return `Could not read the commit message file \`${path}\`. This hook runs before the command, `
+ + "so a file the same command writes does not exist yet: write the message in a separate "
+ + "command first, then commit with its 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"
diff --git a/.claude/hooks/check-commit-attribution.test.mjs b/.claude/hooks/check-commit-attribution.test.mjs
index 8f6ff13a1f..b80337faad 100644
--- a/.claude/hooks/check-commit-attribution.test.mjs
+++ b/.claude/hooks/check-commit-attribution.test.mjs
@@ -44,6 +44,37 @@ describe("checkCommand", () => {
}
});
+ it("reads a message file whose path holds a variable, set before the commit or in the environment", () => {
+ message("good.txt", `fix(x): y
+
+${TRAILER}
+`);
+ process.env.COMMIT_ATTRIBUTION_TEST_DIR = dir;
+ for (const command of [
+ `S="${dir}" && git commit -F "$S/good.txt" -- a.ts`,
+ `export S='${dir}'; git commit --file="\${S}/good.txt"`,
+ "git commit -F \"$COMMIT_ATTRIBUTION_TEST_DIR/good.txt\"",
+ `S="${dir}"; T="$S/good.txt"; git commit -F "$T"`
+ ]) {
+ assert.equal(checkCommand(command, "."), null, 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",
+ "git commit -F \"$UNSET_COMMIT_ATTRIBUTION_DIR/msg.txt\"",
+ `cat > new.txt <<'EOF'
+fix(x): y
+
+${TRAILER}
+EOF
+git commit -F new.txt`
+ ]) {
+ assert.match(checkCommand(command, dir) ?? "", /Could not read the commit message file/, 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");
@@ -56,7 +87,6 @@ describe("checkCommand", () => {
"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);