repown 0.1.1 → 0.3.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 +77 -1
- package/README.md +314 -149
- package/dist/cli.js +18 -17
- package/dist/commands/doctor.js +323 -50
- package/dist/commands/fix.js +14 -12
- package/dist/commands/guard.js +10 -2
- package/dist/commands/scan.js +14 -7
- package/dist/commands/setup.js +27 -0
- package/dist/commands/start.js +56 -0
- package/dist/commands/status.js +244 -41
- package/dist/commands/use.js +139 -10
- package/dist/core/credential/gh.js +96 -9
- package/dist/core/exec.js +55 -6
- package/dist/core/git.js +43 -1
- package/dist/core/guard/check.js +1 -1
- package/dist/core/guard/hook.js +11 -2
- package/dist/core/hosts/github.js +31 -14
- package/dist/core/identity.js +28 -2
- package/dist/core/inspect.js +3 -2
- package/dist/core/unpushed.js +110 -0
- package/dist/core/version.js +17 -0
- package/dist/program.js +13 -0
- package/dist/ui/dispatch.js +23 -5
- package/dist/ui/format.js +15 -1
- package/dist/ui/help.js +7 -2
- package/dist/wizard/clack.js +132 -0
- package/dist/wizard/engine.js +244 -0
- package/dist/wizard/home-context.js +101 -0
- package/dist/wizard/home-flow.js +90 -0
- package/dist/wizard/home-run.js +148 -0
- package/dist/wizard/home-text.js +50 -0
- package/dist/wizard/plain.js +183 -0
- package/dist/wizard/review-text.js +110 -0
- package/dist/wizard/setup-changes.js +73 -0
- package/dist/wizard/setup-context.js +256 -0
- package/dist/wizard/setup-flow.js +733 -0
- package/dist/wizard/setup-run.js +473 -0
- package/npm-shrinkwrap.json +121 -0
- package/package.json +6 -2
|
@@ -0,0 +1,56 @@
|
|
|
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. 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.
|
|
10
|
+
import { Git } from "../core/git.js";
|
|
11
|
+
import { inspectRepo } from "../core/inspect.js";
|
|
12
|
+
import { identityProblems } from "./status.js";
|
|
13
|
+
import { interactive } from "../ui/prompt.js";
|
|
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. */
|
|
17
|
+
export async function chooseStart(deps) {
|
|
18
|
+
return (await pickStart(deps)).choice;
|
|
19
|
+
}
|
|
20
|
+
/** Real deps for this process. Setup and the start screen are runners: nothing is printed until they are called. */
|
|
21
|
+
export async function startDefault(deps = realDeps()) {
|
|
22
|
+
const pick = await pickStart(deps);
|
|
23
|
+
if (pick.choice === 'setup')
|
|
24
|
+
return () => startSetup(pick.pinned);
|
|
25
|
+
if (pick.choice === 'home')
|
|
26
|
+
return startHome;
|
|
27
|
+
return pick.choice;
|
|
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
|
+
}
|
|
39
|
+
async function pickStart(deps) {
|
|
40
|
+
if (!deps.interactive)
|
|
41
|
+
return { choice: 'status', pinned: false };
|
|
42
|
+
if (await deps.git.isBare())
|
|
43
|
+
return { choice: outsideClone(deps.stdoutIsTerminal), pinned: false };
|
|
44
|
+
const repo = await inspectRepo(deps.git);
|
|
45
|
+
if (!repo.isRepo)
|
|
46
|
+
return { choice: outsideClone(deps.stdoutIsTerminal), pinned: false };
|
|
47
|
+
const pinned = identityProblems(repo).length === 0;
|
|
48
|
+
return { choice: deps.stdoutIsTerminal ? 'setup' : 'status', pinned };
|
|
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
|
+
}
|
|
54
|
+
function realDeps() {
|
|
55
|
+
return { interactive: interactive(), stdoutIsTerminal: process.stdout.isTTY === true, git: new Git(process.cwd()) };
|
|
56
|
+
}
|
package/dist/commands/status.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
// What `repown`
|
|
1
|
+
// What `repown status` prints: is this clone set up correctly?
|
|
2
|
+
// Bare `repown` runs this when output isn't a terminal. In a terminal, a clone
|
|
3
|
+
// goes to setup whether or not it is pinned (commands/start.ts).
|
|
2
4
|
//
|
|
3
5
|
// Three separate things decide who you are here, and they fail differently:
|
|
4
6
|
//
|
|
@@ -7,16 +9,19 @@
|
|
|
7
9
|
// the CLI's active account who `gh pr create` acts as
|
|
8
10
|
//
|
|
9
11
|
// All three are printed, because the one that is invisible is the one that
|
|
10
|
-
// catches people out.
|
|
12
|
+
// catches people out. gh's active account is a note: it never affects the push.
|
|
13
|
+
// A gh that could not be queried stays a warning, because that answer is unknown.
|
|
14
|
+
// This reports; it changes nothing.
|
|
11
15
|
import { inspectRepo, inspectAuth, activeAccountLabel } from "../core/inspect.js";
|
|
12
16
|
import { isPinned } from "../core/identity.js";
|
|
17
|
+
import { loadRegistry } from "../core/registry.js";
|
|
13
18
|
import { allowedOwners, shellWord } from "../core/guard/check.js";
|
|
14
19
|
import { existsSync } from 'node:fs';
|
|
15
20
|
import { join } from 'node:path';
|
|
16
21
|
import { gitFor } from "../ui/args.js";
|
|
17
22
|
import * as out from "../ui/format.js";
|
|
18
23
|
export default {
|
|
19
|
-
summary: '
|
|
24
|
+
summary: 'this clone\'s and this machine\'s settings, and what to fix (bare `repown` when output isn\'t a terminal)',
|
|
20
25
|
async run(args) {
|
|
21
26
|
const git = gitFor(args);
|
|
22
27
|
const repo = await inspectRepo(git);
|
|
@@ -26,10 +31,14 @@ export default {
|
|
|
26
31
|
return 1;
|
|
27
32
|
}
|
|
28
33
|
const auth = await inspectAuth(git, repo.originUrl ?? undefined);
|
|
29
|
-
|
|
34
|
+
const registry = await loadRegistry();
|
|
35
|
+
await summary(repo, auth, registry);
|
|
30
36
|
const problems = collectProblems(repo, auth);
|
|
31
|
-
await reportWarnings(repo, auth);
|
|
32
|
-
|
|
37
|
+
const warnings = await reportWarnings(repo, auth, registry);
|
|
38
|
+
const code = verdict(repo, auth, problems);
|
|
39
|
+
const ghNote = printGhNote(pinnedAccount(repo), auth);
|
|
40
|
+
closeStatus({ repo, problems, warnings, account: accountShown(repo, registry), ghNote });
|
|
41
|
+
return code;
|
|
33
42
|
},
|
|
34
43
|
};
|
|
35
44
|
/** "Honours it" only where the helper is GCM, the one measured to read the pin (ADR-001). */
|
|
@@ -47,18 +56,76 @@ function verdict(repo, auth, problems) {
|
|
|
47
56
|
}
|
|
48
57
|
return 1;
|
|
49
58
|
}
|
|
50
|
-
|
|
51
|
-
|
|
59
|
+
const STATUS_TITLE = 'repown status · current settings of this clone';
|
|
60
|
+
async function summary(repo, auth, registry) {
|
|
61
|
+
out.heading(STATUS_TITLE);
|
|
62
|
+
out.line(place(repo));
|
|
63
|
+
await cloneSettings(repo, registry);
|
|
64
|
+
machineSettings(repo, auth);
|
|
52
65
|
out.line();
|
|
53
|
-
|
|
66
|
+
}
|
|
67
|
+
function place(repo) {
|
|
68
|
+
const where = out.displayPath(repo.root ?? repo.git.cwd);
|
|
69
|
+
return repo.branch ? ' ' + where + ' ' + headLabel(repo.branch) : ' ' + where;
|
|
70
|
+
}
|
|
71
|
+
function headLabel(branch) {
|
|
72
|
+
if (branch.kind === 'detached')
|
|
73
|
+
return '(detached at ' + branch.hash + ')';
|
|
74
|
+
return '(branch ' + branch.name + ')';
|
|
75
|
+
}
|
|
76
|
+
async function cloneSettings(repo, registry) {
|
|
77
|
+
const id = repo.identity;
|
|
78
|
+
out.heading('This clone');
|
|
79
|
+
out.field('commits as', person(id.name, id.email, 'NOT SET LOCALLY'));
|
|
54
80
|
out.field('pushes as', pushesAs(repo));
|
|
55
|
-
out.field('
|
|
56
|
-
|
|
57
|
-
|
|
81
|
+
out.field('account', accountOf(repo, registry));
|
|
82
|
+
out.field('origin', originOf(repo));
|
|
83
|
+
await writeUpstream(repo);
|
|
84
|
+
out.field('push guard', repo.guard);
|
|
85
|
+
}
|
|
86
|
+
async function writeUpstream(repo) {
|
|
87
|
+
const value = await upstreamField(repo);
|
|
88
|
+
if (value !== null)
|
|
89
|
+
out.field('upstream', value);
|
|
90
|
+
}
|
|
91
|
+
/** Printed when `push.autoSetupRemote` is on and the branch tracks nothing yet. */
|
|
92
|
+
export const UPSTREAM_ON = 'set on the first push (push.autoSetupRemote)';
|
|
93
|
+
/** The tracked ref, else the auto-setup wording when that flag is on, else nothing. */
|
|
94
|
+
export function upstreamText(tracked, enabled) {
|
|
95
|
+
if (tracked !== null)
|
|
96
|
+
return tracked;
|
|
97
|
+
return enabled === true ? UPSTREAM_ON : null;
|
|
98
|
+
}
|
|
99
|
+
async function upstreamField(repo) {
|
|
100
|
+
const branch = repo.branch;
|
|
101
|
+
if (branch?.kind !== 'branch')
|
|
102
|
+
return null;
|
|
103
|
+
const remotes = await repo.git.remotes();
|
|
104
|
+
if (remotes.length === 0)
|
|
105
|
+
return null;
|
|
106
|
+
const tracked = await repo.git.upstreamRef();
|
|
107
|
+
const enabled = tracked === null ? await repo.git.getBoolConfig('push.autoSetupRemote') : null;
|
|
108
|
+
return upstreamText(tracked, enabled) ?? 'none yet: git push -u ' + pushRemote(remotes) + ' ' + branch.name;
|
|
109
|
+
}
|
|
110
|
+
function pushRemote(remotes) {
|
|
111
|
+
return remotes.includes('origin') ? 'origin' : remotes[0];
|
|
112
|
+
}
|
|
113
|
+
function machineSettings(repo, auth) {
|
|
114
|
+
const id = repo.identity;
|
|
115
|
+
out.heading('This machine');
|
|
116
|
+
out.field('default', person(id.machineName, id.machineEmail, 'none'));
|
|
58
117
|
out.field('helper', repo.helper ?? 'none');
|
|
59
118
|
out.field('gh active', activeAccountLabel(auth));
|
|
60
|
-
|
|
61
|
-
|
|
119
|
+
}
|
|
120
|
+
function person(name, email, absent) {
|
|
121
|
+
if (!name && !email)
|
|
122
|
+
return absent;
|
|
123
|
+
return (name ?? '?') + ' <' + (email ?? '?') + '>';
|
|
124
|
+
}
|
|
125
|
+
function originOf(repo) {
|
|
126
|
+
if (!repo.originUrl)
|
|
127
|
+
return 'no remote';
|
|
128
|
+
return (repo.owner ?? 'unknown') + ' ' + out.dim('(' + repo.provider.label + ')');
|
|
62
129
|
}
|
|
63
130
|
function pushesAs(repo) {
|
|
64
131
|
if (!repo.originUrl)
|
|
@@ -67,6 +134,69 @@ function pushesAs(repo) {
|
|
|
67
134
|
return 'not pinned by repown on ' + repo.provider.label;
|
|
68
135
|
return repo.identity.account ?? 'NOT SET LOCALLY';
|
|
69
136
|
}
|
|
137
|
+
// A registry that cannot be read is reported. Skipping it would look like "none recorded".
|
|
138
|
+
function accountOf(repo, registry) {
|
|
139
|
+
if (!registry.ok)
|
|
140
|
+
return 'registry could not be read: ' + registry.error;
|
|
141
|
+
const pinned = pinnedAccount(repo);
|
|
142
|
+
if (pinned === null)
|
|
143
|
+
return noted('not pinned', recordedList(registry.value));
|
|
144
|
+
const found = recordedAs(registry.value, pinned);
|
|
145
|
+
if (!found)
|
|
146
|
+
return noted(pinned, 'not in this machine\'s registry');
|
|
147
|
+
const aside = matchesRecord(repo, found.entry) ? 'recorded' : recordedAside(found.entry);
|
|
148
|
+
return noted(found.key, aside);
|
|
149
|
+
}
|
|
150
|
+
function recordedList(registry) {
|
|
151
|
+
const keys = Object.keys(registry.accounts);
|
|
152
|
+
return keys.length === 0 ? 'none recorded' : 'recorded: ' + keys.join(', ');
|
|
153
|
+
}
|
|
154
|
+
function recordedAside(entry) {
|
|
155
|
+
return 'recorded as ' + person(entry.name, entry.email, '?');
|
|
156
|
+
}
|
|
157
|
+
function noted(value, aside) {
|
|
158
|
+
return value + ' ' + out.dim('(' + aside + ')');
|
|
159
|
+
}
|
|
160
|
+
function pinnedAccount(repo) {
|
|
161
|
+
return repo.identity.owner ?? repo.identity.account;
|
|
162
|
+
}
|
|
163
|
+
function recordedAs(registry, pinned) {
|
|
164
|
+
const wanted = pinned.toLowerCase();
|
|
165
|
+
const key = Object.keys(registry.accounts).find((candidate) => candidate.toLowerCase() === wanted);
|
|
166
|
+
return key === undefined ? null : { key, entry: registry.accounts[key] };
|
|
167
|
+
}
|
|
168
|
+
function matchesRecord(repo, entry) {
|
|
169
|
+
const id = repo.identity;
|
|
170
|
+
return id.name === entry.name && id.email?.toLowerCase() === entry.email.toLowerCase();
|
|
171
|
+
}
|
|
172
|
+
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
|
+
out.note('');
|
|
177
|
+
out.note(line);
|
|
178
|
+
}
|
|
179
|
+
const GH_OPTIONAL = ' · gh: optional (see the note above)';
|
|
180
|
+
function readyLine(repo, account, warnings, ghNote) {
|
|
181
|
+
const head = repo.credentialKeys.length === 0
|
|
182
|
+
? 'ready: commits use ' + account + '; pushes use this host\'s own sign-in'
|
|
183
|
+
: 'ready: commits and pushes use ' + account;
|
|
184
|
+
if (warnings.length > 0)
|
|
185
|
+
return head + ' · ' + howMany(warnings.length, 'warning');
|
|
186
|
+
return ghNote ? head + GH_OPTIONAL : head;
|
|
187
|
+
}
|
|
188
|
+
function setupHint(repo) {
|
|
189
|
+
return identityProblems(repo).length > 0 ? ': run repown setup' : '';
|
|
190
|
+
}
|
|
191
|
+
function tally(problems, warnings) {
|
|
192
|
+
const parts = [howMany(problems, 'problem'), howMany(warnings, 'warning')];
|
|
193
|
+
return parts.filter((part) => part.length > 0).join(', ');
|
|
194
|
+
}
|
|
195
|
+
function howMany(count, word) {
|
|
196
|
+
if (count === 0)
|
|
197
|
+
return '';
|
|
198
|
+
return count + ' ' + word + (count === 1 ? '' : 's');
|
|
199
|
+
}
|
|
70
200
|
function collectProblems(repo, auth) {
|
|
71
201
|
const gh = auth.ghIsHelper ? [{
|
|
72
202
|
what: 'gh is the git credential helper, so only its ACTIVE account can ' +
|
|
@@ -75,13 +205,14 @@ function collectProblems(repo, auth) {
|
|
|
75
205
|
}] : [];
|
|
76
206
|
return [...identityProblems(repo), ...gh];
|
|
77
207
|
}
|
|
78
|
-
|
|
208
|
+
const SETUP_FIX = 'repown setup (or: repown use <account>)';
|
|
209
|
+
export function identityProblems(repo) {
|
|
79
210
|
const id = repo.identity;
|
|
80
211
|
if (!isPinned(id)) {
|
|
81
212
|
return [{
|
|
82
213
|
what: 'This clone sets no identity of its own, so it inherits the machine default (' +
|
|
83
214
|
(id.inheritedEmail ?? 'nothing') + ').',
|
|
84
|
-
fix:
|
|
215
|
+
fix: SETUP_FIX,
|
|
85
216
|
}];
|
|
86
217
|
}
|
|
87
218
|
if (repo.credentialKeys.length === 0 || id.account)
|
|
@@ -89,20 +220,46 @@ function identityProblems(repo) {
|
|
|
89
220
|
return [{
|
|
90
221
|
what: 'No account is pinned, so pushes fall back to the machine default (' +
|
|
91
222
|
(id.inheritedAccount ?? 'nothing') + ').',
|
|
92
|
-
fix:
|
|
223
|
+
fix: SETUP_FIX,
|
|
93
224
|
}];
|
|
94
225
|
}
|
|
95
|
-
async function reportWarnings(repo, auth) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
226
|
+
async function reportWarnings(repo, auth, registry) {
|
|
227
|
+
const tags = ['origin', 'account', 'guard', 'submodule', 'helper', 'gh'];
|
|
228
|
+
const fired = [
|
|
229
|
+
await ownerWarning(repo),
|
|
230
|
+
accountWarning(repo, registry),
|
|
231
|
+
guardWarning(repo),
|
|
232
|
+
submoduleWarning(repo),
|
|
233
|
+
helperWarning(repo, auth),
|
|
234
|
+
ghWarning(pinnedAccount(repo), auth),
|
|
235
|
+
];
|
|
236
|
+
return tags.filter((_, i) => fired[i]);
|
|
237
|
+
}
|
|
238
|
+
function accountShown(repo, registry) {
|
|
239
|
+
const pinned = pinnedAccount(repo);
|
|
240
|
+
if (pinned === null)
|
|
241
|
+
return repo.identity.email ?? 'this clone\'s identity';
|
|
242
|
+
if (!registry.ok)
|
|
243
|
+
return pinned;
|
|
244
|
+
return recordedAs(registry.value, pinned)?.key ?? pinned;
|
|
245
|
+
}
|
|
246
|
+
function accountWarning(repo, registry) {
|
|
247
|
+
if (!registry.ok)
|
|
248
|
+
return registryUnread();
|
|
249
|
+
const pinned = pinnedAccount(repo);
|
|
250
|
+
const found = pinned === null ? null : recordedAs(registry.value, pinned);
|
|
251
|
+
if (!found || matchesRecord(repo, found.entry))
|
|
252
|
+
return false;
|
|
253
|
+
const id = repo.identity;
|
|
254
|
+
out.warn('account', 'this clone commits as ' + person(id.name, id.email, '?') +
|
|
255
|
+
', but ' + found.key + ' is recorded as ' + person(found.entry.name, found.entry.email, '?') + '.');
|
|
256
|
+
out.detail('fix: repown use ' + found.key);
|
|
257
|
+
return true;
|
|
101
258
|
}
|
|
102
259
|
/** A pinned credential key is only as good as the helper reading it. gh as helper is a problem, reported elsewhere. */
|
|
103
260
|
function helperWarning(repo, auth) {
|
|
104
261
|
if (repo.credentialKeys.length === 0 || auth.helperIsGcm || auth.ghIsHelper)
|
|
105
|
-
return;
|
|
262
|
+
return false;
|
|
106
263
|
if (!auth.helper) {
|
|
107
264
|
out.warn('helper', 'no credential helper is set, so the pinned account selects no credential; git will prompt on push.');
|
|
108
265
|
}
|
|
@@ -110,6 +267,7 @@ function helperWarning(repo, auth) {
|
|
|
110
267
|
out.warn('helper', '"' + auth.helper + '" serves credentials here, and repown cannot tell whether it honours the pinned account.');
|
|
111
268
|
}
|
|
112
269
|
out.detail('see: repown doctor (Git Credential Manager is the helper repown pins for)');
|
|
270
|
+
return true;
|
|
113
271
|
}
|
|
114
272
|
/**
|
|
115
273
|
* A submodule is a clone of its own, with its own config and hooks. This clone's
|
|
@@ -118,9 +276,10 @@ function helperWarning(repo, auth) {
|
|
|
118
276
|
*/
|
|
119
277
|
function submoduleWarning(repo) {
|
|
120
278
|
if (!repo.root || !existsSync(join(repo.root, '.gitmodules')))
|
|
121
|
-
return;
|
|
279
|
+
return false;
|
|
122
280
|
out.warn('submodule', 'this clone has submodules; each is a separate clone with its own identity and hook.');
|
|
123
281
|
out.detail('pin and guard each one too: git submodule foreach "repown use <account> && repown guard on"');
|
|
282
|
+
return true;
|
|
124
283
|
}
|
|
125
284
|
/**
|
|
126
285
|
* An organisation is never an account name, so a bare owner-vs-account
|
|
@@ -129,30 +288,73 @@ function submoduleWarning(repo) {
|
|
|
129
288
|
async function ownerWarning(repo) {
|
|
130
289
|
const allowed = await allowedOwners(repo.git, repo.identity.owner ?? repo.identity.account);
|
|
131
290
|
if (!repo.owner || allowed.length === 0 || allowed.includes(repo.owner.toLowerCase()))
|
|
132
|
-
return;
|
|
291
|
+
return false;
|
|
133
292
|
out.warn('origin', 'origin belongs to "' + repo.owner + '", which is not an owner this clone pushes to.');
|
|
134
293
|
out.detail('if that is an organisation you belong to:');
|
|
135
294
|
out.detail(' git config --local --add repown.allowOwner ' + shellWord(repo.owner));
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
function registryUnread() {
|
|
298
|
+
out.warn('account', 'the account registry could not be read, so this clone\'s account was not compared with it.');
|
|
299
|
+
out.detail('see: repown accounts list');
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
export function ghAdvice(account, auth) {
|
|
303
|
+
if (auth.ghPresent && !auth.gh.ok)
|
|
304
|
+
return unverifiedGh();
|
|
305
|
+
const active = ghActiveOther(account, auth);
|
|
306
|
+
if (!active || account === null)
|
|
307
|
+
return null;
|
|
308
|
+
return { level: 'note', text: ghActiveText(active), detail: ghFix(account, auth) };
|
|
309
|
+
}
|
|
310
|
+
function unverifiedGh() {
|
|
311
|
+
return { level: 'warn', text: GH_UNVERIFIED, detail: 'check it yourself: gh auth status' };
|
|
312
|
+
}
|
|
313
|
+
function ghActiveText(active) {
|
|
314
|
+
return 'active as "' + active + '", so `gh pr create` here would act as that account. ' +
|
|
315
|
+
'git pushes are unaffected; this only matters if you use gh here.';
|
|
316
|
+
}
|
|
317
|
+
const GH_UNVERIFIED = 'could not be queried, so who `gh pr create` would act as is UNVERIFIED.';
|
|
318
|
+
/** `gh auth switch` only works for a login gh already lists, and only in gh's spelling. */
|
|
319
|
+
function ghFix(account, auth) {
|
|
320
|
+
const login = ghReportedLogin(account, auth);
|
|
321
|
+
if (login !== null)
|
|
322
|
+
return 'fix: gh auth switch -u ' + login;
|
|
323
|
+
return 'fix: repown use ' + account + ' --gh (signs ' + account + ' in to gh)';
|
|
324
|
+
}
|
|
325
|
+
function ghReportedLogin(account, auth) {
|
|
326
|
+
if (!auth.gh.ok)
|
|
327
|
+
return null;
|
|
328
|
+
const wanted = account.toLowerCase();
|
|
329
|
+
const found = auth.gh.value.accounts.find((item) => item.login.toLowerCase() === wanted);
|
|
330
|
+
return found === undefined ? null : found.login;
|
|
331
|
+
}
|
|
332
|
+
function ghActiveOther(account, auth) {
|
|
333
|
+
if (!auth.gh.ok || !account || !auth.gh.value.active)
|
|
334
|
+
return null;
|
|
335
|
+
const active = auth.gh.value.active;
|
|
336
|
+
return active.toLowerCase() === account.toLowerCase() ? null : active;
|
|
136
337
|
}
|
|
137
|
-
/**
|
|
138
|
-
* The unknown case is REPORTED, never skipped. Guarding this on the active
|
|
139
|
-
* account being truthy is how a failed lookup used to make the whole warning
|
|
140
|
-
* disappear, leaving output that looked clean rather than uncertain.
|
|
141
|
-
*/
|
|
142
338
|
function ghWarning(account, auth) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
339
|
+
const advice = ghAdvice(account, auth);
|
|
340
|
+
if (advice?.level !== 'warn')
|
|
341
|
+
return false;
|
|
342
|
+
out.warn('gh', advice.text);
|
|
343
|
+
out.detail(advice.detail);
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
/** After the identity line, so a terminal shows the pass before this note. */
|
|
347
|
+
function printGhNote(account, auth) {
|
|
348
|
+
const advice = ghAdvice(account, auth);
|
|
349
|
+
if (advice?.level !== 'note')
|
|
350
|
+
return false;
|
|
351
|
+
out.noted('gh', advice.text);
|
|
352
|
+
out.detail(advice.detail);
|
|
353
|
+
return true;
|
|
152
354
|
}
|
|
153
355
|
function guardWarning(repo) {
|
|
154
356
|
if (repo.guard === 'on')
|
|
155
|
-
return;
|
|
357
|
+
return false;
|
|
156
358
|
if (repo.hook?.redirected) {
|
|
157
359
|
out.warn('guard', 'core.hooksPath makes git run hooks from another tool\'s directory; repown does not install there.');
|
|
158
360
|
out.detail('guard this clone from that tool\'s pre-push hook: repown guard check --remote="$1" --url="$2"');
|
|
@@ -165,4 +367,5 @@ function guardWarning(repo) {
|
|
|
165
367
|
out.warn('guard', 'a pre-push hook repown did not write is installed; it was left alone.');
|
|
166
368
|
out.detail('read it first; if it is safe to drop, delete ' + (repo.hook?.path ?? 'it') + ', then run: repown guard on');
|
|
167
369
|
}
|
|
370
|
+
return true;
|
|
168
371
|
}
|
package/dist/commands/use.js
CHANGED
|
@@ -13,23 +13,26 @@
|
|
|
13
13
|
// never tracks -- which is what keeps a name or address out of a public repo, and
|
|
14
14
|
// also why this is per clone: a second machine, or a re-clone, runs it again.
|
|
15
15
|
//
|
|
16
|
-
// You do not switch this afterwards, and you do not need to.
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//
|
|
16
|
+
// You do not switch this afterwards, and you do not need to. `--gh` keeps the
|
|
17
|
+
// GitHub CLI on the same account: `gh auth switch` when gh already lists it,
|
|
18
|
+
// and, in a terminal, `gh auth login` when it does not. After `repown fix`
|
|
19
|
+
// neither changes which account git pushes as.
|
|
20
20
|
import { inspectRepo, inspectAuth } from "../core/inspect.js";
|
|
21
21
|
import { pinIdentity } from "../core/identity.js";
|
|
22
22
|
import { lookupAccount, saveAccount } from "../core/registry.js";
|
|
23
|
-
import { ghSwitch } from "../core/credential/gh.js";
|
|
23
|
+
import { ghSwitch, ghLogin, readGhLoginVersion, } from "../core/credential/gh.js";
|
|
24
24
|
import { allowedOwners, shellWord } from "../core/guard/check.js";
|
|
25
|
+
import { readUnpushed, unpushedLines } from "../core/unpushed.js";
|
|
25
26
|
import { ask, interactive } from "../ui/prompt.js";
|
|
26
27
|
import { flagString, flagBool, gitFor } from "../ui/args.js";
|
|
27
28
|
import * as out from "../ui/format.js";
|
|
29
|
+
/** The last hint while the guard is off; `repown setup` leaves it out when its next step turns the guard on. */
|
|
30
|
+
export const NEXT_GUARD = ' Next: repown guard on (check every push before it leaves)';
|
|
28
31
|
export default {
|
|
29
32
|
summary: 'pin this clone to an account (repown use <account>)',
|
|
30
33
|
positionals: { min: 1, max: 1, label: '<account>' },
|
|
31
34
|
options: [
|
|
32
|
-
{ name: 'gh', kind: 'boolean', help: "also switch
|
|
35
|
+
{ name: 'gh', kind: 'boolean', help: "also switch gh's active account, or sign the account in to gh when needed (in a terminal)" },
|
|
33
36
|
{ name: 'name', kind: 'string', help: 'the commit author name (given with --email, skips the registry and the prompt)' },
|
|
34
37
|
{ name: 'email', kind: 'string', help: 'the commit author email (given with --name, skips the registry and the prompt)' },
|
|
35
38
|
],
|
|
@@ -49,8 +52,8 @@ export default {
|
|
|
49
52
|
return 1;
|
|
50
53
|
out.pass('identity', values.name + ' <' + values.email + '> push-as:' + account);
|
|
51
54
|
if (flagBool(args, 'gh'))
|
|
52
|
-
await
|
|
53
|
-
await reportConcerns(account, repo);
|
|
55
|
+
await applyGh(account, repo);
|
|
56
|
+
await reportConcerns(account, repo, values.email);
|
|
54
57
|
return 0;
|
|
55
58
|
},
|
|
56
59
|
};
|
|
@@ -120,6 +123,122 @@ async function askProfile(defaults, host) {
|
|
|
120
123
|
}
|
|
121
124
|
return { name: name.value, email: email.value, host };
|
|
122
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* What `--gh` should do. An account gh already lists is a switch even when it
|
|
128
|
+
* is already active: `switchCli` always runs `gh auth switch`. `none` means gh
|
|
129
|
+
* could not be queried, and the caller keeps that same switch attempt.
|
|
130
|
+
*/
|
|
131
|
+
export function ghAction(account, auth, interactive) {
|
|
132
|
+
if (!auth.gh.ok)
|
|
133
|
+
return 'none';
|
|
134
|
+
if (knownToGh(account, auth))
|
|
135
|
+
return 'switch';
|
|
136
|
+
return interactive ? 'login' : 'advise';
|
|
137
|
+
}
|
|
138
|
+
function knownToGh(account, auth) {
|
|
139
|
+
if (!auth.gh.ok)
|
|
140
|
+
return false;
|
|
141
|
+
const wanted = account.toLowerCase();
|
|
142
|
+
return auth.gh.value.accounts.some((item) => item.login.toLowerCase() === wanted);
|
|
143
|
+
}
|
|
144
|
+
const ASK = 'When gh asks "Authenticate Git with your GitHub credentials?", ';
|
|
145
|
+
const ANSWER_NO = ASK + 'type n and press Enter. Enter alone means Yes, and Yes makes gh ' +
|
|
146
|
+
'answer git\'s sign-in requests for every repository.';
|
|
147
|
+
const PIN_KEPT = 'This clone is already pinned. Cancelling the browser (Ctrl-C) only skips the gh sign-in.';
|
|
148
|
+
/** What to say about "Authenticate Git with your GitHub credentials?". Null when gh will not ask. */
|
|
149
|
+
export function ghCredentialAnswer(auth) {
|
|
150
|
+
if (auth.ghIsHelper)
|
|
151
|
+
return null;
|
|
152
|
+
if (auth.helper === null)
|
|
153
|
+
return ANSWER_NO;
|
|
154
|
+
return storesIn(auth.helperIsGcm, auth.helper);
|
|
155
|
+
}
|
|
156
|
+
function storesIn(gcm, helper) {
|
|
157
|
+
const where = gcm ? 'Git Credential Manager' : helper;
|
|
158
|
+
return ASK + 'press Enter (Yes): it also stores this sign-in in ' + where + ', so the first push won\'t ask again.';
|
|
159
|
+
}
|
|
160
|
+
async function applyGh(account, repo) {
|
|
161
|
+
const auth = await inspectAuth(repo.git, repo.originUrl ?? undefined);
|
|
162
|
+
const action = ghAction(account, auth, interactive());
|
|
163
|
+
if (action === 'advise')
|
|
164
|
+
return notSignedIn(account);
|
|
165
|
+
if (action === 'login')
|
|
166
|
+
return signInToGh(account, repo, auth);
|
|
167
|
+
await switchCli(account);
|
|
168
|
+
}
|
|
169
|
+
function notSignedIn(account) {
|
|
170
|
+
out.warn('gh', account + ' isn\'t signed in to gh');
|
|
171
|
+
out.detail('fix: gh auth login, then repown use ' + account + ' --gh');
|
|
172
|
+
}
|
|
173
|
+
async function signInToGh(account, repo, before) {
|
|
174
|
+
const verdict = await readGhLoginVersion();
|
|
175
|
+
if (verdict !== 'ready')
|
|
176
|
+
return oldGh(account, verdict);
|
|
177
|
+
announceLogin(account, before);
|
|
178
|
+
const logged = await ghLogin('github.com');
|
|
179
|
+
if (!logged.ok)
|
|
180
|
+
return loginFailed(logged.error);
|
|
181
|
+
await reportLogin(account, repo, before);
|
|
182
|
+
}
|
|
183
|
+
function oldGh(account, verdict) {
|
|
184
|
+
out.warn('gh', versionReason(verdict));
|
|
185
|
+
out.detail('fix: upgrade gh to 2.40.0 or newer, then repown use ' + account + ' --gh');
|
|
186
|
+
}
|
|
187
|
+
function versionReason(verdict) {
|
|
188
|
+
if (verdict === 'old')
|
|
189
|
+
return 'this gh replaces an account on login instead of adding one';
|
|
190
|
+
return 'gh\'s version could not be read, so sign-in was skipped';
|
|
191
|
+
}
|
|
192
|
+
export function announceLogin(account, auth) {
|
|
193
|
+
out.note('Your browser will open. Sign in to GitHub as ' + account + ' there.');
|
|
194
|
+
out.detail('a private window helps if the browser is signed in as someone else.');
|
|
195
|
+
out.note(PIN_KEPT);
|
|
196
|
+
const answer = ghCredentialAnswer(auth);
|
|
197
|
+
if (answer !== null)
|
|
198
|
+
out.note(answer);
|
|
199
|
+
}
|
|
200
|
+
function loginFailed(failure) {
|
|
201
|
+
if (failure.kind === 'cancelled') {
|
|
202
|
+
out.warn('gh', 'gh sign-in cancelled');
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
out.warn('gh', failure.reason);
|
|
206
|
+
out.detail('fix: gh auth login');
|
|
207
|
+
}
|
|
208
|
+
async function reportLogin(account, repo, before) {
|
|
209
|
+
const after = await inspectAuth(repo.git, repo.originUrl ?? undefined);
|
|
210
|
+
reportActive(account, after);
|
|
211
|
+
if (!before.ghIsHelper && after.ghIsHelper)
|
|
212
|
+
helperTakenOver();
|
|
213
|
+
}
|
|
214
|
+
export function reportActive(account, auth) {
|
|
215
|
+
if (!auth.gh.ok)
|
|
216
|
+
return unreadActive(auth.gh.error);
|
|
217
|
+
const who = auth.gh.value.active;
|
|
218
|
+
if (who !== null && who.toLowerCase() === account.toLowerCase()) {
|
|
219
|
+
out.pass('gh', 'signed in as ' + account + ', now gh\'s active account');
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (who === null)
|
|
223
|
+
return noActive();
|
|
224
|
+
otherActive(account, who);
|
|
225
|
+
}
|
|
226
|
+
function unreadActive(error) {
|
|
227
|
+
out.warn('gh', 'gh signed in, but who is active could not be read (' + error + ')');
|
|
228
|
+
out.detail('check it yourself: gh auth status');
|
|
229
|
+
}
|
|
230
|
+
function noActive() {
|
|
231
|
+
out.warn('gh', 'gh signed in, but no account is active');
|
|
232
|
+
out.detail('check it yourself: gh auth status');
|
|
233
|
+
}
|
|
234
|
+
function otherActive(account, who) {
|
|
235
|
+
out.warn('gh', 'gh signed in as "' + who + '", not ' + account);
|
|
236
|
+
out.detail('sign out of github.com in the browser (or use a private window), then: repown use ' + account + ' --gh');
|
|
237
|
+
}
|
|
238
|
+
function helperTakenOver() {
|
|
239
|
+
out.warn('gh', 'gh now answers git\'s sign-in requests, so clones of your other accounts get password prompts');
|
|
240
|
+
out.detail('fix: repown fix');
|
|
241
|
+
}
|
|
123
242
|
async function switchCli(account) {
|
|
124
243
|
const switched = await ghSwitch(account);
|
|
125
244
|
if (switched.ok)
|
|
@@ -128,7 +247,8 @@ async function switchCli(account) {
|
|
|
128
247
|
out.warn('gh', 'could not switch: ' + switched.error);
|
|
129
248
|
}
|
|
130
249
|
/** Everything that is now true but not yet right. Warnings, never refusals. */
|
|
131
|
-
async function reportConcerns(account, repo) {
|
|
250
|
+
async function reportConcerns(account, repo, email) {
|
|
251
|
+
await reportUnpushed(repo, email);
|
|
132
252
|
if (repo.credentialKeys.length === 0 && repo.url) {
|
|
133
253
|
out.warn('host', repo.provider.label + ' credentials are not pinned by repown.');
|
|
134
254
|
out.detail('commits are pinned and the guard still runs; only credential');
|
|
@@ -138,9 +258,18 @@ async function reportConcerns(account, repo) {
|
|
|
138
258
|
await credentialConcern(account, repo);
|
|
139
259
|
if (repo.guard === 'off' && !repo.hook?.redirected) {
|
|
140
260
|
out.line();
|
|
141
|
-
out.line(
|
|
261
|
+
out.line(NEXT_GUARD);
|
|
142
262
|
}
|
|
143
263
|
}
|
|
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);
|
|
267
|
+
if (!first)
|
|
268
|
+
return;
|
|
269
|
+
out.warn('commits', first);
|
|
270
|
+
for (const line of rest)
|
|
271
|
+
out.detail(line);
|
|
272
|
+
}
|
|
144
273
|
async function ownerConcern(account, repo) {
|
|
145
274
|
if (!repo.owner || repo.owner.toLowerCase() === account.toLowerCase())
|
|
146
275
|
return;
|