-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
executable file
·355 lines (318 loc) · 11.4 KB
/
index.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env node
// @ts-check
const simpleGit = require('simple-git/promise');
const difference = require('lodash.difference');
const program = require('commander');
const emoji = require('node-emoji');
const fs = require('fs');
const yaml = require('js-yaml');
const { ensurePrsExist, readGHKey, checkGHKeyExists } = require('./github');
const colors = require('colors');
const {
DEFAULT_REMOTE,
DEFAULT_BASE_BRANCH,
MERGE_STEP_DELAY_MS,
MERGE_STEP_DELAY_WAIT_FOR_LOCK,
} = require('./consts');
const path = require('path');
// @ts-ignore
const package = require('./package.json');
const inquirer = require('inquirer');
const shelljs = require('shelljs');
const camelCase = require('lodash/camelCase');
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
/**
* Returns `true` is ref `b1` is an ancestor of ref `b2`.
*
* @param {simpleGit.SimpleGit} sg
* @param {string} r1
* @param {string} r2
*/
function isBranchAncestor(sg, r1, r2) {
return shelljs.exec(`git merge-base --is-ancestor ${r1} ${r2}`).code === 0;
}
/**
*
* @param {simpleGit.SimpleGit} sg
* @param {boolean} rebase
* @param {string} from
* @param {string} to
*/
async function combineBranches(sg, rebase, from, to) {
if (program.rebase) {
process.stdout.write(`rebasing ${to} onto branch ${from}... `);
} else {
process.stdout.write(`merging ${from} into branch ${to}... `);
}
try {
await sg.checkout(to);
await (rebase ? sg.rebase([from]) : sg.merge([from]));
} catch (e) {
if (!e.conflicts || e.conflicts.length === 0) {
await sleep(MERGE_STEP_DELAY_WAIT_FOR_LOCK);
await sg.checkout(to);
await (rebase ? sg.rebase([from]) : sg.merge([from]));
}
}
console.log(emoji.get('white_check_mark'));
}
async function pushBranches(sg, branches, forcePush, remote = DEFAULT_REMOTE) {
console.log(`Pushing changes to remote ${remote}...`);
// Ugh... `raw` doesn't allow empty strings or `undefined`s, so let's filter any "empty" args.
const args = ['push', forcePush ? '--force' : undefined, remote].concat(branches).filter(Boolean);
await sg.raw(args);
console.log('All changes pushed ' + emoji.get('white_check_mark'));
}
async function getUnmergedBranches(sg, branches, baseBranch = DEFAULT_BASE_BRANCH) {
const mergedBranchesOutput = await sg.raw(['branch', '--merged', baseBranch]);
const mergedBranches = mergedBranchesOutput
.split('\n')
.map(b => b.trim())
.filter(Boolean);
return difference(branches, mergedBranches);
}
async function getConfigPath(sg) {
const repoRootPath = (await sg.raw(['rev-parse', '--show-toplevel'])).trim();
return `${repoRootPath}/.pr-train.yml`;
}
/**
* @typedef {string | Object.<string, { combined: boolean, initSha?: string }>} BranchCfg
* @typedef {Object.<string, Array.<string | BranchCfg>>} TrainCfg
* @typedef {{ prs?: Object, trains: Array.<TrainCfg>}} YamlCfg
*/
/**
* @param {simpleGit.SimpleGit} sg
* @return {Promise.<YamlCfg>}
*/
async function loadConfig(sg) {
const path = await getConfigPath(sg);
return yaml.safeLoad(fs.readFileSync(path, 'utf8'));
}
/**
* @param {BranchCfg} branchCfg
*/
function getBranchName(branchCfg) {
return typeof branchCfg === 'string' ? branchCfg : Object.keys(branchCfg)[0];
}
/**
* @return {Promise.<Array.<BranchCfg>>}
*/
async function getBranchesConfigInCurrentTrain(sg, config) {
const branches = await sg.branchLocal();
const currentBranch = branches.current;
const { trains } = config;
if (!trains) {
return null;
}
const key = Object.keys(trains).find(trainKey => {
const branches = trains[trainKey];
const branchNames = branches.map(b => getBranchName(b));
return branchNames.indexOf(currentBranch) >= 0;
});
return key && trains[key];
}
/**
* @param {Array.<BranchCfg>} branchConfig
*/
function getBranchesInCurrentTrain(branchConfig) {
return branchConfig.map(b => getBranchName(b));
}
/**
* @param {Array.<BranchCfg>} branchConfig
*/
function getCombinedBranch(branchConfig) {
const combinedBranch = /** @type {Object<string, {combined: boolean}>} */ branchConfig.find(cfg => {
if (!cfg || typeof cfg === 'string') {
return false;
}
const branchName = Object.keys(cfg)[0];
return cfg[branchName].combined;
});
if (!combinedBranch) {
return undefined;
}
const branchName = Object.keys(combinedBranch)[0];
return branchName;
}
async function handleSwitchToBranchCommand(sg, sortedBranches, combinedBranch) {
const switchToBranchIndex = program.args[0];
if (typeof switchToBranchIndex === 'undefined') {
return;
}
let targetBranch;
if (switchToBranchIndex === 'combined') {
targetBranch = combinedBranch;
} else {
targetBranch = sortedBranches[switchToBranchIndex];
}
if (!targetBranch) {
console.log(`Could not find branch with index ${switchToBranchIndex}`.red);
process.exit(3);
}
await sg.checkout(targetBranch);
console.log(`Switched to branch ${targetBranch}`);
process.exit(0);
}
/**
* @param {YamlCfg} ymlConfig
* @param {string} path
*/
function getConfigOption(ymlConfig, path) {
const parts = path.split(/\./g);
let ptr = ymlConfig;
while (ptr && parts.length) {
const part = parts.shift();
// cater for both kebab case and camel cased variants of key, just for developer convenience.
ptr = part in ptr ? ptr[part] : ptr[camelCase(part)];
}
return ptr;
}
async function main() {
const sg = simpleGit();
if (!(await sg.checkIsRepo())) {
console.log('Not a git repo'.red);
process.exit(1);
}
// try to create or init the config first so we can read values from it
if (process.argv.includes('--init')) {
if (fs.existsSync(await getConfigPath(sg))) {
console.log('.pr-train.yml already exists');
process.exit(1);
}
const root = path.dirname(require.main.filename);
const cfgTpl = fs.readFileSync(`${root}/cfg_template.yml`);
fs.writeFileSync(await getConfigPath(sg), cfgTpl);
console.log(`Created a ".pr-train.yml" file. Please make sure it's gitignored.`);
process.exit(0);
}
let ymlConfig;
try {
ymlConfig = await loadConfig(sg);
} catch (e) {
if (e instanceof yaml.YAMLException) {
console.log('There seems to be an error in `.pr-train.yml`.');
console.log(e.message);
process.exit(1);
}
console.log('`.pr-train.yml` file not found. Please run `git pr-train --init` to create one.'.red);
process.exit(1);
}
const defaultBase = getConfigOption(ymlConfig, 'prs.main-branch-name') || DEFAULT_BASE_BRANCH;
const draftByDefault = !!getConfigOption(ymlConfig, 'prs.draft-by-default');
program
.version(package.version)
.option('--init', 'Creates a .pr-train.yml file with an example configuration')
.option('-p, --push', 'Push changes')
.option('-l, --list', 'List branches in current train')
.option('-r, --rebase', 'Rebase branches rather than merging them')
.option('-f, --force', 'Force push to remote')
.option('--push-merged', 'Push all branches (including those that have already been merged into the base branch)')
.option('--remote <remote>', 'Set remote to push to. Defaults to "origin"')
.option('-b, --base <base>', `Specify the base branch to use for the first and combined PRs.`, defaultBase)
.option('-d, --draft', 'Create PRs in draft mode', draftByDefault)
.option('--no-draft', 'Do not create PRs in draft mode', !draftByDefault)
.option('-c, --create-prs', 'Create GitHub PRs from your train branches');
program.on('--help', () => {
console.log('');
console.log(' Switching branches:');
console.log('');
console.log(
' $ `git pr-train <index>` will switch to branch with index <index> (e.g. 0 or 5). ' +
'If <index> is "combined", it will switch to the combined branch.'
);
console.log('');
console.log(' Creating GitHub PRs:');
console.log('');
console.log(
' $ `git pr-train -p --create-prs` will create GH PRs for all branches in your train (with a "table of contents")'
);
console.log(
colors.italic(
` Please note you'll need to create a \`\${HOME}/.pr-train\` file with your GitHub access token first.`
)
);
console.log('');
});
program.parse(process.argv);
program.createPrs && checkGHKeyExists();
const baseBranch = program.base; // will have default value if one is not supplied
const draft = program.draft != null ? program.draft : draftByDefault;
const { current: currentBranch, all: allBranches } = await sg.branchLocal();
const trainCfg = await getBranchesConfigInCurrentTrain(sg, ymlConfig);
if (!trainCfg) {
console.log(`Current branch ${currentBranch} is not a train branch.`);
process.exit(1);
}
const sortedTrainBranches = getBranchesInCurrentTrain(trainCfg);
const combinedTrainBranch = getCombinedBranch(trainCfg);
if (combinedTrainBranch && !allBranches.includes(combinedTrainBranch)) {
const lastBranchBeforeCombined = sortedTrainBranches[sortedTrainBranches.length - 2];
await sg.raw(['branch', combinedTrainBranch, lastBranchBeforeCombined]);
}
await handleSwitchToBranchCommand(sg, sortedTrainBranches, combinedTrainBranch);
console.log(`I've found these partial branches:`);
const branchesToPrint = sortedTrainBranches.map((b, idx) => {
const branch = b === currentBranch ? `${b.green.bold}` : b;
const suffix = b === combinedTrainBranch ? ' (combined)' : '';
return `[${idx}] ${branch}${suffix}`;
});
if (program.list) {
const answer = await inquirer.prompt([
{
type: 'list',
name: 'branch',
message: 'Select a branch to checkout',
choices: branchesToPrint.map((b, i) => ({ name: b, value: sortedTrainBranches[i] })),
pageSize: 20,
},
]);
console.log(`checking out branch ${answer.branch}`);
await sg.checkout(answer.branch);
return;
}
console.log(branchesToPrint.map(b => ` -> ${b}`).join('\n'), '\n');
async function findAndPushBranches() {
let branchesToPush = sortedTrainBranches;
if (!program.pushMerged) {
branchesToPush = await getUnmergedBranches(sg, sortedTrainBranches, baseBranch);
const branchDiff = difference(sortedTrainBranches, branchesToPush);
if (branchDiff.length > 0) {
console.log(`Not pushing already merged branches: ${branchDiff.join(', ')}`);
}
}
pushBranches(sg, branchesToPush, program.force, program.remote);
}
// If we're creating PRs, don't combine branches (that might change branch HEADs and consequently
// the PR titles and descriptions). Just push and create the PRs.
if (program.createPrs) {
await findAndPushBranches();
await ensurePrsExist({
sg,
allBranches: sortedTrainBranches,
combinedBranch: combinedTrainBranch,
remote: program.remote,
draft,
baseBranch,
printLinks: getConfigOption(ymlConfig, 'prs.print-urls'),
});
return;
}
for (let i = 0; i < sortedTrainBranches.length - 1; ++i) {
const b1 = sortedTrainBranches[i];
const b2 = sortedTrainBranches[i + 1];
if (isBranchAncestor(sg, b1, b2)) {
console.log(`Branch ${b1} is an ancestor of ${b2} => nothing to do`);
continue;
}
await combineBranches(sg, program.rebase, b1, b2);
await sleep(MERGE_STEP_DELAY_MS);
}
if (program.push || program.pushMerged) {
await findAndPushBranches();
}
await sg.checkout(currentBranch);
}
main().catch(e => {
console.log(`${emoji.get('x')} An error occurred. Was there a conflict perhaps?`.red);
console.error('error', e);
});