@@ -2,6 +2,7 @@ import execa, {ExecaError} from 'execa';
2
2
import load from '@commitlint/load' ;
3
3
import lint from '@commitlint/lint' ;
4
4
import read from '@commitlint/read' ;
5
+ import { simpleGit } from 'simple-git' ;
5
6
import isFunction from 'lodash.isfunction' ;
6
7
import resolveFrom from 'resolve-from' ;
7
8
import resolveGlobal from 'resolve-global' ;
@@ -204,7 +205,7 @@ async function main(args: MainArgs): Promise<void> {
204
205
typeof flags . last !== 'undefined' &&
205
206
( typeof flags . from !== 'undefined' ||
206
207
typeof flags . to !== 'undefined' ||
207
- typeof flags . edit !== 'undefined' )
208
+ flags . edit )
208
209
) {
209
210
const err = new CliError (
210
211
'Please use the --last flag alone. The --last flag should not be used with --to or --from or --edit.' ,
@@ -215,16 +216,43 @@ async function main(args: MainArgs): Promise<void> {
215
216
throw err ;
216
217
}
217
218
218
- const input = await ( fromStdin
219
- ? stdin ( )
220
- : read ( {
221
- to : flags . to ,
222
- from : flags . from ,
223
- edit : flags . edit ,
224
- cwd : flags . cwd ,
225
- gitLogArgs : flags [ 'git-log-args' ] ,
226
- } ) ) ;
219
+ if (
220
+ typeof flags . from !== 'undefined' &&
221
+ typeof flags . to !== 'undefined' &&
222
+ flags . from === flags . to
223
+ ) {
224
+ const err = new CliError (
225
+ 'Please use a different commit hash for --from and --to, not the same. (Or use the --last flag for analyzing just the last commit.)' ,
226
+ pkg . name
227
+ ) ;
228
+ yargs . showHelp ( 'log' ) ;
229
+ console . log ( err . message ) ;
230
+ throw err ;
231
+ }
232
+
233
+ let input ;
227
234
235
+ if ( typeof flags . last !== 'undefined' ) {
236
+ const log = await simpleGit ( { baseDir : flags . cwd } ) . log ( {
237
+ maxCount : 1 ,
238
+ } ) ;
239
+ input = '' ;
240
+ if ( log . latest ) {
241
+ input = log . latest . message ;
242
+ } else {
243
+ throw new CliError ( 'No commits found in the repository.' , pkg . name ) ;
244
+ }
245
+ } else {
246
+ input = await ( fromStdin
247
+ ? stdin ( )
248
+ : read ( {
249
+ to : flags . to ,
250
+ from : flags . from ,
251
+ edit : flags . edit ,
252
+ cwd : flags . cwd ,
253
+ gitLogArgs : flags [ 'git-log-args' ] ,
254
+ } ) ) ;
255
+ }
228
256
const messages = ( Array . isArray ( input ) ? input : [ input ] )
229
257
. filter ( ( message ) => typeof message === 'string' )
230
258
. filter ( ( message ) => message . trim ( ) !== '' )
0 commit comments