acp-push 2.3.1 → 2.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/bin/acp.js +181 -35
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,6 +31,7 @@ npm install -g acp-push
|
|
|
31
31
|
| `-bpa`, `--branch-pr-auto` | Push to a new branch on `origin`, open a pull request into the default branch, merge it, then delete the branch |
|
|
32
32
|
| `-bopa`, `--branch-origin-pr-auto` | Same as `-bpa` |
|
|
33
33
|
| `-ua-o`, `--upstream-allow-origin` | Same as `-bpa`: the pull request goes to `origin` itself and is merged there |
|
|
34
|
+
| `--parallel[=<n>]` | With `-c -ua-om`: every chunk gets its own branch, all branches are pushed and all PRs opened at once, then merged. `<n>` sets the number of chunks (default 10, can be more). Needs git 2.38+ |
|
|
34
35
|
| `-ua-om`, `--upstream-allow-origin-main` | Push your current branch (not `main`) to `origin`, open a PR into `main`, merge it, then pull `main` back into your branch. The branch is kept. Stops if you are on `main` |
|
|
35
36
|
| `-b`, `--branch <name>` | Push to `<name>` instead of `main` (`-b-<name>` works too, e.g. `-b-nida`) |
|
|
36
37
|
| `-m`, `--message <text>` | Use your own commit message instead of a random one |
|
|
@@ -50,6 +51,8 @@ acp -ua-o # same as -bpa
|
|
|
50
51
|
acp -c -ua-o # per chunk: commit, push, PR into origin, merge, pull main
|
|
51
52
|
acp -ua-om # on branch nida: push nida, PR into main, merge, pull main into nida
|
|
52
53
|
acp -c -ua-om # same, once per chunk (refuses to run on main)
|
|
54
|
+
acp -c -ua-om --parallel # 10 chunks, 10 PRs opened at once, then merged
|
|
55
|
+
acp -c -ua-om --parallel=25 # up to 25 chunks and PRs
|
|
53
56
|
acp -b dev # git push origin dev
|
|
54
57
|
acp --branch=dev # same as above
|
|
55
58
|
acp -b-nida # same as -b nida, git push origin nida
|
package/bin/acp.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
const { spawnSync } = require('child_process');
|
|
4
|
+
const { spawnSync, execFile } = require('child_process');
|
|
5
5
|
const fs = require('fs');
|
|
6
6
|
const path = require('path');
|
|
7
7
|
const messages = require('../lib/messages');
|
|
@@ -44,6 +44,10 @@ Options:
|
|
|
44
44
|
-c, --chunks Split your changes into 10 smaller commits. With -u or
|
|
45
45
|
-ua, each commit is pushed with its own pull request
|
|
46
46
|
(-c-<name> is -c plus -b <name>, e.g. -c-nida)
|
|
47
|
+
--parallel[=<n>] With -c -ua-om: give every chunk its own branch, push
|
|
48
|
+
them all at once, open all the pull requests at once,
|
|
49
|
+
then merge them. <n> sets how many chunks (default 10,
|
|
50
|
+
can be more). Merging still goes one after another
|
|
47
51
|
-h, --help Show this help
|
|
48
52
|
-v, --version Show the version
|
|
49
53
|
|
|
@@ -88,6 +92,9 @@ Examples:
|
|
|
88
92
|
acp -c -ua-o ${dim('# each chunk: commit, push, PR into origin, merge, pull main')}
|
|
89
93
|
acp -ua-om ${dim('# on branch nida: push nida, PR into main, merge, pull main')}
|
|
90
94
|
acp -c -ua-om ${dim('# same, once per chunk (not allowed while on main)')}
|
|
95
|
+
acp -c -ua-om --parallel ${dim('# 10 chunks, 10 PRs opened at once, then merged')}
|
|
96
|
+
acp -c -ua-om --parallel=25
|
|
97
|
+
${dim('# up to 25 chunks and PRs')}
|
|
91
98
|
acp -b dev ${dim('# push to origin/dev')}
|
|
92
99
|
acp -b-nida ${dim('# same as -b nida: push to origin/nida')}
|
|
93
100
|
acp -c -b-nida ${dim('# commit in 10 chunks, then push to origin/nida')}
|
|
@@ -106,7 +113,7 @@ function fail(message) {
|
|
|
106
113
|
|
|
107
114
|
// ---------- argument parsing ----------
|
|
108
115
|
function parseArgs(argv) {
|
|
109
|
-
const options = { branch: 'main', branchGiven: false, message: null, pr: false, merge: false, chunks: false, bpa: false, originMain: false };
|
|
116
|
+
const options = { branch: 'main', branchGiven: false, message: null, pr: false, merge: false, chunks: false, bpa: false, originMain: false, parallel: 0 };
|
|
110
117
|
|
|
111
118
|
for (let i = 0; i < argv.length; i++) {
|
|
112
119
|
let flag = argv[i];
|
|
@@ -173,6 +180,16 @@ function parseArgs(argv) {
|
|
|
173
180
|
options.pr = true;
|
|
174
181
|
options.merge = true;
|
|
175
182
|
break;
|
|
183
|
+
case '--parallel':
|
|
184
|
+
case '--parallal': // common misspelling
|
|
185
|
+
if (inlineValue === undefined) {
|
|
186
|
+
options.parallel = MAX_CHUNKS;
|
|
187
|
+
} else {
|
|
188
|
+
const count = Number(inlineValue);
|
|
189
|
+
if (!Number.isInteger(count) || count < 1) fail(`${flag} needs a whole number, like ${flag}=20`);
|
|
190
|
+
options.parallel = count;
|
|
191
|
+
}
|
|
192
|
+
break;
|
|
176
193
|
case '-b':
|
|
177
194
|
case '--branch':
|
|
178
195
|
options.branch = takeValue();
|
|
@@ -287,14 +304,14 @@ function stage(unit) {
|
|
|
287
304
|
// Commits the changes as MAX_CHUNKS commits (fewer if there are fewer separate
|
|
288
305
|
// changes), splitting the individual changes (hunks) as evenly as possible,
|
|
289
306
|
// even within one file. afterCommit runs after each commit. Returns false when there was nothing to commit.
|
|
290
|
-
function commitInChunks(message, afterCommit) {
|
|
307
|
+
function commitInChunks(message, afterCommit, limit = MAX_CHUNKS) {
|
|
291
308
|
// Start from a clean index so each commit holds only its own chunk.
|
|
292
309
|
git(['reset', '-q'], { quiet: true });
|
|
293
310
|
|
|
294
311
|
const total = changeUnits().length;
|
|
295
312
|
if (!total) return false;
|
|
296
313
|
|
|
297
|
-
const count = Math.min(
|
|
314
|
+
const count = Math.min(limit, total);
|
|
298
315
|
console.log(cyan(`• Splitting ${total} change(s) into ${count} commit(s)`));
|
|
299
316
|
|
|
300
317
|
const pool = [...messages];
|
|
@@ -517,9 +534,7 @@ function sleep(ms) {
|
|
|
517
534
|
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
518
535
|
}
|
|
519
536
|
|
|
520
|
-
|
|
521
|
-
// branch, and merges it when `merge` is set.
|
|
522
|
-
function pullRequest(target, branch, merge) {
|
|
537
|
+
function ghActors() {
|
|
523
538
|
const gh = ghAccounts();
|
|
524
539
|
const accountFor = (owner) => gh && gh.accounts.find((a) => a.toLowerCase() === owner.toLowerCase());
|
|
525
540
|
// GH_TOKEN makes gh act as that account. Without it gh uses its active one.
|
|
@@ -527,36 +542,27 @@ function pullRequest(target, branch, merge) {
|
|
|
527
542
|
const token = login && ghToken(login, login === gh.active);
|
|
528
543
|
return token ? { GH_TOKEN: token } : undefined;
|
|
529
544
|
};
|
|
545
|
+
return { accountFor, envFor };
|
|
546
|
+
}
|
|
530
547
|
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
console.log(cyan(`› gh pr create --repo ${target.repo} --head ${head}`));
|
|
536
|
-
const created = run(
|
|
537
|
-
'gh',
|
|
538
|
-
['pr', 'create', '--repo', target.repo, '--head', head, '--title', title, '--body', ''],
|
|
539
|
-
{ quiet: true, env: envFor(opener) }
|
|
540
|
-
);
|
|
541
|
-
// gh prints the new PR's URL, or the open one's when it already exists.
|
|
548
|
+
// Reads the output of `gh pr create`: the pull request's URL, and whether it
|
|
549
|
+
// is new. gh prints the open one's URL when it already exists. Returns null
|
|
550
|
+
// (after printing gh's error) when there is no pull request.
|
|
551
|
+
function readCreated(created) {
|
|
542
552
|
const output = `${created.stdout || ''}\n${created.stderr || ''}`;
|
|
543
553
|
const found = output.match(/https:\/\/github\.com\/\S+\/pull\/\d+/);
|
|
544
554
|
if (!found || (created.status !== 0 && !/already exists/i.test(output))) {
|
|
545
555
|
if (created.stderr) process.stderr.write(created.stderr);
|
|
546
|
-
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
const url = found[0];
|
|
550
|
-
if (created.status === 0) {
|
|
551
|
-
console.log(green(`✔ Pull request opened: ${url}`));
|
|
552
|
-
} else {
|
|
553
|
-
console.log(yellow(`• Pull request already open, updated by this push: ${url}`));
|
|
556
|
+
return null;
|
|
554
557
|
}
|
|
555
|
-
|
|
558
|
+
return { url: found[0], isNew: created.status === 0 };
|
|
559
|
+
}
|
|
556
560
|
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
561
|
+
// Merges a pull request, but only with write access to upstream (write,
|
|
562
|
+
// maintain or admin). The fork's owner often doesn't have it, so the gh
|
|
563
|
+
// account that owns upstream is tried first when there is one.
|
|
564
|
+
// Returns { state: 'merged' | 'open' | 'failed', status }.
|
|
565
|
+
function mergePullRequest(target, url, opener, { accountFor, envFor }) {
|
|
560
566
|
const owner = accountFor(target.baseOwner);
|
|
561
567
|
let merger = owner || opener;
|
|
562
568
|
let env = envFor(merger);
|
|
@@ -568,7 +574,7 @@ function pullRequest(target, branch, merge) {
|
|
|
568
574
|
}
|
|
569
575
|
if (!allowed) {
|
|
570
576
|
console.log(yellow(`• No write access to ${target.repo}, so the pull request stays open for a maintainer to merge.`));
|
|
571
|
-
return
|
|
577
|
+
return { state: 'open' };
|
|
572
578
|
}
|
|
573
579
|
if (merger && merger !== opener) {
|
|
574
580
|
console.log(cyan(`• Merging with GitHub account "${merger}" (owner of upstream)`));
|
|
@@ -585,10 +591,42 @@ function pullRequest(target, branch, merge) {
|
|
|
585
591
|
if (merged.status !== 0) {
|
|
586
592
|
if (merged.stderr) process.stderr.write(merged.stderr);
|
|
587
593
|
console.error(yellow(`⚠ The pull request is still open: ${url}`));
|
|
588
|
-
|
|
594
|
+
return { state: 'failed', status: merged.status };
|
|
589
595
|
}
|
|
590
596
|
console.log(green(`✔ Merged into ${target.repo}`));
|
|
591
|
-
return
|
|
597
|
+
return { state: 'merged' };
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// Opens a pull request from <branch> on the fork to upstream's default
|
|
601
|
+
// branch, and merges it when `merge` is set.
|
|
602
|
+
function pullRequest(target, branch, merge) {
|
|
603
|
+
const actors = ghActors();
|
|
604
|
+
const { accountFor, envFor } = actors;
|
|
605
|
+
|
|
606
|
+
const opener = target.opener || accountFor(target.headOwner);
|
|
607
|
+
const head = `${target.headOwner}:${branch}`;
|
|
608
|
+
const title = git(['log', '-1', '--pretty=%s'], { quiet: true }).stdout.trim();
|
|
609
|
+
|
|
610
|
+
console.log(cyan(`› gh pr create --repo ${target.repo} --head ${head}`));
|
|
611
|
+
const created = run(
|
|
612
|
+
'gh',
|
|
613
|
+
['pr', 'create', '--repo', target.repo, '--head', head, '--title', title, '--body', ''],
|
|
614
|
+
{ quiet: true, env: envFor(opener) }
|
|
615
|
+
);
|
|
616
|
+
const opened = readCreated(created);
|
|
617
|
+
if (!opened) fail(`"gh pr create" failed (exit code ${created.status}).`);
|
|
618
|
+
|
|
619
|
+
const url = opened.url;
|
|
620
|
+
if (opened.isNew) {
|
|
621
|
+
console.log(green(`✔ Pull request opened: ${url}`));
|
|
622
|
+
} else {
|
|
623
|
+
console.log(yellow(`• Pull request already open, updated by this push: ${url}`));
|
|
624
|
+
}
|
|
625
|
+
if (!merge) return false;
|
|
626
|
+
|
|
627
|
+
const result = mergePullRequest(target, url, opener, actors);
|
|
628
|
+
if (result.state === 'failed') fail(`"gh pr merge" failed (exit code ${result.status}).`);
|
|
629
|
+
return result.state === 'merged';
|
|
592
630
|
}
|
|
593
631
|
|
|
594
632
|
// -ua-om, after the merge: the branch is kept, and main (which now holds the
|
|
@@ -605,6 +643,111 @@ function syncWithBase(remote, base, auth) {
|
|
|
605
643
|
console.log(yellow(`⚠ Could not pull ${base} into your branch, run: git pull ${remote} ${base}`));
|
|
606
644
|
}
|
|
607
645
|
|
|
646
|
+
// ---------- -c -ua-om --parallel ----------
|
|
647
|
+
// Runs gh without blocking, so many pull requests can be opened at once.
|
|
648
|
+
// Always resolves, with the same { status, stdout, stderr } as spawnSync.
|
|
649
|
+
function ghAsync(args, env) {
|
|
650
|
+
return new Promise((resolve) => {
|
|
651
|
+
execFile('gh', args, { encoding: 'utf8', env: env ? { ...process.env, ...env } : process.env }, (error, stdout, stderr) => {
|
|
652
|
+
resolve({ status: error ? (typeof error.code === 'number' ? error.code : 1) : 0, stdout, stderr });
|
|
653
|
+
});
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// The tree you get by applying what `commit` changed on top of its `parent`
|
|
658
|
+
// onto `base`, like a cherry-pick that never touches your working copy.
|
|
659
|
+
// Returns the tree id, or null when the change doesn't fit on `base`.
|
|
660
|
+
function cherryPickTree(base, parent, commit) {
|
|
661
|
+
const result = git(['merge-tree', '--write-tree', `--merge-base=${parent}`, base, commit], { quiet: true });
|
|
662
|
+
if (result.status === 129 || /unknown option/i.test(result.stderr || '')) {
|
|
663
|
+
fail('--parallel needs git 2.38 or newer (for "git merge-tree --write-tree").');
|
|
664
|
+
}
|
|
665
|
+
return result.status === 0 ? result.stdout.split('\n')[0].trim() : null;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// Commits your changes in chunks like -c, but instead of one branch and one
|
|
669
|
+
// pull request after another, every chunk becomes its own branch (cut from
|
|
670
|
+
// where you started), and all of them are pushed and opened as pull requests
|
|
671
|
+
// together. Merging has to happen one at a time, so that part still runs in
|
|
672
|
+
// a row, then your branch gets the merged result.
|
|
673
|
+
async function parallelRun({ message, count, remote, branch, target, auth }) {
|
|
674
|
+
const start = git(['rev-parse', 'HEAD'], { quiet: true });
|
|
675
|
+
if (start.status !== 0) fail('--parallel needs a repo with at least one commit.');
|
|
676
|
+
const base = start.stdout.trim();
|
|
677
|
+
|
|
678
|
+
const commits = [];
|
|
679
|
+
const done = commitInChunks(message, () => commits.push(git(['rev-parse', 'HEAD'], { quiet: true }).stdout.trim()), count);
|
|
680
|
+
if (!done) {
|
|
681
|
+
console.log(yellow('• Nothing new to commit.'));
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
// One branch per chunk, each holding just that chunk on top of `base`.
|
|
686
|
+
const stamp = new Date().toISOString().replace(/\D/g, '').slice(2, 14);
|
|
687
|
+
const heads = commits.map((sha, i) => {
|
|
688
|
+
const tree = cherryPickTree(base, i === 0 ? base : commits[i - 1], sha);
|
|
689
|
+
if (!tree) fail(`Chunk ${i + 1}/${commits.length} could not be applied on its own. Nothing was pushed.`);
|
|
690
|
+
const subject = git(['log', '-1', '--pretty=%s', sha], { quiet: true }).stdout.trim();
|
|
691
|
+
const own = git(['commit-tree', tree, '-p', base, '-m', subject], { quiet: true });
|
|
692
|
+
if (own.status !== 0) fail(`Could not build chunk ${i + 1}: ${(own.stderr || '').trim()}`);
|
|
693
|
+
return { name: `${branch}-chunk-${stamp}-${i + 1}`, sha: own.stdout.trim(), subject };
|
|
694
|
+
});
|
|
695
|
+
|
|
696
|
+
// One push for all the branches.
|
|
697
|
+
step(['push', remote, ...heads.map((h) => `${h.sha}:refs/heads/${h.name}`)], auth);
|
|
698
|
+
console.log(green(`✔ Pushed ${heads.length} branch(es) to ${remote}`));
|
|
699
|
+
|
|
700
|
+
const actors = ghActors();
|
|
701
|
+
const opener = target.opener || actors.accountFor(target.headOwner);
|
|
702
|
+
const env = actors.envFor(opener);
|
|
703
|
+
|
|
704
|
+
console.log(cyan(`› gh pr create ×${heads.length} (all at once)`));
|
|
705
|
+
const created = await Promise.all(
|
|
706
|
+
heads.map((h) =>
|
|
707
|
+
ghAsync(['pr', 'create', '--repo', target.repo, '--head', `${target.headOwner}:${h.name}`, '--title', h.subject, '--body', ''], env)
|
|
708
|
+
)
|
|
709
|
+
);
|
|
710
|
+
created.forEach((result, i) => {
|
|
711
|
+
const opened = readCreated(result);
|
|
712
|
+
heads[i].url = opened && opened.url;
|
|
713
|
+
if (opened) console.log(green(`✔ Pull request opened: ${opened.url}`));
|
|
714
|
+
else console.log(yellow(`⚠ Could not open a pull request for ${heads[i].name}`));
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
// Merging can only be done one pull request at a time.
|
|
718
|
+
for (const h of heads.filter((x) => x.url)) {
|
|
719
|
+
const result = mergePullRequest(target, h.url, opener, actors);
|
|
720
|
+
h.merged = result.state === 'merged';
|
|
721
|
+
if (result.state === 'open') break; // no write access, same answer for the rest
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
const merged = heads.filter((h) => h.merged);
|
|
725
|
+
if (merged.length) {
|
|
726
|
+
const names = merged.map((h) => h.name);
|
|
727
|
+
console.log(cyan(`› git push ${remote} --delete (${names.length} branches)`));
|
|
728
|
+
const deleted = git([...(auth.extraArgs || []), 'push', remote, '--delete', ...names], {
|
|
729
|
+
env: auth.env,
|
|
730
|
+
stdio: ['inherit', 'inherit', 'pipe'],
|
|
731
|
+
});
|
|
732
|
+
if (deleted.status === 0) {
|
|
733
|
+
console.log(green(`✔ Deleted ${names.length} branch(es) on ${remote}`));
|
|
734
|
+
} else {
|
|
735
|
+
if (deleted.stderr) process.stderr.write(deleted.stderr);
|
|
736
|
+
console.log(yellow('⚠ Could not delete the chunk branches, delete them yourself.'));
|
|
737
|
+
}
|
|
738
|
+
syncWithBase(remote, target.base, auth);
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
const kept = heads.filter((h) => !h.merged);
|
|
742
|
+
if (!kept.length) {
|
|
743
|
+
console.log(green(`✔ All ${heads.length} pull requests merged.`));
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
console.log(yellow(`⚠ ${merged.length} of ${heads.length} merged. Left open, with their branches kept:`));
|
|
747
|
+
kept.forEach((h) => console.log(yellow(` ${h.name}${h.url ? ` ${h.url}` : ''}`)));
|
|
748
|
+
process.exitCode = 1;
|
|
749
|
+
}
|
|
750
|
+
|
|
608
751
|
// -bpa, after the merge: deletes the pull request's branch on the remote and,
|
|
609
752
|
// when you are on the default branch, fast-forwards it to the merged result.
|
|
610
753
|
// The pull request is already merged, so problems here only warn.
|
|
@@ -822,9 +965,10 @@ function branchPrAutoPairs(targets, branch) {
|
|
|
822
965
|
|
|
823
966
|
// ---------- main ----------
|
|
824
967
|
function main() {
|
|
825
|
-
const { branch: requestedBranch, branchGiven, message, pr, merge, chunks, bpa, originMain } = parseArgs(
|
|
968
|
+
const { branch: requestedBranch, branchGiven, message, pr, merge, chunks, bpa, originMain, parallel } = parseArgs(
|
|
826
969
|
process.argv.slice(2)
|
|
827
970
|
);
|
|
971
|
+
if (parallel && !(chunks && originMain)) fail('--parallel only works together with -c and -ua-om: acp -c -ua-om --parallel');
|
|
828
972
|
let branch = requestedBranch;
|
|
829
973
|
let remote = 'origin';
|
|
830
974
|
|
|
@@ -889,6 +1033,8 @@ function main() {
|
|
|
889
1033
|
// Without a fork, the account that pushed the branch also opens the pull request.
|
|
890
1034
|
if (target && target.direct && auth && auth.env) target.opener = auth.env.ACP_GH_USER;
|
|
891
1035
|
|
|
1036
|
+
if (parallel) return parallelRun({ message, count: parallel, remote, branch, target, auth });
|
|
1037
|
+
|
|
892
1038
|
// "HEAD:<branch>" pushes whatever branch you're on (main, master, anything)
|
|
893
1039
|
// to <branch> on the remote, so the default always lands on "main".
|
|
894
1040
|
const push = () => {
|
|
@@ -949,4 +1095,4 @@ function main() {
|
|
|
949
1095
|
if (pr) openPullRequest();
|
|
950
1096
|
}
|
|
951
1097
|
|
|
952
|
-
main();
|
|
1098
|
+
Promise.resolve(main()).catch((error) => fail(error && error.message ? error.message : String(error)));
|