-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathbackport_session.js
302 lines (265 loc) · 8.24 KB
/
backport_session.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import Session from './session.js';
import { runSync, runAsync, IGNORE } from './run.js';
import { getPrURL, parsePrURL } from './links.js';
const MAX_HISTORY = 10;
const OLDEST_ID = new Map([
[8, 13000],
[10, 20000],
[11, 23000]
]);
export default class BackportSession extends Session {
constructor(cli, dir, prid, target) {
super(cli, dir, prid);
this.target = target;
}
getChangedFiles(rev) {
return runSync('git',
['diff-tree', '--no-commit-id', '--name-only', '-r', rev]
).trim().split('\n');
}
getPreviousCommits(rev, file, num) {
let logs;
try {
logs = runSync('git',
['log', `-${num}`, '--format=%h', rev, '--', file]
).trim();
} catch (e) {
return null;
}
if (!logs) {
return [];
}
return logs.trim().split('\n');
}
getCommitMessage(rev) {
return runSync('git',
['show', '--format=%B', '-s', rev]
).trim();
}
get stagingBranch() {
return `v${this.target}.x-staging`;
}
getPotentialConflicts(rev, targetBranch) {
const { cli } = this;
const files = this.getChangedFiles(rev);
const notBackported = new Map();
const oldest = OLDEST_ID.get(this.target);
for (const file of files) {
cli.startSpinner(`Analyzing ancestors of ${file}`);
// TODO(joyeecheung): if the file does not exit in the current revision,
// warn about it and skip it.
const ancestors = this.getPreviousCommits(`${rev}~1`, file, MAX_HISTORY);
if (!ancestors) {
cli.stopSpinner(`${file} does not exist in current working tree`,
cli.SPINNER_STATUS.WARN);
continue;
}
if (ancestors.length === 0) {
cli.stopSpinner(`Cannot find ancestor commits of ${file}`,
cli.SPINNER_STATUS.INFO);
continue;
}
for (const ancestor of ancestors) {
const message = this.getCommitMessage(ancestor);
cli.updateSpinner(`Analyzing ${message.split('\n')[0]}...`);
let data = parsePrURL(message);
if (!data) {
const match = message.match('/^PR-URL: #(\\d+)/');
if (!match) {
cli.stopSpinner(
`Commit message of ${ancestor} is ill-formed, skipping`,
cli.SPINNER_STATUS.WARN);
cli.startSpinner(`Analyzing ancestors of ${file}`);
continue;
}
data = {
repo: this.repo,
owner: this.owner,
prid: parseInt(match[1])
};
}
if (data.prid < oldest) {
cli.updateSpinner(
`Commit ${ancestor} iS too old, skipping`,
cli.SPINNER_STATUS.WARN);
break;
}
const backported = this.getCommitsFromBranch(
data.prid, targetBranch
);
if (backported.length === 0) {
const record = notBackported.get(ancestor);
if (record) {
record.files.add(file);
} else {
notBackported.set(ancestor, {
prid: data.prid,
url: getPrURL(data),
commit: ancestor,
title: message.split('\n')[0],
files: new Set([file])
});
}
}
}
cli.stopSpinner(`Analyzed ${file}`);
}
return notBackported;
}
warnForPotentialConflicts(rev) {
const { cli } = this;
const staging = this.stagingBranch;
cli.log(`Looking for potential conflicts of ${rev}...`);
const notBackported = this.getPotentialConflicts(rev, staging);
if (notBackported.size === 0) {
cli.info(`All ancestor commits of ${rev} have been backported`);
return;
}
cli.warn(`The following ancestor commits of ${rev} are not on ${staging}`);
for (const [commit, data] of notBackported) {
cli.log(` - ${commit} ${data.title}, ${data.url}`);
for (const file of data.files) {
cli.log(` ${file}`);
}
}
}
async backport() {
const { cli } = this;
// TODO(joyeechuneg): add more warnings
const { prid } = this;
const url = getPrURL(this);
cli.log(`Looking for commits of ${url} on main...`);
const commits = this.getCommitsFromBranch(prid, 'main');
if (commits.length === 0) {
cli.error('Could not find any commit matching the PR');
throw new Error(IGNORE);
}
cli.ok('Found the following commits:');
for (const commit of commits) {
cli.log(` - ${commit.sha} ${commit.title}`);
}
if (!this.isLocalBranchExists(this.stagingBranch)) {
const shouldCreateStagingBranch = await cli.prompt(
`It seems like ${this.stagingBranch} is missing locally, ` +
'do you want to create it locally to get ready for backporting?', {
defaultAnswer: true
});
if (shouldCreateStagingBranch) {
this.syncBranchWithUpstream(this.stagingBranch);
}
} else if (!this.isBranchUpToDateWithUpstream(this.stagingBranch)) {
const shouldSyncBranch = await cli.prompt(
`It seems like your ${this.stagingBranch} is behind the ${this.upstream} remote ` +
'do you want to sync it?', { defaultAnswer: true });
if (shouldSyncBranch) {
this.syncBranchWithUpstream(this.stagingBranch);
}
}
const newBranch = `backport-${this.prid}-to-${this.target}`;
const shouldCheckout = await cli.prompt(
`Do you want to checkout to a new branch \`${newBranch}\`` +
' to start backporting?', { defaultAnswer: false });
if (shouldCheckout) {
await runAsync('git', ['checkout', '-b', newBranch, this.stagingBranch]);
}
const shouldAnalyze = await cli.prompt(
'Do you want to analyze the dependencies of the commits? ' +
'(this could take a while)');
if (shouldAnalyze) {
for (const commit of commits) {
this.warnForPotentialConflicts(commit.sha);
}
}
const cherries = commits.map(i => i.sha).reverse();
const pendingCommands = [
`git cherry-pick ${cherries.join(' ')}`,
'git push -u <your-fork-remote> <your-branch-name>'
];
const shouldPick = await cli.prompt(
'Do you want to cherry-pick the commits?');
if (!shouldPick) {
this.hintCommands(pendingCommands);
return;
}
cli.log(`Running \`${pendingCommands[0]}\`...`);
pendingCommands.shift();
await runAsync('git', ['cherry-pick', ...cherries]);
this.hintCommands(pendingCommands);
}
hintCommands(commands) {
this.cli.log('Tips: run the following commands to complete backport');
for (const command of commands) {
this.cli.log(`$ ${command}`);
}
}
getCommitsFromBranch(prid, branch, loose = true) {
let re;
const url = getPrURL({ prid, repo: this.repo, owner: this.owner });
re = `--grep=PR-URL: ${url}`;
let commits = runSync('git', [
'log', re, '--format=%h %s', branch
]).trim();
if (!commits) {
if (!loose) {
return [];
}
re = `--grep=PR-URL: #${prid}\\b`;
commits = runSync('git', [
'log', re, '--format=%h %s', branch
]).trim();
if (!commits) {
return [];
}
}
return commits.split('\n').map((i) => {
const match = i.match(/(\w+) (.+)/);
return {
sha: match[1],
title: match[2]
};
});
}
getCurrentBranch() {
return runSync('git',
['rev-parse', '--abbrev-ref', 'HEAD']
).trim();
}
updateUpstreamRefs(branchName) {
runSync('git',
['fetch', this.upstream, branchName]
);
}
getBranchCommit(branch) {
return runSync('git',
['rev-parse', branch]
).trim();
}
isBranchUpToDateWithUpstream(branch) {
this.updateUpstreamRefs(branch);
const localCommit = this.getBranchCommit(branch);
const upstreamCommit = this.getBranchCommit(`${this.upstream}/${branch}`);
return localCommit === upstreamCommit;
};
isLocalBranchExists(branch) {
try {
// will exit with code 1 if branch does not exist
runSync('git',
['rev-parse', '--verify', '--quiet', branch]
);
return true;
} catch (e) {
return false;
}
}
syncBranchWithUpstream(branch) {
const currentBranch = this.getCurrentBranch();
runSync('git',
[
currentBranch !== branch ? 'fetch' : 'pull',
this.upstream,
`${branch}:${branch}`,
'-f'
]
);
}
}