repown 0.1.0 → 0.2.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/dist/cli.js CHANGED
@@ -7,11 +7,12 @@
7
7
  // word is a command you run once per clone or once per machine.
8
8
  //
9
9
  // This file only DECLARES the program: its commands, its version and its top
10
- // help. Dispatch itself -- resolving a command, intercepting `--help`/`-h`/`help`
11
- // before a command's own `run()` ever sees the arguments, and exiting 2 on a
12
- // usage error -- lives in ui/dispatch.ts. Parsing the arguments lives in
13
- // ui/args.ts, which a command can import without pulling in this file --
14
- // importing this module runs repown.
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 --
12
+ // resolving a command, intercepting `--help`/`-h`/`help` before a command's
13
+ // own `run()` ever sees the arguments, and exiting 2 on a usage error -- lives
14
+ // 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.
15
16
  import { readFileSync } from 'node:fs';
16
17
  import { fileURLToPath } from 'node:url';
17
18
  import { renderTopHelp } from "./ui/help.js";
@@ -25,9 +26,17 @@ const COMMANDS = {
25
26
  guard: async () => (await import("./commands/guard.js")).default,
26
27
  accounts: async () => (await import("./commands/accounts.js")).default,
27
28
  scan: async () => (await import("./commands/scan.js")).default,
29
+ setup: async () => (await import("./commands/setup.js")).default,
28
30
  };
29
31
  function version() {
30
32
  const path = fileURLToPath(new URL('../package.json', import.meta.url));
31
33
  return JSON.parse(readFileSync(path, 'utf8')).version;
32
34
  }
33
- start({ name: 'repown', commands: COMMANDS, defaultCommand: 'status', topHelp: renderTopHelp, version });
35
+ start({
36
+ name: 'repown',
37
+ commands: COMMANDS,
38
+ defaultCommand: 'status',
39
+ chooseDefault: () => import("./commands/start.js").then((mod) => mod.startDefault()),
40
+ topHelp: renderTopHelp,
41
+ version,
42
+ });
@@ -10,9 +10,21 @@
10
10
  // account is not the one the repository asked for. gh serves only its active
11
11
  // account and returns nothing for any other, so the fix is not a new token -- it
12
12
  // is to stop gh being the helper.
13
- import { inspectRepo, inspectAuth, activeAccountLabel, storedAccountsLabel } from "../core/inspect.js";
13
+ //
14
+ // Under that diagnosis, one row per account the registry, GCM or gh knows.
15
+ // accountRows decides the cells. A failed lookup stays `unknown`.
16
+ import { inspectRepo, inspectAuth, activeAccountLabel } from "../core/inspect.js";
17
+ import { loadRegistry } from "../core/registry.js";
14
18
  import { gitFor } from "../ui/args.js";
15
19
  import * as out from "../ui/format.js";
20
+ const DOCTOR_TITLE = 'repown doctor · how this machine signs in to git hosts';
21
+ const NAME_WIDTH = 14;
22
+ const NOT_YET = 'not signed in yet (the first push signs in)';
23
+ const HOST_SIGN_IN = 'your host\'s own sign-in';
24
+ const NONE_YET = 'none recorded, stored or signed in yet';
25
+ const NO_GCM = 'no accounts recorded or signed in to gh; Git Credential Manager isn\'t installed';
26
+ const STORED_UNUSED = 'stored in Git Credential Manager, which git isn\'t using';
27
+ const SETUP_POINTER = 'repown setup';
16
28
  export default {
17
29
  summary: 'what serves credentials on this machine, and to whom',
18
30
  examples: ['repown doctor'],
@@ -20,27 +32,262 @@ export default {
20
32
  const git = gitFor(args);
21
33
  const repo = await inspectRepo(git);
22
34
  const auth = await inspectAuth(git, repo.originUrl ?? undefined);
23
- out.line();
24
- out.field('helper', auth.helper ?? 'none configured', 18);
25
- out.field('stored accounts', storedAccountsLabel(auth), 18);
26
- out.field('gh accounts', ghAccountsLabel(auth), 18);
27
- out.field('gh active', activeAccountLabel(auth), 18);
28
- out.field('GCM', auth.gcmPath ?? 'not found', 18);
29
- out.line();
30
- if (auth.ghIsHelper)
31
- return diagnoseGhHelper(auth);
32
- if (!auth.helperIsGcm)
33
- return diagnoseUnknownHelper(auth);
34
- return diagnoseHealthy(auth, repo.provider.label);
35
+ const warned = printReport(repo, auth, await loadRegistry());
36
+ const found = diagnose(auth, ssoTarget(repo));
37
+ closeDoctor(found.verdict + warningTally(warned));
38
+ return found.code;
35
39
  },
36
40
  };
