acp-push 2.1.0 → 2.2.0

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.
Files changed (3) hide show
  1. package/README.md +23 -0
  2. package/bin/acp.js +81 -16
  3. 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). Not supported with 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:
package/bin/acp.js CHANGED
@@ -25,6 +25,9 @@ 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)
28
31
  -b, --branch <name> Push to <name> instead of "main"
29
32
  -m, --message <text> Use your own commit message instead of a random one
30
33
  -c, --chunks Split your changes into 10 smaller commits. With -u or
@@ -47,6 +50,12 @@ Pull requests:
47
50
  and opens the pull request from there. The branch is your current one,
48
51
  -b <name>, or a new acp-<date> branch when you are on upstream's default.
49
52
 
53
+ One repo (-bpa):
54
+ Needs the GitHub CLI (gh) and an "origin" remote on GitHub. The branch is
55
+ named acp-<date> unless you give -b <name>. The merge needs write access to
56
+ origin; without it the pull request stays open and the branch is kept.
57
+ If you are on the default branch, it is updated after the merge.
58
+
50
59
  git-multi-commit:
51
60
  If the repo root has a .git-multi-commit.json file, acp still adds and
52
61
  commits locally, then runs git-multi-commit instead of git push.
@@ -59,6 +68,7 @@ Examples:
59
68
  acp ${dim('# push to origin/main')}
60
69
  acp -u ${dim('# push to origin/main, open a PR to upstream')}
61
70
  acp -ua ${dim('# same, then merge the PR if you can')}
71
+ acp -bpa ${dim('# push a new branch, PR into main, merge, delete the branch')}
62
72
  acp -b dev ${dim('# push to origin/dev')}
63
73
  acp -u --branch fix ${dim('# push to origin/fix, open a PR to upstream')}
64
74
  acp -m "Fix login bug" ${dim('# custom message, push to origin/main')}
