From 356639f900879d7d934b28ef9277fbc828d65bb6 Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Tue, 29 Dec 2020 00:39:11 -0800 Subject: [PATCH] Add prepare-commit-msg commit message hook --- hellotech/.config/git/config-hellotech | 2 + .../git/template/hooks/prepare-commit-msg | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100755 hellotech/.config/git/template/hooks/prepare-commit-msg diff --git a/hellotech/.config/git/config-hellotech b/hellotech/.config/git/config-hellotech index f225c74..7c6f31d 100644 --- a/hellotech/.config/git/config-hellotech +++ b/hellotech/.config/git/config-hellotech @@ -1,3 +1,5 @@ [url "git@github.com:HelloTech"] insteadOf = https://github.com/HelloTech email = buddy@hellotech.com +[init] + templateDir = ~/.config/git/template diff --git a/hellotech/.config/git/template/hooks/prepare-commit-msg b/hellotech/.config/git/template/hooks/prepare-commit-msg new file mode 100755 index 0000000..6827ed3 --- /dev/null +++ b/hellotech/.config/git/template/hooks/prepare-commit-msg @@ -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); +});