@yolo-labs/yolo-cli 0.43.0 → 0.44.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +58 -41
- package/dist/artifact-get.js +2 -9
- package/dist/cli.js +31 -126
- package/dist/deploy-client.js +10 -20
- package/dist/deploy-config.js +2 -3
- package/dist/exit.js +34 -0
- package/dist/git-doctor.js +458 -0
- package/dist/kanban-cli.js +352 -0
- package/dist/preview.js +145 -0
- package/dist/tileapp-oci-assembler.js +2 -2
- package/dist/tileapp-validator.js +6 -0
- package/dist/work-client.js +11 -60
- package/package.json +13 -7
- package/dist/canonicalizer.js +0 -208
- package/dist/lockfile.js +0 -203
- package/dist/plan-frontmatter.schema.json +0 -191
- package/dist/plan-validate.js +0 -270
- package/dist/revision.js +0 -43
- package/dist/run-list.js +0 -148
- package/dist/webapp-url.js +0 -61
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `yolo git doctor [--fix]` — diagnose, and optionally repair, how git in this
|
|
3
|
+
* workspace authenticates to github.com over HTTPS.
|
|
4
|
+
*
|
|
5
|
+
* git and `gh` resolve credentials differently. `gh` (the /usr/local/bin
|
|
6
|
+
* wrapper) re-reads ~/.yolo-env on every call, so it always holds the current
|
|
7
|
+
* token. git asks its credential helpers: `store` (~/.git-credentials), then
|
|
8
|
+
* the github.com-scoped `gh auth git-credential` fallback from setup-git.sh.
|
|
9
|
+
* When the two disagree, `gh` works and `git push` fails with "could not read
|
|
10
|
+
* Username" — which is what a root exec leaving ~/.git-credentials root-owned
|
|
11
|
+
* 0600 looked like (the lane-gc probe, until #1181). This command looks at
|
|
12
|
+
* every link in git's chain and says which one is broken.
|
|
13
|
+
*
|
|
14
|
+
* Checks: the token (and, online, whether GitHub accepts it), the store file
|
|
15
|
+
* (present, readable, owned by the HOME owner, 0600, holding the current
|
|
16
|
+
* token), the helper config, the push URL of the remote, and — online, in a
|
|
17
|
+
* repository — a real `git ls-remote`.
|
|
18
|
+
*
|
|
19
|
+
* --fix repairs what it safely can, then re-runs every check:
|
|
20
|
+
* - rewrites ~/.git-credentials from the current token by temp file + rename
|
|
21
|
+
* (a rename replaces a file we cannot write through; we own the directory),
|
|
22
|
+
* keeping readable non-github lines, or the forge line from the pod env
|
|
23
|
+
* - sets `credential.helper store` / the github.com gh fallback if missing
|
|
24
|
+
* - strips credentials embedded in a github.com remote URL
|
|
25
|
+
* It only writes a token GitHub accepted in this run, and never while the
|
|
26
|
+
* Auth V2 purge fence is up: that fence means GitHub access was withdrawn, and
|
|
27
|
+
* reinstalling the token would undo the revocation.
|
|
28
|
+
*
|
|
29
|
+
* Tokens are never printed; they are masked to prefix…last4.
|
|
30
|
+
*
|
|
31
|
+
* Exit codes: 0 = no failing check; 1 = a check still fails; 64 = usage.
|
|
32
|
+
*/
|
|
33
|
+
import * as fs from 'node:fs';
|
|
34
|
+
import * as path from 'node:path';
|
|
35
|
+
import { spawnSync } from 'node:child_process';
|
|
36
|
+
export const GIT_DOCTOR_HELP = `Usage: yolo git doctor [--fix] [--offline] [--remote <name>] [--json]
|
|
37
|
+
|
|
38
|
+
Diagnose why git (not gh) can't authenticate to github.com over HTTPS.
|
|
39
|
+
|
|
40
|
+
--fix Repair what can be repaired safely, then re-check.
|
|
41
|
+
--offline Skip the GitHub API token check and git ls-remote.
|
|
42
|
+
(--fix won't rewrite ~/.git-credentials without the token check.)
|
|
43
|
+
--remote <name> Remote to inspect and probe (default: origin).
|
|
44
|
+
--json Print {checks, fixed} as JSON.
|
|
45
|
+
`;
|
|
46
|
+
const GH_FALLBACK_KEY = 'credential.https://github.com.helper';
|
|
47
|
+
const GH_FALLBACK_VALUE = '!gh auth git-credential';
|
|
48
|
+
const GITHUB_LINE = /^https:\/\/[^:@/\s]+:([^@\s]+)@github\.com\/?$/;
|
|
49
|
+
/** prefix…last4, so two tokens can be told apart without either being shown. */
|
|
50
|
+
export function maskToken(token) {
|
|
51
|
+
if (token.length <= 12)
|
|
52
|
+
return '****';
|
|
53
|
+
const prefix = /^(github_pat_|gh[pousr]_)/.exec(token)?.[1] ?? token.slice(0, 4);
|
|
54
|
+
return `${prefix}…${token.slice(-4)}`;
|
|
55
|
+
}
|
|
56
|
+
/** Mask anything token-shaped in free text (git stderr, URLs). */
|
|
57
|
+
export function redact(text) {
|
|
58
|
+
return text
|
|
59
|
+
.replace(/(github_pat_|gh[pousr]_)[A-Za-z0-9_]+/g, (t) => maskToken(t))
|
|
60
|
+
.replace(/(\/\/[^:@/\s]+:)[^@\s]+@/g, '$1****@');
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The token `gh` would use: the wrapper sources ~/.yolo-env over the process
|
|
64
|
+
* env, then falls back from GH_TOKEN to GITHUB_TOKEN.
|
|
65
|
+
*/
|
|
66
|
+
export function resolveToken(env, home) {
|
|
67
|
+
const file = env.YOLO_ENV_FILE || path.join(home, '.yolo-env');
|
|
68
|
+
const fromFile = {};
|
|
69
|
+
try {
|
|
70
|
+
for (const line of fs.readFileSync(file, 'utf8').split('\n')) {
|
|
71
|
+
const m = /^export (GH_TOKEN|GITHUB_TOKEN)='([^']*)'$/.exec(line.trim());
|
|
72
|
+
if (m && m[2])
|
|
73
|
+
fromFile[m[1]] = m[2];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch { /* no ~/.yolo-env */ }
|
|
77
|
+
const candidates = [
|
|
78
|
+
[fromFile.GH_TOKEN, 'GH_TOKEN (~/.yolo-env)'],
|
|
79
|
+
[env.GH_TOKEN, 'GH_TOKEN (env)'],
|
|
80
|
+
[fromFile.GITHUB_TOKEN, 'GITHUB_TOKEN (~/.yolo-env)'],
|
|
81
|
+
[env.GITHUB_TOKEN, 'GITHUB_TOKEN (env)'],
|
|
82
|
+
];
|
|
83
|
+
for (const [token, source] of candidates)
|
|
84
|
+
if (token)
|
|
85
|
+
return { token, source };
|
|
86
|
+
return { source: 'none' };
|
|
87
|
+
}
|
|
88
|
+
function defaultGit(cwd, env) {
|
|
89
|
+
return (args, opts = {}) => {
|
|
90
|
+
const r = spawnSync('git', args, {
|
|
91
|
+
cwd,
|
|
92
|
+
env: { ...env, GIT_TERMINAL_PROMPT: '0' },
|
|
93
|
+
encoding: 'utf8',
|
|
94
|
+
timeout: opts.timeoutMs ?? 10_000,
|
|
95
|
+
});
|
|
96
|
+
return { status: r.status, stdout: r.stdout ?? '', stderr: r.stderr ?? (r.error ? String(r.error.message) : '') };
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function defaultHasCommand(env) {
|
|
100
|
+
return (name) => (env.PATH ?? '').split(':').some((dir) => {
|
|
101
|
+
try {
|
|
102
|
+
fs.accessSync(path.join(dir, name), fs.constants.X_OK);
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function getAll(git, args) {
|
|
111
|
+
const r = git(['config', ...args]);
|
|
112
|
+
// One value per line; an empty value (the anonymous-mode reset) is a blank line.
|
|
113
|
+
return r.status === 0 ? r.stdout.replace(/\n$/, '').split('\n') : [];
|
|
114
|
+
}
|
|
115
|
+
async function checkToken(ctx, d) {
|
|
116
|
+
const { token, source } = resolveToken(ctx.env, ctx.home);
|
|
117
|
+
if (!token) {
|
|
118
|
+
d.checks.push({ id: 'token', status: 'fail', detail: 'no GitHub token (GH_TOKEN / GITHUB_TOKEN, env or ~/.yolo-env)' });
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
const label = `${maskToken(token)} from ${source}`;
|
|
122
|
+
if (ctx.opts.offline) {
|
|
123
|
+
d.checks.push({ id: 'token', status: 'skip', detail: `${label}; not verified (--offline)` });
|
|
124
|
+
return token;
|
|
125
|
+
}
|
|
126
|
+
try {
|
|
127
|
+
const res = await ctx.fetchImpl('https://api.github.com/user', {
|
|
128
|
+
headers: { Authorization: `token ${token}`, 'User-Agent': 'yolo-git-doctor', Accept: 'application/vnd.github+json' },
|
|
129
|
+
});
|
|
130
|
+
if (res.status === 200) {
|
|
131
|
+
const login = (await res.json())?.login ?? '?';
|
|
132
|
+
const scopes = res.headers.get('x-oauth-scopes');
|
|
133
|
+
const expires = res.headers.get('github-authentication-token-expiration');
|
|
134
|
+
d.acceptedToken = token;
|
|
135
|
+
d.checks.push({ id: 'token', status: 'ok', detail: `${label}: ${login}` +
|
|
136
|
+
(scopes !== null ? `, scopes: ${scopes || '(none)'}` : '') + (expires ? `, expires ${expires}` : '') });
|
|
137
|
+
}
|
|
138
|
+
else if (res.status === 401) {
|
|
139
|
+
d.checks.push({ id: 'token', status: 'fail', detail: `${label}: GitHub rejected it (401: revoked or expired)` });
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
d.checks.push({ id: 'token', status: 'warn', detail: `${label}: GitHub API answered ${res.status}` });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
catch (err) {
|
|
146
|
+
d.checks.push({ id: 'token', status: 'warn', detail: `${label}: could not reach the GitHub API (${err.message})` });
|
|
147
|
+
}
|
|
148
|
+
return token;
|
|
149
|
+
}
|
|
150
|
+
function describeOwner(st) {
|
|
151
|
+
return `uid ${st.uid}:${st.gid}, mode ${(st.mode & 0o777).toString(8)}`;
|
|
152
|
+
}
|
|
153
|
+
function checkCredentialsFile(ctx, d, token) {
|
|
154
|
+
const file = path.join(ctx.home, '.git-credentials');
|
|
155
|
+
let st;
|
|
156
|
+
try {
|
|
157
|
+
st = fs.statSync(file);
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
d.checks.push({ id: 'credentials-file', status: 'fail', detail: '~/.git-credentials does not exist' });
|
|
161
|
+
d.credsNeedRewrite = true;
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
let content;
|
|
165
|
+
try {
|
|
166
|
+
content = fs.readFileSync(file, 'utf8');
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
d.checks.push({ id: 'credentials-file', status: 'fail', detail: `~/.git-credentials is not readable by uid ${ctx.getuid()} (${describeOwner(st)}): git's store helper finds nothing` });
|
|
170
|
+
d.credsNeedRewrite = true;
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const problems = [];
|
|
174
|
+
let status = 'ok';
|
|
175
|
+
let homeUid;
|
|
176
|
+
try {
|
|
177
|
+
homeUid = fs.statSync(ctx.home).uid;
|
|
178
|
+
}
|
|
179
|
+
catch { /* leave undefined */ }
|
|
180
|
+
if (homeUid !== undefined && st.uid !== homeUid) {
|
|
181
|
+
problems.push(`owned by uid ${st.uid}, not the HOME owner (uid ${homeUid})`);
|
|
182
|
+
status = 'warn';
|
|
183
|
+
d.credsNeedRewrite = true;
|
|
184
|
+
}
|
|
185
|
+
if ((st.mode & 0o077) !== 0) {
|
|
186
|
+
problems.push(`mode ${(st.mode & 0o777).toString(8)} is readable by others (want 600)`);
|
|
187
|
+
status = 'warn';
|
|
188
|
+
d.credsNeedRewrite = true;
|
|
189
|
+
}
|
|
190
|
+
const stored = content.split('\n').map((l) => GITHUB_LINE.exec(l.trim())?.[1]).filter(Boolean);
|
|
191
|
+
if (stored.length === 0) {
|
|
192
|
+
problems.push('has no github.com line');
|
|
193
|
+
status = 'fail';
|
|
194
|
+
d.credsNeedRewrite = true;
|
|
195
|
+
}
|
|
196
|
+
else if (token && !stored.includes(token)) {
|
|
197
|
+
problems.push(`github.com token ${maskToken(stored[0])} is not the current token ${maskToken(token)}`);
|
|
198
|
+
if (status === 'ok')
|
|
199
|
+
status = 'warn';
|
|
200
|
+
d.credsNeedRewrite = true;
|
|
201
|
+
}
|
|
202
|
+
d.checks.push({ id: 'credentials-file', status, detail: problems.length
|
|
203
|
+
? `~/.git-credentials ${problems.join('; ')}`
|
|
204
|
+
: `~/.git-credentials readable, 0600, holds the current github.com token` });
|
|
205
|
+
}
|
|
206
|
+
function checkHelpers(ctx, d) {
|
|
207
|
+
const helpers = getAll(ctx.git, ['--global', '--get-all', 'credential.helper']);
|
|
208
|
+
const fallback = getAll(ctx.git, ['--global', '--get-all', GH_FALLBACK_KEY]);
|
|
209
|
+
const problems = [];
|
|
210
|
+
let status = 'ok';
|
|
211
|
+
if (!helpers.includes('store')) {
|
|
212
|
+
problems.push(`global credential.helper is ${helpers.length ? helpers.map((h) => `'${h}'`).join(', ') : 'unset'}, not 'store'`);
|
|
213
|
+
status = 'fail';
|
|
214
|
+
d.needStoreHelper = true;
|
|
215
|
+
}
|
|
216
|
+
if (helpers.length > 1) {
|
|
217
|
+
problems.push(`credential.helper has ${helpers.length} values; the setup scripts' \`git config --global credential.helper store\` exits 5 on that`);
|
|
218
|
+
if (status === 'ok')
|
|
219
|
+
status = 'warn';
|
|
220
|
+
}
|
|
221
|
+
if (!fallback.includes(GH_FALLBACK_VALUE)) {
|
|
222
|
+
if (ctx.hasCommand('gh')) {
|
|
223
|
+
problems.push(`no github.com gh fallback (${GH_FALLBACK_KEY})`);
|
|
224
|
+
d.needFallback = true;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
problems.push('no github.com gh fallback, and gh is not on PATH');
|
|
228
|
+
}
|
|
229
|
+
if (status === 'ok')
|
|
230
|
+
status = 'warn';
|
|
231
|
+
}
|
|
232
|
+
if (d.inRepo) {
|
|
233
|
+
const local = getAll(ctx.git, ['--local', '--get-all', 'credential.helper']);
|
|
234
|
+
if (local.includes('')) {
|
|
235
|
+
problems.push("this repository resets credential.helper to '' (anonymous mode): git sends no credentials here");
|
|
236
|
+
if (status === 'ok')
|
|
237
|
+
status = 'warn';
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
d.checks.push({ id: 'helper', status, detail: problems.length ? problems.join('; ')
|
|
241
|
+
: `store, then ${GH_FALLBACK_VALUE} for github.com` });
|
|
242
|
+
}
|
|
243
|
+
function checkRemote(ctx, d) {
|
|
244
|
+
if (!d.inRepo) {
|
|
245
|
+
d.checks.push({ id: 'remote', status: 'skip', detail: 'not inside a git repository' });
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
const name = ctx.opts.remote;
|
|
249
|
+
const url = ctx.git(['remote', 'get-url', '--push', name]);
|
|
250
|
+
if (url.status !== 0) {
|
|
251
|
+
d.checks.push({ id: 'remote', status: 'skip', detail: `no remote named '${name}'` });
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const pushUrl = url.stdout.trim();
|
|
255
|
+
const m = /^https:\/\/([^@/]+)@(github\.com\/.*)$/.exec(pushUrl);
|
|
256
|
+
if (m) {
|
|
257
|
+
const hasPushUrl = getAll(ctx.git, ['--get-all', `remote.${name}.pushurl`]).length > 0;
|
|
258
|
+
d.embeddedUrl = { key: hasPushUrl ? 'pushurl' : 'url', clean: `https://${m[2]}` };
|
|
259
|
+
d.checks.push({ id: 'remote', status: 'warn', detail: `${name} push URL embeds credentials (${redact(pushUrl)}); they override the credential helpers and go stale` });
|
|
260
|
+
}
|
|
261
|
+
else if (/^(git@|ssh:\/\/)/.test(pushUrl)) {
|
|
262
|
+
d.checks.push({ id: 'remote', status: 'ok', detail: `${name} pushes over SSH (${pushUrl}); HTTPS credentials aren't used for it` });
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
d.checks.push({ id: 'remote', status: 'ok', detail: `${name} → ${redact(pushUrl)}` });
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
function checkLsRemote(ctx, d) {
|
|
269
|
+
if (ctx.opts.offline) {
|
|
270
|
+
d.checks.push({ id: 'ls-remote', status: 'skip', detail: 'skipped (--offline)' });
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (!d.inRepo || d.checks.find((c) => c.id === 'remote')?.status === 'skip') {
|
|
274
|
+
d.checks.push({ id: 'ls-remote', status: 'skip', detail: 'no remote to probe' });
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
// ls-remote authenticates like a fetch; a push additionally needs push rights,
|
|
278
|
+
// which the token check's repo scope covers.
|
|
279
|
+
const r = ctx.git(['ls-remote', '--exit-code', ctx.opts.remote, 'HEAD'], { timeoutMs: 20_000 });
|
|
280
|
+
if (r.status === 0) {
|
|
281
|
+
d.checks.push({ id: 'ls-remote', status: 'ok', detail: `git ls-remote ${ctx.opts.remote} authenticated` });
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
const last = redact(r.stderr.trim().split('\n').filter(Boolean).pop() ?? `exit ${r.status}`);
|
|
285
|
+
d.checks.push({ id: 'ls-remote', status: 'fail', detail: `git ls-remote ${ctx.opts.remote} failed: ${last}` });
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
async function diagnose(ctx) {
|
|
289
|
+
const inRepo = ctx.git(['rev-parse', '--is-inside-work-tree']).stdout.trim() === 'true';
|
|
290
|
+
const d = { checks: [], inRepo, credsNeedRewrite: false, needStoreHelper: false, needFallback: false };
|
|
291
|
+
const token = await checkToken(ctx, d);
|
|
292
|
+
checkCredentialsFile(ctx, d, token);
|
|
293
|
+
checkHelpers(ctx, d);
|
|
294
|
+
checkRemote(ctx, d);
|
|
295
|
+
checkLsRemote(ctx, d);
|
|
296
|
+
return d;
|
|
297
|
+
}
|
|
298
|
+
/** Temp file + rename: replaces a file we can't write through, as long as we own the directory. */
|
|
299
|
+
function writeCredentialsFile(ctx, token) {
|
|
300
|
+
const file = path.join(ctx.home, '.git-credentials');
|
|
301
|
+
let others;
|
|
302
|
+
try {
|
|
303
|
+
others = fs.readFileSync(file, 'utf8').split('\n').filter((l) => l.trim() && !/@github\.com\/?$/.test(l.trim()));
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
const { FORGEJO_TOKEN, FORGEJO_HOST } = ctx.env;
|
|
307
|
+
others = FORGEJO_TOKEN && FORGEJO_HOST ? [`https://oauth2:${FORGEJO_TOKEN}@${FORGEJO_HOST}`] : [];
|
|
308
|
+
}
|
|
309
|
+
const content = [`https://oauth2:${token}@github.com`, ...others].join('\n') + '\n';
|
|
310
|
+
const tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
311
|
+
try {
|
|
312
|
+
fs.writeFileSync(tmp, content, { mode: 0o600, flag: 'wx' });
|
|
313
|
+
if (ctx.getuid() === 0) {
|
|
314
|
+
const owner = fs.statSync(ctx.home);
|
|
315
|
+
fs.chownSync(tmp, owner.uid, owner.gid);
|
|
316
|
+
}
|
|
317
|
+
fs.renameSync(tmp, file);
|
|
318
|
+
}
|
|
319
|
+
finally {
|
|
320
|
+
try {
|
|
321
|
+
fs.unlinkSync(tmp);
|
|
322
|
+
}
|
|
323
|
+
catch { /* renamed */ }
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
function fencePath(home) {
|
|
327
|
+
return path.join(home, '.yolo', 'auth-v2-github-purge-fence.json');
|
|
328
|
+
}
|
|
329
|
+
function applyFixes(ctx, d) {
|
|
330
|
+
const fixed = [];
|
|
331
|
+
const refused = [];
|
|
332
|
+
if (d.credsNeedRewrite) {
|
|
333
|
+
if (fs.existsSync(fencePath(ctx.home))) {
|
|
334
|
+
refused.push('~/.git-credentials: GitHub access was withdrawn (Auth V2 purge fence is up); not reinstalling the token');
|
|
335
|
+
}
|
|
336
|
+
else if (!d.acceptedToken) {
|
|
337
|
+
refused.push(ctx.opts.offline
|
|
338
|
+
? '~/.git-credentials: not rewritten under --offline (the token must be verified first)'
|
|
339
|
+
: '~/.git-credentials: not rewritten, because GitHub did not accept the current token');
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
try {
|
|
343
|
+
writeCredentialsFile(ctx, d.acceptedToken);
|
|
344
|
+
fixed.push(`rewrote ~/.git-credentials with ${maskToken(d.acceptedToken)}, 0600, owned by the HOME owner`);
|
|
345
|
+
}
|
|
346
|
+
catch (err) {
|
|
347
|
+
refused.push(`~/.git-credentials: rewrite failed (${err.message})`);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (d.needStoreHelper) {
|
|
352
|
+
// --add keeps any other helper the user configured; a single value becomes 'store'.
|
|
353
|
+
const hasAny = getAll(ctx.git, ['--global', '--get-all', 'credential.helper']).length > 0;
|
|
354
|
+
const r = ctx.git(['config', '--global', ...(hasAny ? ['--add'] : []), 'credential.helper', 'store']);
|
|
355
|
+
(r.status === 0 ? fixed : refused).push(r.status === 0 ? "set global credential.helper 'store'"
|
|
356
|
+
: `credential.helper: git config failed (${r.stderr.trim()})`);
|
|
357
|
+
}
|
|
358
|
+
if (d.needFallback) {
|
|
359
|
+
const r = ctx.git(['config', '--global', GH_FALLBACK_KEY, GH_FALLBACK_VALUE]);
|
|
360
|
+
(r.status === 0 ? fixed : refused).push(r.status === 0 ? `set ${GH_FALLBACK_KEY} '${GH_FALLBACK_VALUE}'`
|
|
361
|
+
: `${GH_FALLBACK_KEY}: git config failed (${r.stderr.trim()})`);
|
|
362
|
+
}
|
|
363
|
+
if (d.embeddedUrl) {
|
|
364
|
+
const { key, clean } = d.embeddedUrl;
|
|
365
|
+
const args = key === 'url' ? ['remote', 'set-url', ctx.opts.remote, clean] : ['remote', 'set-url', '--push', ctx.opts.remote, clean];
|
|
366
|
+
const r = ctx.git(args);
|
|
367
|
+
(r.status === 0 ? fixed : refused).push(r.status === 0 ? `removed credentials from ${ctx.opts.remote} ${key} (${clean})`
|
|
368
|
+
: `remote ${key}: git remote set-url failed (${redact(r.stderr.trim())})`);
|
|
369
|
+
}
|
|
370
|
+
return { fixed, refused };
|
|
371
|
+
}
|
|
372
|
+
const ICON = { ok: '✓', warn: '!', fail: '✗', skip: '-' };
|
|
373
|
+
export function parseGitDoctorArgs(args) {
|
|
374
|
+
const opts = { fix: false, json: false, offline: false, remote: 'origin' };
|
|
375
|
+
for (let i = 0; i < args.length; i++) {
|
|
376
|
+
const a = args[i];
|
|
377
|
+
if (a === '--fix')
|
|
378
|
+
opts.fix = true;
|
|
379
|
+
else if (a === '--json')
|
|
380
|
+
opts.json = true;
|
|
381
|
+
else if (a === '--offline')
|
|
382
|
+
opts.offline = true;
|
|
383
|
+
else if (a === '--remote') {
|
|
384
|
+
const v = args[++i];
|
|
385
|
+
if (!v || v.startsWith('-'))
|
|
386
|
+
return { error: '--remote needs a value' };
|
|
387
|
+
opts.remote = v;
|
|
388
|
+
}
|
|
389
|
+
else
|
|
390
|
+
return { error: `unknown option '${a}'` };
|
|
391
|
+
}
|
|
392
|
+
return opts;
|
|
393
|
+
}
|
|
394
|
+
export async function runGitDoctor(opts, deps = {}) {
|
|
395
|
+
const env = deps.env ?? process.env;
|
|
396
|
+
const home = deps.home ?? env.HOME ?? '/home/yolo';
|
|
397
|
+
const cwd = deps.cwd ?? process.cwd();
|
|
398
|
+
const out = deps.stdout ?? ((t) => process.stdout.write(t));
|
|
399
|
+
const ctx = {
|
|
400
|
+
env,
|
|
401
|
+
home,
|
|
402
|
+
git: deps.git ?? defaultGit(cwd, { ...env, HOME: home }),
|
|
403
|
+
fetchImpl: deps.fetchImpl ?? globalThis.fetch,
|
|
404
|
+
getuid: deps.getuid ?? (() => process.getuid?.() ?? -1),
|
|
405
|
+
hasCommand: deps.hasCommand ?? defaultHasCommand(env),
|
|
406
|
+
opts,
|
|
407
|
+
};
|
|
408
|
+
let d = await diagnose(ctx);
|
|
409
|
+
let fixed = [];
|
|
410
|
+
let refused = [];
|
|
411
|
+
if (opts.fix) {
|
|
412
|
+
({ fixed, refused } = applyFixes(ctx, d));
|
|
413
|
+
if (fixed.length)
|
|
414
|
+
d = await diagnose(ctx);
|
|
415
|
+
}
|
|
416
|
+
const failing = d.checks.filter((c) => c.status === 'fail').length;
|
|
417
|
+
if (opts.json) {
|
|
418
|
+
out(JSON.stringify({ checks: d.checks, fixed, refused }, null, 2) + '\n');
|
|
419
|
+
return failing ? 1 : 0;
|
|
420
|
+
}
|
|
421
|
+
const width = Math.max(...d.checks.map((c) => c.id.length));
|
|
422
|
+
const lines = ['yolo git doctor', ...d.checks.map((c) => ` ${ICON[c.status]} ${c.id.padEnd(width)} ${c.detail}`)];
|
|
423
|
+
if (fixed.length)
|
|
424
|
+
lines.push('', 'Fixed:', ...fixed.map((f) => ` - ${f}`));
|
|
425
|
+
if (refused.length)
|
|
426
|
+
lines.push('', 'Not fixed:', ...refused.map((f) => ` - ${f}`));
|
|
427
|
+
const warns = d.checks.filter((c) => c.status === 'warn').length;
|
|
428
|
+
lines.push('');
|
|
429
|
+
if (failing) {
|
|
430
|
+
lines.push(`${failing} failing check${failing > 1 ? 's' : ''}.` + (opts.fix ? '' : ' Run `yolo git doctor --fix` to repair.'));
|
|
431
|
+
}
|
|
432
|
+
else if (warns) {
|
|
433
|
+
lines.push(`No failures; ${warns} warning${warns > 1 ? 's' : ''}.` + (opts.fix ? '' : ' `yolo git doctor --fix` repairs the fixable ones.'));
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
lines.push('git can authenticate to github.com.');
|
|
437
|
+
}
|
|
438
|
+
out(lines.join('\n') + '\n');
|
|
439
|
+
return failing ? 1 : 0;
|
|
440
|
+
}
|
|
441
|
+
export async function runGitCmd(args, deps = {}) {
|
|
442
|
+
const err = deps.stderr ?? ((t) => process.stderr.write(t));
|
|
443
|
+
const out = deps.stdout ?? ((t) => process.stdout.write(t));
|
|
444
|
+
if (args[0] !== 'doctor') {
|
|
445
|
+
err(args[0] ? `yolo git: unknown subcommand '${args[0]}'\n${GIT_DOCTOR_HELP}` : `yolo git: requires a subcommand (doctor)\n${GIT_DOCTOR_HELP}`);
|
|
446
|
+
return 64;
|
|
447
|
+
}
|
|
448
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
449
|
+
out(GIT_DOCTOR_HELP);
|
|
450
|
+
return 0;
|
|
451
|
+
}
|
|
452
|
+
const opts = parseGitDoctorArgs(args.slice(1));
|
|
453
|
+
if ('error' in opts) {
|
|
454
|
+
err(`yolo git doctor: ${opts.error}\n${GIT_DOCTOR_HELP}`);
|
|
455
|
+
return 64;
|
|
456
|
+
}
|
|
457
|
+
return runGitDoctor(opts, deps);
|
|
458
|
+
}
|