acp-push 1.4.0 → 1.4.2

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 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,13 @@ 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
48
+ acp -c -ua # 10 commits, each pushed with its own PR, merged right away
46
49
  ```
47
50
 
48
51
  ## Pull requests to upstream
@@ -56,12 +59,12 @@ git push origin HEAD:main
56
59
  gh pr create --repo <upstream owner>/<repo> --head <your account>:main
57
60
  ```
58
61
 
59
- `acp -ua` does the same and then merges it with `gh pr merge --merge`.
62
+ `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
63
 
61
64
  - 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
65
  - The pull request goes into the default branch of `upstream`. Its title is your last commit message.
63
66
  - 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.
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.
65
68
  - If the merge fails, the pull request stays open and `acp` prints its link.
66
69
 
67
70
  ## Multiple GitHub accounts
package/bin/acp.js CHANGED
@@ -23,9 +23,11 @@ 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 the pull request
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. With -u or
30
+ -ua, each commit is pushed with its own pull request
29
31
  -h, --help Show this help
30
32
  -v, --version Show the version
31
33
 
@@ -38,6 +40,8 @@ Pull requests:
38
40
  -u and -ua need the GitHub CLI (gh) and a remote called "upstream". The
39
41
  pull request goes from <branch> on origin (your fork) to the default
40
42
  branch of upstream. If one is already open, the push just updates it.
43
+ -ua only merges if your account can write to upstream (write, maintain
44
+ or admin role). Otherwise the pull request is left open.
41
45
 
42
46
  git-multi-commit:
43
47
  If the repo root has a .git-multi-commit.json file, acp still adds and
@@ -47,10 +51,12 @@ git-multi-commit:
47
51
  Examples:
48
52
  acp ${dim('# push to origin/main')}
49
53
  acp -u ${dim('# push to origin/main, open a PR to upstream')}
50
- acp -ua ${dim('# same, then merge the PR')}
54
+ acp -ua ${dim('# same, then merge the PR if you can')}
51
55
  acp -b dev ${dim('# push to origin/dev')}
52
56
  acp -u --branch fix ${dim('# push to origin/fix, open a PR to upstream')}
53
57
  acp -m "Fix login bug" ${dim('# custom message, push to origin/main')}
58
+ acp -c ${dim('# commit in 10 chunks, then push')}
59
+ acp -c -ua ${dim('# each chunk: push, open a PR, merge it')}
54
60
  `;
55
61
 
