1+ 'use strict' ;
2+
13require ( '../lib/bootstrap-local' ) ;
24
5+ const fs = require ( 'fs' ) ;
6+ const path = require ( 'path' ) ;
37const validateCommitMessage = require ( './validate-commit-message' ) ;
4- const exec = require ( 'child_process' ) . exec ;
8+ const execSync = require ( 'child_process' ) . execSync ;
59const chalk = require ( 'chalk' ) ;
610const Logger = require ( '@ngtools/logger' ) . Logger ;
11+ const configPath = path . resolve ( __dirname , './validate-commit-message/commit-message.json' ) ;
12+ const config = JSON . parse ( fs . readFileSync ( configPath , 'utf8' ) ) ;
713require ( 'rxjs/add/operator/filter' ) ;
814
915// Configure logger
@@ -23,39 +29,64 @@ logger.subscribe((entry) => {
2329} ) ;
2430
2531logger
26- . filter ( ( entry ) => entry . level == 'fatal' )
32+ . filter ( ( entry ) => entry . level === 'fatal' )
2733 . subscribe ( ( ) => {
2834 process . stderr . write ( 'A fatal error happened. See details above.' ) ;
2935 process . exit ( 1 ) ;
3036 } ) ;
3137
3238// Note: This is based on the gulp task found in the angular/angular repository
39+ execSync ( 'git fetch origin' ) ;
3340
34- exec (
35- 'git fetch origin master:master && git log --reverse --format=%s master.. --no-merges' ,
36- ( error , stdout , stderr ) => {
37- if ( error ) {
38- logger . fatal ( stderr ) ;
39- return ;
40- }
41-
42- const output = stdout . trim ( ) ;
43- if ( output . length == 0 ) {
44- logger . warn ( 'There are zero new commits between this HEAD and master' ) ;
45- return ;
46- }
47-
48- const commitsByLine = output . split ( / \n / ) ;
49-
50- logger . info ( `Examining ${ commitsByLine . length } commit(s) between HEAD and master` ) ;
51-
52- const someCommitsInvalid = ! commitsByLine . every ( validateCommitMessage ) ;
53-
54- if ( someCommitsInvalid ) {
55- logger . error ( 'Please fix the failing commit messages before continuing...' ) ;
56- logger . fatal (
57- 'Commit message guidelines: https://github.com/angular/angular-cli/blob/master/CONTRIBUTING.md#commit' ) ;
58- } else {
59- logger . info ( 'All commit messages are valid.' ) ;
60- }
61- } ) ;
41+ // Find the branch
42+ const branchRefs = { } ;
43+ for ( const name of config [ 'branches' ] ) {
44+ const output = execSync ( `git show-ref --hash ${ name } ` , { encoding : 'utf-8' } ) ;
45+ if ( output ) {
46+ branchRefs [ name ] = output ;
47+ }
48+ }
49+ logger . info ( `Found refs for branches:\n ${ Object . entries ( branchRefs ) . forEach ( ( [ key , value ] ) => {
50+ return `${ key } => ${ value } ` ;
51+ } ) . join ( '\n ' ) } `) ;
52+
53+
54+ const output = execSync ( 'git log --format="%H %s" --no-merges' , { encoding : 'utf-8' } ) ;
55+
56+ if ( output . length === 0 ) {
57+ logger . warn ( 'There are zero new commits between this HEAD and master' ) ;
58+ return ;
59+ }
60+
61+ const commitByLines = [ ] ;
62+ let branch = null ;
63+
64+ // Finding the closest branch marker.
65+ for ( const line of output . split ( / n / ) ) {
66+ const [ hash , ...messageArray ] = line . split ( / / ) ;
67+ const message = messageArray . join ( ' ' ) ;
68+
69+ const maybeBranch = Object . keys ( branchRefs ) . find ( branchName => branchRefs [ branchName ] == hash ) ;
70+ if ( maybeBranch ) {
71+ branch = maybeBranch ;
72+ break ;
73+ }
74+ commitByLines . push ( message ) ;
75+ }
76+
77+ if ( ! branch ) {
78+ logger . fatal ( 'Something wrong happened.' ) ;
79+ return ;
80+ }
81+
82+ logger . info ( `Examining ${ commitsByLine . length } commit(s) between HEAD and ${ branch } ` ) ;
83+
84+ const someCommitsInvalid = ! commitsByLine . every ( validateCommitMessage ) ;
85+
86+ if ( someCommitsInvalid ) {
87+ logger . error ( 'Please fix the failing commit messages before continuing...' ) ;
88+ logger . fatal (
89+ 'Commit message guidelines: https://github.com/angular/angular-cli/blob/master/CONTRIBUTING.md#commit' ) ;
90+ } else {
91+ logger . info ( 'All commit messages are valid.' ) ;
92+ }
0 commit comments