repown 0.2.0 → 0.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/CHANGELOG.md CHANGED
@@ -7,6 +7,61 @@ commands. How a release is cut: [docs/RELEASING.md](docs/RELEASING.md).
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.4.0] - 2026-09-30
11
+
12
+ ### Added
13
+
14
+ - `repown reauthor [--yes]`: gives this branch's unpushed commits by another address
15
+ the pinned identity. It fetches the push destination first, rewrites only commits
16
+ no remote has (from the oldest one by another address), refuses on a merge in that
17
+ range, uncommitted changes, identity overrides or a destination it can't fetch,
18
+ runs without hooks, keeps a backup ref, prints the undo, and never pushes (ADR-026).
19
+ - `repown setup --repoint` and `--fetch`, which Recommended answers Yes where they
20
+ apply. Repoint: where the branch pushes to a URL naming the same repository as a
21
+ remote, setup points it back at that remote (`git config --local <key> <remote>`);
22
+ the URL, which can carry a token, is never printed. Fetch: where commits by another
23
+ address wait behind a remote never fetched, setup fetches it first (prompts off),
24
+ so the count and the rebase advice are right; a failed fetch is a warning (ADR-026).
25
+ - `repown setup --reauthor`: where unpushed commits carry another address, setup
26
+ asks (default No, in Recommended too) whether to re-author them as the account,
27
+ and runs `repown reauthor --yes` as its last step. Like `fix`, Enter at the review
28
+ is Decline and at the step is Skip. An already set-up clone offers **Re-author
29
+ them** on its first screen (ADR-026).
30
+
31
+ ### Fixed
32
+
33
+ - `repown use` and setup's review no longer advise `git rebase --root` for commits a
34
+ never-fetched remote may already have. Where the branch's push destination has no
35
+ remote-tracking refs, pushes elsewhere than it fetches from, or is a URL or a name with no
36
+ remote, they say so, say how to fetch and count again where there is a remote to fetch,
37
+ and offer the rebase only for when the destination has none of those commits. The URL
38
+ is never printed (ADR-025).
39
+ - `repown setup` no longer says `done` when the next push would fail. It names what is in the way, first among the review's notes, on the already-set-up screen, and in its closing line (`set up for <account>; the next push will fail: …`): commits the guard will refuse, a sign-in in the push URL, identity or token variables, `author.email` in config, a destination owner, a diverged branch, a missing upstream, a detached HEAD (ADR-026).
40
+ - With the guard off, setup and status still name commits by another address, a destination owner and `GH_TOKEN`, but as a warning (`the guard is off, so …`): they no longer stop `done` or `ready` (ADR-026).
41
+ - `repown status` no longer says `ready` when the next push would fail. It prints what is in the way first among its warnings (`WARN push`), and ends `the next push will fail: …`; the exit code is unchanged (ADR-026).
42
+ - The re-author advice says `or pin that address` only when one address made them all.
43
+ - The suggested `git rebase` starts at the parent of the oldest commit by another address,
44
+ not the oldest unpushed one, so your own commits before it are never rewritten.
45
+
46
+ ## [0.3.0] - 2026-09-29
47
+
48
+ ### Changed
49
+
50
+ - In a terminal, bare `repown` outside a clone, or in a bare repository, opens a
51
+ start screen. It shows the accounts on this machine, whether gh serves git's
52
+ credentials, and the clones found up to 2 levels below, then offers the next
53
+ command. Nothing changes until you pick one. Quit exits 0; Esc or Ctrl-C exits
54
+ 130. With stdout redirected, a clone still prints status, and outside a clone it
55
+ still prints help (exit 0). Without a terminal on stdin or stderr, it is still
56
+ status.
57
+ - The start screen draws its summary inside its frame, and closes that frame with
58
+ the command it is about to run. Show help closes with `> repown --help`, then
59
+ prints the help. Inside a clone, bare `repown` draws its opening sentence inside
60
+ setup's frame. Typing `<` at Record an account's login question returns to the menu.
61
+ - The start screen looks inside a directory whose `.git` git refuses, with the levels
62
+ it has left, and does not search a bare repository for clones. Account names, and
63
+ the folder in the title, show control characters as escapes.
64
+
10
65
  ## [0.2.0] - 2026-09-28
11
66
 
12
67
  ### Added
@@ -86,7 +141,9 @@ commands. How a release is cut: [docs/RELEASING.md](docs/RELEASING.md).
86
141
  contract for scripts.
87
142
  - Runs on Node 20+ on Windows, macOS and Linux, with zero runtime dependencies.
88
143
 
89
- [Unreleased]: https://github.com/makubexD/repown/compare/v0.2.0...HEAD
144
+ [Unreleased]: https://github.com/makubexD/repown/compare/v0.4.0...HEAD
145
+ [0.4.0]: https://github.com/makubexD/repown/compare/v0.3.0...v0.4.0
146
+ [0.3.0]: https://github.com/makubexD/repown/compare/v0.2.0...v0.3.0
90
147
  [0.2.0]: https://github.com/makubexD/repown/compare/v0.1.1...v0.2.0
91
148
  [0.1.1]: https://github.com/makubexD/repown/compare/v0.1.0...v0.1.1
