2
2
3
3
'use strict'
4
4
5
- const fs = require ( 'fs' )
6
- , path = require ( 'path' )
7
- , commitStream = require ( 'commit-stream' )
8
- , split2 = require ( 'split2' )
9
- , listStream = require ( 'list-stream' )
10
- , pkgtoId = require ( 'pkg-to-id' )
11
- , stripAnsi = require ( 'strip-ansi' )
12
- , map = require ( 'map-async' )
13
- , { commitToOutput } = require ( 'changelog-maker/commit-to-output' )
14
- , collectCommitLabels = require ( 'changelog-maker/collect-commit-labels' )
15
- , groupCommits = require ( 'changelog-maker/group-commits' )
16
- , { isReleaseCommit, toGroups } = require ( 'changelog-maker/groups' )
17
- , gitexec = require ( 'gitexec' )
18
-
19
- , pkgFile = path . join ( process . cwd ( ) , 'package.json' )
20
- , pkgData = fs . existsSync ( pkgFile ) ? require ( pkgFile ) : { }
21
- , pkgId = pkgtoId ( pkgData )
22
- , refcmd = 'git rev-list --max-count=1 {{ref}}'
23
- , commitdatecmd = '$(git show -s --format=%cd `{{refcmd}}`)'
24
- , gitcmd = 'git log {{startCommit}}..{{branch}} --until="{{untilcmd}}"'
25
- , ghId = {
26
- user : pkgId . user || 'nodejs'
27
- , repo : pkgId . name || 'node'
28
- }
29
- , defaultCommitUrl = 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'
5
+ const fs = require ( 'fs' )
6
+ const path = require ( 'path' )
7
+ const commitStream = require ( 'commit-stream' )
8
+ const split2 = require ( 'split2' )
9
+ const listStream = require ( 'list-stream' )
10
+ const pkgtoId = require ( 'pkg-to-id' )
11
+ const stripAnsi = require ( 'strip-ansi' )
12
+ const map = require ( 'map-async' )
13
+ const { commitToOutput } = require ( 'changelog-maker/commit-to-output' )
14
+ const collectCommitLabels = require ( 'changelog-maker/collect-commit-labels' )
15
+ const groupCommits = require ( 'changelog-maker/group-commits' )
16
+ const { isReleaseCommit, toGroups } = require ( 'changelog-maker/groups' )
17
+ const gitexec = require ( 'gitexec' )
18
+
19
+ const pkgFile = path . join ( process . cwd ( ) , 'package.json' )
20
+ const pkgData = fs . existsSync ( pkgFile ) ? require ( pkgFile ) : { }
21
+ const pkgId = pkgtoId ( pkgData )
22
+ const refcmd = 'git rev-list --max-count=1 {{ref}}'
23
+ const commitdatecmd = '$(git show -s --format=%cd `{{refcmd}}`)'
24
+ const gitcmd = 'git log {{startCommit}}..{{branch}} --until="{{untilcmd}}"'
25
+ const ghId = {
26
+ user : pkgId . user || 'nodejs' ,
27
+ repo : pkgId . name || 'node'
28
+ }
29
+ const defaultCommitUrl = 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'
30
30
31
31
const formatType = {
32
32
PLAINTEXT : 'plaintext' ,
@@ -43,58 +43,56 @@ const getFormat = (argv) => {
43
43
}
44
44
return formatType . MARKDOWN
45
45
}
46
-
46
+
47
47
function replace ( s , m ) {
48
48
Object . keys ( m ) . forEach ( function ( k ) {
49
49
s = s . replace ( new RegExp ( '\\{\\{' + k + '\\}\\}' , 'g' ) , m [ k ] )
50
50
} )
51
51
return s
52
52
}
53
53
54
-
55
54
function branchDiff ( branch1 , branch2 , options , callback ) {
56
- if ( ! branch1 || ! branch2 )
55
+ if ( ! branch1 || ! branch2 ) {
57
56
return callback ( new Error ( 'Must supply two branch names to compare' ) )
57
+ }
58
58
59
- let repoPath = options . repoPath || process . cwd ( )
59
+ const repoPath = options . repoPath || process . cwd ( )
60
60
61
61
findMergeBase ( repoPath , branch1 , branch2 , ( err , commit ) => {
62
- if ( err )
63
- return callback ( err )
62
+ if ( err ) { return callback ( err ) }
64
63
map (
65
- [ branch1 , branch2 ] , ( branch , callback ) => {
66
- collect ( repoPath , branch , commit , branch == branch2 && options . endRef ) . pipe ( listStream . obj ( callback ) )
67
- }
64
+ [ branch1 , branch2 ] , ( branch , callback ) => {
65
+ collect ( repoPath , branch , commit , branch = == branch2 && options . endRef ) . pipe ( listStream . obj ( callback ) )
66
+ }
68
67
, ( err , branchCommits ) => err ? callback ( err ) : diffCollected ( options , branchCommits , callback )
69
68
)
70
69
} )
71
70
}
72
71
73
-
74
72
function findMergeBase ( repoPath , branch1 , branch2 , callback ) {
75
- let gitcmd = `git merge-base ${ branch1 } ${ branch2 } `
73
+ const gitcmd = `git merge-base ${ branch1 } ${ branch2 } `
76
74
77
75
gitexec . execCollect ( repoPath , gitcmd , ( err , data ) => {
78
- if ( err )
76
+ if ( err ) {
79
77
return callback ( err )
78
+ }
80
79
81
80
callback ( null , data . substr ( 0 , 10 ) )
82
81
} )
83
82
}
84
83
85
-
86
84
function diffCollected ( options , branchCommits , callback ) {
87
85
function isInList ( commit ) {
88
86
return branchCommits [ 0 ] . some ( ( c ) => {
89
- if ( commit . sha === c . sha )
90
- return true
87
+ if ( commit . sha === c . sha ) { return true }
91
88
if ( commit . summary === c . summary ) {
92
89
if ( commit . prUrl && c . prUrl ) {
93
- return commit . prUrl === c . prUrl
94
- } else if ( commit . author . name === c . author . name
95
- && commit . author . email === c . author . email ) {
96
- if ( process . stderr . isTTY )
90
+ return commit . prUrl === c . prUrl
91
+ } else if ( commit . author . name === c . author . name &&
92
+ commit . author . email === c . author . email ) {
93
+ if ( process . stderr . isTTY ) {
97
94
console . error ( `Note: Commit fell back to author checking: "${ commit . summary } " -` , commit . author )
95
+ }
98
96
return true
99
97
}
100
98
}
@@ -105,8 +103,9 @@ function diffCollected (options, branchCommits, callback) {
105
103
let list = branchCommits [ 1 ] . filter ( ( commit ) => ! isInList ( commit ) )
106
104
107
105
collectCommitLabels ( list , ( err ) => {
108
- if ( err )
106
+ if ( err ) {
109
107
return callback ( err )
108
+ }
110
109
111
110
if ( options . excludeLabels . length > 0 ) {
112
111
list = list . filter ( ( commit ) => {
@@ -124,8 +123,9 @@ function diffCollected (options, branchCommits, callback) {
124
123
} )
125
124
}
126
125
127
- if ( options . group )
126
+ if ( options . group ) {
128
127
list = groupCommits ( list )
128
+ }
129
129
130
130
callback ( null , list )
131
131
} )
@@ -152,83 +152,92 @@ function printCommits (list, format, reverse, commitUrl) {
152
152
}
153
153
list = formatted
154
154
} else {
155
- list = list . map ( ( commit ) => commitToOutput ( commit , formatType . MARKDOWN , ghId , commitUrl ) )
155
+ list = list . map ( ( commit ) => {
156
+ return commitToOutput ( commit , formatType . MARKDOWN , ghId , commitUrl )
157
+ } )
156
158
}
157
159
158
- if ( reverse )
160
+ if ( reverse ) {
159
161
list = list . reverse ( )
162
+ }
160
163
161
164
let out = list . join ( '\n' ) + '\n'
162
165
163
- if ( ! process . stdout . isTTY )
166
+ if ( ! process . stdout . isTTY ) {
164
167
out = stripAnsi ( out )
168
+ }
165
169
166
170
process . stdout . write ( out )
167
171
}
168
172
169
-
170
173
function collect ( repoPath , branch , startCommit , endRef ) {
171
- let endrefcmd = endRef && replace ( refcmd , { ref : endRef } )
172
- , untilcmd = endRef ? replace ( commitdatecmd , { refcmd : endrefcmd } ) : ''
173
- , _gitcmd = replace ( gitcmd , { branch, startCommit, untilcmd } )
174
+ const endrefcmd = endRef && replace ( refcmd , { ref : endRef } )
175
+ const untilcmd = endRef ? replace ( commitdatecmd , { refcmd : endrefcmd } ) : ''
176
+ const _gitcmd = replace ( gitcmd , { branch, startCommit, untilcmd } )
174
177
175
178
return gitexec . exec ( repoPath , _gitcmd )
176
179
. pipe ( split2 ( ) )
177
180
. pipe ( commitStream ( ghId . user , ghId . repo ) )
178
181
}
179
182
180
-
181
183
module . exports = branchDiff
182
184
183
- if ( require . main === module ) {
184
- let minimistConfig = {
185
- boolean : [ 'version' , 'group' , 'patch-only' , 'simple' , 'filter-release' , 'reverse' ]
186
- }
187
- , argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) , minimistConfig )
188
- , branch1 = argv . _ [ 0 ]
189
- , branch2 = argv . _ [ 1 ]
190
- , reverse = argv . reverse
191
- , group = argv . group || argv . g
192
- , endRef = argv [ 'end-ref' ]
193
- , commitUrl = argv [ 'commit-url' ] || defaultCommitUrl
194
- , excludeLabels = [ ]
195
- , requireLabels = [ ]
196
- , options
185
+ function main ( ) {
186
+ const minimistConfig = {
187
+ boolean : [ 'version' , 'group' , 'patch-only' , 'simple' , 'filter-release' , 'reverse' ]
188
+ }
189
+ const argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) , minimistConfig )
190
+ const branch1 = argv . _ [ 0 ]
191
+ const branch2 = argv . _ [ 1 ]
192
+ const reverse = argv . reverse
193
+ const group = argv . group || argv . g
194
+ const endRef = argv [ 'end-ref' ]
195
+ const commitUrl = argv [ 'commit-url' ] || defaultCommitUrl
196
+ let excludeLabels = [ ]
197
+ let requireLabels = [ ]
197
198
198
199
const format = getFormat ( argv )
199
200
200
- if ( argv . version || argv . v )
201
+ if ( argv . version || argv . v ) {
201
202
return console . log ( `v ${ require ( './package.json' ) . version } ` )
203
+ }
202
204
203
- if ( argv [ 'patch-only' ] )
204
- excludeLabels = [ 'semver-minor' , 'semver-major' ]
205
+ if ( argv [ 'patch-only' ] ) {
206
+ excludeLabels = [ 'semver-minor' , 'semver-major' ]
207
+ }
205
208
206
209
if ( argv [ 'exclude-label' ] ) {
207
- if ( ! Array . isArray ( argv [ 'exclude-label' ] ) )
210
+ if ( ! Array . isArray ( argv [ 'exclude-label' ] ) ) {
208
211
argv [ 'exclude-label' ] = argv [ 'exclude-label' ] . split ( ',' )
212
+ }
209
213
excludeLabels = excludeLabels . concat ( argv [ 'exclude-label' ] )
210
214
}
211
215
212
216
if ( argv [ 'require-label' ] ) {
213
- if ( ! Array . isArray ( argv [ 'require-label' ] ) )
217
+ if ( ! Array . isArray ( argv [ 'require-label' ] ) ) {
214
218
argv [ 'require-label' ] = argv [ 'require-label' ] . split ( ',' )
219
+ }
215
220
requireLabels = requireLabels . concat ( argv [ 'require-label' ] )
216
221
}
217
222
218
- options = {
223
+ const options = {
219
224
group,
220
225
excludeLabels,
221
226
requireLabels,
222
227
endRef
223
228
}
224
229
225
230
branchDiff ( branch1 , branch2 , options , ( err , list ) => {
226
- if ( err )
227
- throw err
231
+ if ( err ) { throw err }
228
232
229
- if ( argv [ 'filter-release' ] )
233
+ if ( argv [ 'filter-release' ] ) {
230
234
list = list . filter ( ( commit ) => ! isReleaseCommit ( commit . summary ) )
235
+ }
231
236
232
237
printCommits ( list , format , reverse , commitUrl )
233
238
} )
234
239
}
240
+
241
+ if ( require . main === module ) {
242
+ main ( )
243
+ }
0 commit comments