forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.ts
81 lines (70 loc) · 1.8 KB
/
read.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import minimist from 'minimist';
import type {GitOptions} from 'git-raw-commits';
import {getHistoryCommits} from './get-history-commits.js';
import {getEditCommit} from './get-edit-commit.js';
import {execa} from 'execa';
interface GetCommitMessageOptions {
cwd?: string;
from?: string;
fromLastTag?: boolean;
to?: string;
last?: boolean;
edit?: boolean | string;
gitLogArgs?: string;
}
// Get commit messages
export default async function getCommitMessages(
settings: GetCommitMessageOptions
): Promise<string[]> {
const {cwd, fromLastTag, to, last, edit, gitLogArgs} = settings;
let from = settings.from;
if (edit) {
return getEditCommit(cwd, edit);
}
if (last) {
const gitCommandResult = await execa(
'git',
['log', '-1', '--pretty=format:%B'],
{cwd}
);
let output = gitCommandResult.stdout;
// strip output of extra quotation marks ("")
if (output[0] == '"' && output[output.length - 1] == '"')
output = output.slice(1, -1);
return [output];
}
if (!from && fromLastTag) {
const {stdout} = await execa(
'git',
[
'describe',
'--abbrev=40',
'--always',
'--first-parent',
'--long',
'--tags',
],
{cwd}
);
if (stdout.length === 40) {
// Hash only means no last tag. Use that as the from ref which
// results in a no-op.
from = stdout;
} else {
// Description will be in the format: <tag>-<count>-g<hash>
// Example: v3.2.0-11-g9057371a52adaae5180d93fe4d0bb808d874b9fb
// Minus zero based (1), dash (1), "g" prefix (1), hash (40) = -43
const tagSlice = stdout.lastIndexOf('-', stdout.length - 43);
from = stdout.slice(0, tagSlice);
}
}
let gitOptions: GitOptions = {from, to};
if (gitLogArgs) {
gitOptions = {
...minimist(gitLogArgs.split(' ')),
from,
to,
};
}
return getHistoryCommits(gitOptions, {cwd});
}