56
62
  function fail(message) {
@@ -60,7 +66,7 @@ function fail(message) {
60
66
 
61
67
  // ---------- argument parsing ----------
62
68
  function parseArgs(argv) {
63
- const options = { branch: 'main', message: null, pr: false, merge: false };
69
+ const options = { branch: 'main', message: null, pr: false, merge: false, chunks: false };
64
70
 
65
71
  for (let i = 0; i < argv.length; i++) {
66
72
  let flag = argv[i];
@@ -101,6 +107,10 @@ function parseArgs(argv) {
101
107
  case '--message':
102
108
  options.message = takeValue();
103
109
  break;
110
+ case '-c':
111
+ case '--chunks':
112
+ options.chunks = true;
113
+ break;
104
114
  case '-h':
105
115
  case '--help':
106
116
  console.log(HELP);
@@ -162,6 +172,76 @@ function randomMessage() {
162
172
  return messages[Math.floor(Math.random() * messages.length)];
163
173
  }
164
174
 
175
+ // ---------- chunked commits ----------
176
+ const MAX_CHUNKS = 10;
177
+
178
+ // The changes that are still uncommitted, as a list of units: each hunk of
179
+ // a tracked file's diff, any change without hunks (binary, mode change), and
180
+ // each untracked file as a whole. Units of one file stay in file order.
181
+ function changeUnits() {
182
+ const diff = git(['diff', '--no-color', '--no-renames', '--binary', '-U0'], { quiet: true }).stdout;
183
+ const units = [];
184
+
185
+ for (const section of diff.split(/^(?=diff --git )/m).filter(Boolean)) {
186
+ const at = section.search(/^@@ /m);
187
+ if (at === -1) {
188
+ units.push({ patch: section });
189
+ continue;
190
+ }
191
+ const header = section.slice(0, at);
192
+ for (const hunk of section.slice(at).split(/^(?=@@ )/m)) units.push({ patch: header + hunk });
193
+ }
194
+
195
+ const untracked = git(['ls-files', '-o', '--exclude-standard', '-z'], { quiet: true }).stdout;
196
+ for (const file of untracked.split('\0').filter(Boolean)) units.push({ file });
197
+ return units;
198
+ }
199
+
200
+ function stage(unit) {
201
+ if (unit.file) {
202
+ git(['add', '--', unit.file], { quiet: true });
203
+ return;
204
+ }
205
+ const result = spawnSync('git', ['apply', '--cached', '--unidiff-zero', '--binary'], {
206
+ input: unit.patch,
207
+ encoding: 'utf8',
208
+ });
209
+ if (result.status !== 0) fail(`Could not stage a change: ${(result.stderr || '').trim()}`);
210
+ }
211
+
212
+ // Commits the changes as MAX_CHUNKS commits (fewer if there are fewer separate
213
+ // changes), splitting the individual changes (hunks) as evenly as possible,
214
+ // even within one file. afterCommit runs after each commit. Returns false when there was nothing to commit.
215
+ function commitInChunks(message, afterCommit) {
216
+ // Start from a clean index so each commit holds only its own chunk.
217
+ git(['reset', '-q'], { quiet: true });
218
+
219
+ const total = changeUnits().length;
220
+ if (!total) return false;
221
+
222
+ const count = Math.min(MAX_CHUNKS, total);
223
+ console.log(cyan(`• Splitting ${total} change(s) into ${count} commit(s)`));
224
+
225
+ const pool = [...messages];
226
+ for (let i = 0; i < count; i++) {
227
+ // The diff is read again after every commit, so line numbers stay right.
228
+ const units = changeUnits();
229
+ const take = Math.ceil(units.length / (count - i));
230
+ units.slice(0, take).forEach(stage);
231
+
232
+ let text;
233
+ if (message) {
234
+ text = `${message} (${i + 1}/${count})`;
235
+ } else {
236
+ // Draw without repeats so the log doesn't show the same message twice.
237
+ text = pool.length ? pool.splice(Math.floor(Math.random() * pool.length), 1)[0] : randomMessage();
238
+ }
239
+ step(['commit', '-m', text]);
240
+ if (afterCommit) afterCommit(i + 1, count);
241
+ }
242
+ return true;
243
+ }
244
+
165
245
  // ---------- GitHub account picking (via gh CLI) ----------
166
246
  // Returns { owner, name } for a GitHub remote URL (HTTPS or SSH), or null.
167
247
  function githubRepo(url) {
@@ -284,6 +364,17 @@ function pullRequestTarget(originUrl) {
284
364
  return { repo: `${base.owner}/${base.name}`, baseOwner: base.owner, headOwner: head.owner };
285
365
  }
286
366
 
367
+ // True when the gh account behind `env` has write access to `repo`
368
+ // (the write, maintain or admin role), which is what merging needs.
369
+ function canPush(repo, env) {
370
+ const result = run('gh', ['api', `repos/${repo}`], { quiet: true, env });
371
+ try {
372
+ return JSON.parse(result.stdout).permissions.push === true;
373
+ } catch (_) {
374
+ return false;
375
+ }
376
+ }
377
+
287
378
  function sleep(ms) {
288
379
  Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
289
380
  }
@@ -325,13 +416,25 @@ function pullRequest(target, branch, merge) {
325
416
  }
326
417
  if (!merge) return;
327
418
 
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;
419
+ // Only merge with write access to upstream (write, maintain or admin).
420
+ // The fork's owner often doesn't have it, so the gh account that owns
421
+ // upstream is tried first when there is one.
422
+ const owner = accountFor(target.baseOwner);
423
+ let merger = owner || opener;
424
+ let env = envFor(merger);
425
+ let allowed = canPush(target.repo, env);
426
+ if (!allowed && merger !== opener) {
427
+ merger = opener;
428
+ env = envFor(merger);
429
+ allowed = canPush(target.repo, env);
430
+ }
431
+ if (!allowed) {
432
+ console.log(yellow(`• No write access to ${target.repo}, so the pull request stays open for a maintainer to merge.`));
433
+ return;
434
+ }
331
435
  if (merger && merger !== opener) {
332
436
  console.log(cyan(`• Merging with GitHub account "${merger}" (owner of upstream)`));
333
437
  }
334
- const env = envFor(merger);
335
438
 
336
439
  console.log(cyan(`› gh pr merge ${url} --merge`));
337
440
  let merged;
@@ -376,7 +479,7 @@ function multiCommit() {
376
479
 
377
480
  // ---------- main ----------
378
481
  function main() {
379
- const { branch, message, pr, merge } = parseArgs(process.argv.slice(2));
482
+ const { branch, message, pr, merge, chunks } = parseArgs(process.argv.slice(2));
380
483
  const remote = 'origin';
381
484
 
382
485
  if (git(['rev-parse', '--is-inside-work-tree'], { quiet: true }).status !== 0) {
@@ -397,13 +500,41 @@ function main() {
397
500
  if (pr) target = pullRequestTarget(remoteUrl.stdout.trim());
398
501
  }
399
502
 
400
- step(['add', '.']);
503
+ if (multi && chunks) console.log(yellow('⚠ -c is ignored for git-multi-commit.'));
401
504
 
402
- // "git diff --cached --quiet" exits 0 when nothing is staged.
403
- if (git(['diff', '--cached', '--quiet'], { quiet: true }).status === 0) {
404
- console.log(yellow('• Nothing new to commit, pushing existing commits.'));
505
+ const auth = multi ? null : githubAuth(remoteUrl.stdout.trim()) || {};
506
+
507
+ // "HEAD:<branch>" pushes whatever branch you're on (main, master, anything)
508
+ // to <branch> on the remote, so the default always lands on "main".
509
+ const push = () => {
510
+ step(['push', remote, `HEAD:${branch}`], auth);
511
+ console.log(green(`✔ Pushed to ${remote}/${branch}`));
512
+ };
513
+
514
+ // With -c and -u/-ua, every chunk is pushed and gets its own pull request
515
+ // (merged right away with -ua), instead of one push at the end.
516
+ let pushedPerChunk = false;
517
+
518
+ if (chunks && !multi) {
519
+ const afterCommit = pr
520
+ ? (n, count) => {
521
+ console.log(cyan(`• Chunk ${n}/${count}`));
522
+ push();
523
+ pullRequest(target, branch, merge);
524
+ }
525
+ : undefined;
526
+ const done = commitInChunks(message, afterCommit);
527
+ pushedPerChunk = done && pr;
528
+ if (!done) console.log(yellow('• Nothing new to commit, pushing existing commits.'));
405
529
  } else {
406
- step(['commit', '-m', message || randomMessage()]);
530
+ step(['add', '.']);
531
+
532
+ // "git diff --cached --quiet" exits 0 when nothing is staged.
533
+ if (git(['diff', '--cached', '--quiet'], { quiet: true }).status === 0) {
534
+ console.log(yellow('• Nothing new to commit, pushing existing commits.'));
535
+ } else {
536
+ step(['commit', '-m', message || randomMessage()]);
537
+ }
407
538
  }
408
539
 
409
540
  if (multi) {
@@ -412,13 +543,9 @@ function main() {
412
543
  return;
413
544
  }
414
545
 
415
- const auth = githubAuth(remoteUrl.stdout.trim()) || {};
416
-
417
- // "HEAD:<branch>" pushes whatever branch you're on (main, master, anything)
418
- // to <branch> on the remote, so the default always lands on "main".
419
- step(['push', remote, `HEAD:${branch}`], auth);
420
- console.log(green(`✔ Pushed to ${remote}/${branch}`));
546
+ if (pushedPerChunk) return;
421
547
 
548
+ push();
422
549
  if (pr) pullRequest(target, branch, merge);
423
550
  }
424
551
 
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.0",
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.2",
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
+ }