acp-push 1.3.2 → 1.4.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 +25 -5
- package/bin/acp.js +119 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,7 +26,8 @@ npm install -g acp-push
|
|
|
26
26
|
|
|
27
27
|
| Flag | Description |
|
|
28
28
|
| --- | --- |
|
|
29
|
-
| `-u`, `--upstream` | Push to
|
|
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 |
|
|
30
31
|
| `-b`, `--branch <name>` | Push to `<name>` instead of `main` |
|
|
31
32
|
| `-m`, `--message <text>` | Use your own commit message instead of a random one |
|
|
32
33
|
| `-h`, `--help` | Show help |
|
|
@@ -36,13 +37,33 @@ npm install -g acp-push
|
|
|
36
37
|
|
|
37
38
|
```bash
|
|
38
39
|
acp # git push origin main
|
|
39
|
-
acp -u # git push
|
|
40
|
+
acp -u # git push origin main, then open a PR to upstream
|
|
41
|
+
acp -ua # same, then merge the PR
|
|
40
42
|
acp -b dev # git push origin dev
|
|
41
43
|
acp --branch=dev # same as above
|
|
42
|
-
acp -u -b
|
|
44
|
+
acp -u -b fix # git push origin fix, then open a PR to upstream
|
|
43
45
|
acp -m "Fix login bug" # custom message
|
|
44
46
|
```
|
|
45
47
|
|
|
48
|
+
## Pull requests to upstream
|
|
49
|
+
|
|
50
|
+
If `origin` is your fork and `upstream` is the original repo, `acp -u` pushes to your fork and opens a pull request:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
git add .
|
|
54
|
+
git commit -m "<one of 25 random messages>"
|
|
55
|
+
git push origin HEAD:main
|
|
56
|
+
gh pr create --repo <upstream owner>/<repo> --head <your account>:main
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`acp -ua` does the same and then merges it with `gh pr merge --merge`.
|
|
60
|
+
|
|
61
|
+
- You need the [GitHub CLI](https://cli.github.com) (`gh`) and a remote called `upstream`. Add one with `git remote add upstream <url>`. Both remotes must be GitHub repos.
|
|
62
|
+
- The pull request goes into the default branch of `upstream`. Its title is your last commit message.
|
|
63
|
+
- If a pull request from that branch is already open, the push updates it and `acp` doesn't open a second one. `-ua` merges the open one.
|
|
64
|
+
- Merging needs write access to `upstream`. If you're logged in to `gh` as the owner of `upstream`, `acp` merges with that account. Otherwise it uses the same account that opened the pull request.
|
|
65
|
+
- If the merge fails, the pull request stays open and `acp` prints its link.
|
|
66
|
+
|
|
46
67
|
## Multiple GitHub accounts
|
|
47
68
|
|
|
48
69
|
If you have the [GitHub CLI](https://cli.github.com) (`gh`) installed, `acp` picks the right account for each repo automatically:
|
|
@@ -72,13 +93,12 @@ git commit -m "<one of 25 random messages>"
|
|
|
72
93
|
git-multi-commit
|
|
73
94
|
```
|
|
74
95
|
|
|
75
|
-
No `origin` remote is needed in this case, and `-u` / `-b` are ignored. Install it with `npm install -g git-multi-commit`.
|
|
96
|
+
No `origin` remote is needed in this case, and `-u` / `-ua` / `-b` are ignored. Install it with `npm install -g git-multi-commit`.
|
|
76
97
|
|
|
77
98
|
## Notes
|
|
78
99
|
|
|
79
100
|
- If there is nothing new to commit, `acp` skips the commit and still pushes, so any commits you made earlier get sent.
|
|
80
101
|
- `-b <name>` pushes your current local branch to `<name>` on the remote, creating it there if it doesn't exist yet.
|
|
81
|
-
- `-u` needs a remote called `upstream`. Add one with `git remote add upstream <url>`.
|
|
82
102
|
|
|
83
103
|
## Custom messages
|
|
84
104
|
|
package/bin/acp.js
CHANGED
|
@@ -22,7 +22,8 @@ ${cyan('acp')} — git add, commit and push in one go
|
|
|
22
22
|
Usage: acp [options]
|
|
23
23
|
|
|
24
24
|
Options:
|
|
25
|
-
-u, --upstream Push to
|
|
25
|
+
-u, --upstream Push to origin, then open a pull request to "upstream"
|
|
26
|
+
-ua, --upstream-merge Same as -u, then merge the pull request
|
|
26
27
|
-b, --branch <name> Push to <name> instead of "main"
|
|
27
28
|
-m, --message <text> Use your own commit message instead of a random one
|
|
28
29
|
-h, --help Show this help
|
|
@@ -33,16 +34,22 @@ GitHub accounts:
|
|
|
33
34
|
the repo owner (github.com/<owner>/...). If none matches, it uses the
|
|
34
35
|
account you used last. Log in to each account once with: gh auth login
|
|
35
36
|
|
|
37
|
+
Pull requests:
|
|
38
|
+
-u and -ua need the GitHub CLI (gh) and a remote called "upstream". The
|
|
39
|
+
pull request goes from <branch> on origin (your fork) to the default
|
|
40
|
+
branch of upstream. If one is already open, the push just updates it.
|
|
41
|
+
|
|
36
42
|
git-multi-commit:
|
|
37
43
|
If the repo root has a .git-multi-commit.json file, acp still adds and
|
|
38
44
|
commits locally, then runs git-multi-commit instead of git push.
|
|
39
|
-
-u and -b are ignored in that case.
|
|
45
|
+
-u, -ua and -b are ignored in that case.
|
|
40
46
|
|
|
41
47
|
Examples:
|
|
42
48
|
acp ${dim('# push to origin/main')}
|
|
43
|
-
acp -u ${dim('# push to
|
|
49
|
+
acp -u ${dim('# push to origin/main, open a PR to upstream')}
|
|
50
|
+
acp -ua ${dim('# same, then merge the PR')}
|
|
44
51
|
acp -b dev ${dim('# push to origin/dev')}
|
|
45
|
-
acp -u --branch
|
|
52
|
+
acp -u --branch fix ${dim('# push to origin/fix, open a PR to upstream')}
|
|
46
53
|
acp -m "Fix login bug" ${dim('# custom message, push to origin/main')}
|
|
47
54
|
`;
|
|
48
55
|
|
|
@@ -53,7 +60,7 @@ function fail(message) {
|
|
|
53
60
|
|
|
54
61
|
// ---------- argument parsing ----------
|
|
55
62
|
function parseArgs(argv) {
|
|
56
|
-
const options = {
|
|
63
|
+
const options = { branch: 'main', message: null, pr: false, merge: false };
|
|
57
64
|
|
|
58
65
|
for (let i = 0; i < argv.length; i++) {
|
|
59
66
|
let flag = argv[i];
|
|
@@ -79,7 +86,12 @@ function parseArgs(argv) {
|
|
|
79
86
|
switch (flag) {
|
|
80
87
|
case '-u':
|
|
81
88
|
case '--upstream':
|
|
82
|
-
options.
|
|
89
|
+
options.pr = true;
|
|
90
|
+
break;
|
|
91
|
+
case '-ua':
|
|
92
|
+
case '--upstream-merge':
|
|
93
|
+
options.pr = true;
|
|
94
|
+
options.merge = true;
|
|
83
95
|
break;
|
|
84
96
|
case '-b':
|
|
85
97
|
case '--branch':
|
|
@@ -151,9 +163,12 @@ function randomMessage() {
|
|
|
151
163
|
}
|
|
152
164
|
|
|
153
165
|
// ---------- GitHub account picking (via gh CLI) ----------
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
166
|
+
// Returns { owner, name } for a GitHub remote URL (HTTPS or SSH), or null.
|
|
167
|
+
function githubRepo(url) {
|
|
168
|
+
const match = url.match(
|
|
169
|
+
/^(?:https?:\/\/(?:[^@/]+@)?github\.com\/|(?:ssh:\/\/)?git@github\.com[:/])([^/]+)\/([^/]+?)(?:\.git)?\/?$/i
|
|
170
|
+
);
|
|
171
|
+
return match ? { owner: match[1], name: match[2] } : null;
|
|
157
172
|
}
|
|
158
173
|
|
|
159
174
|
// Returns { accounts: [...logins], active: login } or null.
|
|
@@ -202,8 +217,9 @@ function githubAuth(remoteUrl) {
|
|
|
202
217
|
return null;
|
|
203
218
|
}
|
|
204
219
|
|
|
205
|
-
const
|
|
206
|
-
if (!
|
|
220
|
+
const repo = githubRepo(remoteUrl);
|
|
221
|
+
if (!repo) return null; // not a GitHub https remote
|
|
222
|
+
const owner = repo.owner;
|
|
207
223
|
|
|
208
224
|
const gh = ghAccounts();
|
|
209
225
|
if (!gh) {
|
|
@@ -248,6 +264,91 @@ function githubAuth(remoteUrl) {
|
|
|
248
264
|
};
|
|
249
265
|
}
|
|
250
266
|
|
|
267
|
+
// ---------- pull requests (via gh CLI) ----------
|
|
268
|
+
// Checks everything -u needs before anything is committed. Returns the
|
|
269
|
+
// upstream repo ("owner/name") plus the owners of upstream and the fork.
|
|
270
|
+
function pullRequestTarget(originUrl) {
|
|
271
|
+
const upstreamUrl = git(['remote', 'get-url', 'upstream'], { quiet: true });
|
|
272
|
+
if (upstreamUrl.status !== 0) {
|
|
273
|
+
fail('Remote "upstream" does not exist. Add it with: git remote add upstream <url>');
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const head = githubRepo(originUrl);
|
|
277
|
+
const base = githubRepo(upstreamUrl.stdout.trim());
|
|
278
|
+
if (!head || !base) fail('Pull requests need "origin" and "upstream" to both be GitHub repos.');
|
|
279
|
+
|
|
280
|
+
if (run('gh', ['--version'], { quiet: true }).status !== 0) {
|
|
281
|
+
fail('Pull requests need the GitHub CLI (gh). Install it from https://cli.github.com');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return { repo: `${base.owner}/${base.name}`, baseOwner: base.owner, headOwner: head.owner };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function sleep(ms) {
|
|
288
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Opens a pull request from <branch> on the fork to upstream's default
|
|
292
|
+
// branch, and merges it when `merge` is set.
|
|
293
|
+
function pullRequest(target, branch, merge) {
|
|
294
|
+
const gh = ghAccounts();
|
|
295
|
+
const accountFor = (owner) => gh && gh.accounts.find((a) => a.toLowerCase() === owner.toLowerCase());
|
|
296
|
+
// GH_TOKEN makes gh act as that account. Without it gh uses its active one.
|
|
297
|
+
const envFor = (login) => {
|
|
298
|
+
const token = login && ghToken(login, login === gh.active);
|
|
299
|
+
return token ? { GH_TOKEN: token } : undefined;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const opener = accountFor(target.headOwner);
|
|
303
|
+
const head = `${target.headOwner}:${branch}`;
|
|
304
|
+
const title = git(['log', '-1', '--pretty=%s'], { quiet: true }).stdout.trim();
|
|
305
|
+
|
|
306
|
+
console.log(cyan(`› gh pr create --repo ${target.repo} --head ${head}`));
|
|
307
|
+
const created = run(
|
|
308
|
+
'gh',
|
|
309
|
+
['pr', 'create', '--repo', target.repo, '--head', head, '--title', title, '--body', ''],
|
|
310
|
+
{ quiet: true, env: envFor(opener) }
|
|
311
|
+
);
|
|
312
|
+
// gh prints the new PR's URL, or the open one's when it already exists.
|
|
313
|
+
const output = `${created.stdout || ''}\n${created.stderr || ''}`;
|
|
314
|
+
const found = output.match(/https:\/\/github\.com\/\S+\/pull\/\d+/);
|
|
315
|
+
if (!found || (created.status !== 0 && !/already exists/i.test(output))) {
|
|
316
|
+
if (created.stderr) process.stderr.write(created.stderr);
|
|
317
|
+
fail(`"gh pr create" failed (exit code ${created.status}).`);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const url = found[0];
|
|
321
|
+
if (created.status === 0) {
|
|
322
|
+
console.log(green(`✔ Pull request opened: ${url}`));
|
|
323
|
+
} else {
|
|
324
|
+
console.log(yellow(`• Pull request already open, updated by this push: ${url}`));
|
|
325
|
+
}
|
|
326
|
+
if (!merge) return;
|
|
327
|
+
|
|
328
|
+
// The fork's owner often can't merge into upstream, so prefer the gh
|
|
329
|
+
// account that owns upstream when there is one.
|
|
330
|
+
const merger = accountFor(target.baseOwner) || opener;
|
|
331
|
+
if (merger && merger !== opener) {
|
|
332
|
+
console.log(cyan(`• Merging with GitHub account "${merger}" (owner of upstream)`));
|
|
333
|
+
}
|
|
334
|
+
const env = envFor(merger);
|
|
335
|
+
|
|
336
|
+
console.log(cyan(`› gh pr merge ${url} --merge`));
|
|
337
|
+
let merged;
|
|
338
|
+
for (let attempt = 1; attempt <= 3; attempt++) {
|
|
339
|
+
merged = run('gh', ['pr', 'merge', url, '--merge'], { quiet: true, env });
|
|
340
|
+
// GitHub needs a moment to work out whether a brand new PR can be merged.
|
|
341
|
+
if (merged.status === 0 || !/GraphQL: .*not mergeable/i.test(merged.stderr || '')) break;
|
|
342
|
+
if (attempt < 3) sleep(2000);
|
|
343
|
+
}
|
|
344
|
+
if (merged.status !== 0) {
|
|
345
|
+
if (merged.stderr) process.stderr.write(merged.stderr);
|
|
346
|
+
console.error(yellow(`⚠ The pull request is still open: ${url}`));
|
|
347
|
+
fail(`"gh pr merge" failed (exit code ${merged.status}).`);
|
|
348
|
+
}
|
|
349
|
+
console.log(green(`✔ Merged into ${target.repo}`));
|
|
350
|
+
}
|
|
351
|
+
|
|
251
352
|
// ---------- git-multi-commit ----------
|
|
252
353
|
// Projects set up with git-multi-commit keep this file in the repo root
|
|
253
354
|
// (the same place git-multi-commit looks for it). For those, acp runs
|
|
@@ -275,7 +376,8 @@ function multiCommit() {
|
|
|
275
376
|
|
|
276
377
|
// ---------- main ----------
|
|
277
378
|
function main() {
|
|
278
|
-
const {
|
|
379
|
+
const { branch, message, pr, merge } = parseArgs(process.argv.slice(2));
|
|
380
|
+
const remote = 'origin';
|
|
279
381
|
|
|
280
382
|
if (git(['rev-parse', '--is-inside-work-tree'], { quiet: true }).status !== 0) {
|
|
281
383
|
fail('Not inside a git repository.');
|
|
@@ -283,14 +385,16 @@ function main() {
|
|
|
283
385
|
|
|
284
386
|
const multi = usesMultiCommit();
|
|
285
387
|
let remoteUrl;
|
|
388
|
+
let target;
|
|
286
389
|
if (multi) {
|
|
287
390
|
console.log(cyan(`• Found ${MULTI_COMMIT_CONFIG}, pushing with git-multi-commit instead of git push.`));
|
|
288
|
-
if (
|
|
391
|
+
if (pr || branch !== 'main') console.log(yellow('⚠ -u, -ua and -b are ignored for git-multi-commit.'));
|
|
289
392
|
} else {
|
|
290
393
|
remoteUrl = git(['remote', 'get-url', remote], { quiet: true });
|
|
291
394
|
if (remoteUrl.status !== 0) {
|
|
292
395
|
fail(`Remote "${remote}" does not exist. Add it with: git remote add ${remote} <url>`);
|
|
293
396
|
}
|
|
397
|
+
if (pr) target = pullRequestTarget(remoteUrl.stdout.trim());
|
|
294
398
|
}
|
|
295
399
|
|
|
296
400
|
step(['add', '.']);
|
|
@@ -314,6 +418,8 @@ function main() {
|
|
|
314
418
|
// to <branch> on the remote, so the default always lands on "main".
|
|
315
419
|
step(['push', remote, `HEAD:${branch}`], auth);
|
|
316
420
|
console.log(green(`✔ Pushed to ${remote}/${branch}`));
|
|
421
|
+
|
|
422
|
+
if (pr) pullRequest(target, branch, merge);
|
|
317
423
|
}
|
|
318
424
|
|
|
319
425
|
main();
|