Compare commits
2 Commits
security/f
...
feature/co
| Author | SHA1 | Date | |
|---|---|---|---|
| 758828c70a | |||
| 73e7967735 |
13
CLAUDE.md
13
CLAUDE.md
@@ -29,6 +29,19 @@ td session --new # force a new session in the same terminal context
|
||||
|
||||
Task state is stored in `.todos/issues.db` (SQLite).
|
||||
|
||||
## Git Hooks
|
||||
|
||||
A commit-msg hook normalizes commit messages on every commit (capitalizes subject, strips trailing period, trims whitespace, warns when subject exceeds 72 characters). The hook never blocks a commit.
|
||||
|
||||
**Wire hooks after cloning:**
|
||||
```bash
|
||||
sh scripts/install-hooks.sh
|
||||
# or via npm script:
|
||||
cd scripts && npm run hooks:install
|
||||
```
|
||||
|
||||
The hook script lives at `scripts/commit-msg` and is invoked by `.git/hooks/commit-msg`. The normalizer logic is in `scripts/normalize-commit-msg.js` with unit tests in `scripts/__tests__/normalize-commit-msg.test.js` (run with `cd scripts && npm test`).
|
||||
|
||||
## Development
|
||||
|
||||
**Run production stack (Docker):**
|
||||
|
||||
90
scripts/__tests__/normalize-commit-msg.test.js
Normal file
90
scripts/__tests__/normalize-commit-msg.test.js
Normal file
@@ -0,0 +1,90 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { normalizeSubject, normalizeMessage } from '../normalize-commit-msg.js';
|
||||
|
||||
describe('normalizeSubject', () => {
|
||||
it('passes an already-valid subject unchanged', () => {
|
||||
const { subject, warned } = normalizeSubject('Add feature flag support');
|
||||
expect(subject).toBe('Add feature flag support');
|
||||
expect(warned).toBe(false);
|
||||
});
|
||||
|
||||
it('capitalizes the first letter', () => {
|
||||
const { subject } = normalizeSubject('add feature flag support');
|
||||
expect(subject).toBe('Add feature flag support');
|
||||
});
|
||||
|
||||
it('strips a trailing period', () => {
|
||||
const { subject } = normalizeSubject('Add feature flag support.');
|
||||
expect(subject).toBe('Add feature flag support');
|
||||
});
|
||||
|
||||
it('trims leading whitespace', () => {
|
||||
const { subject } = normalizeSubject(' Fix the bug');
|
||||
expect(subject).toBe('Fix the bug');
|
||||
});
|
||||
|
||||
it('trims trailing whitespace', () => {
|
||||
const { subject } = normalizeSubject('Fix the bug ');
|
||||
expect(subject).toBe('Fix the bug');
|
||||
});
|
||||
|
||||
it('capitalizes and strips period together', () => {
|
||||
const { subject } = normalizeSubject('fix the bug.');
|
||||
expect(subject).toBe('Fix the bug');
|
||||
});
|
||||
|
||||
it('does not strip a period that is not trailing', () => {
|
||||
const { subject } = normalizeSubject('Fix bug in v1.0 release');
|
||||
expect(subject).toBe('Fix bug in v1.0 release');
|
||||
});
|
||||
|
||||
it('warns when subject exceeds 72 characters', () => {
|
||||
const long = 'A'.repeat(73);
|
||||
const { warned } = normalizeSubject(long);
|
||||
expect(warned).toBe(true);
|
||||
});
|
||||
|
||||
it('does not warn when subject is exactly 72 characters', () => {
|
||||
const exact = 'A'.repeat(72);
|
||||
const { warned } = normalizeSubject(exact);
|
||||
expect(warned).toBe(false);
|
||||
});
|
||||
|
||||
it('does not warn when subject is under 72 characters', () => {
|
||||
const { warned } = normalizeSubject('Short message');
|
||||
expect(warned).toBe(false);
|
||||
});
|
||||
|
||||
it('handles an empty subject gracefully', () => {
|
||||
const { subject, warned } = normalizeSubject('');
|
||||
expect(subject).toBe('');
|
||||
expect(warned).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeMessage', () => {
|
||||
it('normalizes only the subject line of a multi-line message', () => {
|
||||
const input = 'fix the bug.\n\nThis is the body paragraph.';
|
||||
const { message } = normalizeMessage(input);
|
||||
expect(message).toBe('Fix the bug\n\nThis is the body paragraph.');
|
||||
});
|
||||
|
||||
it('skips comment lines when finding the subject', () => {
|
||||
const input = '# Comment\nfix the bug.';
|
||||
const { message } = normalizeMessage(input);
|
||||
expect(message).toBe('# Comment\nFix the bug');
|
||||
});
|
||||
|
||||
it('returns warned true for long subject inside full message', () => {
|
||||
const longSubject = 'x'.repeat(73);
|
||||
const input = `${longSubject}\n\nBody.`;
|
||||
const { warned } = normalizeMessage(input);
|
||||
expect(warned).toBe(true);
|
||||
});
|
||||
|
||||
it('preserves body lines exactly as-is', () => {
|
||||
const input = 'Fix bug\n\n - detail one\n - detail two.';
|
||||
const { message } = normalizeMessage(input);
|
||||
expect(message).toBe('Fix bug\n\n - detail one\n - detail two.');
|
||||
});
|
||||
});
|
||||
4
scripts/commit-msg
Executable file
4
scripts/commit-msg
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
# Git commit-msg hook — delegates to normalize-commit-msg.js
|
||||
# This file is symlinked into .git/hooks/commit-msg by scripts/install-hooks.sh
|
||||
node "$(git rev-parse --show-toplevel)/scripts/normalize-commit-msg.js" "$1"
|
||||
34
scripts/install-hooks.sh
Executable file
34
scripts/install-hooks.sh
Executable file
@@ -0,0 +1,34 @@
|
||||
#!/bin/sh
|
||||
# install-hooks.sh
|
||||
# Installs the project's git hooks into .git/hooks/.
|
||||
# Run this once after cloning: sh scripts/install-hooks.sh
|
||||
|
||||
set -e
|
||||
|
||||
REPO_ROOT="$(git rev-parse --show-toplevel)"
|
||||
HOOKS_DIR="$REPO_ROOT/.git/hooks"
|
||||
SCRIPTS_DIR="$REPO_ROOT/scripts"
|
||||
|
||||
install_hook() {
|
||||
local name="$1"
|
||||
local src="$SCRIPTS_DIR/$name"
|
||||
local dst="$HOOKS_DIR/$name"
|
||||
|
||||
if [ ! -f "$src" ]; then
|
||||
echo "install-hooks: source not found: $src" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [ -f "$dst" ] && [ ! -L "$dst" ]; then
|
||||
echo "install-hooks: warning: $dst already exists and is not a symlink — skipping"
|
||||
return 0
|
||||
fi
|
||||
|
||||
ln -sf "$src" "$dst"
|
||||
chmod +x "$src"
|
||||
echo "install-hooks: installed $name -> $dst"
|
||||
}
|
||||
|
||||
install_hook "commit-msg"
|
||||
|
||||
echo "install-hooks: done"
|
||||
95
scripts/normalize-commit-msg.js
Executable file
95
scripts/normalize-commit-msg.js
Executable file
@@ -0,0 +1,95 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* normalize-commit-msg.js
|
||||
*
|
||||
* Git commit-msg hook: reads the commit message file, applies normalization
|
||||
* rules to the subject line, rewrites the file in place.
|
||||
*
|
||||
* Rules:
|
||||
* 1. Trim leading/trailing whitespace from the subject line
|
||||
* 2. Capitalize the first letter of the subject
|
||||
* 3. Strip a trailing period from the subject
|
||||
* 4. Warn (but do not block) if the subject exceeds 72 characters
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
const MAX_SUBJECT_LEN = 72;
|
||||
|
||||
/**
|
||||
* Normalize the subject line of a commit message.
|
||||
* Returns { subject, warned } where warned is true if a length warning was emitted.
|
||||
*
|
||||
* @param {string} subject
|
||||
* @returns {{ subject: string, warned: boolean }}
|
||||
*/
|
||||
function normalizeSubject(subject) {
|
||||
let s = subject.trimEnd();
|
||||
|
||||
// Trim leading whitespace
|
||||
s = s.trimStart();
|
||||
|
||||
// Capitalize first letter
|
||||
if (s.length > 0) {
|
||||
s = s[0].toUpperCase() + s.slice(1);
|
||||
}
|
||||
|
||||
// Strip trailing period
|
||||
if (s.endsWith('.')) {
|
||||
s = s.slice(0, -1);
|
||||
}
|
||||
|
||||
const warned = s.length > MAX_SUBJECT_LEN;
|
||||
|
||||
return { subject: s, warned };
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a full commit message string.
|
||||
* Only the subject line (first non-empty, non-comment line) is modified.
|
||||
*
|
||||
* @param {string} message
|
||||
* @returns {{ message: string, warned: boolean }}
|
||||
*/
|
||||
function normalizeMessage(message) {
|
||||
const lines = message.split('\n');
|
||||
let warned = false;
|
||||
|
||||
// Find the subject line (first non-comment line)
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
if (!line.startsWith('#')) {
|
||||
const result = normalizeSubject(line);
|
||||
lines[i] = result.subject;
|
||||
warned = result.warned;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return { message: lines.join('\n'), warned };
|
||||
}
|
||||
|
||||
// Only run as a hook when invoked directly (not when required in tests)
|
||||
if (require.main === module) {
|
||||
const msgFile = process.argv[2];
|
||||
if (!msgFile) {
|
||||
process.stderr.write('commit-msg hook: no message file argument\n');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const original = fs.readFileSync(msgFile, 'utf8');
|
||||
const { message, warned } = normalizeMessage(original);
|
||||
|
||||
if (warned) {
|
||||
process.stderr.write(
|
||||
`commit-msg warning: subject line exceeds ${MAX_SUBJECT_LEN} characters — consider shortening it.\n`
|
||||
);
|
||||
}
|
||||
|
||||
fs.writeFileSync(msgFile, message, 'utf8');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
module.exports = { normalizeSubject, normalizeMessage };
|
||||
1249
scripts/package-lock.json
generated
Normal file
1249
scripts/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
scripts/package.json
Normal file
12
scripts/package.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "budget-scripts",
|
||||
"version": "1.0.0",
|
||||
"scripts": {
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest",
|
||||
"hooks:install": "sh install-hooks.sh"
|
||||
},
|
||||
"devDependencies": {
|
||||
"vitest": "^4.1.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user