-
Notifications
You must be signed in to change notification settings - Fork 20
/
branch-diff.js
executable file
·201 lines (158 loc) · 5.64 KB
/
branch-diff.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env node
'use strict'
const fs = require('fs')
, path = require('path')
, commitStream = require('commit-stream')
, split2 = require('split2')
, listStream = require('list-stream')
, pkgtoId = require('pkg-to-id')
, chalk = require('chalk')
, map = require('map-async')
, commitToOutput = require('changelog-maker/commit-to-output')
, collectCommitLabels = require('changelog-maker/collect-commit-labels')
, groupCommits = require('changelog-maker/group-commits')
, isReleaseCommit = require('changelog-maker/groups').isReleaseCommit
, gitexec = require('gitexec')
, pkgFile = path.join(process.cwd(), 'package.json')
, pkgData = fs.existsSync(pkgFile) ? require(pkgFile) : {}
, pkgId = pkgtoId(pkgData)
, refcmd = 'git rev-list --max-count=1 {{ref}}'
, commitdatecmd = '$(git show -s --format=%cd `{{refcmd}}`)'
, gitcmd = 'git log {{startCommit}}..{{branch}} --until="{{untilcmd}}"'
, ghId = {
user: pkgId.user || 'nodejs'
, name: pkgId.name || 'node'
}
function replace (s, m) {
Object.keys(m).forEach(function (k) {
s = s.replace(new RegExp('\\{\\{' + k + '\\}\\}', 'g'), m[k])
})
return s
}
function branchDiff (branch1, branch2, options, callback) {
if (!branch1 || !branch2)
return callback(new Error('Must supply two branch names to compare'))
let repoPath = options.repoPath || process.cwd()
findMergeBase(repoPath, branch1, branch2, (err, commit) => {
if (err)
return callback(err)
map(
[ branch1, branch2 ], (branch, callback) => {
collect(repoPath, branch, commit, branch == branch2 && options.endRef).pipe(listStream.obj(callback))
}
, (err, branchCommits) => err ? callback(err) : diffCollected(options, branchCommits, callback)
)
})
}
function findMergeBase (repoPath, branch1, branch2, callback) {
let gitcmd = `git merge-base ${branch1} ${branch2}`
gitexec.execCollect(repoPath, gitcmd, (err, data) => {
if (err)
return callback(err)
callback(null, data.substr(0, 10))
})
}
function diffCollected (options, branchCommits, callback) {
function isInList (commit) {
return branchCommits[0].some((c) => {
if (commit.sha === c.sha)
return true
if (commit.summary === c.summary) {
if (commit.prUrl && c.prUrl) {
return commit.prUrl === c.prUrl
} else if (commit.author.name === c.author.name
&& commit.author.email === c.author.email) {
if (process.stderr.isTTY)
console.error(`Note: Commit fell back to author checking: "${commit.summary}" -`, commit.author)
return true
}
}
return false
})
}
let list = branchCommits[1].filter((commit) => !isInList(commit))
collectCommitLabels(list, (err) => {
if (err)
return callback(err)
if (options.excludeLabels.length > 0) {
list = list.filter((commit) => {
return !commit.labels || !commit.labels.some((label) => {
return options.excludeLabels.indexOf(label) >= 0
})
})
}
if (options.requireLabels.length > 0) {
list = list.filter((commit) => {
return commit.labels && commit.labels.some((label) => {
return options.requireLabels.indexOf(label) >= 0
})
})
}
if (options.group)
list = groupCommits(list)
callback(null, list)
})
}
function printCommits (list, format, reverse) {
if (format === 'sha') {
list = list.map((commit) => `${commit.sha.substr(0, 10)}`)
} else {
list = list.map((commit) => commitToOutput(commit, format === 'simple', ghId))
}
if (reverse) list = list.reverse();
let out = list.join('\n') + '\n'
if (!process.stdout.isTTY)
out = chalk.stripColor(out)
process.stdout.write(out)
}
function collect (repoPath, branch, startCommit, endRef) {
let endrefcmd = endRef && replace(refcmd, { ref: endRef })
, untilcmd = endRef ? replace(commitdatecmd, { refcmd: endrefcmd }) : ''
, _gitcmd = replace(gitcmd, { branch, startCommit, untilcmd })
return gitexec.exec(repoPath, _gitcmd)
.pipe(split2())
.pipe(commitStream(ghId.user, ghId.name))
}
module.exports = branchDiff
if (require.main === module) {
let argv = require('minimist')(process.argv.slice(2))
, branch1 = argv._[0]
, branch2 = argv._[1]
, format = argv.format
, reverse = argv.reverse
, group = argv.group || argv.g
, endRef = argv['end-ref']
, excludeLabels = []
, requireLabels = []
, options
if (argv.version || argv.v)
return console.log(`v ${require('./package.json').version}`)
if (argv.simple || argv.s)
format = 'simple'
if (argv['patch-only'])
excludeLabels = [ 'semver-minor', 'semver-major' ]
if (argv['exclude-label']) {
if (!Array.isArray(argv['exclude-label']))
argv['exclude-label'] = argv['exclude-label'].split(',')
excludeLabels = excludeLabels.concat(argv['exclude-label'])
}
if (argv['require-label']) {
if (!Array.isArray(argv['require-label']))
argv['require-label'] = argv['require-label'].split(',')
requireLabels = requireLabels.concat(argv['require-label'])
}
options = {
simple: format === 'simple',
group,
excludeLabels,
requireLabels,
endRef
}
branchDiff(branch1, branch2, options, (err, list) => {
if (err)
throw err
if (argv['filter-release'])
list = list.filter((commit) => !isReleaseCommit(commit.summary))
printCommits(list, format, reverse)
})
}