Skip to content

Commit 0116609

Browse files
author
Ben Quarmby
committed
feat: support linting from the last tag
1 parent a2f27fa commit 0116609

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

Diff for: @commitlint/cli/src/cli.ts

+7
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ const cli = yargs(process.argv.slice(2))
9090
'lower end of the commit range to lint; applies if edit=false',
9191
type: 'string',
9292
},
93+
'from-last-tag': {
94+
description:
95+
'uses the last tag as the lower end of the commit range to lint; applies if edit=false and from is not set',
96+
type: 'boolean',
97+
},
9398
'git-log-args': {
9499
description:
95100
"additional git log arguments as space separated string, example '--first-parent --cherry-pick'",
@@ -237,6 +242,7 @@ async function main(args: MainArgs): Promise<void> {
237242
: read({
238243
to: flags.to,
239244
from: flags.from,
245+
fromLastTag: flags['from-last-tag'],
240246
last: flags.last,
241247
edit: flags.edit,
242248
cwd: flags.cwd,
@@ -395,6 +401,7 @@ function checkFromEdit(flags: CliFlags): boolean {
395401
function checkFromHistory(flags: CliFlags): boolean {
396402
return (
397403
typeof flags.from === 'string' ||
404+
typeof flags['from-last-tag'] === 'boolean' ||
398405
typeof flags.to === 'string' ||
399406
typeof flags.last === 'boolean'
400407
);

Diff for: @commitlint/cli/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface CliFlags {
88
help?: boolean;
99
'help-url'?: string;
1010
from?: string;
11+
'from-last-tag'?: boolean;
1112
'git-log-args'?: string;
1213
last?: boolean;
1314
format?: string;

Diff for: @commitlint/read/src/read.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,42 @@ import {execa} from 'execa';
99
interface GetCommitMessageOptions {
1010
cwd?: string;
1111
from?: string;
12+
fromLastTag?: boolean;
1213
to?: string;
1314
last?: boolean;
1415
edit?: boolean | string;
1516
gitLogArgs?: string;
1617
}
1718

19+
async function getLastTag() {
20+
const {stdout} = await execa('git', [
21+
'describe',
22+
'--abbrev=40',
23+
'--always',
24+
'--first-parent',
25+
'--long',
26+
'--tags',
27+
]);
28+
29+
if (stdout.length === 40) {
30+
// Hash only means no last tag. Use that as the from ref.
31+
return stdout;
32+
}
33+
34+
// Description will be in the format: <tag>-<count>-g<hash>
35+
// Example: v3.2.0-11-g9057371a52adaae5180d93fe4d0bb808d874b9fb
36+
// Minus zero based (1), dash (1), "g" prefix (1), hash (40) = -43
37+
const tagSlice = stdout.lastIndexOf('-', stdout.length - 43);
38+
39+
return stdout.slice(0, tagSlice);
40+
}
41+
1842
// Get commit messages
1943
export default async function getCommitMessages(
2044
settings: GetCommitMessageOptions
2145
): Promise<string[]> {
22-
const {cwd, from, to, last, edit, gitLogArgs} = settings;
46+
const {cwd, fromLastTag, to, last, edit, gitLogArgs} = settings;
47+
let from = settings.from;
2348

2449
if (edit) {
2550
return getEditCommit(cwd, edit);
@@ -38,6 +63,10 @@ export default async function getCommitMessages(
3863
return [output];
3964
}
4065

66+
if (!from && fromLastTag) {
67+
from = await getLastTag();
68+
}
69+
4170
let gitOptions: GitOptions = {from, to};
4271
if (gitLogArgs) {
4372
gitOptions = {

Diff for: docs/reference/cli.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Options:
2222
-H, --help-url help url in error message [string]
2323
-f, --from lower end of the commit range to lint; applies if
2424
edit=false [string]
25+
--from-last-tag uses the last tag as the lower end of the commit range to
26+
lint; applies if edit=false and from is not set [boolean]
2527
-o, --format output format of the results [string]
2628
-p, --parser-preset configuration preset to use for
2729
conventional-commits-parser [string]

0 commit comments

Comments
 (0)