37
- function ghAccountsLabel(auth) {
41
+ export function accountRows(registry, auth, pinned) {
42
+ const rows = mergeNames(registry, auth).map((item) => toRow(item, auth, pinned));
43
+ return reportOf(rows, registry, auth);
44
+ }
45
+ function reportOf(rows, registry, auth) {
46
+ return {
47
+ rows,
48
+ none: rows.length === 0 && sourcesEmpty(registry, auth),
49
+ gcmMissing: noGcmEmpty(rows, registry, auth),
50
+ warnings: sourceWarnings(registry, auth),
51
+ };
52
+ }
53
+ /** No rows, GCM is absent, and the registry and gh were read and named nobody. */
54
+ function noGcmEmpty(rows, registry, auth) {
55
+ if (rows.length > 0 || auth.gcmPresent)
56
+ return false;
57
+ if (!registry.ok || Object.keys(registry.value.accounts).length > 0)
58
+ return false;
38
59
  if (!auth.ghPresent)
39
- return 'not installed';
60
+ return true;
61
+ return auth.gh.ok && auth.gh.value.accounts.length === 0;
62
+ }
63
+ function mergeNames(registry, auth) {
64
+ const recorded = registry.ok ? recordedNames(registry.value) : [];
65
+ const mark = registry.ok ? 'absent' : 'unknown';
66
+ return [...recorded, ...extraNames(recorded, auth, mark)];
67
+ }
68
+ function recordedNames(registry) {
69
+ return Object.entries(registry.accounts).map(([name, entry]) => ({
70
+ name, entry, recorded: 'recorded',
71
+ }));
72
+ }
73
+ function extraNames(recorded, auth, mark) {
74
+ const seen = new Set(recorded.map((item) => keyOf(item.name)));
75
+ const extras = [...storeNames(auth), ...ghNames(auth)].filter((name) => keep(seen, name));
76
+ extras.sort(byName);
77
+ return extras.map((name) => ({ name, recorded: mark }));
78
+ }
79
+ function keep(seen, name) {
80
+ const key = keyOf(name);
81
+ if (seen.has(key))
82
+ return false;
83
+ seen.add(key);
84
+ return true;
85
+ }
86
+ function storeNames(auth) {
87
+ return auth.stored.ok ? auth.stored.value : [];
88
+ }
89
+ function ghNames(auth) {
90
+ if (!auth.gh.ok)
91
+ return [];
92
+ return auth.gh.value.accounts.map((account) => account.login);
93
+ }
94
+ function byName(left, right) {
95
+ const [a, b] = [keyOf(left), keyOf(right)];
96
+ if (a === b)
97
+ return 0;
98
+ return a < b ? -1 : 1;
99
+ }
100
+ function toRow(item, auth, pinned) {
101
+ return {
102
+ name: item.name,
103
+ thisClone: pinned !== null && same(pinned, item.name),
104
+ recorded: item.recorded,
105
+ git: gitCell(item, auth),
106
+ gh: ghCell(item, auth),
107
+ };
108
+ }
109
+ /**
110
+ * `stored` only when GCM is the helper and holds the account. Held under
111
+ * another helper is named, not called `stored`. "The first push signs in"
112
+ * is GCM's promise, so only then. A missing account is otherwise `unknown`,
113
+ * including a store that could not be read: a failed read must not look
114
+ * like "not signed in".
115
+ */
116
+ function gitCell(item, auth) {
117
+ if (item.entry && !githubHost(item.entry))
118
+ return HOST_SIGN_IN;
119
+ if (!auth.gcmPresent || !auth.stored.ok)
120
+ return 'unknown';
121
+ if (listed(auth.stored.value, item.name))
122
+ return storedHow(auth);
123
+ return auth.helperIsGcm ? NOT_YET : 'unknown';
124
+ }
125
+ function storedHow(auth) {
126
+ return auth.helperIsGcm ? 'stored' : STORED_UNUSED;
127
+ }
128
+ /**
129
+ * `not installed` is stated once, on the machine line (`gh active`). A row
130
+ * that repeated it would hide the git cell. Non-GitHub accounts have no gh
131
+ * cell either: gh does not sign those hosts in (ADR-009).
132
+ */
133
+ function ghCell(item, auth) {
134
+ if ((item.entry && !githubHost(item.entry)) || !auth.ghPresent)
135
+ return null;
40
136
  if (!auth.gh.ok)
41
- return 'unknown -- gh could not be queried';
42
- const logins = auth.gh.value.accounts.map((account) => account.login);
43
- return logins.length > 0 ? logins.join(', ') : 'none';
137
+ return 'unknown';
138
+ return ghReadiness(item.name, auth);
139
+ }
140
+ function ghReadiness(name, auth) {
141
+ if (!auth.gh.ok)
142
+ return 'unknown';
143
+ const login = auth.gh.value.accounts.find((account) => same(account.login, name));
144
+ if (!login)
145
+ return 'not signed in';
146
+ const active = auth.gh.value.active;
147
+ return active !== null && same(active, name) ? 'active' : 'signed in';
148
+ }
149
+ function githubHost(entry) {
150
+ return (entry.host ?? 'github') === 'github';
151
+ }
152
+ function sourcesEmpty(registry, auth) {
153
+ if (!registry.ok || Object.keys(registry.value.accounts).length > 0)
154
+ return false;
155
+ if (!auth.gcmPresent || !auth.stored.ok || auth.stored.value.length > 0)
156
+ return false;
157
+ if (!auth.ghPresent)
158
+ return true;
159
+ return auth.gh.ok && auth.gh.value.accounts.length === 0;
160
+ }
161
+ function sourceWarnings(registry, auth) {
162
+ return [registryWarning(registry), storeWarning(auth), ghQueryWarning(auth)].filter(isWarning);
163
+ }
164
+ function isWarning(warning) {
165
+ return warning !== null;
166
+ }
167
+ /** Same shape as status's registryUnread. Outside a clone there is no clone account to compare. */
168
+ function registryWarning(registry) {
169
+ if (registry.ok)
170
+ return null;
171
+ return {
172
+ tag: 'account',
173
+ message: 'the account registry could not be read, so recorded accounts are unknown.',
174
+ detail: 'see: repown accounts list',
175
+ };
176
+ }
177
+ /** Not installed is the machine line `GCM not found`. A warning is a read that failed. */
178
+ function storeWarning(auth) {
179
+ if (!auth.gcmPresent || auth.stored.ok)
180
+ return null;
181
+ return { tag: 'store', message: 'stored accounts could not be read: ' + auth.stored.error, detail: null };
182
+ }
183
+ function ghQueryWarning(auth) {
184
+ if (!auth.ghPresent || auth.gh.ok)
185
+ return null;
186
+ return {
187
+ tag: 'gh',
188
+ message: 'could not be queried (' + auth.gh.error + '), so signed-in accounts are unknown.',
189
+ detail: 'check it yourself: gh auth status',
190
+ };
191
+ }
192
+ function same(left, right) {
193
+ return keyOf(left) === keyOf(right);
194
+ }
195
+ function keyOf(name) {
196
+ return name.toLowerCase();
197
+ }
198
+ function listed(names, name) {
199
+ return names.some((candidate) => same(candidate, name));
200
+ }
201
+ export function formatAccountLines(report) {
202
+ if (report.rows.length === 0)
203
+ return emptyLines(report);
204
+ const width = Math.max(NAME_WIDTH, ...report.rows.map((row) => row.name.length + 1));
205
+ return report.rows.map((row) => formatRow(row, width));
206
+ }
207
+ function emptyLines(report) {
208
+ if (report.none)
209
+ return pointed(NONE_YET);
210
+ if (report.gcmMissing)
211
+ return pointed(NO_GCM);
212
+ return [];
213
+ }
214
+ function pointed(line) {
215
+ return [' ' + line, ' ' + SETUP_POINTER];
216
+ }
217
+ /** Names share one column, widened for the longest so no row runs into the next cell. */
218
+ function formatRow(row, width) {
219
+ return ' ' + row.name.padEnd(width) + ' ' + rowRest(row);
220
+ }
221
+ function rowRest(row) {
222
+ const body = rowBody(row);
223
+ return row.thisClone ? '(this clone) ' + body : body;
224
+ }
225
+ function rowBody(row) {
226
+ const parts = [recordedPhrase(row), 'git: ' + row.git];
227
+ if (row.gh)
228
+ parts.push('gh: ' + row.gh);
229
+ return parts.filter((part) => part !== '').join(' · ');
230
+ }
231
+ function recordedPhrase(row) {
232
+ if (row.recorded === 'absent')
233
+ return 'not recorded by repown';
234
+ if (row.recorded === 'unknown')
235
+ return 'recorded: unknown';
236
+ return '';
237
+ }
238
+ /** Prints the machine and its accounts; returns how many sources could not be read. */
239
+ function printReport(repo, auth, registry) {
240
+ out.heading(DOCTOR_TITLE);
241
+ printMachine(auth);
242
+ const report = accountRows(registry, auth, pinnedOf(repo));
243
+ printAccounts(report);
244
+ out.line();
245
+ return report.warnings.length;
246
+ }
247
+ function printMachine(auth) {
248
+ out.heading('This machine');
249
+ out.field('helper', auth.helper ?? 'none configured');
250
+ out.field('GCM', auth.gcmPath === null ? 'not found' : out.displayPath(auth.gcmPath));
251
+ out.field('gh active', activeAccountLabel(auth));
252
+ }
253
+ /** repown.account, else the credential username. No marker outside a clone. */
254
+ function pinnedOf(repo) {
255
+ if (!repo.isRepo)
256
+ return null;
257
+ return repo.identity.owner ?? repo.identity.account;
258
+ }
259
+ function printAccounts(report) {
260
+ out.heading('Accounts');
261
+ for (const line of formatAccountLines(report))
262
+ out.line(line);
263
+ for (const warning of report.warnings)
264
+ warnSource(warning);
265
+ }
266
+ function warnSource(warning) {
267
+ out.warn(warning.tag, warning.message);
268
+ if (warning.detail)
269
+ out.detail(warning.detail);
270
+ }
271
+ function diagnose(auth, target) {
272
+ if (auth.ghIsHelper)
273
+ return diagnoseGhHelper(auth);
274
+ if (!auth.helperIsGcm)
275
+ return diagnoseUnknownHelper(auth);
276
+ return diagnoseHealthy(auth, target);
277
+ }
278
+ const READY = 'ready: each clone signs in as its own account through Git Credential Manager';
279
+ const READY_EMPTY = 'ready · no accounts stored yet: the first push signs in once';
280
+ const GH_PROBLEM = '1 problem: gh answers git\'s sign-in requests: run repown fix';
281
+ /** Like status: a blank line and the verdict, on stderr. The report above adds no blank of its own. */
282
+ function closeDoctor(line) {
283
+ out.note('');
284
+ out.note(line);
285
+ }
286
+ /** A source that could not be read is a warning above; the verdict counts it, as status does. */
287
+ function warningTally(count) {
288
+ if (count === 0)
289
+ return '';
290
+ return ' · ' + count + (count === 1 ? ' warning' : ' warnings');
44
291
  }
45
292
  function diagnoseGhHelper(auth) {
46
293
  out.fail('helper', 'gh is the credential helper for github.com.');
@@ -48,53 +295,79 @@ function diagnoseGhHelper(auth) {
48
295
  out.detail('other, so every repository pinned to a different account is');
49
296
  out.detail('prompted for a password -- and `gh auth switch` moves the problem');
50
297
  out.detail('rather than fixing it.');
51
- // On its own line, and only when known: interpolating it inline printed an
52
- // empty "()" whenever gh could not be queried, which reads as a bug.
298
+ ghHelperActive(auth);
299
+ ghHelperFiles(auth);
300
+ out.line();
301
+ out.line(' fix: repown fix');
302
+ return { code: 1, verdict: GH_PROBLEM };
303
+ }
304
+ /** On its own line, and only when known: an empty "()" used to read as a bug. */
305
+ function ghHelperActive(auth) {
53
306
  if (auth.gh.ok && auth.gh.value.active)
54
307
  out.detail('Active right now: ' + auth.gh.value.active);
308
+ }
309
+ function ghHelperFiles(auth) {
55
310
  const files = [...new Set(auth.ghHelperOrigins.map((entry) => entry.file))];
56
- if (files.length > 0) {
57
- out.detail('');
58
- out.detail('Installed by `gh auth setup-git`, in:');
59
- for (const file of files)
60
- out.detail(' ' + file);
61
- }
62
- out.line();
63
- out.line(' fix: repown fix');
64
- out.line();
65
- return 1;
311
+ if (files.length === 0)
312
+ return;
313
+ out.detail('');
314
+ out.detail('Installed by `gh auth setup-git`, in:');
315
+ for (const file of files)
316
+ out.detail(' ' + file);
66
317
  }
67
318
  function diagnoseUnknownHelper(auth) {
68
- out.warn('helper', 'github.com is served by "' + (auth.helper ?? 'nothing') +
69
- '", which repown has no opinion about.');
319
+ const helper = auth.helper ?? 'nothing';
320
+ out.warn('helper', 'github.com is served by "' + helper + '", which repown has no opinion about.');
70
321
  out.detail('The per-repository pin (credential.<url>.username) only works if');
71
322
  out.detail('that helper honours it.');
72
- out.line();
73
- return 0;
323
+ return { code: 0, verdict: unchecked(helper) };
324
+ }
325
+ function unchecked(helper) {
326
+ return 'unchecked: repown can\'t tell whether ' + helper + ' honours the per-clone pin';
327
+ }
328
+ function diagnoseHealthy(auth, target) {
329
+ explainGcm();
330
+ const empty = emptyStore(auth);
331
+ if (empty)
332
+ warnEmptyStore();
333
+ ssoNote(target);
334
+ return { code: 0, verdict: empty ? READY_EMPTY : READY };
74
335
  }
75
- function diagnoseHealthy(auth, providerLabel) {
336
+ function explainGcm() {
76
337
  out.line(' Credentials come from Git Credential Manager, which stores one per');
77
338
  out.line(' account and picks per repository from credential.<url>.username. No');
78
339
  out.line(' switching is needed for git, and `gh auth switch` affects the CLI only.');
79
340
  out.line();
80
- if (auth.stored.ok && auth.stored.value.length === 0) {
81
- out.warn('store', 'no accounts stored yet -- the first push will sign in once.');
82
- out.line();
83
- }
84
- ssoNote(providerLabel);
85
- return 0;
341
+ }
342
+ function emptyStore(auth) {
343
+ return auth.stored.ok && auth.stored.value.length === 0;
344
+ }
345
+ function warnEmptyStore() {
346
+ out.warn('store', 'no accounts stored yet -- the first push will sign in once.');
347
+ out.line();
86
348
  }
87
349
  // Not a check -- repown cannot see this coming (ADR-013, "Deliberately
88
350
  // not done"). A credential that is otherwise healthy still fails the moment
89
351
  // it touches an org it is not SSO-authorized for, and that failure looks
90
352
  // identical to a bad token. Printed unconditionally so it is there before it
91
- // is needed. The exact fix command is gh-specific, so it only names `gh` for
92
- // a GitHub origin -- gh does not manage auth for any other host.
93
- function ssoNote(providerLabel) {
94
- out.line(' If a push or fetch still fails right after this, the credential may');
95
- out.line(' be valid but not yet SSO-authorized for that organisation. Re-authorize it:');
96
- out.line(providerLabel === 'GitHub'
97
- ? ' gh auth refresh -h <host>, or via the org\'s SSO settings.'
98
- : " check your git host's SSO / conditional-access settings.");
99
- out.line();
353
+ // is needed. On GitHub the authorization is the org's SSO settings, named
354
+ // with the origin's host (github.com outside a clone). Any other provider
355
+ // is named only.
356
+ function ssoNote(target) {
357
+ out.line(' If a push fails although the account is stored, the org may need SSO');
358
+ out.line(' authorization: ' + ssoFix(target));
359
+ }
360
+ function ssoFix(target) {
361
+ if (target.host !== null)
362
+ return 'authorize it in the org\'s SSO settings on ' + target.host;
363
+ return 'check ' + target.label + "'s SSO settings";
364
+ }
365
+ function ssoTarget(repo) {
366
+ return { label: repo.provider.label, host: ssoHost(repo) };
367
+ }
368
+ /** The origin's host on GitHub, github.com outside a clone, null for any other provider. */
369
+ function ssoHost(repo) {
370
+ if (repo.provider.id !== 'github' && repo.isRepo)
371
+ return null;
372
+ return repo.url?.host ?? 'github.com';
100
373
  }
@@ -49,18 +49,20 @@ async function confirmed(args) {
49
49
  return false;
50
50
  }
51
51
  function preview(planned) {
52
- out.line();
53
- out.line(' These entries make gh the credential helper, and will be removed:');
54
- out.line();
55
- for (const entry of planned) {
56
- out.line(' ' + entry.scope + ': ' + entry.key);
57
- for (const value of entry.values)
58
- out.line(' = ' + describeValue(value));
59
- }
60
- out.line();
61
- out.line(' Nothing else in your config is touched.');
62
- out.line(' Undo at any time: gh auth setup-git');
63
- out.line();
52
+ for (const text of previewLines(planned))
53
+ out.line(text);
54
+ }
55
+ /** What `fix` would remove, and the undo. `repown setup` shows the same lines in its review. */
56
+ export function previewLines(planned) {
57
+ const entries = planned.flatMap((entry) => [
58
+ ' ' + entry.scope + ': ' + entry.key,
59
+ ...entry.values.map((value) => ' = ' + describeValue(value)),
60
+ ]);
61
+ return [
62
+ '', ' These entries make gh the credential helper, and will be removed:', '',
63
+ ...entries,
64
+ '', ' Nothing else in your config is touched.', ' Undo at any time: gh auth setup-git', '',
65
+ ];
64
66
  }
65
67
  async function apply(results, git) {
66
68
  const failed = reportRemovals(results);
@@ -3,7 +3,7 @@
3
3
  // `guard check` is what the installed hook invokes, with the exact syntax baked
4
4
  // into every hook already on disk (src/core/guard/hook.ts) -- `--remote` and
5
5
  // `--url` on this action are a frozen contract, not just today's flags.
6
- import { installGuard, uninstallGuard, guardState } from "../core/guard/hook.js";
6
+ import { installGuard, uninstallGuard, guardState, fromNpxCache } from "../core/guard/hook.js";
7
7
  import { check } from "../core/guard/check.js";
8
8
  import { flagString, gitFor } from "../ui/args.js";
9
9
  import * as out from "../ui/format.js";
@@ -25,8 +25,16 @@ async function enable(args) {
25
25
  }
26
26
  out.pass('guard', 'on -- every push is checked before it leaves');
27
27
  out.line(out.dim(' ' + installed.value.path));
28
+ if (fromNpxCache(installed.value.entry))
29
+ warnNpxCache();
28
30
  return 0;
29
31
  }
32
+ /** The hook is installed, but it calls a copy that nothing keeps (ADR-003, amended). */
33
+ function warnNpxCache() {
34
+ out.warn('guard', 'repown ran from npx\'s cache, and this hook calls that copy. If that folder is ' +
35
+ 'deleted, every push is refused unless another repown is on PATH.');
36
+ out.detail('fix: npm install -g repown, then run: repown guard on');
37
+ }
30
38
  async function disable(args) {
31
39
  const removed = await uninstallGuard(gitFor(args));
32
40
  if (!removed.ok) {
@@ -49,7 +57,7 @@ async function runCheck(args) {
49
57
  onNote: (note) => out.warn('guard', note),
50
58
  });
51
59
  if (refusals.length === 0) {
52
- out.pass('guard', 'every commit in this push carries this clone’s identity');
60
+ out.pass('guard', 'every commit in this push carries this clone\'s identity');
53
61
  return 0;
54
62
  }
55
63
  report(refusals);
@@ -27,7 +27,7 @@ export default {
27
27
  summary: 'audit every clone under a directory',
28
28
  positionals: { min: 0, max: Infinity, label: '<dir>' },
29
29
  options: [
30
- { name: 'emails', kind: 'boolean', help: 'show exact addresses instead of domains and counts' },
30
+ { name: 'emails', kind: 'boolean', help: 'show exact addresses instead of domains (counts stay)' },
31
31
  { name: 'depth', kind: 'string', default: '3', help: 'how many directories deep to look for a clone' },
32
32
  FORMAT_OPTION,
33
33
  ],
@@ -0,0 +1,27 @@
1
+ // A guided first setup of this clone, for someone new to repown. The first question is
2
+ // how it should work: Recommended fills in the guard, upstream and a gh switch, and
3
+ // still asks when origin belongs to someone else. Step by step asks each one.
4
+ // It shows the commands its answers stand for, and runs exactly those --
5
+ // `accounts add`, `use`, `guard on`, `fix` -- so anything it
6
+ // does can be done, or scripted, without it (src/wizard/setup-run.ts).
7
+ import { providers } from "../core/hosts/index.js";
8
+ import { interactive } from "../ui/prompt.js";
9
+ import { runSetup } from "../wizard/setup-run.js";
10
+ export default {
11
+ summary: 'guided setup of this clone: asks, shows each step and its command, then runs them',
12
+ positionals: { min: 0, max: 1, label: '<account>' },
13
+ options: [
14
+ { name: 'name', kind: 'string', help: 'commit name, for an account not yet recorded' },
15
+ { name: 'email', kind: 'string', help: 'commit email, for an account not yet recorded' },
16
+ { name: 'host', kind: 'string', choices: providers().map((provider) => provider.id), help: "host of an account not yet recorded (default: origin's host)" },
17
+ { name: 'gh', kind: 'boolean', help: "also switch the GitHub CLI's active account (use --gh)" },
18
+ { name: 'allow-owner', kind: 'string', help: "allow pushes to origin's owner: an organisation, or an account you collaborate with (repown.allowOwner)" },
19
+ { name: 'guard', kind: 'boolean', help: 'turn the push guard on (guard on)' },
20
+ { name: 'auto-upstream', kind: 'boolean', help: 'push branches without -u: the first push sets the upstream, in this clone only (push.autoSetupRemote)' },
21
+ { name: 'fix', kind: 'boolean', help: 'stop gh being the credential helper, where it is (fix --yes)' },
22
+ { name: 'step-by-step', kind: 'boolean', help: 'ask every question instead of the recommended answers (not with --no-input)' },
23
+ { 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
+ ],
25
+ examples: ['repown setup', 'repown setup octocat --guard --no-input'],
26
+ run: (args) => runSetup(args, { interactive: interactive() }),
27
+ };
@@ -0,0 +1,38 @@
1
+ // What bare `repown` runs. Not a command: cli.ts loads it only when no
2
+ // arguments were typed, so `repown guard check` never imports it.
3
+ //
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.
7
+ import { Git } from "../core/git.js";
8
+ import { inspectRepo } from "../core/inspect.js";
9
+ import { identityProblems } from "./status.js";
10
+ import * as out from "../ui/format.js";
11
+ 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. */
15
+ export async function chooseStart(deps) {
16
+ return (await pickStart(deps)).choice;
17
+ }
18
+ /** Real deps for this process, plus the one stderr line when setup starts. */
19
+ export async function startDefault(deps = realDeps()) {
20
+ const pick = await pickStart(deps);
21
+ if (pick.choice === 'setup')
22
+ out.note(pick.pinned ? PINNED_NOTE : SETUP_NOTE);
23
+ return pick.choice;
24
+ }
25
+ async function pickStart(deps) {
26
+ if (!deps.interactive)
27
+ return { choice: 'status', pinned: false };
28
+ if (await deps.git.isBare())
29
+ return { choice: 'help', pinned: false };
30
+ const repo = await inspectRepo(deps.git);
31
+ if (!repo.isRepo)
32
+ return { choice: 'help', pinned: false };
33
+ const pinned = identityProblems(repo).length === 0;
34
+ return { choice: deps.stdoutIsTerminal ? 'setup' : 'status', pinned };
35
+ }
36
+ function realDeps() {
37
+ return { interactive: interactive(), stdoutIsTerminal: process.stdout.isTTY === true, git: new Git(process.cwd()) };
38
+ }