-
Notifications
You must be signed in to change notification settings - Fork 113
/
backport.js
252 lines (231 loc) · 6.64 KB
/
backport.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
'use strict';
const path = require('path');
const execa = require('execa');
const fs = require('fs-extra');
const inquirer = require('inquirer');
const Listr = require('listr');
const input = require('listr-input');
const { shortSha } = require('../utils');
const common = require('./common');
exports.checkOptions = async function checkOptions(options) {
if (options.sha.length > 1 && options.squash) {
const { wantSquash } = await inquirer.prompt([{
type: 'confirm',
name: 'wantSquash',
message: 'Squashing commits should be avoided if possible, because it ' +
'can make git bisection difficult. Only squash commits if they would ' +
'break the build when applied individually. Are you sure?',
default: false
}]);
if (!wantSquash) {
return true;
}
}
};
exports.doBackport = function doBackport(options) {
const todo = [
common.getCurrentV8Version(),
generatePatches()
];
if (options.squash) {
todo.push(applyPatches());
if (options.bump !== false) {
if (options.nodeMajorVersion < 9) {
todo.push(incrementV8Version());
} else {
todo.push(incrementEmbedderVersion());
}
}
todo.push(commitSquashedBackport());
} else {
todo.push(applyAndCommitPatches());
}
return {
title: 'V8 commit backport',
task: () => {
return new Listr(todo);
}
};
};
function commitSquashedBackport() {
return {
title: 'Commit backport',
task: async(ctx) => {
const { patches } = ctx;
const messageTitle = formatMessageTitle(patches);
let messageBody;
if (patches.length === 1) {
const [patch] = patches;
messageBody = formatMessageBody(patch, false);
} else {
messageBody = '';
for (const patch of patches) {
const formatted = formatMessageBody(patch, true);
messageBody += formatted + '\n\n';
}
}
await ctx.execGitNode('add', 'deps/v8');
await ctx.execGitNode('commit', '-m', messageTitle, '-m', messageBody);
}
};
};
function commitPatch(patch) {
return {
title: 'Commit patch',
task: async(ctx) => {
const messageTitle = formatMessageTitle([patch]);
const messageBody = formatMessageBody(patch, false);
await ctx.execGitNode('add', 'deps/v8');
await ctx.execGitNode('commit', '-m', messageTitle, '-m', messageBody);
}
};
}
function formatMessageTitle(patches) {
const action =
patches.some(patch => patch.hadConflicts) ? 'backport' : 'cherry-pick';
if (patches.length === 1) {
return `deps: V8: ${action} ${shortSha(patches[0].sha)}`;
} else if (patches.length === 2) {
return `deps: V8: ${action} ${shortSha(patches[0].sha)} and ${
shortSha(patches[1].sha)
}`;
} else if (patches.length === 3) {
return `deps: V8: ${action} ${shortSha(patches[0].sha)}, ${
shortSha(patches[1].sha)
} and ${shortSha(patches[2].sha)}`;
} else {
return `deps: V8: ${action} ${patches.length} commits`;
}
}
function formatMessageBody(patch, prefixTitle) {
const indentedMessage = patch.message.replace(/\n/g, '\n ');
const body =
'Original commit message:\n\n' +
` ${indentedMessage}\n\n` +
`Refs: https://github.com/v8/v8/commit/${patch.sha}`;
if (prefixTitle) {
const action = patch.hadConflicts ? 'Backport' : 'Cherry-pick';
return `${action} ${shortSha(patch.sha)}.\n` + body;
}
return body;
}
function generatePatches() {
return {
title: 'Generate patches',
task: async(ctx) => {
const shas = ctx.sha;
try {
const fullShas = await Promise.all(
shas.map(async(sha) => {
const { stdout } = await ctx.execGitV8('rev-parse', sha);
return stdout;
})
);
ctx.patches = await Promise.all(fullShas.map(async(sha) => {
const [patch, message] = await Promise.all([
ctx.execGitV8('format-patch', '--stdout', `${sha}^..${sha}`),
ctx.execGitV8('log', '--format=%B', '-n', '1', sha)
]);
return {
sha,
data: patch.stdout,
message: message.stdout
};
}));
} catch (e) {
throw new Error(e.stderr);
}
}
};
}
function applyPatches() {
return {
title: 'Apply patches to deps/v8',
task: async(ctx) => {
const { patches } = ctx;
for (const patch of patches) {
await applyPatch(ctx, patch);
}
}
};
}
function applyAndCommitPatches() {
return {
title: 'Apply and commit patches to deps/v8',
task: (ctx) => {
return new Listr(ctx.patches.map(applyPatchTask));
}
};
}
function applyPatchTask(patch) {
return {
title: `Commit ${shortSha(patch.sha)}`,
task: (ctx) => {
const todo = [
{
title: 'Apply patch',
task: (ctx) => applyPatch(ctx, patch)
}
];
if (ctx.bump !== false) {
if (ctx.nodeMajorVersion < 9) {
todo.push(incrementV8Version());
} else {
todo.push(incrementEmbedderVersion());
}
}
todo.push(commitPatch(patch));
return new Listr(todo);
}
};
}
async function applyPatch(ctx, patch) {
try {
await execa(
'patch',
['-p1', '--merge', '--no-backup-if-mismatch', '--directory=deps/v8'],
{
cwd: ctx.nodeDir,
input: patch.data
}
);
} catch (e) {
patch.hadConflicts = true;
return input("Resolve merge conflicts and enter 'RESOLVED'", {
validate: value => value.toUpperCase() === 'RESOLVED'
});
}
}
function incrementV8Version() {
return {
title: 'Increment V8 version',
task: async(ctx) => {
const incremented = ++ctx.currentVersion[3];
const versionHPath = `${ctx.nodeDir}/deps/v8/include/v8-version.h`;
let versionH = await fs.readFile(versionHPath, 'utf8');
versionH = versionH.replace(
/V8_PATCH_LEVEL (\d+)/,
`V8_PATCH_LEVEL ${incremented}`
);
await fs.writeFile(versionHPath, versionH);
}
};
}
const embedderRegex = /'v8_embedder_string': '-node\.(\d+)'/;
function incrementEmbedderVersion() {
return {
title: 'Increment embedder version number',
task: async(ctx) => {
const commonGypiPath = path.join(ctx.nodeDir, 'common.gypi');
const commonGypi = await fs.readFile(commonGypiPath, 'utf8');
const embedderValue = parseInt(embedderRegex.exec(commonGypi)[1], 10);
const embedderString = `'v8_embedder_string': '-node.${embedderValue +
1}'`;
await fs.writeFile(
commonGypiPath,
commonGypi.replace(embedderRegex, embedderString)
);
await ctx.execGitNode('add', 'common.gypi');
}
};
}