acp-push 2.0.0 → 2.1.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.
- package/README.md +16 -2
- package/bin/acp.js +96 -28
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ npm install -g acp-push
|
|
|
27
27
|
| Flag | Description |
|
|
28
28
|
| --- | --- |
|
|
29
29
|
| `-u`, `--upstream` | Push to `origin`, then open a pull request to `upstream` |
|
|
30
|
-
| `-ua`, `--upstream-merge` | Same as `-u`, then merge the pull request if you have write access to `upstream` |
|
|
30
|
+
| `-ua`, `--upstream-allow` (or `--upstream-merge`) | Same as `-u`, then merge the pull request if you have write access to `upstream` |
|
|
31
31
|
| `-b`, `--branch <name>` | Push to `<name>` instead of `main` |
|
|
32
32
|
| `-m`, `--message <text>` | Use your own commit message instead of a random one |
|
|
33
33
|
| `-c`, `--chunks` | Split your changes into 10 smaller commits (by change, not by file), then push |
|
|
@@ -67,6 +67,20 @@ gh pr create --repo <upstream owner>/<repo> --head <your account>:main
|
|
|
67
67
|
- For the merge, `acp` checks two `gh` accounts for write access: the one named like the owner of `upstream` (if you're logged in to it), then the one that opened the pull request. It merges with the first that has it.
|
|
68
68
|
- If the merge fails, the pull request stays open and `acp` prints its link.
|
|
69
69
|
|
|
70
|
+
### No `origin`, only `upstream`
|
|
71
|
+
|
|
72
|
+
If the repo has an `upstream` remote but no `origin`, `acp -u` / `acp -ua` push a branch straight to `upstream`, open a pull request from it into `upstream`'s default branch, and (with `-ua`) merge it:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
git push upstream HEAD:<branch>
|
|
76
|
+
gh pr create --repo <upstream owner>/<repo> --head <upstream owner>:<branch>
|
|
77
|
+
gh pr merge <pr> --merge
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
- `<branch>` is `-b <name>` if you gave one, otherwise the branch you are on. On `upstream`'s default branch (a pull request can't go from a branch into itself) `acp` creates a new `acp-<date>` branch instead.
|
|
81
|
+
- 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
|
+
- The branch is not deleted after the merge.
|
|
83
|
+
|
|
70
84
|
## Multiple GitHub accounts
|
|
71
85
|
|
|
72
86
|
If you have the [GitHub CLI](https://cli.github.com) (`gh`) installed, `acp` picks the right account for each repo automatically:
|
|
@@ -96,7 +110,7 @@ git commit -m "<one of 25 random messages>"
|
|
|
96
110
|
git-multi-commit
|
|
97
111
|
```
|
|
98
112
|
|
|
99
|
-
No `origin` remote is needed in this case. With `-u` / `-ua`, `acp`
|
|
113
|
+
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`.
|
|
100
114
|
|
|
101
115
|
## Notes
|
|
102
116
|
|
package/bin/acp.js
CHANGED
|
@@ -23,7 +23,8 @@ Usage: acp [options]
|
|
|
23
23
|
|
|
24
24
|
Options:
|
|
25
25
|
-u, --upstream Push to origin, then open a pull request to "upstream"
|
|
26
|
-
-ua, --upstream-
|
|
26
|
+
-ua, --upstream-allow Same as -u, then merge it if you have write access
|
|
27
|
+
(--upstream-merge works too)
|
|
27
28
|
-b, --branch <name> Push to <name> instead of "main"
|
|
28
29
|
-m, --message <text> Use your own commit message instead of a random one
|
|
29
30
|
-c, --chunks Split your changes into 10 smaller commits. With -u or
|
|
@@ -42,14 +43,17 @@ Pull requests:
|
|
|
42
43
|
branch of upstream. If one is already open, the push just updates it.
|
|
43
44
|
-ua only merges if your account can write to upstream (write, maintain
|
|
44
45
|
or admin role). Otherwise the pull request is left open.
|
|
46
|
+
Without an "origin" remote, acp pushes the branch straight to upstream
|
|
47
|
+
and opens the pull request from there. The branch is your current one,
|
|
48
|
+
-b <name>, or a new acp-<date> branch when you are on upstream's default.
|
|
45
49
|
|
|
46
50
|
git-multi-commit:
|
|
47
51
|
If the repo root has a .git-multi-commit.json file, acp still adds and
|
|
48
52
|
commits locally, then runs git-multi-commit instead of git push.
|
|
49
|
-
With -u or -ua
|
|
50
|
-
|
|
51
|
-
merges) the pull request.
|
|
52
|
-
part. -b only applies when the pull request step runs.
|
|
53
|
+
With -u or -ua and an "upstream" remote, acp also pushes <branch> (to
|
|
54
|
+
origin, or straight to upstream when there is no origin) and opens (and
|
|
55
|
+
with -ua merges) the pull request. Without "upstream" it warns and skips
|
|
56
|
+
that part. -b only applies when the pull request step runs.
|
|
53
57
|
|
|
54
58
|
Examples:
|
|
55
59
|
acp ${dim('# push to origin/main')}
|
|
@@ -60,6 +64,7 @@ Examples:
|
|
|
60
64
|
acp -m "Fix login bug" ${dim('# custom message, push to origin/main')}
|
|
61
65
|
acp -c ${dim('# commit in 10 chunks, then push')}
|
|
62
66
|
acp -c -ua ${dim('# each chunk: push, open a PR, merge it')}
|
|
67
|
+
acp -ua ${dim('# only "upstream" set: push a branch there, PR + merge')}
|
|
63
68
|
`;
|
|
64
69
|
|
|
65
70
|
function fail(message) {
|
|
@@ -69,7 +74,7 @@ function fail(message) {
|
|
|
69
74
|
|
|
70
75
|
// ---------- argument parsing ----------
|
|
71
76
|
function parseArgs(argv) {
|
|
72
|
-
const options = { branch: 'main', message: null, pr: false, merge: false, chunks: false };
|
|
77
|
+
const options = { branch: 'main', branchGiven: false, message: null, pr: false, merge: false, chunks: false };
|
|
73
78
|
|
|
74
79
|
for (let i = 0; i < argv.length; i++) {
|
|
75
80
|
let flag = argv[i];
|
|
@@ -98,6 +103,7 @@ function parseArgs(argv) {
|
|
|
98
103
|
options.pr = true;
|
|
99
104
|
break;
|
|
100
105
|
case '-ua':
|
|
106
|
+
case '--upstream-allow':
|
|
101
107
|
case '--upstream-merge':
|
|
102
108
|
options.pr = true;
|
|
103
109
|
options.merge = true;
|
|
@@ -105,6 +111,7 @@ function parseArgs(argv) {
|
|
|
105
111
|
case '-b':
|
|
106
112
|
case '--branch':
|
|
107
113
|
options.branch = takeValue();
|
|
114
|
+
options.branchGiven = true;
|
|
108
115
|
break;
|
|
109
116
|
case '-m':
|
|
110
117
|
case '--message':
|
|
@@ -294,7 +301,9 @@ function ghToken(login, isActive) {
|
|
|
294
301
|
|
|
295
302
|
// Works out which gh account to push with and returns the extra git
|
|
296
303
|
// arguments + env needed to use it. Returns null to use normal git login.
|
|
297
|
-
|
|
304
|
+
// With `writeRepo` ("owner/name"), an account that can push to that repo is
|
|
305
|
+
// preferred, because the owner's account may not exist (an organisation).
|
|
306
|
+
function githubAuth(remoteUrl, writeRepo) {
|
|
298
307
|
if (/^(ssh:\/\/)?git@github\.com[:/]/i.test(remoteUrl)) {
|
|
299
308
|
console.log(dim('• SSH remote, using your SSH key.'));
|
|
300
309
|
return null;
|
|
@@ -312,10 +321,22 @@ function githubAuth(remoteUrl) {
|
|
|
312
321
|
|
|
313
322
|
// GitHub usernames are case-insensitive (Gobibahu === GobiBahu).
|
|
314
323
|
const match = gh.accounts.find((a) => a.toLowerCase() === owner.toLowerCase());
|
|
315
|
-
|
|
324
|
+
let account = match || gh.active;
|
|
325
|
+
let writer = null;
|
|
326
|
+
|
|
327
|
+
if (writeRepo) {
|
|
328
|
+
const candidates = [match, gh.active, ...gh.accounts].filter(Boolean);
|
|
329
|
+
writer = candidates.find((a) => {
|
|
330
|
+
const token = ghToken(a, a === gh.active);
|
|
331
|
+
return token && canPush(writeRepo, { GH_TOKEN: token });
|
|
332
|
+
});
|
|
333
|
+
if (writer) account = writer;
|
|
334
|
+
}
|
|
316
335
|
|
|
317
|
-
if (match) {
|
|
336
|
+
if (match && account === match) {
|
|
318
337
|
console.log(cyan(`• Using GitHub account "${account}" (owner of this repo)`));
|
|
338
|
+
} else if (writer) {
|
|
339
|
+
console.log(cyan(`• Using GitHub account "${account}" (has write access to ${writeRepo})`));
|
|
319
340
|
} else {
|
|
320
341
|
console.log(cyan(`• No gh account for "${owner}", using last used account "${account}"`));
|
|
321
342
|
}
|
|
@@ -350,21 +371,54 @@ function githubAuth(remoteUrl) {
|
|
|
350
371
|
// ---------- pull requests (via gh CLI) ----------
|
|
351
372
|
// Checks everything -u needs before anything is committed. Returns the
|
|
352
373
|
// upstream repo ("owner/name") plus the owners of upstream and the fork.
|
|
374
|
+
// With no originUrl there is no fork: the branch goes straight to upstream
|
|
375
|
+
// (`direct`), so the pull request's head and base are the same repo.
|
|
353
376
|
function pullRequestTarget(originUrl) {
|
|
354
377
|
const upstreamUrl = git(['remote', 'get-url', 'upstream'], { quiet: true });
|
|
355
378
|
if (upstreamUrl.status !== 0) {
|
|
356
379
|
fail('Remote "upstream" does not exist. Add it with: git remote add upstream <url>');
|
|
357
380
|
}
|
|
358
381
|
|
|
359
|
-
const head = githubRepo(originUrl);
|
|
382
|
+
const head = originUrl ? githubRepo(originUrl) : null;
|
|
360
383
|
const base = githubRepo(upstreamUrl.stdout.trim());
|
|
361
|
-
if (!
|
|
384
|
+
if (!base || (originUrl && !head)) {
|
|
385
|
+
fail(
|
|
386
|
+
originUrl
|
|
387
|
+
? 'Pull requests need "origin" and "upstream" to both be GitHub repos.'
|
|
388
|
+
: 'Pull requests need "upstream" to be a GitHub repo.'
|
|
389
|
+
);
|
|
390
|
+
}
|
|
362
391
|
|
|
363
392
|
if (run('gh', ['--version'], { quiet: true }).status !== 0) {
|
|
364
393
|
fail('Pull requests need the GitHub CLI (gh). Install it from https://cli.github.com');
|
|
365
394
|
}
|
|
366
395
|
|
|
367
|
-
return {
|
|
396
|
+
return {
|
|
397
|
+
repo: `${base.owner}/${base.name}`,
|
|
398
|
+
baseOwner: base.owner,
|
|
399
|
+
headOwner: head ? head.owner : base.owner,
|
|
400
|
+
direct: !originUrl,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// The branch to push to upstream when there is no fork: -b if given, else the
|
|
405
|
+
// branch you are on, else a new acp-<date> one (a pull request can't go from
|
|
406
|
+
// upstream's default branch into itself).
|
|
407
|
+
function directBranch(target, requested) {
|
|
408
|
+
const result = run('gh', ['api', `repos/${target.repo}`, '--jq', '.default_branch'], { quiet: true });
|
|
409
|
+
const base = result.status === 0 ? result.stdout.trim() : '';
|
|
410
|
+
if (!base) fail(`Could not read the default branch of ${target.repo} with gh. Is gh logged in?`);
|
|
411
|
+
|
|
412
|
+
if (requested) {
|
|
413
|
+
if (requested === base) fail(`-b ${requested} is the default branch of ${target.repo}, a pull request needs another one.`);
|
|
414
|
+
return requested;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const current = git(['rev-parse', '--abbrev-ref', 'HEAD'], { quiet: true }).stdout.trim();
|
|
418
|
+
if (current && current !== 'HEAD' && current !== base) return current;
|
|
419
|
+
|
|
420
|
+
const stamp = new Date().toISOString().replace(/\D/g, '').slice(0, 14);
|
|
421
|
+
return `acp-${stamp}`;
|
|
368
422
|
}
|
|
369
423
|
|
|
370
424
|
// True when the gh account behind `env` has write access to `repo`
|
|
@@ -393,7 +447,7 @@ function pullRequest(target, branch, merge) {
|
|
|
393
447
|
return token ? { GH_TOKEN: token } : undefined;
|
|
394
448
|
};
|
|
395
449
|
|
|
396
|
-
const opener = accountFor(target.headOwner);
|
|
450
|
+
const opener = target.opener || accountFor(target.headOwner);
|
|
397
451
|
const head = `${target.headOwner}:${branch}`;
|
|
398
452
|
const title = git(['log', '-1', '--pretty=%s'], { quiet: true }).stdout.trim();
|
|
399
453
|
|
|
@@ -482,13 +536,15 @@ function multiCommit() {
|
|
|
482
536
|
|
|
483
537
|
// ---------- main ----------
|
|
484
538
|
function main() {
|
|
485
|
-
const { branch, message, pr, merge, chunks } = parseArgs(process.argv.slice(2));
|
|
486
|
-
|
|
539
|
+
const { branch: requestedBranch, branchGiven, message, pr, merge, chunks } = parseArgs(process.argv.slice(2));
|
|
540
|
+
let branch = requestedBranch;
|
|
541
|
+
let remote = 'origin';
|
|
487
542
|
|
|
488
543
|
if (git(['rev-parse', '--is-inside-work-tree'], { quiet: true }).status !== 0) {
|
|
489
544
|
fail('Not inside a git repository.');
|
|
490
545
|
}
|
|
491
546
|
|
|
547
|
+
const remoteUrlOf = (name) => git(['remote', 'get-url', name], { quiet: true });
|
|
492
548
|
const multi = usesMultiCommit();
|
|
493
549
|
let remoteUrl;
|
|
494
550
|
let target;
|
|
@@ -496,30 +552,42 @@ function main() {
|
|
|
496
552
|
console.log(cyan(`• Found ${MULTI_COMMIT_CONFIG}, pushing with git-multi-commit instead of git push.`));
|
|
497
553
|
if (pr) {
|
|
498
554
|
// git-multi-commit pushes to its own repos, so -u/-ua only apply when
|
|
499
|
-
//
|
|
500
|
-
const hasOrigin =
|
|
501
|
-
const hasUpstream =
|
|
502
|
-
if (
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
555
|
+
// "upstream" exists. Without "origin" the branch goes straight to it.
|
|
556
|
+
const hasOrigin = remoteUrlOf('origin');
|
|
557
|
+
const hasUpstream = remoteUrlOf('upstream');
|
|
558
|
+
if (hasUpstream.status === 0) {
|
|
559
|
+
if (hasOrigin.status !== 0) remote = 'upstream';
|
|
560
|
+
remoteUrl = hasOrigin.status === 0 ? hasOrigin : hasUpstream;
|
|
561
|
+
target = pullRequestTarget(hasOrigin.status === 0 ? remoteUrl.stdout.trim() : null);
|
|
562
|
+
console.log(cyan(`• Found remote "upstream", will also push to ${remote} and open a pull request.`));
|
|
506
563
|
} else {
|
|
507
|
-
|
|
508
|
-
console.log(yellow(`⚠ Remote "${missing}" does not exist, skipping -u/-ua (git-multi-commit only).`));
|
|
564
|
+
console.log(yellow('⚠ Remote "upstream" does not exist, skipping -u/-ua (git-multi-commit only).'));
|
|
509
565
|
}
|
|
510
566
|
}
|
|
511
567
|
if (!target && branch !== 'main') console.log(yellow('⚠ -b is ignored for git-multi-commit.'));
|
|
512
568
|
} else {
|
|
513
|
-
remoteUrl =
|
|
569
|
+
remoteUrl = remoteUrlOf('origin');
|
|
514
570
|
if (remoteUrl.status !== 0) {
|
|
515
|
-
|
|
571
|
+
const upstream = pr ? remoteUrlOf('upstream') : null;
|
|
572
|
+
if (!upstream || upstream.status !== 0) {
|
|
573
|
+
fail(`Remote "${remote}" does not exist. Add it with: git remote add ${remote} <url>`);
|
|
574
|
+
}
|
|
575
|
+
console.log(cyan('• No "origin" remote, pushing a branch straight to "upstream".'));
|
|
576
|
+
remote = 'upstream';
|
|
577
|
+
remoteUrl = upstream;
|
|
578
|
+
target = pullRequestTarget(null);
|
|
579
|
+
} else if (pr) {
|
|
580
|
+
target = pullRequestTarget(remoteUrl.stdout.trim());
|
|
516
581
|
}
|
|
517
|
-
if (pr) target = pullRequestTarget(remoteUrl.stdout.trim());
|
|
518
582
|
}
|
|
519
583
|
|
|
584
|
+
if (target && target.direct) branch = directBranch(target, branchGiven ? requestedBranch : null);
|
|
585
|
+
|
|
520
586
|
if (multi && chunks) console.log(yellow('⚠ -c is ignored for git-multi-commit.'));
|
|
521
587
|
|
|
522
|
-
const auth = remoteUrl ? githubAuth(remoteUrl.stdout.trim()) || {} : null;
|
|
588
|
+
const auth = remoteUrl ? githubAuth(remoteUrl.stdout.trim(), target && target.direct ? target.repo : null) || {} : null;
|
|
589
|
+
// Without a fork, the account that pushed the branch also opens the pull request.
|
|
590
|
+
if (target && target.direct && auth && auth.env) target.opener = auth.env.ACP_GH_USER;
|
|
523
591
|
|
|
524
592
|
// "HEAD:<branch>" pushes whatever branch you're on (main, master, anything)
|
|
525
593
|
// to <branch> on the remote, so the default always lands on "main".
|