#!/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); });