You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.3 KiB
JavaScript

// chezmoi:template:left-delimiter="/*{{"
// chezmoi:template:right-delimiter="}}*/"
/*{{- /* vim: set filetype=javascript: */ -}}*/
/*{{ if lookPath "node" -}}*/
#!/usr/bin/env node
// Add a "# [TICKET-ID]" comment hint to the editor when running a plain
// `git commit` on a branch containing a ticket ID (e.g. buddy/BE-1234/foo,
// fix/BE-1234/bar). The hint is a comment, so git strips it unless you
// uncomment or retype it.
//
// Every other way a commit message gets made is left alone: -m/-F
// ("message"), merge, squash, amend/-c ("commit"), and commit templates
// ("template") all arrive with a mode argument, and we exit immediately.
import * as fs from "node:fs";
import { execSync } from "node:child_process";
const [, , messagePath, mode] = process.argv;
if (mode !== undefined) process.exit(0);
let branch;
try {
branch = execSync("git rev-parse --abbrev-ref HEAD", {
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
}).trim();
} catch {
process.exit(0); // detached HEAD, rebase, etc.
}
const ids = branch.match(/[A-Z]+-\d+/g);
if (!ids) process.exit(0);
const tags = [...new Set(ids)].map(id => `[${id}]`);
const file = fs.readFileSync(messagePath, "utf8");
if (tags.some(tag => file.includes(tag))) process.exit(0);
fs.writeFileSync(messagePath, `\n\n# ${tags.join(" ")}\n${file}`);
/*{{- end }}*/