92
149
  [0.1.0]: https://github.com/makubexD/repown/releases/tag/v0.1.0
package/README.md CHANGED
@@ -84,7 +84,7 @@ npm install -g repown
84
84
  - **Update** with `npm install -g repown@latest`; guarded clones keep working
85
85
  ([ADR-003](docs/decisions/ADR-003-hook-calls-installed-cli.md)).
86
86
  - **What gets installed:** one optional dependency, for the arrow-key prompts (without it,
87
- `setup` asks with numbered choices), pinned exactly
87
+ `repown setup` and the start screen ask with numbered choices), pinned exactly
88
88
  ([ADR-016](docs/decisions/ADR-016-clack-for-the-setup-wizard.md)). The package is
89
89
  published from CI with provenance ([how](SECURITY.md#how-the-package-is-published)).
90
90
 
@@ -109,11 +109,14 @@ repown # in a terminal: starts the guided setup of this clone
109
109
  | Question | Recommended | Step by step | Flag |
110
110
  | --- | --- | --- | --- |
111
111
  | Which account should this clone belong to? | Asked. `repown setup <account>` skips the mode question and uses Recommended, so a recorded account goes straight to the review when nothing else must be asked. A new account also asks where it's hosted, the name and the email. | Asked | `<account>`, `--host`, `--name`, `--email` |
112
+ | Push through the remote instead of a URL? Asked when the branch pushes to a URL (which can carry its own sign-in) that names the same repository as a remote here. The review shows the key and the remote, never the URL. | Answers Yes without asking | Asked, default Yes | `--repoint` |
113
+ | Fetch the remote first? Asked when commits by another address wait behind a remote this clone has never fetched, so repown can count which it already has. Prompts are off; a failed fetch is a warning. | Answers Yes without asking | Asked, default Yes | `--fetch` |
112
114
  | Let this clone push to origin's owner? Asked when origin belongs to someone else, like an organisation. | Asked, default Yes. No means the guard refuses pushes there. | Asked, default Yes | `--allow-owner <owner>` |
113
115
  | Turn the guard on? | Answers Yes without asking | Asked | `--guard` |
114
116
  | Push branches without -u? Only on git 2.37+. It sets `push.autoSetupRemote` in this clone only; the guard still checks that first push. | Answers Yes without asking | Asked, default Yes | `--auto-upstream` |
115
117
  | gh. "Switch gh too" makes `gh pr create` match. | Switches without asking when gh already lists the account and the clone is not already pinned to it. A sign-in, which opens a browser, is asked, default No. Skips the question when the clone is already pinned to that account. | Asked | `--gh` |
116
118
  | Take git's sign-ins back from gh? Machine-wide. | Asked, default No. Answer Yes if `repown doctor` said gh is the helper, or pushes from your other account's clones fail. | Asked, default No | `--fix` |
119
+ | Re-author your unpushed commits by another address as this account? Asked when commits you made before pinning (or an IDE made) carry another email. Only if you made them. It runs last, as `repown reauthor --yes`: it fetches first, rewrites only what no remote has, keeps a backup, and never pushes. | Asked, default No. Enter at the review is Decline when this is a step. | Asked, default No; Enter at the step is Skip | `--reauthor` |
117
120
 
118
121
  Step by step asks every question: `--step-by-step`.
119
122
  `--no-input` asks nothing ([scripts and CI](docs/CONFIGURATION.md#scripts-and-ci)).
@@ -196,7 +199,8 @@ with no account and no flags, and setup opens on this screen, before any questio
196
199
  **Done** changes nothing. **Use another account** asks which account this clone should
197
200
  use. When gh acts as someone else, a third option is **Sign in to gh as `<account>`** (or
198
201
  **Make `<account>` gh's active account** when gh already lists it) and runs
199
- `repown use <account> --gh`. The upstream line shows the tracked branch (for example
202
+ `repown use <account> --gh`. With unpushed commits by another address, **Re-author them**
203
+ is offered too (`repown reauthor --yes`). The upstream line shows the tracked branch (for example
200
204
  `origin/main`) when there is one, otherwise `set on the first push (push.autoSetupRemote)`
201
205
  when that setting is on, otherwise nothing. It is the same value `repown status` shows,
202
206
  and `repown status` shows all of this without asking. Every screen:
@@ -213,8 +217,9 @@ then commit and push.
213
217
  | | `repown fix [--dry-run] [--yes]` | undo `gh auth setup-git`, so each clone's pinned account is used; shows what it removes, and the undo, first |
214
218
  | Once per clone | `repown setup [<account>]` | guided setup of this clone. Questions, flags and the review: [Quick start](#quick-start). A clone that is already set up opens on that screen |
215
219
  | | `repown use <account> [--gh]` | what setup runs: pin this clone to an account. `--gh` switches gh's active account, or signs the account in to gh when needed (in a terminal). `--name` with `--email` skips the registry, the file where repown remembers accounts |
220
+ | | `repown reauthor [--yes]` | give this branch's unpushed commits by another address your pinned identity, so the guard lets them through. Fetches the push destination first, rewrites only what no remote has, keeps a backup ref, never pushes |
216
221
  | | `repown guard on \| off \| status` | install, remove or show the pre-push hook (bare `repown guard` shows it) |
217
- | Any time | `repown status` | this clone's and this machine's settings, and what to fix. Exits 1 on a problem; warnings alone exit 0 |
222
+ | Any time | `repown status` | this clone's and this machine's settings, and what to fix. It ends `ready: …`, or `the next push will fail: …` with what is in the way. Exits 1 on a problem; warnings alone exit 0 |
218
223
  | | `repown scan [dir...] [--emails] [--depth <n>] [--format json]` | every clone under the folders (default: this one, 3 levels deep): owner, host, identity, guard, and which email domains its history has. Changes nothing |
219
224
  | Rarely | `repown accounts list \| add \| remove` | the accounts this machine knows (bare `repown accounts` lists them; `add` takes `--name`, `--email`, `--host github\|azdo\|generic`; `list --format json` for scripts) |
220
225
  | | `repown off` | unpin this clone, leaving global config alone; warns if the guard is still on |
@@ -223,9 +228,14 @@ then commit and push.
223
228
 
224
229
  | Where | What it does |
225
230
  | --- | --- |
226
- | A terminal, inside a clone (pinned or not) | Starts `repown setup` |
227
- | A terminal, outside a clone | Shows help, exit 0 |
228
- | No terminal, or the output is redirected | Status. Exit 1 outside a clone |
231
+ | A terminal, inside a clone (pinned or not) | Starts `repown setup`. A line inside that screen says the clone isn't set up yet, or that a pinned clone is being checked ([card 5](docs/HOW-IT-WORKS.md#5-check-where-you-are)) |
232
+ | A terminal, outside a clone or in a bare repository | Opens the start screen ([card 14](docs/HOW-IT-WORKS.md#14-outside-a-clone)) |
233
+ | stdout redirected (stdin and stderr still terminals) | Status inside a clone. The top help outside a clone, exit 0 |
234
+ | No terminal (stdin or stderr is not one) | Status. Exit 1 outside a clone |
235
+
236
+ The start screen lists the accounts on this machine and the clones it found, then offers
237
+ the next command. The summary is in the frame. Picking a command closes the frame with
238
+ that command. Nothing changes until you pick one.
229
239
 
230
240
  **Who is this clone?** `repown status` prints:
231
241
 
package/dist/cli.js CHANGED
@@ -6,28 +6,20 @@
6
6
  // type as `gh auth switch`, or it will not be typed. Everything longer than a
7
7
  // word is a command you run once per clone or once per machine.
8
8
  //
9
- // This file only DECLARES the program: its commands, its version and its top
10
- // help. With no arguments, `chooseDefault` loads commands/start.ts and picks
11
- // setup, status or the top help; any argument skips that. Dispatch itself --
9
+ // This file only DECLARES the program: its version and its top help. The command
10
+ // table lives in program.ts, so the start screen can print that help without
11
+ // importing this file -- importing this module runs repown. With no arguments,
12
+ // `chooseDefault` loads commands/start.ts and picks setup, the start screen,
13
+ // status or the top help; any argument skips that. Dispatch itself --
12
14
  // resolving a command, intercepting `--help`/`-h`/`help` before a command's
13
15
  // own `run()` ever sees the arguments, and exiting 2 on a usage error -- lives
14
16
  // in ui/dispatch.ts. Parsing the arguments lives in ui/args.ts, which a command
15
- // can import without pulling in this file -- importing this module runs repown.
17
+ // can import without pulling in this file.
16
18
  import { readFileSync } from 'node:fs';
17
19
  import { fileURLToPath } from 'node:url';
20
+ import { COMMANDS } from "./program.js";
18
21
  import { renderTopHelp } from "./ui/help.js";
19
22
  import { start } from "./ui/dispatch.js";
20
- const COMMANDS = {
21
- status: async () => (await import("./commands/status.js")).default,
22
- use: async () => (await import("./commands/use.js")).default,
23
- off: async () => (await import("./commands/off.js")).default,
24
- doctor: async () => (await import("./commands/doctor.js")).default,
25
- fix: async () => (await import("./commands/fix.js")).default,
26
- guard: async () => (await import("./commands/guard.js")).default,
27
- accounts: async () => (await import("./commands/accounts.js")).default,
28
- scan: async () => (await import("./commands/scan.js")).default,
29
- setup: async () => (await import("./commands/setup.js")).default,
30
- };
31
23
  function version() {
32
24
  const path = fileURLToPath(new URL('../package.json', import.meta.url));
33
25
  return JSON.parse(readFileSync(path, 'utf8')).version;
@@ -0,0 +1,64 @@
1
+ // `repown reauthor`: give this branch's unpushed commits by another address the pinned
2
+ // identity, so the guard lets them through. It fetches the push destination first, rewrites
3
+ // only commits no remote has, keeps a backup ref, and never pushes (ADR-026).
4
+ import { planReauthor, applyReauthor } from "../core/reauthor.js";
5
+ import { confirm, interactive } from "../ui/prompt.js";
6
+ import { flagBool, gitFor } from "../ui/args.js";
7
+ import * as out from "../ui/format.js";
8
+ import { printable } from "../ui/format.js";
9
+ export default {
10
+ summary: 'rewrite this branch\'s unpushed commits by another address as the pinned account',
11
+ options: [
12
+ { name: 'yes', kind: 'boolean', help: 'rewrite without asking to confirm' },
13
+ ],
14
+ examples: ['repown reauthor', 'repown reauthor --yes'],
15
+ async run(args) {
16
+ const git = gitFor(args);
17
+ const email = await git.getConfig('user.email', 'local') ?? '';
18
+ out.detail('fetching the push destination first, so only what it lacks is rewritten');
19
+ const planned = await planReauthor(git, email, interactive());
20
+ if (!planned.ok) {
21
+ out.fail('reauthor', printable(planned.error));
22
+ return 1;
23
+ }
24
+ if (!planned.value) {
25
+ out.pass('reauthor', 'nothing to re-author: every unpushed commit here is by ' + printable(email));
26
+ return 0;
27
+ }
28
+ preview(planned.value, email);
29
+ if (!await confirmed(args))
30
+ return 1;
31
+ return apply(git, planned.value, email);
32
+ },
33
+ };
34
+ function preview(plan, email) {
35
+ const noun = plan.count === 1 ? '1 commit' : plan.count + ' commits';
36
+ out.detail(noun + ' on ' + printable(plan.branch) + ' that ' + printable(plan.remote) + ' does not have will carry ' + printable(email) +
37
+ ' (now by ' + plan.addresses.map(printable).join(', ') + ')');
38
+ out.detail('from ' + (plan.base === '--root' ? 'the first commit' : plan.base.slice(0, 12)) + ' on; their author dates are reset, and a backup is kept');
39
+ }
40
+ async function confirmed(args) {
41
+ if (flagBool(args, 'yes'))
42
+ return true;
43
+ if (!interactive()) {
44
+ out.fail('reauthor', 'this rewrites commits, so it needs confirmation.');
45
+ out.detail('re-run with --yes to proceed without being asked.');
46
+ return false;
47
+ }
48
+ if (await confirm('Rewrite these commits?'))
49
+ return true;
50
+ out.warn('reauthor', 'declined; nothing was changed.');
51
+ return false;
52
+ }
53
+ async function apply(git, plan, email) {
54
+ const done = await applyReauthor(git, plan, email);
55
+ if (!done.ok) {
56
+ out.fail('reauthor', printable(done.error));
57
+ return 1;
58
+ }
59
+ const noun = done.value.count === 1 ? '1 commit' : done.value.count + ' commits';
60
+ out.pass('reauthor', noun + ' now by ' + printable(email));
61
+ out.detail('undo: git reset --keep ' + printable(done.value.backup) + ' (drops commits made since)');
62
+ out.detail('nothing was pushed: git push when you are ready');
63
+ return 0;
64
+ }
@@ -65,8 +65,8 @@ export default {
65
65
  * clones launched a thousand; calls then hit exec's timeout, and a killed
66
66
  * `git config --get` reads as "not set" -- rows that looked unpinned but weren't.
67
67
  */
68
- const CONCURRENCY = 6;
69
- async function mapLimited(items, limit, work) {
68
+ export const CONCURRENCY = 6;
69
+ export async function mapLimited(items, limit, work) {
70
70
  const results = new Array(items.length);
71
71
  let next = 0;
72
72
  const worker = async () => {
@@ -96,8 +96,12 @@ function invalidRoot(root) {
96
96
  return 'not a directory: ' + root;
97
97
  return null;
98
98
  }
99
- /** A directory that cannot be listed is reported, never silently left out of the audit. */
100
- async function discover(at, depth, unreadable) {
99
+ /**
100
+ * A directory that cannot be listed is reported, never silently left out of the audit.
101
+ * `skip`, when given, drops those child names before they are entered. Scan does not pass one,
102
+ * so its walk is unchanged; the start screen passes the extra names it ignores.
103
+ */
104
+ export async function discover(at, depth, unreadable, skip) {
101
105
  if (depth < 0 || !existsSync(at.path))
102
106
  return [];
103
107
  if (existsSync(join(at.path, '.git')))
@@ -111,11 +115,14 @@ async function discover(at, depth, unreadable) {
111
115
  return [];
112
116
  }
113
117
  const directories = entries
114
- .filter((entry) => entry.isDirectory() && entry.name !== 'node_modules')
118
+ .filter((entry) => entry.isDirectory() && entry.name !== 'node_modules' && !skip?.(entry.name))
115
119
  .map((entry) => join(at.path, entry.name));
116
- const nested = await Promise.all(directories.map((child) => discover({ root: at.root, path: child }, depth - 1, unreadable)));
120
+ const nested = await Promise.all(directories.map((child) => discover(childAt(at.root, child), depth - 1, unreadable, skip)));
117
121
  return nested.flat();
118
122
  }
123
+ function childAt(root, path) {
124
+ return { root, path };
125
+ }
119
126
  /**
120
127
  * A mirror branch, where configured, is someone else's history passing through
121
128
  * this clone. Counting it reports their contributors, not this clone's
@@ -2,7 +2,7 @@
2
2
  // how it should work: Recommended fills in the guard, upstream and a gh switch, and
3
3
  // still asks when origin belongs to someone else. Step by step asks each one.
4
4
  // It shows the commands its answers stand for, and runs exactly those --
5
- // `accounts add`, `use`, `guard on`, `fix` -- so anything it
5
+ // `accounts add`, `use`, `guard on`, `fix`, `reauthor`, and a few git lines -- so anything it
6
6
  // does can be done, or scripted, without it (src/wizard/setup-run.ts).
7
7
  import { providers } from "../core/hosts/index.js";
8
8
  import { interactive } from "../ui/prompt.js";
@@ -19,6 +19,9 @@ export default {
19
19
  { name: 'guard', kind: 'boolean', help: 'turn the push guard on (guard on)' },
20
20
  { name: 'auto-upstream', kind: 'boolean', help: 'push branches without -u: the first push sets the upstream, in this clone only (push.autoSetupRemote)' },
21
21
  { name: 'fix', kind: 'boolean', help: 'stop gh being the credential helper, where it is (fix --yes)' },
22
+ { name: 'repoint', kind: 'boolean', help: 'where the branch pushes to a URL naming the same repository as a remote, push through that remote instead (git config --local)' },
23
+ { name: 'fetch', kind: 'boolean', help: 'fetch the push destination first, where commits by another address wait behind a remote never fetched (prompts off)' },
24
+ { name: 'reauthor', kind: 'boolean', help: 'rewrite this branch\'s unpushed commits by another address as the account, last (reauthor --yes)' },
22
25
  { name: 'step-by-step', kind: 'boolean', help: 'ask every question instead of the recommended answers (not with --no-input)' },
23
26
  { name: 'no-input', kind: 'boolean', help: 'ask nothing: a question not given as a flag is No; exit 2 if the account is incomplete' },
24
27
  ],
@@ -2,37 +2,55 @@
2
2
  // arguments were typed, so `repown guard check` never imports it.
3
3
  //
4
4
  // Setup starts when stdin, stderr and stdout are all terminals, in any clone,
5
- // pinned or not. A terminal that isn't a clone, including a bare repository,
6
- // prints the top help. Everything else is status.
5
+ // pinned or not. It is a runner, so setup loads only then, and the lead line
6
+ // is drawn inside setup's frame rather than before it. Those same terminals
7
+ // outside a clone, including a bare repository, open the start screen: a
8
+ // runner too. With stdout redirected outside a clone, the top help.
9
+ // Everything else is status.
7
10
  import { Git } from "../core/git.js";
8
11
  import { inspectRepo } from "../core/inspect.js";
9
12
  import { identityProblems } from "./status.js";
10
- import * as out from "../ui/format.js";
11
13
  import { interactive } from "../ui/prompt.js";
12
- const SETUP_NOTE = 'This clone isn\'t set up yet, so repown is starting setup (repown status shows its settings).';
13
- const PINNED_NOTE = 'Starting setup to check this clone (repown status shows its settings without asking anything).';
14
- /** Setup, status, or the top help. No output: the caller prints the setup line. */
14
+ export const SETUP_NOTE = 'This clone isn\'t set up yet, so repown is starting setup (repown status shows its settings).';
15
+ export const PINNED_NOTE = 'Starting setup to check this clone (repown status shows its settings without asking anything).';
16
+ /** Setup, status, the start screen, or the top help. No output. */
15
17
  export async function chooseStart(deps) {
16
18
  return (await pickStart(deps)).choice;
17
19
  }
18
- /** Real deps for this process, plus the one stderr line when setup starts. */
20
+ /** Real deps for this process. Setup and the start screen are runners: nothing is printed until they are called. */
19
21
  export async function startDefault(deps = realDeps()) {
20
22
  const pick = await pickStart(deps);
21
23
  if (pick.choice === 'setup')
22
- out.note(pick.pinned ? PINNED_NOTE : SETUP_NOTE);
24
+ return () => startSetup(pick.pinned);
25
+ if (pick.choice === 'home')
26
+ return startHome;
23
27
  return pick.choice;
24
28
  }
29
+ function startHome() {
30
+ return import("../wizard/home-run.js").then((mod) => mod.runHome());
31
+ }
32
+ function startSetup(pinned) {
33
+ const lead = pinned ? PINNED_NOTE : SETUP_NOTE;
34
+ return import("../wizard/setup-run.js").then((mod) => mod.runSetup(bareArgs(), { interactive: true, lead }));
35
+ }
36
+ function bareArgs() {
37
+ return { positional: [], flags: new Map() };
38
+ }
25
39
  async function pickStart(deps) {
26
40
  if (!deps.interactive)
27
41
  return { choice: 'status', pinned: false };
28
42
  if (await deps.git.isBare())
29
- return { choice: 'help', pinned: false };
43
+ return { choice: outsideClone(deps.stdoutIsTerminal), pinned: false };
30
44
  const repo = await inspectRepo(deps.git);
31
45
  if (!repo.isRepo)
32
- return { choice: 'help', pinned: false };
46
+ return { choice: outsideClone(deps.stdoutIsTerminal), pinned: false };
33
47
  const pinned = identityProblems(repo).length === 0;
34
48
  return { choice: deps.stdoutIsTerminal ? 'setup' : 'status', pinned };
35
49
  }
50
+ /** A terminal outside a clone opens the start screen. Redirected stdout still prints the top help. */
51
+ function outsideClone(stdoutIsTerminal) {
52
+ return stdoutIsTerminal ? 'home' : 'help';
53
+ }
36
54
  function realDeps() {
37
55
  return { interactive: interactive(), stdoutIsTerminal: process.stdout.isTTY === true, git: new Git(process.cwd()) };
38
56
  }
@@ -11,7 +11,9 @@
11
11
  // All three are printed, because the one that is invisible is the one that
12
12
  // catches people out. gh's active account is a note: it never affects the push.
13
13
  // A gh that could not be queried stays a warning, because that answer is unknown.
14
- // This reports; it changes nothing.
14
+ // What would stop the next push (blockers.ts, the list setup reads too) comes first among
15
+ // the warnings, and `ready:` is said only without it (ADR-026). It is still a warning:
16
+ // the exit code is unchanged (ADR-020). This reports; it changes nothing.
15
17
  import { inspectRepo, inspectAuth, activeAccountLabel } from "../core/inspect.js";
16
18
  import { isPinned } from "../core/identity.js";
17
19
  import { loadRegistry } from "../core/registry.js";
@@ -20,6 +22,10 @@ import { existsSync } from 'node:fs';
20
22
  import { join } from 'node:path';
21
23
  import { gitFor } from "../ui/args.js";
22
24
  import * as out from "../ui/format.js";
25
+ import { printable } from "../ui/format.js";
26
+ import { blockers } from "../core/blockers.js";
27
+ import { readPushFacts } from "../core/push-state.js";
28
+ import { readUnpushed } from "../core/unpushed.js";
23
29
  export default {
24
30
  summary: 'this clone\'s and this machine\'s settings, and what to fix (bare `repown` when output isn\'t a terminal)',
25
31
  async run(args) {
@@ -34,10 +40,12 @@ export default {
34
40
  const registry = await loadRegistry();
35
41
  await summary(repo, auth, registry);
36
42
  const problems = collectProblems(repo, auth);
37
- const warnings = await reportWarnings(repo, auth, registry);
43
+ const account = accountShown(repo, registry);
44
+ const found = problems.length === 0 ? await pushBlockers(repo, account) : [];
45
+ const warnings = await reportWarnings(repo, auth, registry, found);
38
46
  const code = verdict(repo, auth, problems);
39
47
  const ghNote = printGhNote(pinnedAccount(repo), auth);
40
- closeStatus({ repo, problems, warnings, account: accountShown(repo, registry), ghNote });
48
+ closeStatus({ repo, problems, warnings, blockers: found, account, ghNote });
41
49
  return code;
42
50
  },
43
51
  };
@@ -169,12 +177,39 @@ function matchesRecord(repo, entry) {
169
177
  const id = repo.identity;
170
178
  return id.name === entry.name && id.email?.toLowerCase() === entry.email.toLowerCase();
171
179
  }
180
+ /** Read only when status finds no problem: otherwise the problem comes first. */
181
+ async function pushBlockers(repo, account) {
182
+ const facts = await readPushFacts(repo.git, await readUnpushed(repo.git));
183
+ const guarded = repo.guard !== 'off' || repo.hook?.redirected === true;
184
+ // The upstream field already says how to push a branch without one: not a warning here.
185
+ return blockers(facts, { email: repo.identity.email ?? '', account, autoUpstream: true, guarded });
186
+ }
187
+ /** First among the warnings. An owner the origin warning names is not said twice. */
188
+ function printBlockers(found, ownerWarned) {
189
+ for (const blocker of found) {
190
+ if (blocker.kind === 'owner' && ownerWarned)
191
+ continue;
192
+ const [first = '', ...rest] = blocker.lines;
193
+ out.warn('push', printable(first));
194
+ for (const line of rest)
195
+ out.detail(printable(line));
196
+ }
197
+ }
172
198
  function closeStatus(end) {
173
- const line = end.problems.length === 0
174
- ? readyLine(end.repo, end.account, end.warnings, end.ghNote)
175
- : tally(end.problems.length, end.warnings.length) + setupHint(end.repo);
176
199
  out.note('');
177
- out.note(line);
200
+ out.note(closingLine(end));
201
+ }
202
+ function closingLine(end) {
203
+ if (end.problems.length > 0)
204
+ return tally(end.problems.length, end.warnings.length) + setupHint(end.repo);
205
+ const blocking = end.blockers.filter((blocker) => blocker.blocks);
206
+ const [first] = blocking;
207
+ // What blocks nothing (the guard is off) is still a warning, and counted as one.
208
+ const quiet = end.blockers.filter((blocker) => !blocker.blocks).map(() => 'push');
209
+ if (!first)
210
+ return readyLine(end.repo, end.account, [...quiet, ...end.warnings], end.ghNote);
211
+ const more = blocking.length > 1 ? ' (and ' + (blocking.length - 1) + ' more above)' : '';
212
+ return 'the next push will fail: ' + printable(first.summary) + more;
178
213
  }
179
214
  const GH_OPTIONAL = ' · gh: optional (see the note above)';
180
215
  function readyLine(repo, account, warnings, ghNote) {
@@ -223,10 +258,12 @@ export function identityProblems(repo) {
223
258
  fix: SETUP_FIX,
224
259
  }];
225
260
  }
226
- async function reportWarnings(repo, auth, registry) {
261
+ async function reportWarnings(repo, auth, registry, found) {
227
262
  const tags = ['origin', 'account', 'guard', 'submodule', 'helper', 'gh'];
263
+ const foreignOwner = await originForeign(repo);
264
+ printBlockers(found, foreignOwner);
228
265
  const fired = [
229
- await ownerWarning(repo),
266
+ foreignOwner && ownerWarning(repo),
230
267
  accountWarning(repo, registry),
231
268
  guardWarning(repo),
232
269
  submoduleWarning(repo),
@@ -285,9 +322,12 @@ function submoduleWarning(repo) {
285
322
  * An organisation is never an account name, so a bare owner-vs-account
286
323
  * comparison warns on every org repository -- which is most of them at work.
287
324
  */
288
- async function ownerWarning(repo) {
325
+ async function originForeign(repo) {
289
326
  const allowed = await allowedOwners(repo.git, repo.identity.owner ?? repo.identity.account);
290
- if (!repo.owner || allowed.length === 0 || allowed.includes(repo.owner.toLowerCase()))
327
+ return !!repo.owner && allowed.length > 0 && !allowed.includes(repo.owner.toLowerCase());
328
+ }
329
+ function ownerWarning(repo) {
330
+ if (!repo.owner)
291
331
  return false;
292
332
  out.warn('origin', 'origin belongs to "' + repo.owner + '", which is not an owner this clone pushes to.');
293
333
  out.detail('if that is an organisation you belong to:');
@@ -248,7 +248,7 @@ async function switchCli(account) {
248
248
  }
249
249
  /** Everything that is now true but not yet right. Warnings, never refusals. */
250
250
  async function reportConcerns(account, repo, email) {
251
- await reportUnpushed(repo, email);
251
+ await reportUnpushed(repo, email, account);
252
252
  if (repo.credentialKeys.length === 0 && repo.url) {
253
253
  out.warn('host', repo.provider.label + ' credentials are not pinned by repown.');
254
254
  out.detail('commits are pinned and the guard still runs; only credential');
@@ -262,8 +262,8 @@ async function reportConcerns(account, repo, email) {
262
262
  }
263
263
  }
264
264
  /** A warning only. Rewriting the commits stays the owner's call (ADR-013). */
265
- async function reportUnpushed(repo, email) {
266
- const [first, ...rest] = unpushedLines(await readUnpushed(repo.git), email);
265
+ async function reportUnpushed(repo, email, account) {
266
+ const [first, ...rest] = unpushedLines(await readUnpushed(repo.git), email, account);
267
267
  if (!first)
268
268
  return;
269
269
  out.warn('commits', first);
@@ -0,0 +1,132 @@
1
+ // What will make the next commit, pull or push fail in this clone, from facts read once.
2
+ //
3
+ // Pure: setup's review and settled screen, its closing line and `repown status` all
4
+ // read the same list, so none of them can say "done" or "ready" over a push the guard
5
+ // (or git) is about to refuse. A fact that could not be read is a blocker of its own,
6
+ // never an absence (a skipped check must not look passed). What only the guard would
7
+ // refuse is still said with the guard off, as a warning that blocks nothing.
8
+ import { unpushedLines, foreignCount } from "./unpushed.js";
9
+ import { shellWord } from "./guard/check.js";
10
+ const TOKENS = ['GH_TOKEN', 'GITHUB_TOKEN'];
11
+ export function blockers(facts, choice) {
12
+ const branch = facts.unpushed.branch ?? 'HEAD';
13
+ return [
14
+ ...unpushedBlocker(facts.unpushed, choice),
15
+ ...elsewhereBlocker(facts.elsewhere, facts.destination, branch, choice),
16
+ ...signinBlocker(facts, branch, choice.account),
17
+ ...facts.env.map((name) => envBlocker(name, choice)),
18
+ ...facts.configOverrides.map((key) => configBlocker(key, choice.email)),
19
+ ...ownerBlocker(facts.destination, branch, choice),
20
+ ...divergenceBlocker(facts.divergence, branch),
21
+ ...upstreamBlocker(facts, branch, choice.autoUpstream),
22
+ ...(facts.detached ? [detachedBlocker()] : []),
23
+ ];
24
+ }
25
+ /** What the guard does with them: refuses, or, while it is off, lets them through. */
26
+ function guardWord(guarded, many) {
27
+ if (guarded)
28
+ return 'the guard will refuse ' + (many ? 'them' : 'it');
29
+ return 'the guard is off, so ' + (many ? 'they push as they are' : 'it pushes as it is');
30
+ }
31
+ function unpushedBlocker(fact, choice) {
32
+ const lines = unpushedLines(fact, choice.email, choice.account, choice.guarded);
33
+ if (lines.length === 0)
34
+ return [];
35
+ if (!fact.commits.ok)
36
+ return [{ kind: 'unpushed', summary: 'unpushed commits could not be read', lines, blocks: choice.guarded }];
37
+ const count = foreignCount(fact, choice.email);
38
+ return [{ kind: 'unpushed', summary: count + (count === 1 ? ' commit' : ' commits') + ' by another address', lines, blocks: choice.guarded }];
39
+ }
40
+ /** Where a remote names the same repository, the fix is setup's repoint. */
41
+ function signinBlocker(facts, branch, account) {
42
+ const key = facts.signinKey;
43
+ if (!key)
44
+ return [];
45
+ const fix = facts.repoint?.key === key ? ': point it back at ' + facts.repoint.remote + ' with repown setup --repoint' : '';
46
+ return [{
47
+ kind: 'signin',
48
+ summary: 'the branch pushes with its own sign-in',
49
+ lines: [key + ' carries its own sign-in, so pushes from ' + branch + ' use it, not ' + account + fix],
50
+ blocks: true,
51
+ }];
52
+ }
53
+ /** Counted with the guard's own exclusion, so a fork's upstream commits are not missed. */
54
+ function elsewhereBlocker(elsewhere, destination, branch, choice) {
55
+ if (!elsewhere.ok) {
56
+ return [{ kind: 'elsewhere', summary: 'commits on other remotes could not be read', lines: ['commits on other remotes could not be read (' + elsewhere.error + ')'], blocks: choice.guarded }];
57
+ }
58
+ const count = foreignCount({ branch, commits: elsewhere, unknown: null }, choice.email);
59
+ if (count === 0)
60
+ return [];
61
+ const where = destination?.remote ?? 'the destination';
62
+ const noun = count === 1 ? '1 commit' : count + ' commits';
63
+ return [{
64
+ kind: 'elsewhere',
65
+ summary: noun + ' from another remote',
66
+ lines: [noun + ' on ' + branch + ' by another address ' + (count === 1 ? 'is' : 'are') + ' on another remote but not on ' + where +
67
+ ': ' + guardWord(choice.guarded, count > 1) + ' (a fork mirroring upstream? see repown.mirrorBranch)'],
68
+ blocks: choice.guarded,
69
+ }];
70
+ }
71
+ function configBlocker(key, email) {
72
+ return {
73
+ kind: 'config',
74
+ summary: key + ' is set',
75
+ lines: [key + " is set in git config: commits made here won't use " + email + ' (unset it: git config --unset ' + key + ')'],
76
+ blocks: true,
77
+ };
78
+ }
79
+ /** An identity variable still changes every commit; a token only matters to the guard. */
80
+ function envBlocker(name, choice) {
81
+ const token = TOKENS.includes(name);
82
+ const identity = token ? '' : ": commits made here won't use " + choice.email;
83
+ const guard = choice.guarded ? 'the guard refuses every push while it is set'
84
+ : token ? 'the guard is off, so it stops nothing now, but with the guard on every push is refused' : '';
85
+ const joiner = !guard ? '' : identity ? ', and ' : ': ';
86
+ return {
87
+ kind: 'env',
88
+ summary: name + ' is set',
89
+ lines: [name + ' is set in this shell' + identity + joiner + guard + ' (unset it)'],
90
+ blocks: choice.guarded || !token,
91
+ };
92
+ }
93
+ function ownerBlocker(destination, branch, choice) {
94
+ if (!destination)
95
+ return [];
96
+ const owner = destination.owner.toLowerCase();
97
+ if (owner === choice.account.toLowerCase() || destination.allowed.includes(owner))
98
+ return [];
99
+ const where = destination.remote ?? 'its URL';
100
+ const guard = choice.guarded ? 'the guard will refuse it' : 'the guard is off, so it pushes there anyway';
101
+ return [{
102
+ kind: 'owner',
103
+ summary: 'the push goes to "' + destination.owner + '"',
104
+ lines: [branch + ' pushes to ' + where + ', owned by "' + destination.owner + '", not ' + choice.account +
105
+ ': ' + guard + '. If you belong there: git config --local --add repown.allowOwner ' + shellWord(destination.owner)],
106
+ blocks: choice.guarded,
107
+ }];
108
+ }
109
+ function divergenceBlocker(divergence, branch) {
110
+ if (!divergence.ok) {
111
+ return [{ kind: 'divergence', summary: branch + ' could not be compared with its upstream', lines: [branch + ' could not be compared with its upstream (' + divergence.error + ')'], blocks: true }];
112
+ }
113
+ const found = divergence.value;
114
+ if (!found || found.behind === 0 || found.ahead === 0)
115
+ return [];
116
+ return [{
117
+ kind: 'divergence',
118
+ summary: branch + ' has diverged from ' + found.tracked,
119
+ lines: [branch + ' is ' + found.behind + ' behind and ' + found.ahead + ' ahead of ' + found.tracked +
120
+ ': push and pull fail until you git pull --rebase'],
121
+ blocks: true,
122
+ }];
123
+ }
124
+ function upstreamBlocker(facts, branch, autoUpstream) {
125
+ if (facts.upstream !== 'missing' || autoUpstream)
126
+ return [];
127
+ const remote = shellWord(facts.destination?.remote ?? 'origin');
128
+ return [{ kind: 'upstream', summary: branch + ' has no upstream', lines: [branch + ' has no upstream: the first push needs git push -u ' + remote + ' ' + shellWord(branch)], blocks: true }];
129
+ }
130
+ function detachedBlocker() {
131
+ return { kind: 'detached', summary: 'HEAD is detached', lines: ['HEAD is detached: git pull and a plain git push fail here; git switch to a branch first'], blocks: true };
132
+ }