-
Notifications
You must be signed in to change notification settings - Fork 113
/
backport.js
245 lines (225 loc) · 6.55 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
import path from 'node:path';
import {
promises as fs
} from 'node:fs';
import inquirer from 'inquirer';
import { ListrEnquirerPromptAdapter } from '@listr2/prompt-adapter-enquirer';
import { shortSha } from '../utils.js';
import { getCurrentV8Version } from './common.js';
export 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;
}
}
};
export function doBackport(options) {
const todo = [
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: (ctx, task) => {
return task.newListr(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;
const fullShas = await Promise.all(
shas.map(async(sha) => {
const stdout = await ctx.execGitV8('rev-parse', sha);
return stdout.trim();
})
);
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,
message
};
}));
}
};
}
function applyPatches() {
return {
title: 'Apply patches to deps/v8',
task: async(ctx, task) => {
const { patches } = ctx;
for (const patch of patches) {
await applyPatch(ctx, task, patch);
}
}
};
}
function applyAndCommitPatches() {
return {
title: 'Apply and commit patches to deps/v8',
task: (ctx, task) => {
return task.newListr(ctx.patches.map(applyPatchTask));
}
};
}
function applyPatchTask(patch) {
return {
title: `Commit ${shortSha(patch.sha)}`,
task: (ctx, task) => {
const todo = [
{
title: 'Apply patch',
task: (ctx, task) => applyPatch(ctx, task, patch)
}
];
if (ctx.bump !== false) {
if (ctx.nodeMajorVersion < 9) {
todo.push(incrementV8Version());
} else {
todo.push(incrementEmbedderVersion());
}
}
todo.push(commitPatch(patch));
return task.newListr(todo);
}
};
}
async function applyPatch(ctx, task, patch) {
try {
await ctx.execGitNode(
'apply',
['-p1', '--3way', '--directory=deps/v8'],
patch.data /* input */
);
} catch (e) {
patch.hadConflicts = true;
return task.prompt(ListrEnquirerPromptAdapter).run({
type: 'input',
message: "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.patch;
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']);
}
};
}