acp-push 2.1.0 → 2.2.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 +42 -0
- package/bin/acp.js +274 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,6 +28,7 @@ npm install -g acp-push
|
|
|
28
28
|
| --- | --- |
|
|
29
29
|
| `-u`, `--upstream` | Push to `origin`, then open a pull request to `upstream` |
|
|
30
30
|
| `-ua`, `--upstream-allow` (or `--upstream-merge`) | Same as `-u`, then merge the pull request if you have write access to `upstream` |
|
|
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 |
|
|
31
32
|
| `-b`, `--branch <name>` | Push to `<name>` instead of `main` |
|
|
32
33
|
| `-m`, `--message <text>` | Use your own commit message instead of a random one |
|
|
33
34
|
| `-c`, `--chunks` | Split your changes into 10 smaller commits (by change, not by file), then push |
|
|
@@ -40,6 +41,7 @@ npm install -g acp-push
|
|
|
40
41
|
acp # git push origin main
|
|
41
42
|
acp -u # git push origin main, then open a PR to upstream
|
|
42
43
|
acp -ua # same, then merge the PR if you have write access
|
|
44
|
+
acp -bpa # new branch, PR into main, merge, delete the branch
|
|
43
45
|
acp -b dev # git push origin dev
|
|
44
46
|
acp --branch=dev # same as above
|
|
45
47
|
acp -u -b fix # git push origin fix, then open a PR to upstream
|
|
@@ -81,6 +83,27 @@ gh pr merge <pr> --merge
|
|
|
81
83
|
- Of your logged-in `gh` accounts, `acp` uses one that can push to `upstream` (the owner's account first) for the push, the pull request and the merge.
|
|
82
84
|
- The branch is not deleted after the merge.
|
|
83
85
|
|
|
86
|
+
## One repo: branch, PR, merge, delete (`-bpa`)
|
|
87
|
+
|
|
88
|
+
`acp -bpa` does the whole branch workflow in the repo you are in, with no fork or `upstream` needed:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
git add .
|
|
92
|
+
git commit -m "<one of 25 random messages>"
|
|
93
|
+
git push origin HEAD:acp-<date>
|
|
94
|
+
gh pr create --repo <owner>/<repo> --head <owner>:acp-<date>
|
|
95
|
+
gh pr merge <pr> --merge
|
|
96
|
+
git push origin --delete acp-<date>
|
|
97
|
+
git pull --ff-only origin <default branch> # only if you are on the default branch
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
- You need the [GitHub CLI](https://cli.github.com) (`gh`) and an `origin` remote on GitHub.
|
|
101
|
+
- The branch is `acp-<date>` unless you pass `-b <name>`. It only exists on the remote, so no local branch is created or left behind.
|
|
102
|
+
- The pull request goes into the default branch of `origin`. Its title is your last commit message.
|
|
103
|
+
- If your account can't merge (no write access), the pull request stays open and the branch is **not** deleted.
|
|
104
|
+
- If the branch was already deleted (for example by the repo's "automatically delete head branches" setting), `acp` just says so.
|
|
105
|
+
- Works with `-c` (each chunk gets its own branch round trip). With git-multi-commit projects, see [below](#git-multi-commit-projects).
|
|
106
|
+
|
|
84
107
|
## Multiple GitHub accounts
|
|
85
108
|
|
|
86
109
|
If you have the [GitHub CLI](https://cli.github.com) (`gh`) installed, `acp` picks the right account for each repo automatically:
|
|
@@ -112,6 +135,25 @@ git-multi-commit
|
|
|
112
135
|
|
|
113
136
|
No `origin` remote is needed in this case. With `-u` / `-ua`, `acp` looks for an `upstream` remote: if it exists, it also pushes the branch (to `origin`, or straight to `upstream` when there is no `origin`) and opens (and with `-ua`, merges) the pull request after `git-multi-commit` finishes. If `upstream` is missing, it prints a warning and skips that part. `-b` only applies when the pull request step runs. Install it with `npm install -g git-multi-commit`.
|
|
114
137
|
|
|
138
|
+
With `-bpa`, every repo in `.git-multi-commit.json` goes through the branch workflow instead of being pushed to directly:
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
git add .
|
|
142
|
+
git commit -m "<one of 25 random messages>"
|
|
143
|
+
# for every repo in the config, using that pair's account:
|
|
144
|
+
gh api -X POST repos/<owner>/<repo>/git/refs # acp-<date>, from the default branch
|
|
145
|
+
git-multi-commit # pushes to acp-<date>
|
|
146
|
+
gh pr create --repo <owner>/<repo> --head <owner>:acp-<date>
|
|
147
|
+
gh pr merge <pr> --merge
|
|
148
|
+
gh api -X DELETE repos/<owner>/<repo>/git/refs/heads/acp-<date>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
- The pull request goes into each repo's default branch, even if the pair has another `branch` in its config.
|
|
152
|
+
- Every account must be logged in to `gh` and able to push to its repo, and every repo needs at least one commit. `acp` checks this before committing anything.
|
|
153
|
+
- A repo with nothing new gets no pull request, and its branch is removed. If one pair fails, the others still finish, and `acp` exits with an error at the end.
|
|
154
|
+
- `.git-multi-commit.json` is pointed at the branch while `git-multi-commit` runs, then put back exactly as it was.
|
|
155
|
+
- `-c` is ignored, as with any git-multi-commit run. Your local branch is not updated, because these repos have their own histories.
|
|
156
|
+
|
|
115
157
|
## Notes
|
|
116
158
|
|
|
117
159
|
- If there is nothing new to commit, `acp` skips the commit and still pushes, so any commits you made earlier get sent.
|
package/bin/acp.js
CHANGED
|
@@ -25,6 +25,11 @@ Options:
|
|
|
25
25
|
-u, --upstream Push to origin, then open a pull request to "upstream"
|
|
26
26
|
-ua, --upstream-allow Same as -u, then merge it if you have write access
|
|
27
27
|
(--upstream-merge works too)
|
|
28
|
+
-bpa, --branch-pr-auto Push to a new branch on origin, open a pull request into
|
|
29
|
+
the default branch, merge it, then delete the branch
|
|
30
|
+
(-b <name> picks the branch name)
|
|
31
|
+
-bopa, --branch-origin-pr-auto
|
|
32
|
+
Same as -bpa
|
|
28
33
|
-b, --branch <name> Push to <name> instead of "main"
|
|
29
34
|
-m, --message <text> Use your own commit message instead of a random one
|
|
30
35
|
-c, --chunks Split your changes into 10 smaller commits. With -u or
|
|
@@ -47,6 +52,12 @@ Pull requests:
|
|
|
47
52
|
and opens the pull request from there. The branch is your current one,
|
|
48
53
|
-b <name>, or a new acp-<date> branch when you are on upstream's default.
|
|
49
54
|
|
|
55
|
+
One repo (-bpa, -bopa):
|
|
56
|
+
Needs the GitHub CLI (gh) and an "origin" remote on GitHub. The branch is
|
|
57
|
+
named acp-<date> unless you give -b <name>. The merge needs write access to
|
|
58
|
+
origin; without it the pull request stays open and the branch is kept.
|
|
59
|
+
If you are on the default branch, it is updated after the merge.
|
|
60
|
+
|
|
50
61
|
git-multi-commit:
|
|
51
62
|
If the repo root has a .git-multi-commit.json file, acp still adds and
|
|
52
63
|
commits locally, then runs git-multi-commit instead of git push.
|
|
@@ -54,11 +65,15 @@ git-multi-commit:
|
|
|
54
65
|
origin, or straight to upstream when there is no origin) and opens (and
|
|
55
66
|
with -ua merges) the pull request. Without "upstream" it warns and skips
|
|
56
67
|
that part. -b only applies when the pull request step runs.
|
|
68
|
+
With -bpa, every repo in the config gets its own branch, pull request into
|
|
69
|
+
its default branch, merge and branch delete. -b <name> works here too.
|
|
57
70
|
|
|
58
71
|
Examples:
|
|
59
72
|
acp ${dim('# push to origin/main')}
|
|
60
73
|
acp -u ${dim('# push to origin/main, open a PR to upstream')}
|
|
61
74
|
acp -ua ${dim('# same, then merge the PR if you can')}
|
|
75
|
+
acp -bpa ${dim('# push a new branch, PR into main, merge, delete the branch')}
|
|
76
|
+
acp -bopa ${dim('# same as -bpa')}
|
|
62
77
|
acp -b dev ${dim('# push to origin/dev')}
|
|
63
78
|
acp -u --branch fix ${dim('# push to origin/fix, open a PR to upstream')}
|
|
64
79
|
acp -m "Fix login bug" ${dim('# custom message, push to origin/main')}
|
|
@@ -74,7 +89,7 @@ function fail(message) {
|
|
|
74
89
|
|
|
75
90
|
// ---------- argument parsing ----------
|
|
76
91
|
function parseArgs(argv) {
|
|
77
|
-
const options = { branch: 'main', branchGiven: false, message: null, pr: false, merge: false, chunks: false };
|
|
92
|
+
const options = { branch: 'main', branchGiven: false, message: null, pr: false, merge: false, chunks: false, bpa: false };
|
|
78
93
|
|
|
79
94
|
for (let i = 0; i < argv.length; i++) {
|
|
80
95
|
let flag = argv[i];
|
|
@@ -108,6 +123,14 @@ function parseArgs(argv) {
|
|
|
108
123
|
options.pr = true;
|
|
109
124
|
options.merge = true;
|
|
110
125
|
break;
|
|
126
|
+
case '-bpa':
|
|
127
|
+
case '--branch-pr-auto':
|
|
128
|
+
case '-bopa':
|
|
129
|
+
case '--branch-origin-pr-auto':
|
|
130
|
+
options.bpa = true;
|
|
131
|
+
options.pr = true;
|
|
132
|
+
options.merge = true;
|
|
133
|
+
break;
|
|
111
134
|
case '-b':
|
|
112
135
|
case '--branch':
|
|
113
136
|
options.branch = takeValue();
|
|
@@ -373,10 +396,12 @@ function githubAuth(remoteUrl, writeRepo) {
|
|
|
373
396
|
// upstream repo ("owner/name") plus the owners of upstream and the fork.
|
|
374
397
|
// With no originUrl there is no fork: the branch goes straight to upstream
|
|
375
398
|
// (`direct`), so the pull request's head and base are the same repo.
|
|
376
|
-
|
|
377
|
-
|
|
399
|
+
// `baseRemote` is the remote the pull request goes into ("upstream", or
|
|
400
|
+
// "origin" for -bpa, where the branch and the pull request live in one repo).
|
|
401
|
+
function pullRequestTarget(originUrl, baseRemote = 'upstream') {
|
|
402
|
+
const upstreamUrl = git(['remote', 'get-url', baseRemote], { quiet: true });
|
|
378
403
|
if (upstreamUrl.status !== 0) {
|
|
379
|
-
fail(
|
|
404
|
+
fail(`Remote "${baseRemote}" does not exist. Add it with: git remote add ${baseRemote} <url>`);
|
|
380
405
|
}
|
|
381
406
|
|
|
382
407
|
const head = originUrl ? githubRepo(originUrl) : null;
|
|
@@ -385,7 +410,7 @@ function pullRequestTarget(originUrl) {
|
|
|
385
410
|
fail(
|
|
386
411
|
originUrl
|
|
387
412
|
? 'Pull requests need "origin" and "upstream" to both be GitHub repos.'
|
|
388
|
-
:
|
|
413
|
+
: `Pull requests need "${baseRemote}" to be a GitHub repo.`
|
|
389
414
|
);
|
|
390
415
|
}
|
|
391
416
|
|
|
@@ -404,18 +429,24 @@ function pullRequestTarget(originUrl) {
|
|
|
404
429
|
// The branch to push to upstream when there is no fork: -b if given, else the
|
|
405
430
|
// branch you are on, else a new acp-<date> one (a pull request can't go from
|
|
406
431
|
// upstream's default branch into itself).
|
|
407
|
-
|
|
408
|
-
|
|
432
|
+
// With `fresh` (-bpa) it is always a new acp-<date> branch unless -b names one,
|
|
433
|
+
// because the branch is deleted after the merge.
|
|
434
|
+
// Also stores the default branch as target.base.
|
|
435
|
+
function directBranch(target, requested, fresh = false) {
|
|
436
|
+
const result = run('gh', ['api', `repos/${target.repo}`, '--jq', '.default_branch'], { quiet: true, env: target.env });
|
|
409
437
|
const base = result.status === 0 ? result.stdout.trim() : '';
|
|
410
438
|
if (!base) fail(`Could not read the default branch of ${target.repo} with gh. Is gh logged in?`);
|
|
439
|
+
target.base = base;
|
|
411
440
|
|
|
412
441
|
if (requested) {
|
|
413
442
|
if (requested === base) fail(`-b ${requested} is the default branch of ${target.repo}, a pull request needs another one.`);
|
|
414
443
|
return requested;
|
|
415
444
|
}
|
|
416
445
|
|
|
417
|
-
|
|
418
|
-
|
|
446
|
+
if (!fresh) {
|
|
447
|
+
const current = git(['rev-parse', '--abbrev-ref', 'HEAD'], { quiet: true }).stdout.trim();
|
|
448
|
+
if (current && current !== 'HEAD' && current !== base) return current;
|
|
449
|
+
}
|
|
419
450
|
|
|
420
451
|
const stamp = new Date().toISOString().replace(/\D/g, '').slice(0, 14);
|
|
421
452
|
return `acp-${stamp}`;
|
|
@@ -471,7 +502,7 @@ function pullRequest(target, branch, merge) {
|
|
|
471
502
|
} else {
|
|
472
503
|
console.log(yellow(`• Pull request already open, updated by this push: ${url}`));
|
|
473
504
|
}
|
|
474
|
-
if (!merge) return;
|
|
505
|
+
if (!merge) return false;
|
|
475
506
|
|
|
476
507
|
// Only merge with write access to upstream (write, maintain or admin).
|
|
477
508
|
// The fork's owner often doesn't have it, so the gh account that owns
|
|
@@ -487,7 +518,7 @@ function pullRequest(target, branch, merge) {
|
|
|
487
518
|
}
|
|
488
519
|
if (!allowed) {
|
|
489
520
|
console.log(yellow(`• No write access to ${target.repo}, so the pull request stays open for a maintainer to merge.`));
|
|
490
|
-
return;
|
|
521
|
+
return false;
|
|
491
522
|
}
|
|
492
523
|
if (merger && merger !== opener) {
|
|
493
524
|
console.log(cyan(`• Merging with GitHub account "${merger}" (owner of upstream)`));
|
|
@@ -507,6 +538,35 @@ function pullRequest(target, branch, merge) {
|
|
|
507
538
|
fail(`"gh pr merge" failed (exit code ${merged.status}).`);
|
|
508
539
|
}
|
|
509
540
|
console.log(green(`✔ Merged into ${target.repo}`));
|
|
541
|
+
return true;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// -bpa, after the merge: deletes the pull request's branch on the remote and,
|
|
545
|
+
// when you are on the default branch, fast-forwards it to the merged result.
|
|
546
|
+
// The pull request is already merged, so problems here only warn.
|
|
547
|
+
function cleanUp(remote, branch, base, auth) {
|
|
548
|
+
const extraArgs = auth.extraArgs || [];
|
|
549
|
+
const quietly = (args) => git([...extraArgs, ...args], { env: auth.env, stdio: ['inherit', 'inherit', 'pipe'] });
|
|
550
|
+
|
|
551
|
+
console.log(cyan(`› git push ${remote} --delete ${branch}`));
|
|
552
|
+
const deleted = quietly(['push', remote, '--delete', branch]);
|
|
553
|
+
if (deleted.status === 0) {
|
|
554
|
+
console.log(green(`✔ Deleted ${remote}/${branch}`));
|
|
555
|
+
} else if (/remote ref does not exist/i.test(deleted.stderr || '')) {
|
|
556
|
+
console.log(dim(`• ${remote}/${branch} was already deleted.`));
|
|
557
|
+
} else {
|
|
558
|
+
if (deleted.stderr) process.stderr.write(deleted.stderr);
|
|
559
|
+
console.log(yellow(`⚠ Could not delete ${remote}/${branch}, delete it yourself.`));
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
const current = git(['rev-parse', '--abbrev-ref', 'HEAD'], { quiet: true }).stdout.trim();
|
|
563
|
+
if (current !== base) return;
|
|
564
|
+
|
|
565
|
+
console.log(cyan(`› git pull --ff-only ${remote} ${base}`));
|
|
566
|
+
const pulled = quietly(['pull', '--ff-only', remote, base]);
|
|
567
|
+
if (pulled.status === 0) return;
|
|
568
|
+
if (pulled.stderr) process.stderr.write(pulled.stderr);
|
|
569
|
+
console.log(yellow(`⚠ Could not update local ${base}, run: git pull ${remote} ${base}`));
|
|
510
570
|
}
|
|
511
571
|
|
|
512
572
|
// ---------- git-multi-commit ----------
|
|
@@ -515,28 +575,190 @@ function pullRequest(target, branch, merge) {
|
|
|
515
575
|
// git-multi-commit instead of git push.
|
|
516
576
|
const MULTI_COMMIT_CONFIG = '.git-multi-commit.json';
|
|
517
577
|
|
|
518
|
-
|
|
578
|
+
const MULTI_COMMIT_NOT_INSTALLED = 'git-multi-commit is not installed. Install it with: npm install -g git-multi-commit';
|
|
579
|
+
const isWindows = process.platform === 'win32';
|
|
580
|
+
|
|
581
|
+
function multiCommitConfigPath() {
|
|
519
582
|
const root = git(['rev-parse', '--show-toplevel'], { quiet: true }).stdout.trim();
|
|
520
|
-
return
|
|
583
|
+
return path.join(root, MULTI_COMMIT_CONFIG);
|
|
521
584
|
}
|
|
522
585
|
|
|
523
|
-
function
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
if (isWindows && run('where', ['git-multi-commit'], { quiet: true }).status !== 0) fail(
|
|
586
|
+
function usesMultiCommit() {
|
|
587
|
+
return fs.existsSync(multiCommitConfigPath());
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
// cmd.exe's "not found" exit code isn't reliable, so look it up first.
|
|
591
|
+
function requireMultiCommit() {
|
|
592
|
+
if (isWindows && run('where', ['git-multi-commit'], { quiet: true }).status !== 0) fail(MULTI_COMMIT_NOT_INSTALLED);
|
|
593
|
+
}
|
|
530
594
|
|
|
595
|
+
// npm installs global CLIs as .cmd shims on Windows, which only start
|
|
596
|
+
// through a shell. No arguments are passed, so no user input reaches it.
|
|
597
|
+
function runMultiCommit() {
|
|
531
598
|
console.log(cyan('› git-multi-commit'));
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
599
|
+
return run('git-multi-commit', [], { shell: isWindows });
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// What went wrong with a git-multi-commit run, or null if nothing did.
|
|
603
|
+
function multiCommitProblem(result) {
|
|
604
|
+
if (result.error) return result.error.code === 'ENOENT' ? MULTI_COMMIT_NOT_INSTALLED : result.error.message;
|
|
605
|
+
return result.status === 0 ? null : `"git-multi-commit" failed (exit code ${result.status}).`;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function multiCommit() {
|
|
609
|
+
requireMultiCommit();
|
|
610
|
+
const problem = multiCommitProblem(runMultiCommit());
|
|
611
|
+
if (problem) fail(problem);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// ---------- -bpa with git-multi-commit ----------
|
|
615
|
+
// Each pair is its own repo with its own history, so every one gets its own
|
|
616
|
+
// branch, pull request and merge. Checks everything before anything is committed.
|
|
617
|
+
function pairTargets(requested) {
|
|
618
|
+
let config;
|
|
619
|
+
try {
|
|
620
|
+
config = JSON.parse(fs.readFileSync(multiCommitConfigPath(), 'utf8'));
|
|
621
|
+
} catch (error) {
|
|
622
|
+
fail(`Could not read ${MULTI_COMMIT_CONFIG}: ${error.message}`);
|
|
623
|
+
}
|
|
624
|
+
const pairs = (config && config.pairs) || [];
|
|
625
|
+
if (!pairs.length) fail(`${MULTI_COMMIT_CONFIG} has no pairs. Set them up with: git-multi-commit --config`);
|
|
626
|
+
|
|
627
|
+
const gh = ghAccounts();
|
|
628
|
+
if (!gh) fail('-bpa needs the GitHub CLI (gh), logged in. Install it from https://cli.github.com');
|
|
629
|
+
|
|
630
|
+
let branch = requested;
|
|
631
|
+
const targets = pairs.map((pair) => {
|
|
632
|
+
const repo = githubRepo(pair.repoUrl || '');
|
|
633
|
+
if (!repo) fail(`Pull requests need every pair to be a GitHub repo, "${pair.repoUrl}" is not.`);
|
|
634
|
+
|
|
635
|
+
const account = String(pair.account).toLowerCase();
|
|
636
|
+
const login = gh.accounts.find((a) => a.toLowerCase() === account);
|
|
637
|
+
const token = login && ghToken(login, login === gh.active);
|
|
638
|
+
if (!token) fail(`GitHub account "${pair.account}" is not logged in. Log in with: gh auth login`);
|
|
639
|
+
|
|
640
|
+
const target = {
|
|
641
|
+
repo: `${repo.owner}/${repo.name}`,
|
|
642
|
+
baseOwner: repo.owner,
|
|
643
|
+
headOwner: repo.owner,
|
|
644
|
+
direct: true,
|
|
645
|
+
opener: login,
|
|
646
|
+
env: { GH_TOKEN: token },
|
|
647
|
+
};
|
|
648
|
+
branch = directBranch(target, branch, true);
|
|
649
|
+
if (!canPush(target.repo, target.env)) fail(`GitHub account "${login}" can't push to ${target.repo}.`);
|
|
650
|
+
|
|
651
|
+
const tip = run('gh', ['api', `repos/${target.repo}/git/ref/heads/${target.base}`, '--jq', '.object.sha'], {
|
|
652
|
+
quiet: true,
|
|
653
|
+
env: target.env,
|
|
654
|
+
});
|
|
655
|
+
if (tip.status !== 0) fail(`Could not read ${target.base} of ${target.repo}. A pull request needs a repo with commits.`);
|
|
656
|
+
target.baseSha = tip.stdout.trim();
|
|
657
|
+
return target;
|
|
658
|
+
});
|
|
659
|
+
return { targets, branch };
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// Creates <branch> from the default branch. Returns false when it failed.
|
|
663
|
+
// A branch that already exists is used as it is, and is not ours to delete.
|
|
664
|
+
function createPairBranch(target, branch) {
|
|
665
|
+
const created = run(
|
|
666
|
+
'gh',
|
|
667
|
+
['api', '-X', 'POST', `repos/${target.repo}/git/refs`, '-f', `ref=refs/heads/${branch}`, '-f', `sha=${target.baseSha}`],
|
|
668
|
+
{ quiet: true, env: target.env }
|
|
669
|
+
);
|
|
670
|
+
target.createdBranch = created.status === 0;
|
|
671
|
+
if (created.status === 0 || /already exists/i.test(created.stderr || '')) return true;
|
|
672
|
+
if (created.stderr) process.stderr.write(created.stderr);
|
|
673
|
+
return false;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
function deletePairBranch(target, branch) {
|
|
677
|
+
console.log(cyan(`› gh api -X DELETE repos/${target.repo}/git/refs/heads/${branch}`));
|
|
678
|
+
const deleted = run('gh', ['api', '-X', 'DELETE', `repos/${target.repo}/git/refs/heads/${branch}`], {
|
|
679
|
+
quiet: true,
|
|
680
|
+
env: target.env,
|
|
681
|
+
});
|
|
682
|
+
if (deleted.status === 0) {
|
|
683
|
+
console.log(green(`✔ Deleted ${target.repo}:${branch}`));
|
|
684
|
+
} else if (/reference does not exist/i.test(deleted.stderr || '')) {
|
|
685
|
+
console.log(dim(`• ${target.repo}:${branch} was already deleted.`));
|
|
686
|
+
} else {
|
|
687
|
+
if (deleted.stderr) process.stderr.write(deleted.stderr);
|
|
688
|
+
console.log(yellow(`⚠ Could not delete ${target.repo}:${branch}, delete it yourself.`));
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
// How many commits <branch> is ahead of the default branch, or null if unknown.
|
|
693
|
+
function commitsAhead(target, branch) {
|
|
694
|
+
const compared = run('gh', ['api', `repos/${target.repo}/compare/${target.base}...${branch}`, '--jq', '.ahead_by'], {
|
|
695
|
+
quiet: true,
|
|
696
|
+
env: target.env,
|
|
697
|
+
});
|
|
698
|
+
return compared.status === 0 ? Number(compared.stdout.trim()) : null;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// git-multi-commit has no branch option and only reads the branch from its
|
|
702
|
+
// config, so the config points at <branch> just for this one run. The branch
|
|
703
|
+
// has to exist first: on a missing branch git-multi-commit starts from nothing,
|
|
704
|
+
// and a pull request can't be opened between unrelated histories.
|
|
705
|
+
function runMultiCommitOnBranch(branch) {
|
|
706
|
+
const file = multiCommitConfigPath();
|
|
707
|
+
const original = fs.readFileSync(file, 'utf8');
|
|
708
|
+
const config = JSON.parse(original);
|
|
709
|
+
config.branch = branch;
|
|
710
|
+
config.pairs.forEach((pair) => {
|
|
711
|
+
pair.branch = branch;
|
|
712
|
+
});
|
|
713
|
+
|
|
714
|
+
// Ctrl+C must stop git-multi-commit but not acp, or the config stays changed.
|
|
715
|
+
const ignore = () => {};
|
|
716
|
+
process.on('SIGINT', ignore);
|
|
717
|
+
fs.writeFileSync(file, `${JSON.stringify(config, null, 2)}\n`);
|
|
718
|
+
try {
|
|
719
|
+
return runMultiCommit();
|
|
720
|
+
} finally {
|
|
721
|
+
fs.writeFileSync(file, original);
|
|
722
|
+
process.removeListener('SIGINT', ignore);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
function branchPrAutoPairs(targets, branch) {
|
|
727
|
+
for (const target of targets) {
|
|
728
|
+
if (createPairBranch(target, branch)) continue;
|
|
729
|
+
targets.filter((t) => t.createdBranch).forEach((t) => deletePairBranch(t, branch));
|
|
730
|
+
fail(`Could not create branch "${branch}" on ${target.repo}.`);
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
const result = runMultiCommitOnBranch(branch);
|
|
734
|
+
|
|
735
|
+
// 0xC000013A is how Windows reports a process stopped with Ctrl+C.
|
|
736
|
+
if (result.signal === 'SIGINT' || result.status === 130 || result.status === 0xc000013a) {
|
|
737
|
+
fail(`Interrupted, nothing was merged. Branch "${branch}" may be left on the repos.`);
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
// A pair with nothing new, or one that failed, has an empty branch. A failed
|
|
741
|
+
// pair doesn't stop the others, so the ones that were pushed still go through.
|
|
742
|
+
for (const target of targets) {
|
|
743
|
+
console.log(cyan(`• ${target.repo}`));
|
|
744
|
+
const ahead = commitsAhead(target, branch);
|
|
745
|
+
if (ahead === null) {
|
|
746
|
+
console.log(yellow(`⚠ Could not compare "${branch}" with ${target.base}, leaving it for you to check.`));
|
|
747
|
+
} else if (ahead === 0) {
|
|
748
|
+
console.log(dim('• No new commits, skipping the pull request.'));
|
|
749
|
+
if (target.createdBranch) deletePairBranch(target, branch);
|
|
750
|
+
} else if (pullRequest(target, branch, true)) {
|
|
751
|
+
deletePairBranch(target, branch);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
const problem = multiCommitProblem(result);
|
|
756
|
+
if (problem) fail(problem);
|
|
535
757
|
}
|
|
536
758
|
|
|
537
759
|
// ---------- main ----------
|
|
538
760
|
function main() {
|
|
539
|
-
const { branch: requestedBranch, branchGiven, message, pr, merge, chunks } = parseArgs(process.argv.slice(2));
|
|
761
|
+
const { branch: requestedBranch, branchGiven, message, pr, merge, chunks, bpa } = parseArgs(process.argv.slice(2));
|
|
540
762
|
let branch = requestedBranch;
|
|
541
763
|
let remote = 'origin';
|
|
542
764
|
|
|
@@ -548,9 +770,14 @@ function main() {
|
|
|
548
770
|
const multi = usesMultiCommit();
|
|
549
771
|
let remoteUrl;
|
|
550
772
|
let target;
|
|
773
|
+
let pairs;
|
|
551
774
|
if (multi) {
|
|
552
775
|
console.log(cyan(`• Found ${MULTI_COMMIT_CONFIG}, pushing with git-multi-commit instead of git push.`));
|
|
553
|
-
if (
|
|
776
|
+
if (bpa) {
|
|
777
|
+
requireMultiCommit();
|
|
778
|
+
({ targets: pairs, branch } = pairTargets(branchGiven ? requestedBranch : null));
|
|
779
|
+
console.log(cyan(`• Using branch "${branch}" on ${pairs.length} repo(s)`));
|
|
780
|
+
} else if (pr) {
|
|
554
781
|
// git-multi-commit pushes to its own repos, so -u/-ua only apply when
|
|
555
782
|
// "upstream" exists. Without "origin" the branch goes straight to it.
|
|
556
783
|
const hasOrigin = remoteUrlOf('origin');
|
|
@@ -564,11 +791,11 @@ function main() {
|
|
|
564
791
|
console.log(yellow('⚠ Remote "upstream" does not exist, skipping -u/-ua (git-multi-commit only).'));
|
|
565
792
|
}
|
|
566
793
|
}
|
|
567
|
-
if (!target && branch !== 'main') console.log(yellow('⚠ -b is ignored for git-multi-commit.'));
|
|
794
|
+
if (!target && !bpa && branch !== 'main') console.log(yellow('⚠ -b is ignored for git-multi-commit.'));
|
|
568
795
|
} else {
|
|
569
796
|
remoteUrl = remoteUrlOf('origin');
|
|
570
797
|
if (remoteUrl.status !== 0) {
|
|
571
|
-
const upstream = pr ? remoteUrlOf('upstream') : null;
|
|
798
|
+
const upstream = pr && !bpa ? remoteUrlOf('upstream') : null;
|
|
572
799
|
if (!upstream || upstream.status !== 0) {
|
|
573
800
|
fail(`Remote "${remote}" does not exist. Add it with: git remote add ${remote} <url>`);
|
|
574
801
|
}
|
|
@@ -576,12 +803,18 @@ function main() {
|
|
|
576
803
|
remote = 'upstream';
|
|
577
804
|
remoteUrl = upstream;
|
|
578
805
|
target = pullRequestTarget(null);
|
|
806
|
+
} else if (bpa) {
|
|
807
|
+
// One repo: the branch and the pull request both live on origin.
|
|
808
|
+
target = pullRequestTarget(null, 'origin');
|
|
579
809
|
} else if (pr) {
|
|
580
810
|
target = pullRequestTarget(remoteUrl.stdout.trim());
|
|
581
811
|
}
|
|
582
812
|
}
|
|
583
813
|
|
|
584
|
-
if (target && target.direct)
|
|
814
|
+
if (target && target.direct) {
|
|
815
|
+
branch = directBranch(target, branchGiven ? requestedBranch : null, bpa);
|
|
816
|
+
if (bpa) console.log(cyan(`• Using branch "${branch}"`));
|
|
817
|
+
}
|
|
585
818
|
|
|
586
819
|
if (multi && chunks) console.log(yellow('⚠ -c is ignored for git-multi-commit.'));
|
|
587
820
|
|
|
@@ -596,6 +829,11 @@ function main() {
|
|
|
596
829
|
console.log(green(`✔ Pushed to ${remote}/${branch}`));
|
|
597
830
|
};
|
|
598
831
|
|
|
832
|
+
const openPullRequest = () => {
|
|
833
|
+
const merged = pullRequest(target, branch, merge);
|
|
834
|
+
if (bpa && merged) cleanUp(remote, branch, target.base, auth);
|
|
835
|
+
};
|
|
836
|
+
|
|
599
837
|
// With -c and -u/-ua, every chunk is pushed and gets its own pull request
|
|
600
838
|
// (merged right away with -ua), instead of one push at the end.
|
|
601
839
|
let pushedPerChunk = false;
|
|
@@ -605,7 +843,7 @@ function main() {
|
|
|
605
843
|
? (n, count) => {
|
|
606
844
|
console.log(cyan(`• Chunk ${n}/${count}`));
|
|
607
845
|
push();
|
|
608
|
-
|
|
846
|
+
openPullRequest();
|
|
609
847
|
}
|
|
610
848
|
: undefined;
|
|
611
849
|
const done = commitInChunks(message, afterCommit);
|
|
@@ -622,12 +860,17 @@ function main() {
|
|
|
622
860
|
}
|
|
623
861
|
}
|
|
624
862
|
|
|
863
|
+
if (pairs) {
|
|
864
|
+
branchPrAutoPairs(pairs, branch);
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
|
|
625
868
|
if (multi) {
|
|
626
869
|
multiCommit();
|
|
627
870
|
console.log(green('✔ Pushed with git-multi-commit'));
|
|
628
871
|
if (target) {
|
|
629
872
|
push();
|
|
630
|
-
|
|
873
|
+
openPullRequest();
|
|
631
874
|
}
|
|
632
875
|
return;
|
|
633
876
|
}
|
|
@@ -635,7 +878,7 @@ function main() {
|
|
|
635
878
|
if (pushedPerChunk) return;
|
|
636
879
|
|
|
637
880
|
push();
|
|
638
|
-
if (pr)
|
|
881
|
+
if (pr) openPullRequest();
|
|
639
882
|
}
|
|
640
883
|
|
|
641
884
|
main();
|