acp-push 2.2.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 +20 -1
- package/bin/acp.js +195 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -102,7 +102,7 @@ git pull --ff-only origin <default branch> # only if you are on the default br
|
|
|
102
102
|
- The pull request goes into the default branch of `origin`. Its title is your last commit message.
|
|
103
103
|
- If your account can't merge (no write access), the pull request stays open and the branch is **not** deleted.
|
|
104
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).
|
|
105
|
+
- Works with `-c` (each chunk gets its own branch round trip). With git-multi-commit projects, see [below](#git-multi-commit-projects).
|
|
106
106
|
|
|
107
107
|
## Multiple GitHub accounts
|
|
108
108
|
|
|
@@ -135,6 +135,25 @@ git-multi-commit
|
|
|
135
135
|
|
|
136
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`.
|
|
137
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
|
+
|
|
138
157
|
## Notes
|
|
139
158
|
|
|
140
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
|
@@ -28,6 +28,8 @@ Options:
|
|
|
28
28
|
-bpa, --branch-pr-auto Push to a new branch on origin, open a pull request into
|
|
29
29
|
the default branch, merge it, then delete the branch
|
|
30
30
|
(-b <name> picks the branch name)
|
|
31
|
+
-bopa, --branch-origin-pr-auto
|
|
32
|
+
Same as -bpa
|
|
31
33
|
-b, --branch <name> Push to <name> instead of "main"
|
|
32
34
|
-m, --message <text> Use your own commit message instead of a random one
|
|
33
35
|
-c, --chunks Split your changes into 10 smaller commits. With -u or
|
|
@@ -50,7 +52,7 @@ Pull requests:
|
|
|
50
52
|
and opens the pull request from there. The branch is your current one,
|
|
51
53
|
-b <name>, or a new acp-<date> branch when you are on upstream's default.
|
|
52
54
|
|
|
53
|
-
One repo (-bpa):
|
|
55
|
+
One repo (-bpa, -bopa):
|
|
54
56
|
Needs the GitHub CLI (gh) and an "origin" remote on GitHub. The branch is
|
|
55
57
|
named acp-<date> unless you give -b <name>. The merge needs write access to
|
|
56
58
|
origin; without it the pull request stays open and the branch is kept.
|
|
@@ -63,12 +65,15 @@ git-multi-commit:
|
|
|
63
65
|
origin, or straight to upstream when there is no origin) and opens (and
|
|
64
66
|
with -ua merges) the pull request. Without "upstream" it warns and skips
|
|
65
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.
|
|
66
70
|
|
|
67
71
|
Examples:
|
|
68
72
|
acp ${dim('# push to origin/main')}
|
|
69
73
|
acp -u ${dim('# push to origin/main, open a PR to upstream')}
|
|
70
74
|
acp -ua ${dim('# same, then merge the PR if you can')}
|
|
71
75
|
acp -bpa ${dim('# push a new branch, PR into main, merge, delete the branch')}
|
|
76
|
+
acp -bopa ${dim('# same as -bpa')}
|
|
72
77
|
acp -b dev ${dim('# push to origin/dev')}
|
|
73
78
|
acp -u --branch fix ${dim('# push to origin/fix, open a PR to upstream')}
|
|
74
79
|
acp -m "Fix login bug" ${dim('# custom message, push to origin/main')}
|
|
@@ -120,6 +125,8 @@ function parseArgs(argv) {
|
|
|
120
125
|
break;
|
|
121
126
|
case '-bpa':
|
|
122
127
|
case '--branch-pr-auto':
|
|
128
|
+
case '-bopa':
|
|
129
|
+
case '--branch-origin-pr-auto':
|
|
123
130
|
options.bpa = true;
|
|
124
131
|
options.pr = true;
|
|
125
132
|
options.merge = true;
|
|
@@ -426,7 +433,7 @@ function pullRequestTarget(originUrl, baseRemote = 'upstream') {
|
|
|
426
433
|
// because the branch is deleted after the merge.
|
|
427
434
|
// Also stores the default branch as target.base.
|
|
428
435
|
function directBranch(target, requested, fresh = false) {
|
|
429
|
-
const result = run('gh', ['api', `repos/${target.repo}`, '--jq', '.default_branch'], { quiet: true });
|
|
436
|
+
const result = run('gh', ['api', `repos/${target.repo}`, '--jq', '.default_branch'], { quiet: true, env: target.env });
|
|
430
437
|
const base = result.status === 0 ? result.stdout.trim() : '';
|
|
431
438
|
if (!base) fail(`Could not read the default branch of ${target.repo} with gh. Is gh logged in?`);
|
|
432
439
|
target.base = base;
|
|
@@ -568,23 +575,185 @@ function cleanUp(remote, branch, base, auth) {
|
|
|
568
575
|
// git-multi-commit instead of git push.
|
|
569
576
|
const MULTI_COMMIT_CONFIG = '.git-multi-commit.json';
|
|
570
577
|
|
|
571
|
-
|
|
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() {
|
|
572
582
|
const root = git(['rev-parse', '--show-toplevel'], { quiet: true }).stdout.trim();
|
|
573
|
-
return
|
|
583
|
+
return path.join(root, MULTI_COMMIT_CONFIG);
|
|
574
584
|
}
|
|
575
585
|
|
|
576
|
-
function
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
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
|
+
}
|
|
583
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() {
|
|
584
598
|
console.log(cyan('› git-multi-commit'));
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
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);
|
|
588
757
|
}
|
|
589
758
|
|
|
590
759
|
// ---------- main ----------
|
|
@@ -601,10 +770,14 @@ function main() {
|
|
|
601
770
|
const multi = usesMultiCommit();
|
|
602
771
|
let remoteUrl;
|
|
603
772
|
let target;
|
|
773
|
+
let pairs;
|
|
604
774
|
if (multi) {
|
|
605
|
-
if (bpa) fail('-bpa does not work with git-multi-commit.');
|
|
606
775
|
console.log(cyan(`• Found ${MULTI_COMMIT_CONFIG}, pushing with git-multi-commit instead of git push.`));
|
|
607
|
-
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) {
|
|
608
781
|
// git-multi-commit pushes to its own repos, so -u/-ua only apply when
|
|
609
782
|
// "upstream" exists. Without "origin" the branch goes straight to it.
|
|
610
783
|
const hasOrigin = remoteUrlOf('origin');
|
|
@@ -618,7 +791,7 @@ function main() {
|
|
|
618
791
|
console.log(yellow('⚠ Remote "upstream" does not exist, skipping -u/-ua (git-multi-commit only).'));
|
|
619
792
|
}
|
|
620
793
|
}
|
|
621
|
-
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.'));
|
|
622
795
|
} else {
|
|
623
796
|
remoteUrl = remoteUrlOf('origin');
|
|
624
797
|
if (remoteUrl.status !== 0) {
|
|
@@ -687,6 +860,11 @@ function main() {
|
|
|
687
860
|
}
|
|
688
861
|
}
|
|
689
862
|
|
|
863
|
+
if (pairs) {
|
|
864
|
+
branchPrAutoPairs(pairs, branch);
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
|
|
690
868
|
if (multi) {
|
|
691
869
|
multiCommit();
|
|
692
870
|
console.log(green('✔ Pushed with git-multi-commit'));
|