acp-push 1.2.0 → 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 +36 -4
- package/bin/acp.js +170 -17
- 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:
|
|
@@ -62,11 +83,22 @@ gh auth login # run again for the second account
|
|
|
62
83
|
|
|
63
84
|
`acp` switches gh's active account to whichever one it used, so "last used" is always the most recent one. The token is only used for that one push and doesn't replace your saved git login. SSH remotes and non-GitHub remotes use your normal git login.
|
|
64
85
|
|
|
86
|
+
## git-multi-commit projects
|
|
87
|
+
|
|
88
|
+
If your repo root contains a `.git-multi-commit.json` file (created by [`git-multi-commit --config`](https://www.npmjs.com/package/git-multi-commit)), `acp` still runs `git add .` and `git commit` locally, but then runs `git-multi-commit` instead of `git push`:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
git add .
|
|
92
|
+
git commit -m "<one of 25 random messages>"
|
|
93
|
+
git-multi-commit
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
No `origin` remote is needed in this case, and `-u` / `-ua` / `-b` are ignored. Install it with `npm install -g git-multi-commit`.
|
|
97
|
+
|
|
65
98
|
## Notes
|
|
66
99
|
|
|
67
100
|
- If there is nothing new to commit, `acp` skips the commit and still pushes, so any commits you made earlier get sent.
|
|
68
101
|
- `-b <name>` pushes your current local branch to `<name>` on the remote, creating it there if it doesn't exist yet.
|
|
69
|
-
- `-u` needs a remote called `upstream`. Add one with `git remote add upstream <url>`.
|
|
70
102
|
|
|
71
103
|
## Custom messages
|
|
72
104
|
|
package/bin/acp.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
const { spawnSync } = require('child_process');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
5
7
|
const messages = require('../lib/messages');
|
|
6
8
|
const pkg = require('../package.json');
|
|
7
9
|
|
|
@@ -20,7 +22,8 @@ ${cyan('acp')} — git add, commit and push in one go
|
|
|
20
22
|
Usage: acp [options]
|
|
21
23
|
|
|
22
24
|
Options:
|
|
23
|
-
-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
|
|
24
27
|
-b, --branch <name> Push to <name> instead of "main"
|
|
25
28
|
-m, --message <text> Use your own commit message instead of a random one
|
|
26
29
|
-h, --help Show this help
|
|
@@ -31,11 +34,22 @@ GitHub accounts:
|
|
|
31
34
|
the repo owner (github.com/<owner>/...). If none matches, it uses the
|
|
32
35
|
account you used last. Log in to each account once with: gh auth login
|
|
33
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
|
+
|
|
42
|
+
git-multi-commit:
|
|
43
|
+
If the repo root has a .git-multi-commit.json file, acp still adds and
|
|
44
|
+
commits locally, then runs git-multi-commit instead of git push.
|
|
45
|
+
-u, -ua and -b are ignored in that case.
|
|
46
|
+
|
|
34
47
|
Examples:
|
|
35
48
|
acp ${dim('# push to origin/main')}
|
|
36
|
-
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')}
|
|
37
51
|
acp -b dev ${dim('# push to origin/dev')}
|
|
38
|
-
acp -u --branch
|
|
52
|
+
acp -u --branch fix ${dim('# push to origin/fix, open a PR to upstream')}
|
|
39
53
|
acp -m "Fix login bug" ${dim('# custom message, push to origin/main')}
|
|
40
54
|
`;
|
|
41
55
|
|
|
@@ -46,7 +60,7 @@ function fail(message) {
|
|
|
46
60
|
|
|
47
61
|
// ---------- argument parsing ----------
|
|
48
62
|
function parseArgs(argv) {
|
|
49
|
-
const options = {
|
|
63
|
+
const options = { branch: 'main', message: null, pr: false, merge: false };
|
|
50
64
|
|
|
51
65
|
for (let i = 0; i < argv.length; i++) {
|
|
52
66
|
let flag = argv[i];
|
|
@@ -72,7 +86,12 @@ function parseArgs(argv) {
|
|
|
72
86
|
switch (flag) {
|
|
73
87
|
case '-u':
|
|
74
88
|
case '--upstream':
|
|
75
|
-
options.
|
|
89
|
+
options.pr = true;
|
|
90
|
+
break;
|
|
91
|
+
case '-ua':
|
|
92
|
+
case '--upstream-merge':
|
|
93
|
+
options.pr = true;
|
|
94
|
+
options.merge = true;
|
|
76
95
|
break;
|
|
77
96
|
case '-b':
|
|
78
97
|
case '--branch':
|
|
@@ -102,12 +121,14 @@ function parseArgs(argv) {
|
|
|
102
121
|
|
|
103
122
|
// ---------- process helpers ----------
|
|
104
123
|
// Arguments are passed as an array (no shell), so commit messages with
|
|
105
|
-
// quotes, spaces or special characters are always safe.
|
|
106
|
-
|
|
124
|
+
// quotes, spaces or special characters are always safe. Only pass
|
|
125
|
+
// `shell: true` for commands that take no user input.
|
|
126
|
+
function run(cmd, args, { quiet = false, env, stdio, shell = false } = {}) {
|
|
107
127
|
return spawnSync(cmd, args, {
|
|
108
128
|
stdio: stdio || (quiet ? 'pipe' : 'inherit'),
|
|
109
129
|
encoding: 'utf8',
|
|
110
130
|
env: env ? { ...process.env, ...env } : process.env,
|
|
131
|
+
shell,
|
|
111
132
|
});
|
|
112
133
|
}
|
|
113
134
|
|
|
@@ -131,7 +152,7 @@ function step(args, { extraArgs = [], env, hint } = {}) {
|
|
|
131
152
|
});
|
|
132
153
|
if (hint && result.stderr) process.stderr.write(result.stderr);
|
|
133
154
|
if (result.status !== 0) {
|
|
134
|
-
const authError = /\b403\b|permission|denied|authentication failed|could not read username/i;
|
|
155
|
+
const authError = /\b403\b|permission|denied|authentication failed|could not read username|repository not found/i;
|
|
135
156
|
if (hint && authError.test(result.stderr || '')) console.error(yellow(hint));
|
|
136
157
|
fail(`"git ${args[0]}" failed (exit code ${result.status}).`);
|
|
137
158
|
}
|
|
@@ -142,9 +163,12 @@ function randomMessage() {
|
|
|
142
163
|
}
|
|
143
164
|
|
|
144
165
|
// ---------- GitHub account picking (via gh CLI) ----------
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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;
|
|
148
172
|
}
|
|
149
173
|
|
|
150
174
|
// Returns { accounts: [...logins], active: login } or null.
|
|
@@ -193,8 +217,9 @@ function githubAuth(remoteUrl) {
|
|
|
193
217
|
return null;
|
|
194
218
|
}
|
|
195
219
|
|
|
196
|
-
const
|
|
197
|
-
if (!
|
|
220
|
+
const repo = githubRepo(remoteUrl);
|
|
221
|
+
if (!repo) return null; // not a GitHub https remote
|
|
222
|
+
const owner = repo.owner;
|
|
198
223
|
|
|
199
224
|
const gh = ghAccounts();
|
|
200
225
|
if (!gh) {
|
|
@@ -239,17 +264,137 @@ function githubAuth(remoteUrl) {
|
|
|
239
264
|
};
|
|
240
265
|
}
|
|
241
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
|
+
|
|
352
|
+
// ---------- git-multi-commit ----------
|
|
353
|
+
// Projects set up with git-multi-commit keep this file in the repo root
|
|
354
|
+
// (the same place git-multi-commit looks for it). For those, acp runs
|
|
355
|
+
// git-multi-commit instead of git push.
|
|
356
|
+
const MULTI_COMMIT_CONFIG = '.git-multi-commit.json';
|
|
357
|
+
|
|
358
|
+
function usesMultiCommit() {
|
|
359
|
+
const root = git(['rev-parse', '--show-toplevel'], { quiet: true }).stdout.trim();
|
|
360
|
+
return fs.existsSync(path.join(root, MULTI_COMMIT_CONFIG));
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function multiCommit() {
|
|
364
|
+
const notInstalled = 'git-multi-commit is not installed. Install it with: npm install -g git-multi-commit';
|
|
365
|
+
// npm installs global CLIs as .cmd shims on Windows, which only start
|
|
366
|
+
// through a shell. No arguments are passed, so no user input reaches it.
|
|
367
|
+
// cmd.exe's "not found" exit code isn't reliable, so look it up first.
|
|
368
|
+
const isWindows = process.platform === 'win32';
|
|
369
|
+
if (isWindows && run('where', ['git-multi-commit'], { quiet: true }).status !== 0) fail(notInstalled);
|
|
370
|
+
|
|
371
|
+
console.log(cyan('› git-multi-commit'));
|
|
372
|
+
const result = run('git-multi-commit', [], { shell: isWindows });
|
|
373
|
+
if (result.error) fail(result.error.code === 'ENOENT' ? notInstalled : result.error.message);
|
|
374
|
+
if (result.status !== 0) fail(`"git-multi-commit" failed (exit code ${result.status}).`);
|
|
375
|
+
}
|
|
376
|
+
|
|
242
377
|
// ---------- main ----------
|
|
243
378
|
function main() {
|
|
244
|
-
const {
|
|
379
|
+
const { branch, message, pr, merge } = parseArgs(process.argv.slice(2));
|
|
380
|
+
const remote = 'origin';
|
|
245
381
|
|
|
246
382
|
if (git(['rev-parse', '--is-inside-work-tree'], { quiet: true }).status !== 0) {
|
|
247
383
|
fail('Not inside a git repository.');
|
|
248
384
|
}
|
|
249
385
|
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
|
|
386
|
+
const multi = usesMultiCommit();
|
|
387
|
+
let remoteUrl;
|
|
388
|
+
let target;
|
|
389
|
+
if (multi) {
|
|
390
|
+
console.log(cyan(`• Found ${MULTI_COMMIT_CONFIG}, pushing with git-multi-commit instead of git push.`));
|
|
391
|
+
if (pr || branch !== 'main') console.log(yellow('⚠ -u, -ua and -b are ignored for git-multi-commit.'));
|
|
392
|
+
} else {
|
|
393
|
+
remoteUrl = git(['remote', 'get-url', remote], { quiet: true });
|
|
394
|
+
if (remoteUrl.status !== 0) {
|
|
395
|
+
fail(`Remote "${remote}" does not exist. Add it with: git remote add ${remote} <url>`);
|
|
396
|
+
}
|
|
397
|
+
if (pr) target = pullRequestTarget(remoteUrl.stdout.trim());
|
|
253
398
|
}
|
|
254
399
|
|
|
255
400
|
step(['add', '.']);
|
|
@@ -261,12 +406,20 @@ function main() {
|
|
|
261
406
|
step(['commit', '-m', message || randomMessage()]);
|
|
262
407
|
}
|
|
263
408
|
|
|
409
|
+
if (multi) {
|
|
410
|
+
multiCommit();
|
|
411
|
+
console.log(green('✔ Pushed with git-multi-commit'));
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
|
|
264
415
|
const auth = githubAuth(remoteUrl.stdout.trim()) || {};
|
|
265
416
|
|
|
266
417
|
// "HEAD:<branch>" pushes whatever branch you're on (main, master, anything)
|
|
267
418
|
// to <branch> on the remote, so the default always lands on "main".
|
|
268
419
|
step(['push', remote, `HEAD:${branch}`], auth);
|
|
269
420
|
console.log(green(`✔ Pushed to ${remote}/${branch}`));
|
|
421
|
+
|
|
422
|
+
if (pr) pullRequest(target, branch, merge);
|
|
270
423
|
}
|
|
271
424
|
|
|
272
425
|
main();
|