-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbranch-diff.js
executable file
·218 lines (190 loc) · 6.29 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env node
import fs from 'fs'
import { createRequire } from 'module'
import path from 'path'
import process from 'process'
import { pipeline as _pipeline } from 'stream'
import { promisify } from 'util'
import commitStream from 'commit-stream'
import split2 from 'split2'
import pkgtoId from 'pkg-to-id'
import minimist from 'minimist'
import { isReleaseCommit } from 'changelog-maker/groups'
import { processCommits } from 'changelog-maker/process-commits'
import { collectCommitLabels } from 'changelog-maker/collect-commit-labels'
import gitexec from 'gitexec'
const pipeline = promisify(_pipeline)
const pkgFile = path.join(process.cwd(), 'package.json')
const require = createRequire(import.meta.url)
const pkgData = fs.existsSync(pkgFile) ? require(pkgFile) : {}
const pkgId = pkgtoId(pkgData)
const refcmd = 'git rev-list --max-count=1 {{ref}}'
const commitdatecmd = '$(git show -s --format=%cd `{{refcmd}}`)'
const gitcmd = 'git log {{startCommit}}..{{branch}} --until="{{untilcmd}}"'
const ghId = {
user: pkgId.user || 'nodejs',
repo: pkgId.name || 'node'
}
const cacheFilename = path.resolve('.branch-diff-cache.json')
let excludedLabelsCache = new Map()
function replace (s, m) {
Object.keys(m).forEach(function (k) {
s = s.replace(new RegExp('\\{\\{' + k + '\\}\\}', 'g'), m[k])
})
return s
}
export async function branchDiff (branch1, branch2, options) {
if (!branch1 || !branch2) {
throw new Error('Must supply two branch names to compare')
}
const repoPath = options.repoPath || process.cwd()
const commit = await findMergeBase(repoPath, branch1, branch2)
const branchCommits = await Promise.all([branch1, branch2].map(async (branch) => {
return collect(repoPath, branch, commit, branch === branch2 && options.endRef)
}))
return await diffCollected(options, branchCommits)
}
async function findMergeBase (repoPath, branch1, branch2) {
const gitcmd = `git merge-base ${branch1} ${branch2}`
const data = await promisify(gitexec.execCollect)(repoPath, gitcmd)
return data.substr(0, 10)
}
async function diffCollected (options, branchCommits) {
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))
list = list.filter((commit) => !excludedLabelsCache.has(commit.sha))
await collectCommitLabels(list)
if (options.excludeLabels.length > 0) {
list = list.filter((commit) => {
const included = !commit.labels || !commit.labels.some((label) => {
return options.excludeLabels.indexOf(label) >= 0
})
// excluded commits will be stored in a cache to avoid hitting the
// GH API again for as long as the cache option is active
if (!included) {
excludedLabelsCache.set(commit.sha, 1)
}
return included
})
}
if (options.requireLabels.length > 0) {
list = list.filter((commit) => {
return commit.labels && commit.labels.some((label) => {
return options.requireLabels.indexOf(label) >= 0
})
})
}
return list
}
async function collect (repoPath, branch, startCommit, endRef) {
const endrefcmd = endRef && replace(refcmd, { ref: endRef })
const untilcmd = endRef ? replace(commitdatecmd, { refcmd: endrefcmd }) : ''
const _gitcmd = replace(gitcmd, { branch, startCommit, untilcmd })
const commitList = []
await pipeline(
gitexec.exec(repoPath, _gitcmd),
split2(),
commitStream(ghId.user, ghId.repo),
async function * (source) {
for await (const commit of source) {
commitList.push(commit)
}
})
return commitList
}
async function main () {
const minimistConfig = {
boolean: ['version', 'group', 'patch-only', 'simple', 'filter-release', 'reverse']
}
const argv = minimist(process.argv.slice(2), minimistConfig)
const branch1 = argv._[0]
const branch2 = argv._[1]
const group = argv.group || argv.g
const endRef = argv['end-ref']
let excludeLabels = []
let requireLabels = []
if (argv.version || argv.v) {
return console.log(`v ${require('./package.json').version}`)
}
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'])
}
if (argv.user) {
ghId.user = argv.user
}
if (argv.repo) {
ghId.repo = argv.repo
}
if (argv.cache) {
let cacheFile
try {
cacheFile = fs.readFileSync(cacheFilename, 'utf8')
} catch (err) {
console.log(`No cache file found. A new file:
${path.resolve(cacheFilename)}
will be created when completing a successful branch-diff run.
`)
}
if (cacheFile) {
const cacheData = JSON.parse(cacheFile)
const excludedLabels = cacheData.excludedLabels
if (excludedLabels && excludedLabels.length >= 0) {
excludedLabelsCache = new Map(excludedLabels.map(i => ([i, 1])))
} else {
console.error('Missing excludedLabels on cache file.')
}
}
}
const options = {
group,
excludeLabels,
requireLabels,
endRef
}
let list = await branchDiff(branch1, branch2, options)
if (argv['filter-release']) {
list = list.filter((commit) => !isReleaseCommit(commit.summary))
}
await processCommits(argv, ghId, list)
// only stores to cache on success
if (argv.cache) {
fs.writeFileSync(
cacheFilename,
JSON.stringify({
excludedLabels: [...excludedLabelsCache.keys()],
}, null, 2)
)
}
}
main().catch((err) => {
console.error(err)
process.exit(1)
})