Add prepare-commit-msg commit message hook

main
Buddy Sandidge 4 years ago
parent 0c03ecd167
commit 356639f900

@ -1,3 +1,5 @@
[url "git@github.com:HelloTech"]
insteadOf = https://github.com/HelloTech
email = buddy@hellotech.com
[init]
templateDir = ~/.config/git/template

@ -0,0 +1,37 @@
#!/usr/bin/env node
const fs = require("fs");
const childProcess = require("child_process");
const { promisify } = require("util");
const exec = promisify(childProcess.exec);
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const [, , commitMsg, comitSrc, sha1] = process.argv;
async function main() {
const file = await readFile(commitMsg, { encoding: "utf8" });
const result = await exec("git symbolic-ref --short HEAD");
const tags = result.stdout
.split("\n")
.map(branch => branch.match(/[A-Z]+-\d+/g))
.filter(x => !!x)
.reduce((allIDs, matchedIDs) => [...allIDs, ...matchedIDs], [])
.map(id => `[${id}]`);
if (tags.length === 0) {
return;
}
if (tags.find(tag => file.includes(tag))) {
return;
}
const newFile = "\n\n# " + tags.join(" ") + "\n" + file;
await writeFile(commitMsg, newFile);
}
main().catch(err => {
console.error(err);
process.exit(1);
});
Loading…
Cancel
Save