Skip to content

Commit 1851fba

Browse files
committed
Clean up code and document edge cases before release
1 parent 50830d1 commit 1851fba

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

src/git.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ import { Lifecycle } from './types';
44

55
const git = SimpleGit();
66

7+
/**
8+
* Gets the second item on the reflog. In post-merge hook, this
9+
* should be the HEAD of the branch before the merge.
10+
*
11+
* If this assumption turns out not to be correct, this may
12+
* under- or over-report notifications
13+
*/
714
export function getLastRef(): Promise<string> {
815
return git
916
.raw('reflog', '--pretty=format:"%h"', '--no-patch', '-1', 'HEAD@{1}')
@@ -13,17 +20,20 @@ export function getLastRef(): Promise<string> {
1320
export async function getLogStream(lifecycle: Lifecycle, args: string[]) {
1421
switch (lifecycle) {
1522
case 'merge':
23+
// all commits since the previous HEAD on this branch
1624
return log({ from: await getLastRef() });
1725

1826
case 'rewrite':
1927
// perhaps not the most accurate method, PRs welcome
2028
return log({ from: 'origin', to: 'HEAD' });
2129

2230
case 'checkout':
31+
// post-checkout hook receives old and new branch HEAD as args
2332
const [from, to] = args;
2433
return log({ from, to });
2534

2635
case 'since':
36+
// since is our own command, not called by any git hook
2737
return log({ from: args[0] });
2838

2939
default:

src/index.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
// import commits from 'git-raw-commits';
21
import meow from 'meow';
32
import showNotifications from './showNotifications';
4-
import { Lifecycle, Flags } from './types';
53
import { getLogStream } from './git';
6-
7-
async function hook(lifecycle: Lifecycle, args: string[], flags: Flags) {
8-
// get all commit messages for the relevant revision range
9-
// depending on which git hook / command was executed
10-
const logs = await getLogStream(lifecycle, args);
11-
12-
// stream through logs and print any found notifications
13-
showNotifications(logs, flags);
14-
}
4+
import { Lifecycle, Flags } from './types';
155

166
const cli = meow(
177
`
@@ -60,4 +50,19 @@ const cli = meow(
6050
}
6151
);
6252

63-
hook(cli.input[0] as Lifecycle, cli.input.slice(1), cli.flags);
53+
async function hook(lifecycle: Lifecycle, args: string[], flags: Flags) {
54+
// get all commit messages for the relevant revision range
55+
// depending on which git hook / command was executed
56+
const logs = await getLogStream(lifecycle, args);
57+
58+
// stream through logs and print any found notifications
59+
showNotifications(logs, flags);
60+
}
61+
62+
// first argument is the git hook method
63+
const lifecycle = cli.input[0] as Lifecycle;
64+
65+
// rest of the positional args come from the git hook
66+
const gitHookArgs = cli.input.slice(1);
67+
68+
hook(lifecycle, gitHookArgs, cli.flags);

src/showNotifications.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import detectNewline from 'detect-newline';
44
import boxen from 'boxen';
55
import chalk from 'chalk';
66

7+
/**
8+
* Reads given stream of git log messages and prints
9+
* out any lines prefixed with the git-notify tag
10+
*/
711
export default async function showNotifications(
812
messageStream: Stream.Readable,
913
flags: Flags

0 commit comments

Comments
 (0)