git-notify
Version:
Communicate important updates to your team via git commit messages
158 lines (132 loc) • 4.65 kB
JavaScript
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var meow = _interopDefault(require('meow'));
var detectNewline = _interopDefault(require('detect-newline'));
var boxen = _interopDefault(require('boxen'));
var chalk = _interopDefault(require('chalk'));
var SimpleGit = _interopDefault(require('simple-git'));
var log = _interopDefault(require('git-raw-commits'));
/**
* Reads given stream of git log messages and prints
* out any lines prefixed with the git-notify tag
*/
async function showNotifications(messageStream, flags) {
let detectedNewline; // for each commit message
for await (const chunk of messageStream) {
// read message in stream
const message = chunk.toString();
detectedNewline = detectedNewline || detectNewline(message); // for each paragraph in message
const newline = detectedNewline || '\n';
const paragraphs = message.split(`${newline}${newline}`);
for (const paragraph of paragraphs) {
const notifications = paragraph.split(flags.prefix).slice(1); // for each notification on this paragraph
notifications.forEach(notification => {
// display notification
notify(notification.trim(), flags);
});
}
}
}
function notify(message, flags) {
const chalkPreset = chalk[flags.color];
const simpleColor = chalkPreset || chalk.hex(flags.color);
const borderColor = chalkPreset ? flags.color : flags.color.startsWith('#') ? flags.color : `#${flags.color}`;
const display = flags.simple ? simpleColor(message) : boxen(message, {
padding: 1,
margin: 1,
borderStyle: 'doubleSingle',
borderColor,
float: 'center',
align: 'center'
}); // notify to console
console.log(display);
}
const git = /*#__PURE__*/SimpleGit();
/**
* Gets the second item on the reflog. In post-merge hook, this
* should be the HEAD of the branch before the merge.
*
* If this assumption turns out not to be correct, this may
* under- or over-report notifications
*/
function getLastRef() {
return git.raw('reflog', '--pretty=format:"%h"', '--no-patch', '-1', 'HEAD@{1}').then(value => value.replace(/"/g, ''));
}
async function getLogStream(lifecycle, args) {
switch (lifecycle) {
case 'merge':
// all commits since the previous HEAD on this branch
return log({
from: await getLastRef()
});
case 'rewrite':
// perhaps not the most accurate method, PRs welcome
return log({
from: 'origin',
to: 'HEAD'
});
case 'checkout':
// post-checkout hook receives old and new branch HEAD as args
const [from, to] = args;
return log({
from,
to
});
case 'since':
// since is our own command, not called by any git hook
return log({
from: args[0]
});
default:
throw new Error('[git-notify] unsupported git hook: ' + lifecycle);
}
}
const cli = /*#__PURE__*/meow(`
Usage
$ git-notify <method> [options] $GIT_PARAMS
Methods
since <commit> show all notifications since commit
merge run on git pull/merge
rewrite run on git rebase
checkout run on git checkout/switch
Options
--prefix, -p prefix to look for in commit messages (default: "git-notify:")
--simple, -s show a plain, unboxed notification
--color, -c color of displayed notification
Examples
$ git-notify since HEAD~5
$ git-notify checkout $GIT_PARAMS --prefix "\@everyone:"
$ git-notify merge $GIT_PARAMS --simple --color "\#ff6f6f"
`, {
flags: {
toast: {
type: 'boolean',
alias: 't'
},
simple: {
type: 'boolean',
alias: 's'
},
prefix: {
type: 'string',
alias: 'p',
default: 'git-notify:'
},
color: {
type: 'string',
alias: 'c',
default: '#ff6f6f',
description: 'hex value like #ff0000, or one of: black, red, green, yellow, blue, magenta, cyan, white, gray'
}
}
});
async function hook(lifecycle, args, flags) {
// get all commit messages for the relevant revision range
// depending on which git hook / command was executed
const logs = await getLogStream(lifecycle, args); // stream through logs and print any found notifications
showNotifications(logs, flags);
} // first argument is the git hook method
const lifecycle = cli.input[0]; // rest of the positional args come from the git hook
const gitHookArgs = /*#__PURE__*/cli.input.slice(1);
hook(lifecycle, gitHookArgs, cli.flags);
//# sourceMappingURL=git-notify.cjs.development.js.map
;