@@ -74,7 +84,7 @@ function fail(message) {
74
84
 
75
85
  // ---------- argument parsing ----------
76
86
  function parseArgs(argv) {
77
- const options = { branch: 'main', branchGiven: false, message: null, pr: false, merge: false, chunks: false };
87
+ const options = { branch: 'main', branchGiven: false, message: null, pr: false, merge: false, chunks: false, bpa: false };
78
88
 
79
89
  for (let i = 0; i < argv.length; i++) {
80
90
  let flag = argv[i];
@@ -108,6 +118,12 @@ function parseArgs(argv) {
108
118
  options.pr = true;
109
119
  options.merge = true;
110
120
  break;
121
+ case '-bpa':
122
+ case '--branch-pr-auto':
123
+ options.bpa = true;
124
+ options.pr = true;
125
+ options.merge = true;
126
+ break;
111
127
  case '-b':
112
128
  case '--branch':
113
129
  options.branch = takeValue();
@@ -373,10 +389,12 @@ function githubAuth(remoteUrl, writeRepo) {
373
389
  // upstream repo ("owner/name") plus the owners of upstream and the fork.
374
390
  // With no originUrl there is no fork: the branch goes straight to upstream
375
391
  // (`direct`), so the pull request's head and base are the same repo.
376
- function pullRequestTarget(originUrl) {
377
- const upstreamUrl = git(['remote', 'get-url', 'upstream'], { quiet: true });
392
+ // `baseRemote` is the remote the pull request goes into ("upstream", or
393
+ // "origin" for -bpa, where the branch and the pull request live in one repo).
394
+ function pullRequestTarget(originUrl, baseRemote = 'upstream') {
395
+ const upstreamUrl = git(['remote', 'get-url', baseRemote], { quiet: true });
378
396
  if (upstreamUrl.status !== 0) {
379
- fail('Remote "upstream" does not exist. Add it with: git remote add upstream <url>');
397
+ fail(`Remote "${baseRemote}" does not exist. Add it with: git remote add ${baseRemote} <url>`);
380
398
  }
381
399
 
382
400
  const head = originUrl ? githubRepo(originUrl) : null;
@@ -385,7 +403,7 @@ function pullRequestTarget(originUrl) {
385
403
  fail(
386
404
  originUrl
387
405
  ? 'Pull requests need "origin" and "upstream" to both be GitHub repos.'
388
- : 'Pull requests need "upstream" to be a GitHub repo.'
406
+ : `Pull requests need "${baseRemote}" to be a GitHub repo.`
389
407
  );
390
408
  }
391
409
 
@@ -404,18 +422,24 @@ function pullRequestTarget(originUrl) {
404
422
  // The branch to push to upstream when there is no fork: -b if given, else the
405
423
  // branch you are on, else a new acp-<date> one (a pull request can't go from
406
424
  // upstream's default branch into itself).
407
- function directBranch(target, requested) {
425
+ // With `fresh` (-bpa) it is always a new acp-<date> branch unless -b names one,
426
+ // because the branch is deleted after the merge.
427
+ // Also stores the default branch as target.base.
428
+ function directBranch(target, requested, fresh = false) {
408
429
  const result = run('gh', ['api', `repos/${target.repo}`, '--jq', '.default_branch'], { quiet: true });
409
430
  const base = result.status === 0 ? result.stdout.trim() : '';
410
431
  if (!base) fail(`Could not read the default branch of ${target.repo} with gh. Is gh logged in?`);
432
+ target.base = base;
411
433
 
412
434
  if (requested) {
413
435
  if (requested === base) fail(`-b ${requested} is the default branch of ${target.repo}, a pull request needs another one.`);
414
436
  return requested;
415
437
  }
416
438
 
417
- const current = git(['rev-parse', '--abbrev-ref', 'HEAD'], { quiet: true }).stdout.trim();
418
- if (current && current !== 'HEAD' && current !== base) return current;
439
+ if (!fresh) {
440
+ const current = git(['rev-parse', '--abbrev-ref', 'HEAD'], { quiet: true }).stdout.trim();
441
+ if (current && current !== 'HEAD' && current !== base) return current;
442
+ }
419
443
 
420
444
  const stamp = new Date().toISOString().replace(/\D/g, '').slice(0, 14);
421
445
  return `acp-${stamp}`;
@@ -471,7 +495,7 @@ function pullRequest(target, branch, merge) {
471
495
  } else {
472
496
  console.log(yellow(`• Pull request already open, updated by this push: ${url}`));
473
497
  }
474
- if (!merge) return;
498
+ if (!merge) return false;
475
499
 
476
500
  // Only merge with write access to upstream (write, maintain or admin).
477
501
  // The fork's owner often doesn't have it, so the gh account that owns
@@ -487,7 +511,7 @@ function pullRequest(target, branch, merge) {
487
511
  }
488
512
  if (!allowed) {
489
513
  console.log(yellow(`• No write access to ${target.repo}, so the pull request stays open for a maintainer to merge.`));
490
- return;
514
+ return false;
491
515
  }
492
516
  if (merger && merger !== opener) {
493
517
  console.log(cyan(`• Merging with GitHub account "${merger}" (owner of upstream)`));
@@ -507,6 +531,35 @@ function pullRequest(target, branch, merge) {
507
531
  fail(`"gh pr merge" failed (exit code ${merged.status}).`);
508
532
  }
509
533
  console.log(green(`✔ Merged into ${target.repo}`));
534
+ return true;
535
+ }
536
+
537
+ // -bpa, after the merge: deletes the pull request's branch on the remote and,
538
+ // when you are on the default branch, fast-forwards it to the merged result.
539
+ // The pull request is already merged, so problems here only warn.
540
+ function cleanUp(remote, branch, base, auth) {
541
+ const extraArgs = auth.extraArgs || [];
542
+ const quietly = (args) => git([...extraArgs, ...args], { env: auth.env, stdio: ['inherit', 'inherit', 'pipe'] });
543
+
544
+ console.log(cyan(`› git push ${remote} --delete ${branch}`));
545
+ const deleted = quietly(['push', remote, '--delete', branch]);
546
+ if (deleted.status === 0) {
547
+ console.log(green(`✔ Deleted ${remote}/${branch}`));
548
+ } else if (/remote ref does not exist/i.test(deleted.stderr || '')) {
549
+ console.log(dim(`• ${remote}/${branch} was already deleted.`));
550
+ } else {
551
+ if (deleted.stderr) process.stderr.write(deleted.stderr);
552
+ console.log(yellow(`⚠ Could not delete ${remote}/${branch}, delete it yourself.`));
553
+ }
554
+
555
+ const current = git(['rev-parse', '--abbrev-ref', 'HEAD'], { quiet: true }).stdout.trim();
556
+ if (current !== base) return;
557
+
558
+ console.log(cyan(`› git pull --ff-only ${remote} ${base}`));
559
+ const pulled = quietly(['pull', '--ff-only', remote, base]);
560
+ if (pulled.status === 0) return;
561
+ if (pulled.stderr) process.stderr.write(pulled.stderr);
562
+ console.log(yellow(`⚠ Could not update local ${base}, run: git pull ${remote} ${base}`));
510
563
  }
511
564
 
512
565
  // ---------- git-multi-commit ----------
@@ -536,7 +589,7 @@ function multiCommit() {
536
589
 
537
590
  // ---------- main ----------
538
591
  function main() {
539
- const { branch: requestedBranch, branchGiven, message, pr, merge, chunks } = parseArgs(process.argv.slice(2));
592
+ const { branch: requestedBranch, branchGiven, message, pr, merge, chunks, bpa } = parseArgs(process.argv.slice(2));
540
593
  let branch = requestedBranch;
541
594
  let remote = 'origin';
542
595
 
@@ -549,6 +602,7 @@ function main() {
549
602
  let remoteUrl;
550
603
  let target;
551
604
  if (multi) {
605
+ if (bpa) fail('-bpa does not work with git-multi-commit.');
552
606
  console.log(cyan(`• Found ${MULTI_COMMIT_CONFIG}, pushing with git-multi-commit instead of git push.`));
553
607
  if (pr) {
554
608
  // git-multi-commit pushes to its own repos, so -u/-ua only apply when
@@ -568,7 +622,7 @@ function main() {
568
622
  } else {
569
623
  remoteUrl = remoteUrlOf('origin');
570
624
  if (remoteUrl.status !== 0) {
571
- const upstream = pr ? remoteUrlOf('upstream') : null;
625
+ const upstream = pr && !bpa ? remoteUrlOf('upstream') : null;
572
626
  if (!upstream || upstream.status !== 0) {
573
627
  fail(`Remote "${remote}" does not exist. Add it with: git remote add ${remote} <url>`);
574
628
  }
@@ -576,12 +630,18 @@ function main() {
576
630
  remote = 'upstream';
577
631
  remoteUrl = upstream;
578
632
  target = pullRequestTarget(null);
633
+ } else if (bpa) {
634
+ // One repo: the branch and the pull request both live on origin.
635
+ target = pullRequestTarget(null, 'origin');
579
636
  } else if (pr) {
580
637
  target = pullRequestTarget(remoteUrl.stdout.trim());
581
638
  }
582
639
  }
583
640
 
584
- if (target && target.direct) branch = directBranch(target, branchGiven ? requestedBranch : null);
641
+ if (target && target.direct) {
642
+ branch = directBranch(target, branchGiven ? requestedBranch : null, bpa);
643
+ if (bpa) console.log(cyan(`• Using branch "${branch}"`));
644
+ }
585
645
 
586
646
  if (multi && chunks) console.log(yellow('⚠ -c is ignored for git-multi-commit.'));
587
647
 
@@ -596,6 +656,11 @@ function main() {
596
656
  console.log(green(`✔ Pushed to ${remote}/${branch}`));
597
657
  };
598
658
 
659
+ const openPullRequest = () => {
660
+ const merged = pullRequest(target, branch, merge);
661
+ if (bpa && merged) cleanUp(remote, branch, target.base, auth);
662
+ };
663
+
599
664
  // With -c and -u/-ua, every chunk is pushed and gets its own pull request
600
665
  // (merged right away with -ua), instead of one push at the end.
601
666
  let pushedPerChunk = false;
@@ -605,7 +670,7 @@ function main() {
605
670
  ? (n, count) => {
606
671
  console.log(cyan(`• Chunk ${n}/${count}`));
607
672
  push();
608
- pullRequest(target, branch, merge);
673
+ openPullRequest();
609
674
  }
610
675
  : undefined;
611
676
  const done = commitInChunks(message, afterCommit);
@@ -627,7 +692,7 @@ function main() {
627
692
  console.log(green('✔ Pushed with git-multi-commit'));
628
693
  if (target) {
629
694
  push();
630
- pullRequest(target, branch, merge);
695
+ openPullRequest();
631
696
  }
632
697
  return;
633
698
  }
@@ -635,7 +700,7 @@ function main() {
635
700
  if (pushedPerChunk) return;
636
701
 
637
702
  push();
638
- if (pr) pullRequest(target, branch, merge);
703
+ if (pr) openPullRequest();
639
704
  }
640
705
 
641
706
  main();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "acp-push",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "git add, commit (with a random message) and push in one command: acp",
5
5
  "bin": {
6
6
  "acp": "bin/acp.js"