fix: prepare commit message hook no longer fires on merge/squash/etc
This commit is contained in:
parent
329cf76ff9
commit
6e6961544e
@ -4,52 +4,38 @@
|
|||||||
/*{{ if lookPath "node" -}}*/
|
/*{{ if lookPath "node" -}}*/
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const fs = require("fs");
|
// Add a "# [TICKET-ID]" comment hint to the editor when running a plain
|
||||||
const childProcess = require("child_process");
|
// `git commit` on a branch containing a ticket ID (e.g. buddy/BE-1234/foo,
|
||||||
const { promisify } = require("util");
|
// 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.
|
||||||
|
|
||||||
const exec = promisify(childProcess.exec);
|
import * as fs from "node:fs";
|
||||||
const readFile = promisify(fs.readFile);
|
import { execSync } from "node:child_process";
|
||||||
const writeFile = promisify(fs.writeFile);
|
|
||||||
|
|
||||||
const [, , message, mode] = process.argv;
|
const [, , messagePath, mode] = process.argv;
|
||||||
|
|
||||||
async function main() {
|
if (mode !== undefined) process.exit(0);
|
||||||
const file = await readFile(message, { encoding: "utf8" });
|
|
||||||
let result;
|
let branch;
|
||||||
try {
|
try {
|
||||||
result = await exec("git rev-parse --abbrev-ref HEAD");
|
branch = execSync("git rev-parse --abbrev-ref HEAD", {
|
||||||
} catch (err) {
|
encoding: "utf8",
|
||||||
return
|
stdio: ["ignore", "pipe", "ignore"],
|
||||||
|
}).trim();
|
||||||
|
} catch {
|
||||||
|
process.exit(0); // detached HEAD, rebase, etc.
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.match(/^#/)) {
|
const ids = branch.match(/[A-Z]+-\d+/g);
|
||||||
return
|
if (!ids) process.exit(0);
|
||||||
}
|
|
||||||
|
|
||||||
const tags = result.stdout
|
const tags = [...new Set(ids)].map(id => `[${id}]`);
|
||||||
.split("\n")
|
const file = fs.readFileSync(messagePath, "utf8");
|
||||||
.map(branch => branch.match(/[A-Z]+-\d+/g))
|
if (tags.some(tag => file.includes(tag))) process.exit(0);
|
||||||
.filter(x => !!x)
|
|
||||||
.reduce((allIDs, matchedIDs) => [...allIDs, ...matchedIDs], [])
|
|
||||||
.map(id => `[${id}]`);
|
|
||||||
|
|
||||||
if (tags.length === 0) {
|
fs.writeFileSync(messagePath, `\n\n# ${tags.join(" ")}\n${file}`);
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (tags.find(tag => file.includes(tag))) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const newFile = mode === 'message'
|
|
||||||
? file + '\n\n' + tags.join(" ")
|
|
||||||
: `\n\n# ` + tags.join(" ") + "\n" + file;
|
|
||||||
|
|
||||||
await writeFile(message, newFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
main().catch(err => {
|
|
||||||
console.error(err);
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
/*{{- end }}*/
|
/*{{- end }}*/
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user