acp-push 1.4.0 → 1.4.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 +6 -4
- package/bin/acp.js +119 -13
- package/lib/messages.js +30 -30
- package/package.json +24 -24
package/README.md
CHANGED
|
@@ -27,9 +27,10 @@ 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 |
|
|
30
|
+
| `-ua`, `--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
|
+
| `-c`, `--chunks` | Split your changes into 10 smaller commits (by change, not by file), then push |
|
|
33
34
|
| `-h`, `--help` | Show help |
|
|
34
35
|
| `-v`, `--version` | Show version |
|
|
35
36
|
|
|
@@ -38,11 +39,12 @@ npm install -g acp-push
|
|
|
38
39
|
```bash
|
|
39
40
|
acp # git push origin main
|
|
40
41
|
acp -u # git push origin main, then open a PR to upstream
|
|
41
|
-
acp -ua # same, then merge the PR
|
|
42
|
+
acp -ua # same, then merge the PR if you have write access
|
|
42
43
|
acp -b dev # git push origin dev
|
|
43
44
|
acp --branch=dev # same as above
|
|
44
45
|
acp -u -b fix # git push origin fix, then open a PR to upstream
|
|
45
46
|
acp -m "Fix login bug" # custom message
|
|
47
|
+
acp -c # 10 smaller commits, then push
|
|
46
48
|
```
|
|
47
49
|
|
|
48
50
|
## Pull requests to upstream
|
|
@@ -56,12 +58,12 @@ git push origin HEAD:main
|
|
|
56
58
|
gh pr create --repo <upstream owner>/<repo> --head <your account>:main
|
|
57
59
|
```
|
|
58
60
|
|
|
59
|
-
`acp -ua` does the same and then merges it with `gh pr merge --merge
|
|
61
|
+
`acp -ua` does the same and then merges it with `gh pr merge --merge`, but only if your account has write access to `upstream` (the write, maintain or admin role). Without it, the pull request is left open for a maintainer and `acp` still finishes successfully.
|
|
60
62
|
|
|
61
63
|
- 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
64
|
- The pull request goes into the default branch of `upstream`. Its title is your last commit message.
|
|
63
65
|
- 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
|
-
-
|
|
66
|
+
- 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.
|
|
65
67
|
- If the merge fails, the pull request stays open and `acp` prints its link.
|
|
66
68
|
|
|
67
69
|
## Multiple GitHub accounts
|
package/bin/acp.js
CHANGED
|
@@ -23,9 +23,10 @@ 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-merge Same as -u, then merge
|
|
26
|
+
-ua, --upstream-merge Same as -u, then merge it if you have write access
|
|
27
27
|
-b, --branch <name> Push to <name> instead of "main"
|
|
28
28
|
-m, --message <text> Use your own commit message instead of a random one
|
|
29
|
+
-c, --chunks Split your changes into 10 smaller commits
|
|
29
30
|
-h, --help Show this help
|
|
30
31
|
-v, --version Show the version
|
|
31
32
|
|
|
@@ -38,6 +39,8 @@ Pull requests:
|
|
|
38
39
|
-u and -ua need the GitHub CLI (gh) and a remote called "upstream". The
|
|
39
40
|
pull request goes from <branch> on origin (your fork) to the default
|
|
40
41
|
branch of upstream. If one is already open, the push just updates it.
|
|
42
|
+
-ua only merges if your account can write to upstream (write, maintain
|
|
43
|
+
or admin role). Otherwise the pull request is left open.
|
|
41
44
|
|
|
42
45
|
git-multi-commit:
|
|
43
46
|
If the repo root has a .git-multi-commit.json file, acp still adds and
|
|
@@ -47,10 +50,11 @@ git-multi-commit:
|
|
|
47
50
|
Examples:
|
|
48
51
|
acp ${dim('# push to origin/main')}
|
|
49
52
|
acp -u ${dim('# push to origin/main, open a PR to upstream')}
|
|
50
|
-
acp -ua ${dim('# same, then merge the PR')}
|
|
53
|
+
acp -ua ${dim('# same, then merge the PR if you can')}
|
|
51
54
|
acp -b dev ${dim('# push to origin/dev')}
|
|
52
55
|
acp -u --branch fix ${dim('# push to origin/fix, open a PR to upstream')}
|
|
53
56
|
acp -m "Fix login bug" ${dim('# custom message, push to origin/main')}
|
|
57
|
+
acp -c ${dim('# commit in up to 10 chunks, then push')}
|
|
54
58
|
`;
|
|
55
59
|
|
|
56
60
|
function fail(message) {
|
|
@@ -60,7 +64,7 @@ function fail(message) {
|
|
|
60
64
|
|
|
61
65
|
// ---------- argument parsing ----------
|
|
62
66
|
function parseArgs(argv) {
|
|
63
|
-
const options = { branch: 'main', message: null, pr: false, merge: false };
|
|
67
|
+
const options = { branch: 'main', message: null, pr: false, merge: false, chunks: false };
|
|
64
68
|
|
|
65
69
|
for (let i = 0; i < argv.length; i++) {
|
|
66
70
|
let flag = argv[i];
|
|
@@ -101,6 +105,10 @@ function parseArgs(argv) {
|
|
|
101
105
|
case '--message':
|
|
102
106
|
options.message = takeValue();
|
|
103
107
|
break;
|
|
108
|
+
case '-c':
|
|
109
|
+
case '--chunks':
|
|
110
|
+
options.chunks = true;
|
|
111
|
+
break;
|
|
104
112
|
case '-h':
|
|
105
113
|
case '--help':
|
|
106
114
|
console.log(HELP);
|
|
@@ -162,6 +170,75 @@ function randomMessage() {
|
|
|
162
170
|
return messages[Math.floor(Math.random() * messages.length)];
|
|
163
171
|
}
|
|
164
172
|
|
|
173
|
+
// ---------- chunked commits ----------
|
|
174
|
+
const MAX_CHUNKS = 10;
|
|
175
|
+
|
|
176
|
+
// The changes that are still uncommitted, as a list of units: each hunk of
|
|
177
|
+
// a tracked file's diff, any change without hunks (binary, mode change), and
|
|
178
|
+
// each untracked file as a whole. Units of one file stay in file order.
|
|
179
|
+
function changeUnits() {
|
|
180
|
+
const diff = git(['diff', '--no-color', '--no-renames', '--binary', '-U0'], { quiet: true }).stdout;
|
|
181
|
+
const units = [];
|
|
182
|
+
|
|
183
|
+
for (const section of diff.split(/^(?=diff --git )/m).filter(Boolean)) {
|
|
184
|
+
const at = section.search(/^@@ /m);
|
|
185
|
+
if (at === -1) {
|
|
186
|
+
units.push({ patch: section });
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const header = section.slice(0, at);
|
|
190
|
+
for (const hunk of section.slice(at).split(/^(?=@@ )/m)) units.push({ patch: header + hunk });
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const untracked = git(['ls-files', '-o', '--exclude-standard', '-z'], { quiet: true }).stdout;
|
|
194
|
+
for (const file of untracked.split('\0').filter(Boolean)) units.push({ file });
|
|
195
|
+
return units;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function stage(unit) {
|
|
199
|
+
if (unit.file) {
|
|
200
|
+
git(['add', '--', unit.file], { quiet: true });
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const result = spawnSync('git', ['apply', '--cached', '--unidiff-zero', '--binary'], {
|
|
204
|
+
input: unit.patch,
|
|
205
|
+
encoding: 'utf8',
|
|
206
|
+
});
|
|
207
|
+
if (result.status !== 0) fail(`Could not stage a change: ${(result.stderr || '').trim()}`);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Commits the changes as MAX_CHUNKS commits (fewer if there are fewer separate
|
|
211
|
+
// changes), splitting the individual changes (hunks) as evenly as possible,
|
|
212
|
+
// even within one file. Returns false when there was nothing to commit.
|
|
213
|
+
function commitInChunks(message) {
|
|
214
|
+
// Start from a clean index so each commit holds only its own chunk.
|
|
215
|
+
git(['reset', '-q'], { quiet: true });
|
|
216
|
+
|
|
217
|
+
const total = changeUnits().length;
|
|
218
|
+
if (!total) return false;
|
|
219
|
+
|
|
220
|
+
const count = Math.min(MAX_CHUNKS, total);
|
|
221
|
+
console.log(cyan(`• Splitting ${total} change(s) into ${count} commit(s)`));
|
|
222
|
+
|
|
223
|
+
const pool = [...messages];
|
|
224
|
+
for (let i = 0; i < count; i++) {
|
|
225
|
+
// The diff is read again after every commit, so line numbers stay right.
|
|
226
|
+
const units = changeUnits();
|
|
227
|
+
const take = Math.ceil(units.length / (count - i));
|
|
228
|
+
units.slice(0, take).forEach(stage);
|
|
229
|
+
|
|
230
|
+
let text;
|
|
231
|
+
if (message) {
|
|
232
|
+
text = `${message} (${i + 1}/${count})`;
|
|
233
|
+
} else {
|
|
234
|
+
// Draw without repeats so the log doesn't show the same message twice.
|
|
235
|
+
text = pool.length ? pool.splice(Math.floor(Math.random() * pool.length), 1)[0] : randomMessage();
|
|
236
|
+
}
|
|
237
|
+
step(['commit', '-m', text]);
|
|
238
|
+
}
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
|
|
165
242
|
// ---------- GitHub account picking (via gh CLI) ----------
|
|
166
243
|
// Returns { owner, name } for a GitHub remote URL (HTTPS or SSH), or null.
|
|
167
244
|
function githubRepo(url) {
|
|
@@ -284,6 +361,17 @@ function pullRequestTarget(originUrl) {
|
|
|
284
361
|
return { repo: `${base.owner}/${base.name}`, baseOwner: base.owner, headOwner: head.owner };
|
|
285
362
|
}
|
|
286
363
|
|
|
364
|
+
// True when the gh account behind `env` has write access to `repo`
|
|
365
|
+
// (the write, maintain or admin role), which is what merging needs.
|
|
366
|
+
function canPush(repo, env) {
|
|
367
|
+
const result = run('gh', ['api', `repos/${repo}`], { quiet: true, env });
|
|
368
|
+
try {
|
|
369
|
+
return JSON.parse(result.stdout).permissions.push === true;
|
|
370
|
+
} catch (_) {
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
287
375
|
function sleep(ms) {
|
|
288
376
|
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
289
377
|
}
|
|
@@ -325,13 +413,25 @@ function pullRequest(target, branch, merge) {
|
|
|
325
413
|
}
|
|
326
414
|
if (!merge) return;
|
|
327
415
|
|
|
328
|
-
//
|
|
329
|
-
//
|
|
330
|
-
|
|
416
|
+
// Only merge with write access to upstream (write, maintain or admin).
|
|
417
|
+
// The fork's owner often doesn't have it, so the gh account that owns
|
|
418
|
+
// upstream is tried first when there is one.
|
|
419
|
+
const owner = accountFor(target.baseOwner);
|
|
420
|
+
let merger = owner || opener;
|
|
421
|
+
let env = envFor(merger);
|
|
422
|
+
let allowed = canPush(target.repo, env);
|
|
423
|
+
if (!allowed && merger !== opener) {
|
|
424
|
+
merger = opener;
|
|
425
|
+
env = envFor(merger);
|
|
426
|
+
allowed = canPush(target.repo, env);
|
|
427
|
+
}
|
|
428
|
+
if (!allowed) {
|
|
429
|
+
console.log(yellow(`• No write access to ${target.repo}, so the pull request stays open for a maintainer to merge.`));
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
331
432
|
if (merger && merger !== opener) {
|
|
332
433
|
console.log(cyan(`• Merging with GitHub account "${merger}" (owner of upstream)`));
|
|
333
434
|
}
|
|
334
|
-
const env = envFor(merger);
|
|
335
435
|
|
|
336
436
|
console.log(cyan(`› gh pr merge ${url} --merge`));
|
|
337
437
|
let merged;
|
|
@@ -376,7 +476,7 @@ function multiCommit() {
|
|
|
376
476
|
|
|
377
477
|
// ---------- main ----------
|
|
378
478
|
function main() {
|
|
379
|
-
const { branch, message, pr, merge } = parseArgs(process.argv.slice(2));
|
|
479
|
+
const { branch, message, pr, merge, chunks } = parseArgs(process.argv.slice(2));
|
|
380
480
|
const remote = 'origin';
|
|
381
481
|
|
|
382
482
|
if (git(['rev-parse', '--is-inside-work-tree'], { quiet: true }).status !== 0) {
|
|
@@ -397,13 +497,19 @@ function main() {
|
|
|
397
497
|
if (pr) target = pullRequestTarget(remoteUrl.stdout.trim());
|
|
398
498
|
}
|
|
399
499
|
|
|
400
|
-
|
|
500
|
+
if (multi && chunks) console.log(yellow('⚠ -c is ignored for git-multi-commit.'));
|
|
401
501
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
console.log(yellow('• Nothing new to commit, pushing existing commits.'));
|
|
502
|
+
if (chunks && !multi) {
|
|
503
|
+
if (!commitInChunks(message)) console.log(yellow('• Nothing new to commit, pushing existing commits.'));
|
|
405
504
|
} else {
|
|
406
|
-
step(['
|
|
505
|
+
step(['add', '.']);
|
|
506
|
+
|
|
507
|
+
// "git diff --cached --quiet" exits 0 when nothing is staged.
|
|
508
|
+
if (git(['diff', '--cached', '--quiet'], { quiet: true }).status === 0) {
|
|
509
|
+
console.log(yellow('• Nothing new to commit, pushing existing commits.'));
|
|
510
|
+
} else {
|
|
511
|
+
step(['commit', '-m', message || randomMessage()]);
|
|
512
|
+
}
|
|
407
513
|
}
|
|
408
514
|
|
|
409
515
|
if (multi) {
|
package/lib/messages.js
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// Edit this list freely — acp picks one at random for each commit.
|
|
4
|
-
module.exports = [
|
|
5
|
-
'Update project files',
|
|
6
|
-
'Minor improvements',
|
|
7
|
-
'Small fixes and tweaks',
|
|
8
|
-
'Code cleanup',
|
|
9
|
-
'Refactor for readability',
|
|
10
|
-
'Work in progress',
|
|
11
|
-
'Quick update',
|
|
12
|
-
'Polish things up',
|
|
13
|
-
'Improve code structure',
|
|
14
|
-
'Fix minor issues',
|
|
15
|
-
'Tidy up the codebase',
|
|
16
|
-
'Incremental progress',
|
|
17
|
-
'Save progress',
|
|
18
|
-
'Routine update',
|
|
19
|
-
'Make small adjustments',
|
|
20
|
-
'Keep moving forward',
|
|
21
|
-
'Improve overall quality',
|
|
22
|
-
'Housekeeping',
|
|
23
|
-
'Sync latest changes',
|
|
24
|
-
'Update content',
|
|
25
|
-
'Various small improvements',
|
|
26
|
-
'Clean up and reorganize',
|
|
27
|
-
'Iterate on existing work',
|
|
28
|
-
'Checkpoint commit',
|
|
29
|
-
'Apply latest changes',
|
|
30
|
-
];
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Edit this list freely — acp picks one at random for each commit.
|
|
4
|
+
module.exports = [
|
|
5
|
+
'Update project files',
|
|
6
|
+
'Minor improvements',
|
|
7
|
+
'Small fixes and tweaks',
|
|
8
|
+
'Code cleanup',
|
|
9
|
+
'Refactor for readability',
|
|
10
|
+
'Work in progress',
|
|
11
|
+
'Quick update',
|
|
12
|
+
'Polish things up',
|
|
13
|
+
'Improve code structure',
|
|
14
|
+
'Fix minor issues',
|
|
15
|
+
'Tidy up the codebase',
|
|
16
|
+
'Incremental progress',
|
|
17
|
+
'Save progress',
|
|
18
|
+
'Routine update',
|
|
19
|
+
'Make small adjustments',
|
|
20
|
+
'Keep moving forward',
|
|
21
|
+
'Improve overall quality',
|
|
22
|
+
'Housekeeping',
|
|
23
|
+
'Sync latest changes',
|
|
24
|
+
'Update content',
|
|
25
|
+
'Various small improvements',
|
|
26
|
+
'Clean up and reorganize',
|
|
27
|
+
'Iterate on existing work',
|
|
28
|
+
'Checkpoint commit',
|
|
29
|
+
'Apply latest changes',
|
|
30
|
+
];
|
package/package.json
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "acp-push",
|
|
3
|
-
"version": "1.4.
|
|
4
|
-
"description": "git add, commit (with a random message) and push in one command: acp",
|
|
5
|
-
"bin": {
|
|
6
|
-
"acp": "bin/acp.js"
|
|
7
|
-
},
|
|
8
|
-
"files": [
|
|
9
|
-
"bin",
|
|
10
|
-
"lib"
|
|
11
|
-
],
|
|
12
|
-
"keywords": [
|
|
13
|
-
"git",
|
|
14
|
-
"cli",
|
|
15
|
-
"add",
|
|
16
|
-
"commit",
|
|
17
|
-
"push",
|
|
18
|
-
"acp"
|
|
19
|
-
],
|
|
20
|
-
"engines": {
|
|
21
|
-
"node": ">=14"
|
|
22
|
-
},
|
|
23
|
-
"license": "MIT"
|
|
24
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "acp-push",
|
|
3
|
+
"version": "1.4.1",
|
|
4
|
+
"description": "git add, commit (with a random message) and push in one command: acp",
|
|
5
|
+
"bin": {
|
|
6
|
+
"acp": "bin/acp.js"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"bin",
|
|
10
|
+
"lib"
|
|
11
|
+
],
|
|
12
|
+
"keywords": [
|
|
13
|
+
"git",
|
|
14
|
+
"cli",
|
|
15
|
+
"add",
|
|
16
|
+
"commit",
|
|
17
|
+
"push",
|
|
18
|
+
"acp"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=14"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT"
|
|
24
|
+
}
|