Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(git-v8): preserve original author when backporting #872

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions components/git/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,22 @@ export function builder(yargs) {
describe: 'Bump V8 embedder version number or patch version',
default: true
})
.option('gpg-sign', {
alias: 'S',
type: 'boolean',
describe: 'GPG-sign commits',
default: false
})
.option('preserve-original-author', {
type: 'boolean',
describe: 'Preserve original commit author and date',
default: true
})
.option('squash', {
type: 'boolean',
describe:
'If multiple commits are backported, squash them into one',
'If multiple commits are backported, squash them into one. When ' +
'`--squash` is passed, `--preserve-original-author` will be ignored',
default: false
});
}
Expand Down Expand Up @@ -88,7 +100,7 @@ export function handler(argv) {
input,
spawnArgs: {
cwd: options.nodeDir,
stdio: input ? ['pipe', 'ignore', 'ignore'] : 'ignore'
stdio: input ? ['pipe', 'inherit', 'inherit'] : 'inherit'
}
});
};
Expand All @@ -97,7 +109,7 @@ export function handler(argv) {
return forceRunAsync('git', args, {
ignoreFailure: false,
captureStdout: true,
spawnArgs: { cwd: options.v8Dir, stdio: ['ignore', 'pipe', 'ignore'] }
spawnArgs: { cwd: options.v8Dir, stdio: ['ignore', 'pipe', 'inherit'] }
});
};

Expand Down
84 changes: 74 additions & 10 deletions lib/update-v8/backport.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ListrEnquirerPromptAdapter } from '@listr2/prompt-adapter-enquirer';
import { shortSha } from '../utils.js';

import { getCurrentV8Version } from './common.js';
import { forceRunAsync } from '../run.js';

export async function checkOptions(options) {
if (options.sha.length > 1 && options.squash) {
Expand Down Expand Up @@ -41,6 +42,8 @@ export function doBackport(options) {
}
}
todo.push(commitSquashedBackport());
} else if (options.preserveOriginalAuthor) {
todo.push(cherryPickV8Commits(options));
} else {
todo.push(applyAndCommitPatches());
}
Expand Down Expand Up @@ -76,18 +79,47 @@ function commitSquashedBackport() {
};
};

function commitPatch(patch) {
const commitTask = (patch, extraArgs, trailers) => async(ctx) => {
const messageTitle = formatMessageTitle([patch]);
const messageBody = formatMessageBody(patch, false, trailers);
await ctx.execGitNode('add', ['deps/v8']);
await ctx.execGitNode('commit', [
...ctx.gpgSign, ...extraArgs,
'-m', messageTitle, '-m', messageBody
]);
};

function amendHEAD(patch) {
return {
title: 'Commit patch',
title: 'Amend/commit',
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]);
let coAuthor;
if (patch.hadConflicts) {
const getGitConfigEntry = async(configKey) => {
const output = await forceRunAsync('git', ['config', configKey], {
ignoreFailure: false,
captureStdout: true,
spawnArgs: { cwd: ctx.nodeDir }
});
return output.trim();
};
await ctx.execGitNode('am', [...ctx.gpgSign, '--continue']);
coAuthor = `\nCo-authored-by: ${
await getGitConfigEntry('user.name')} <${
await getGitConfigEntry('user.email')}>`;
}
await commitTask(patch, ['--amend'], coAuthor)(ctx);
}
};
}

function commitPatch(patch) {
return {
title: 'Commit patch',
task: commitTask(patch)
};
}

function formatMessageTitle(patches) {
const action =
patches.some(patch => patch.hadConflicts) ? 'backport' : 'cherry-pick';
Expand All @@ -106,12 +138,12 @@ function formatMessageTitle(patches) {
}
}

function formatMessageBody(patch, prefixTitle) {
function formatMessageBody(patch, prefixTitle, trailers = '') {
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}`;
`Refs: https://github.com/v8/v8/commit/${patch.sha}${trailers}`;

if (prefixTitle) {
const action = patch.hadConflicts ? 'Backport' : 'Cherry-pick';
Expand Down Expand Up @@ -167,6 +199,15 @@ function applyAndCommitPatches() {
};
}

function cherryPickV8Commits() {
return {
title: 'Cherry-pick commit from V8 clone to deps/v8',
task: (ctx, task) => {
return task.newListr(ctx.patches.map(cherryPickV8CommitTask));
}
};
}

function applyPatchTask(patch) {
return {
title: `Commit ${shortSha(patch.sha)}`,
Expand All @@ -190,10 +231,33 @@ function applyPatchTask(patch) {
};
}

async function applyPatch(ctx, task, patch) {
function cherryPickV8CommitTask(patch) {
return {
title: `Commit ${shortSha(patch.sha)}`,
task: (ctx, task) => {
const todo = [
{
title: 'Cherry-pick',
task: (ctx, task) => applyPatch(ctx, task, patch, 'am')
}
];
if (ctx.bump !== false) {
if (ctx.nodeMajorVersion < 9) {
todo.push(incrementV8Version());
} else {
todo.push(incrementEmbedderVersion());
}
}
todo.push(amendHEAD(patch));
return task.newListr(todo);
}
};
}

async function applyPatch(ctx, task, patch, method = 'apply') {
try {
await ctx.execGitNode(
'apply',
method,
['-p1', '--3way', '--directory=deps/v8'],
patch.data /* input */
);
Expand Down
1 change: 1 addition & 0 deletions lib/update-v8/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function minor(options) {
export async function backport(options) {
const shouldStop = await checkOptions(options);
if (shouldStop) return;
options.gpgSign = options.gpgSign ? ['-S'] : [];
const tasks = new Listr(
[updateV8Clone(), doBackport(options)],
getOptions(options)
Expand Down
Loading