repown 0.1.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 +27 -0
- package/LICENSE +21 -0
- package/README.md +217 -0
- package/dist/cli.js +33 -0
- package/dist/commands/accounts.js +146 -0
- package/dist/commands/doctor.js +100 -0
- package/dist/commands/fix.js +90 -0
- package/dist/commands/guard.js +103 -0
- package/dist/commands/off.js +58 -0
- package/dist/commands/scan.js +224 -0
- package/dist/commands/status.js +168 -0
- package/dist/commands/use.js +164 -0
- package/dist/core/credential/gcm.js +83 -0
- package/dist/core/credential/gh.js +78 -0
- package/dist/core/credential/repair.js +84 -0
- package/dist/core/exec.js +82 -0
- package/dist/core/git.js +226 -0
- package/dist/core/guard/check.js +245 -0
- package/dist/core/guard/hook.js +184 -0
- package/dist/core/hosts/azdo.js +59 -0
- package/dist/core/hosts/generic.js +16 -0
- package/dist/core/hosts/github.js +35 -0
- package/dist/core/hosts/index.js +14 -0
- package/dist/core/hosts/types.js +9 -0
- package/dist/core/identity.js +96 -0
- package/dist/core/inspect.js +74 -0
- package/dist/core/registry.js +122 -0
- package/dist/core/result.js +13 -0
- package/dist/core/url.js +82 -0
- package/dist/ui/args.js +145 -0
- package/dist/ui/command.js +25 -0
- package/dist/ui/dispatch.js +142 -0
- package/dist/ui/format.js +84 -0
- package/dist/ui/help.js +97 -0
- package/dist/ui/prompt.js +43 -0
- package/dist/ui/suggest.js +33 -0
- package/package.json +59 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// The pre-push guard: turning it on, off, and running the check itself.
|
|
2
|
+
//
|
|
3
|
+
// `guard check` is what the installed hook invokes, with the exact syntax baked
|
|
4
|
+
// into every hook already on disk (src/core/guard/hook.ts) -- `--remote` and
|
|
5
|
+
// `--url` on this action are a frozen contract, not just today's flags.
|
|
6
|
+
import { installGuard, uninstallGuard, guardState } from "../core/guard/hook.js";
|
|
7
|
+
import { check } from "../core/guard/check.js";
|
|
8
|
+
import { flagString, gitFor } from "../ui/args.js";
|
|
9
|
+
import * as out from "../ui/format.js";
|
|
10
|
+
/** "off" is an answer about a repository; outside one, it would be a guess. */
|
|
11
|
+
async function status(args) {
|
|
12
|
+
const git = gitFor(args);
|
|
13
|
+
if (!(await git.isRepo())) {
|
|
14
|
+
out.fail('guard', 'Not a git repository: ' + git.cwd);
|
|
15
|
+
return 1;
|
|
16
|
+
}
|
|
17
|
+
out.field('push guard', await guardState(git));
|
|
18
|
+
return 0;
|
|
19
|
+
}
|
|
20
|
+
async function enable(args) {
|
|
21
|
+
const installed = await installGuard(gitFor(args));
|
|
22
|
+
if (!installed.ok) {
|
|
23
|
+
out.fail('guard', installed.error);
|
|
24
|
+
return 1;
|
|
25
|
+
}
|
|
26
|
+
out.pass('guard', 'on -- every push is checked before it leaves');
|
|
27
|
+
out.line(out.dim(' ' + installed.value.path));
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
30
|
+
async function disable(args) {
|
|
31
|
+
const removed = await uninstallGuard(gitFor(args));
|
|
32
|
+
if (!removed.ok) {
|
|
33
|
+
out.fail('guard', removed.error);
|
|
34
|
+
return 1;
|
|
35
|
+
}
|
|
36
|
+
if (!removed.value) {
|
|
37
|
+
out.warn('guard', 'was not installed; nothing to remove.');
|
|
38
|
+
return 0;
|
|
39
|
+
}
|
|
40
|
+
out.pass('guard', 'off -- pushes are no longer checked');
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
async function runCheck(args) {
|
|
44
|
+
const refusals = await check({
|
|
45
|
+
git: gitFor(args),
|
|
46
|
+
remote: flagString(args, 'remote') ?? 'origin',
|
|
47
|
+
url: flagString(args, 'url') ?? '',
|
|
48
|
+
stdin: await readStdin(),
|
|
49
|
+
onNote: (note) => out.warn('guard', note),
|
|
50
|
+
});
|
|
51
|
+
if (refusals.length === 0) {
|
|
52
|
+
out.pass('guard', 'every commit in this push carries this clone’s identity');
|
|
53
|
+
return 0;
|
|
54
|
+
}
|
|
55
|
+
report(refusals);
|
|
56
|
+
return 1;
|
|
57
|
+
}
|
|
58
|
+
function report(refusals) {
|
|
59
|
+
out.line();
|
|
60
|
+
for (const refusal of refusals) {
|
|
61
|
+
out.fail('guard', refusal.reason);
|
|
62
|
+
for (const line of refusal.detail)
|
|
63
|
+
out.detail(line);
|
|
64
|
+
out.line();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** Empty when nothing is piped in, so `repown guard check` by hand is not a hang. */
|
|
68
|
+
async function readStdin() {
|
|
69
|
+
if (process.stdin.isTTY)
|
|
70
|
+
return '';
|
|
71
|
+
const chunks = [];
|
|
72
|
+
for await (const chunk of process.stdin)
|
|
73
|
+
chunks.push(chunk);
|
|
74
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
75
|
+
}
|
|
76
|
+
const onAction = {
|
|
77
|
+
summary: 'install the pre-push hook',
|
|
78
|
+
examples: ['repown guard on'],
|
|
79
|
+
run: enable,
|
|
80
|
+
};
|
|
81
|
+
const offAction = {
|
|
82
|
+
summary: 'remove the pre-push hook',
|
|
83
|
+
examples: ['repown guard off'],
|
|
84
|
+
run: disable,
|
|
85
|
+
};
|
|
86
|
+
const statusAction = {
|
|
87
|
+
summary: 'show whether the guard is installed',
|
|
88
|
+
run: status,
|
|
89
|
+
};
|
|
90
|
+
const checkAction = {
|
|
91
|
+
summary: 'run the check the installed hook calls (not for direct use)',
|
|
92
|
+
options: [
|
|
93
|
+
{ name: 'remote', kind: 'string', default: 'origin', help: 'the remote name being pushed to' },
|
|
94
|
+
{ name: 'url', kind: 'string', default: '', help: 'the remote URL being pushed to' },
|
|
95
|
+
],
|
|
96
|
+
run: runCheck,
|
|
97
|
+
};
|
|
98
|
+
export default {
|
|
99
|
+
summary: 'check every push before it leaves (repown guard on | off | status)',
|
|
100
|
+
defaultAction: 'status',
|
|
101
|
+
actions: { on: onAction, off: offAction, status: statusAction, check: checkAction },
|
|
102
|
+
aliases: { enable: 'on', disable: 'off' },
|
|
103
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Unpin this clone, leaving it exactly as it was found.
|
|
2
|
+
//
|
|
3
|
+
// Only the repo-local keys are touched. Global config is never edited here --
|
|
4
|
+
// this is the command you run when handing a machine back or when a clone should
|
|
5
|
+
// go back to inheriting the machine default, and it should be able to do that
|
|
6
|
+
// without being able to break anything else.
|
|
7
|
+
import { inspectRepo } from "../core/inspect.js";
|
|
8
|
+
import { clearIdentity, legacyAccountKey } from "../core/identity.js";
|
|
9
|
+
import { gitFor } from "../ui/args.js";
|
|
10
|
+
import * as out from "../ui/format.js";
|
|
11
|
+
export default {
|
|
12
|
+
summary: 'unpin this clone (leaves global config alone)',
|
|
13
|
+
examples: ['repown off'],
|
|
14
|
+
async run(args) {
|
|
15
|
+
const git = gitFor(args);
|
|
16
|
+
const repo = await inspectRepo(git);
|
|
17
|
+
if (!repo.isRepo) {
|
|
18
|
+
out.fail('off', 'Not a git repository: ' + git.cwd);
|
|
19
|
+
return 1;
|
|
20
|
+
}
|
|
21
|
+
const id = repo.identity;
|
|
22
|
+
const legacy = repo.url ? [legacyAccountKey(repo.url)] : [];
|
|
23
|
+
const had = id.name ?? id.email ?? id.owner ?? id.account ??
|
|
24
|
+
(legacy[0] ? await git.getConfig(legacy[0], 'local') : null);
|
|
25
|
+
const keys = [...new Set([...repo.credentialKeys, ...legacy])];
|
|
26
|
+
const failed = (await clearIdentity(git, keys)).filter((outcome) => !outcome.written);
|
|
27
|
+
if (failed.length > 0) {
|
|
28
|
+
out.fail('off', 'could not remove ' + failed.map((outcome) => outcome.key).join(', '));
|
|
29
|
+
out.detail('this clone is still (partly) pinned -- is .git/config locked by another git?');
|
|
30
|
+
return 1;
|
|
31
|
+
}
|
|
32
|
+
if (!had)
|
|
33
|
+
out.warn('off', 'this clone was not pinned; nothing to remove.');
|
|
34
|
+
else
|
|
35
|
+
out.pass('off', 'repo-local identity removed from ' + (repo.root ?? git.cwd));
|
|
36
|
+
await reportInherited(git, repo);
|
|
37
|
+
return 0;
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
/** Read AFTER the clear: before it, the "inherited" value is still the local one. */
|
|
41
|
+
async function reportInherited(git, before) {
|
|
42
|
+
const id = (await inspectRepo(git)).identity;
|
|
43
|
+
out.line();
|
|
44
|
+
out.line(' This clone now inherits the machine default:');
|
|
45
|
+
out.field('commits as', describe(id.inheritedName, id.inheritedEmail));
|
|
46
|
+
if (before.credentialKeys.length > 0)
|
|
47
|
+
out.field('pushes as', id.inheritedAccount ?? 'nothing pinned');
|
|
48
|
+
out.line();
|
|
49
|
+
if (before.guard === 'on') {
|
|
50
|
+
out.warn('guard', 'still installed, and with no identity pinned it will refuse every push.');
|
|
51
|
+
out.detail('remove it too: repown guard off (or pin again: repown use <account>)');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function describe(name, email) {
|
|
55
|
+
if (!name && !email)
|
|
56
|
+
return 'nothing set anywhere -- git will refuse to commit';
|
|
57
|
+
return (name ?? '?') + ' <' + (email ?? '?') + '>';
|
|
58
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// Audit every clone under one or more directories.
|
|
2
|
+
//
|
|
3
|
+
// The point is to make remediation a decision taken with numbers. A clone that
|
|
4
|
+
// inherits the machine identity is not a hypothetical risk: its commits already
|
|
5
|
+
// carry whichever address the machine defaults to, and if it has a remote those
|
|
6
|
+
// commits are already published.
|
|
7
|
+
//
|
|
8
|
+
// DOMAINS AND COUNTS BY DEFAULT, never addresses. This output is the kind of
|
|
9
|
+
// thing that gets pasted into a chat window or an issue; a domain says
|
|
10
|
+
// everything needed to spot the problem and nothing that identifies a person.
|
|
11
|
+
// `--emails` opts in for when you need the exact value to fix it.
|
|
12
|
+
//
|
|
13
|
+
// EVERY identity in the history is listed, including the one the clone would
|
|
14
|
+
// inherit. Filtering against the inherited address was the obvious thing to do
|
|
15
|
+
// and it was wrong: an unpinned clone's problem IS that its commits carry the
|
|
16
|
+
// machine default, so treating that address as the expected one hid precisely
|
|
17
|
+
// the repositories worth looking at.
|
|
18
|
+
import { readdir } from 'node:fs/promises';
|
|
19
|
+
import { join, relative, resolve, sep } from 'node:path';
|
|
20
|
+
import { existsSync, statSync } from 'node:fs';
|
|
21
|
+
import { Git } from "../core/git.js";
|
|
22
|
+
import { inspectRepo } from "../core/inspect.js";
|
|
23
|
+
import { ok, err } from "../core/result.js";
|
|
24
|
+
import { flagBool, flagString, wantsJson, FORMAT_OPTION } from "../ui/args.js";
|
|
25
|
+
import * as out from "../ui/format.js";
|
|
26
|
+
export default {
|
|
27
|
+
summary: 'audit every clone under a directory',
|
|
28
|
+
positionals: { min: 0, max: Infinity, label: '<dir>' },
|
|
29
|
+
options: [
|
|
30
|
+
{ name: 'emails', kind: 'boolean', help: 'show exact addresses instead of domains and counts' },
|
|
31
|
+
{ name: 'depth', kind: 'string', default: '3', help: 'how many directories deep to look for a clone' },
|
|
32
|
+
FORMAT_OPTION,
|
|
33
|
+
],
|
|
34
|
+
examples: ['repown scan', 'repown scan ~/code ~/work --emails', 'repown scan ~/code --format json'],
|
|
35
|
+
async run(args) {
|
|
36
|
+
const target = scanTarget(args);
|
|
37
|
+
if (!target.ok) {
|
|
38
|
+
out.fail('scan', target.error);
|
|
39
|
+
return 2;
|
|
40
|
+
}
|
|
41
|
+
const { roots, depth } = target.value;
|
|
42
|
+
const unreadable = [];
|
|
43
|
+
const found = (await Promise.all(roots.map((root) => discover({ root, path: root }, depth, unreadable)))).flat();
|
|
44
|
+
for (const path of unreadable)
|
|
45
|
+
out.warn('scan', 'could not read ' + path + ' -- not scanned');
|
|
46
|
+
if (found.length === 0) {
|
|
47
|
+
out.warn('scan', 'no git repositories found under: ' + roots.join(', '));
|
|
48
|
+
if (wantsJson(args))
|
|
49
|
+
out.json([]);
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
const showEmails = flagBool(args, 'emails');
|
|
53
|
+
const rows = await mapLimited(found, CONCURRENCY, (entry) => describe(entry, showEmails));
|
|
54
|
+
rows.sort((a, b) => a.name.localeCompare(b.name));
|
|
55
|
+
if (wantsJson(args)) {
|
|
56
|
+
out.json(rows.map((row) => asJson(row, showEmails)));
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
render(rows);
|
|
60
|
+
return summarise(rows);
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Each clone costs about ten git processes. All at once, a folder of a hundred
|
|
65
|
+
* clones launched a thousand; calls then hit exec's timeout, and a killed
|
|
66
|
+
* `git config --get` reads as "not set" -- rows that looked unpinned but weren't.
|
|
67
|
+
*/
|
|
68
|
+
const CONCURRENCY = 6;
|
|
69
|
+
async function mapLimited(items, limit, work) {
|
|
70
|
+
const results = new Array(items.length);
|
|
71
|
+
let next = 0;
|
|
72
|
+
const worker = async () => {
|
|
73
|
+
while (next < items.length) {
|
|
74
|
+
const index = next++;
|
|
75
|
+
results[index] = await work(items[index]);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
79
|
+
return results;
|
|
80
|
+
}
|
|
81
|
+
function scanTarget(args) {
|
|
82
|
+
const roots = args.positional.length > 0 ? args.positional : [flagString(args, 'cwd') ?? process.cwd()];
|
|
83
|
+
const depth = parseDepth(flagString(args, 'depth'));
|
|
84
|
+
if (depth === null)
|
|
85
|
+
return err(`invalid --depth '${flagString(args, 'depth')}': expected a whole number >= 0`);
|
|
86
|
+
const badRoot = roots.map(invalidRoot).find((problem) => problem !== null);
|
|
87
|
+
return badRoot ? err(badRoot) : ok({ roots, depth });
|
|
88
|
+
}
|
|
89
|
+
function parseDepth(raw) {
|
|
90
|
+
return /^\d+$/.test(raw) ? Number(raw) : null;
|
|
91
|
+
}
|
|
92
|
+
function invalidRoot(root) {
|
|
93
|
+
if (!existsSync(root))
|
|
94
|
+
return 'no such directory: ' + root;
|
|
95
|
+
if (!statSync(root).isDirectory())
|
|
96
|
+
return 'not a directory: ' + root;
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
/** A directory that cannot be listed is reported, never silently left out of the audit. */
|
|
100
|
+
async function discover(at, depth, unreadable) {
|
|
101
|
+
if (depth < 0 || !existsSync(at.path))
|
|
102
|
+
return [];
|
|
103
|
+
if (existsSync(join(at.path, '.git')))
|
|
104
|
+
return [at];
|
|
105
|
+
let entries;
|
|
106
|
+
try {
|
|
107
|
+
entries = await readdir(at.path, { withFileTypes: true });
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
unreadable.push(at.path);
|
|
111
|
+
return [];
|
|
112
|
+
}
|
|
113
|
+
const directories = entries
|
|
114
|
+
.filter((entry) => entry.isDirectory() && entry.name !== 'node_modules')
|
|
115
|
+
.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)));
|
|
117
|
+
return nested.flat();
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* A mirror branch, where configured, is someone else's history passing through
|
|
121
|
+
* this clone. Counting it reports their contributors, not this clone's
|
|
122
|
+
* behaviour, so it is excluded -- when it exists; excluding a ref that is not
|
|
123
|
+
* there made git log fail and the row read as empty history.
|
|
124
|
+
*
|
|
125
|
+
* The name is relative to the scanned root, so two clones of the same project
|
|
126
|
+
* in different folders are told apart instead of printed as one name twice.
|
|
127
|
+
*/
|
|
128
|
+
async function describe(found, showEmails) {
|
|
129
|
+
const git = new Git(found.path);
|
|
130
|
+
const repo = await inspectRepo(git);
|
|
131
|
+
const configured = await git.getConfig('repown.mirrorBranch', 'local');
|
|
132
|
+
const mirror = configured && await git.hasCommit('refs/heads/' + configured) ? configured : null;
|
|
133
|
+
const counts = await git.emailCounts(mirror ? 'refs/heads/' + mirror : undefined);
|
|
134
|
+
return {
|
|
135
|
+
name: relative(found.root, found.path).split(sep).join('/') || found.path,
|
|
136
|
+
path: resolve(found.path),
|
|
137
|
+
remote: repo.originUrl !== null,
|
|
138
|
+
owner: repo.owner,
|
|
139
|
+
host: repo.url ? repo.provider.id : null,
|
|
140
|
+
identity: identityOf(repo),
|
|
141
|
+
guard: repo.guard,
|
|
142
|
+
history: counts.ok ? grouped(counts.value, showEmails) : null,
|
|
143
|
+
mirrored: mirror !== null,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function identityOf(repo) {
|
|
147
|
+
if (!repo.identity.name || !repo.identity.email)
|
|
148
|
+
return 'inherited';
|
|
149
|
+
return repo.identity.account || repo.identity.owner ? 'pinned' : 'commits-only';
|
|
150
|
+
}
|
|
151
|
+
/** Commits per domain (or address), most first. */
|
|
152
|
+
function grouped(counts, showEmails) {
|
|
153
|
+
const totals = new Map();
|
|
154
|
+
for (const [address, count] of counts) {
|
|
155
|
+
const key = showEmails ? address : domainOf(address);
|
|
156
|
+
totals.set(key, (totals.get(key) ?? 0) + count);
|
|
157
|
+
}
|
|
158
|
+
return new Map([...totals.entries()].sort((a, b) => b[1] - a[1]));
|
|
159
|
+
}
|
|
160
|
+
function historyText(history) {
|
|
161
|
+
if (history === null)
|
|
162
|
+
return 'unknown -- history could not be read';
|
|
163
|
+
if (history.size === 0)
|
|
164
|
+
return '-';
|
|
165
|
+
const ranked = [...history.entries()];
|
|
166
|
+
const shown = ranked.slice(0, 3).map(([key, count]) => key + '=' + count);
|
|
167
|
+
if (ranked.length > shown.length)
|
|
168
|
+
shown.push('+' + (ranked.length - shown.length) + ' more');
|
|
169
|
+
return shown.join(' ');
|
|
170
|
+
}
|
|
171
|
+
/** The --format json shape (ADR-014): `history` is null when unread, never an empty guess. */
|
|
172
|
+
function asJson(row, showEmails) {
|
|
173
|
+
const key = showEmails ? 'email' : 'domain';
|
|
174
|
+
return {
|
|
175
|
+
repo: row.name, path: row.path, remote: row.remote, owner: row.owner, host: row.host,
|
|
176
|
+
identity: row.identity, guard: row.guard, mirrorExcluded: row.mirrored,
|
|
177
|
+
history: row.history && [...row.history].map(([value, count]) => ({ [key]: value, count })),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/** Malformed addresses do occur in old history; they are reported, not hidden. */
|
|
181
|
+
function domainOf(address) {
|
|
182
|
+
const at = address.lastIndexOf('@');
|
|
183
|
+
if (at === -1 || at === address.length - 1)
|
|
184
|
+
return '(no domain)';
|
|
185
|
+
return address.slice(at + 1);
|
|
186
|
+
}
|
|
187
|
+
function render(rows) {
|
|
188
|
+
const width = Math.max(4, ...rows.map((row) => row.name.length));
|
|
189
|
+
out.line();
|
|
190
|
+
out.line(' ' + header(width));
|
|
191
|
+
out.line(' ' + '-'.repeat(width + 52));
|
|
192
|
+
for (const row of rows) {
|
|
193
|
+
out.line(' ' + [
|
|
194
|
+
row.name.padEnd(width), (row.owner ?? (row.remote ? '?' : 'no remote')).padEnd(14),
|
|
195
|
+
(row.host ?? '-').padEnd(7), IDENTITY_TEXT[row.identity].padEnd(12), row.guard.padEnd(7),
|
|
196
|
+
historyText(row.history) + (row.mirrored ? out.dim(' (excl. mirror)') : ''),
|
|
197
|
+
].join(' '));
|
|
198
|
+
}
|
|
199
|
+
out.line();
|
|
200
|
+
}
|
|
201
|
+
const IDENTITY_TEXT = {
|
|
202
|
+
'pinned': 'pinned', 'commits-only': 'commits only', 'inherited': 'INHERITED',
|
|
203
|
+
};
|
|
204
|
+
function header(width) {
|
|
205
|
+
return ['repo'.padEnd(width), 'owner'.padEnd(14), 'host'.padEnd(7),
|
|
206
|
+
'identity'.padEnd(12), 'guard'.padEnd(7), 'identities in history'].join(' ');
|
|
207
|
+
}
|
|
208
|
+
function summarise(rows) {
|
|
209
|
+
const unpinned = rows.filter((row) => row.identity === 'inherited');
|
|
210
|
+
const unguarded = rows.filter((row) => row.guard !== 'on');
|
|
211
|
+
out.field('repositories', String(rows.length), 16);
|
|
212
|
+
out.field('not pinned', String(unpinned.length), 16);
|
|
213
|
+
out.field('not guarded', String(unguarded.length), 16);
|
|
214
|
+
out.line();
|
|
215
|
+
out.line(' The identity column is a FACT, not a verdict: a shared repository');
|
|
216
|
+
out.line(' legitimately carries many addresses. What is worth acting on is a');
|
|
217
|
+
out.line(' repository you own whose history carries an address that is not yours.');
|
|
218
|
+
out.line();
|
|
219
|
+
if (unpinned.length > 0) {
|
|
220
|
+
out.line(' Pin one: cd <repo> && repown use <account> && repown guard on');
|
|
221
|
+
out.line();
|
|
222
|
+
}
|
|
223
|
+
return 0;
|
|
224
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// What `repown` with no arguments prints: is this clone set up correctly?
|
|
2
|
+
//
|
|
3
|
+
// Three separate things decide who you are here, and they fail differently:
|
|
4
|
+
//
|
|
5
|
+
// user.name / user.email who AUTHORED the commit
|
|
6
|
+
// credential.<host>.username which stored credential serves the push
|
|
7
|
+
// the CLI's active account who `gh pr create` acts as
|
|
8
|
+
//
|
|
9
|
+
// All three are printed, because the one that is invisible is the one that
|
|
10
|
+
// catches people out. This reports; it changes nothing.
|
|
11
|
+
import { inspectRepo, inspectAuth, activeAccountLabel } from "../core/inspect.js";
|
|
12
|
+
import { isPinned } from "../core/identity.js";
|
|
13
|
+
import { allowedOwners, shellWord } from "../core/guard/check.js";
|
|
14
|
+
import { existsSync } from 'node:fs';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
import { gitFor } from "../ui/args.js";
|
|
17
|
+
import * as out from "../ui/format.js";
|
|
18
|
+
export default {
|
|
19
|
+
summary: 'the state of this repository and this machine (the default)',
|
|
20
|
+
async run(args) {
|
|
21
|
+
const git = gitFor(args);
|
|
22
|
+
const repo = await inspectRepo(git);
|
|
23
|
+
if (!repo.isRepo) {
|
|
24
|
+
out.fail('repown', 'Not a git repository: ' + git.cwd);
|
|
25
|
+
out.detail('repown pins an identity per clone, so it needs one to work in.');
|
|
26
|
+
return 1;
|
|
27
|
+
}
|
|
28
|
+
const auth = await inspectAuth(git, repo.originUrl ?? undefined);
|
|
29
|
+
summary(repo, auth);
|
|
30
|
+
const problems = collectProblems(repo, auth);
|
|
31
|
+
await reportWarnings(repo, auth);
|
|
32
|
+
return verdict(repo, auth, problems);
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
/** "Honours it" only where the helper is GCM, the one measured to read the pin (ADR-001). */
|
|
36
|
+
function verdict(repo, auth, problems) {
|
|
37
|
+
if (problems.length === 0) {
|
|
38
|
+
out.pass('identity', repo.credentialKeys.length > 0 && auth.helperIsGcm
|
|
39
|
+
? 'this clone is pinned, and its credential mechanism honours it'
|
|
40
|
+
: 'this clone\'s commit identity is pinned; its credentials are left to ' + (repo.helper ?? 'nothing'));
|
|
41
|
+
return 0;
|
|
42
|
+
}
|
|
43
|
+
out.line();
|
|
44
|
+
for (const problem of problems) {
|
|
45
|
+
out.fail('identity', problem.what);
|
|
46
|
+
out.detail('fix: ' + problem.fix);
|
|
47
|
+
}
|
|
48
|
+
return 1;
|
|
49
|
+
}
|
|
50
|
+
function summary(repo, auth) {
|
|
51
|
+
const id = repo.identity;
|
|
52
|
+
out.line();
|
|
53
|
+
out.field('commits as', id.name || id.email ? (id.name ?? '?') + ' <' + (id.email ?? '?') + '>' : 'NOT SET LOCALLY');
|
|
54
|
+
out.field('pushes as', pushesAs(repo));
|
|
55
|
+
out.field('origin', repo.originUrl
|
|
56
|
+
? (repo.owner ?? 'unknown') + ' ' + out.dim('(' + repo.provider.label + ')')
|
|
57
|
+
: 'no remote');
|
|
58
|
+
out.field('helper', repo.helper ?? 'none');
|
|
59
|
+
out.field('gh active', activeAccountLabel(auth));
|
|
60
|
+
out.field('push guard', repo.guard);
|
|
61
|
+
out.line();
|
|
62
|
+
}
|
|
63
|
+
function pushesAs(repo) {
|
|
64
|
+
if (!repo.originUrl)
|
|
65
|
+
return 'no remote to push to';
|
|
66
|
+
if (repo.credentialKeys.length === 0)
|
|
67
|
+
return 'not pinned by repown on ' + repo.provider.label;
|
|
68
|
+
return repo.identity.account ?? 'NOT SET LOCALLY';
|
|
69
|
+
}
|
|
70
|
+
function collectProblems(repo, auth) {
|
|
71
|
+
const gh = auth.ghIsHelper ? [{
|
|
72
|
+
what: 'gh is the git credential helper, so only its ACTIVE account can ' +
|
|
73
|
+
'authenticate and every clone pinned elsewhere is prompted for a password.',
|
|
74
|
+
fix: 'repown fix',
|
|
75
|
+
}] : [];
|
|
76
|
+
return [...identityProblems(repo), ...gh];
|
|
77
|
+
}
|
|
78
|
+
function identityProblems(repo) {
|
|
79
|
+
const id = repo.identity;
|
|
80
|
+
if (!isPinned(id)) {
|
|
81
|
+
return [{
|
|
82
|
+
what: 'This clone sets no identity of its own, so it inherits the machine default (' +
|
|
83
|
+
(id.inheritedEmail ?? 'nothing') + ').',
|
|
84
|
+
fix: 'repown use <account>',
|
|
85
|
+
}];
|
|
86
|
+
}
|
|
87
|
+
if (repo.credentialKeys.length === 0 || id.account)
|
|
88
|
+
return [];
|
|
89
|
+
return [{
|
|
90
|
+
what: 'No account is pinned, so pushes fall back to the machine default (' +
|
|
91
|
+
(id.inheritedAccount ?? 'nothing') + ').',
|
|
92
|
+
fix: 'repown use <account>',
|
|
93
|
+
}];
|
|
94
|
+
}
|
|
95
|
+
async function reportWarnings(repo, auth) {
|
|
96
|
+
await ownerWarning(repo);
|
|
97
|
+
guardWarning(repo);
|
|
98
|
+
submoduleWarning(repo);
|
|
99
|
+
helperWarning(repo, auth);
|
|
100
|
+
ghWarning(repo.identity.account, auth);
|
|
101
|
+
}
|
|
102
|
+
/** A pinned credential key is only as good as the helper reading it. gh as helper is a problem, reported elsewhere. */
|
|
103
|
+
function helperWarning(repo, auth) {
|
|
104
|
+
if (repo.credentialKeys.length === 0 || auth.helperIsGcm || auth.ghIsHelper)
|
|
105
|
+
return;
|
|
106
|
+
if (!auth.helper) {
|
|
107
|
+
out.warn('helper', 'no credential helper is set, so the pinned account selects no credential; git will prompt on push.');
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
out.warn('helper', '"' + auth.helper + '" serves credentials here, and repown cannot tell whether it honours the pinned account.');
|
|
111
|
+
}
|
|
112
|
+
out.detail('see: repown doctor (Git Credential Manager is the helper repown pins for)');
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* A submodule is a clone of its own, with its own config and hooks. This clone's
|
|
116
|
+
* guard never sees its commits -- not even when `git push --recurse-submodules`
|
|
117
|
+
* publishes them from here.
|
|
118
|
+
*/
|
|
119
|
+
function submoduleWarning(repo) {
|
|
120
|
+
if (!repo.root || !existsSync(join(repo.root, '.gitmodules')))
|
|
121
|
+
return;
|
|
122
|
+
out.warn('submodule', 'this clone has submodules; each is a separate clone with its own identity and hook.');
|
|
123
|
+
out.detail('pin and guard each one too: git submodule foreach "repown use <account> && repown guard on"');
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* An organisation is never an account name, so a bare owner-vs-account
|
|
127
|
+
* comparison warns on every org repository -- which is most of them at work.
|
|
128
|
+
*/
|
|
129
|
+
async function ownerWarning(repo) {
|
|
130
|
+
const allowed = await allowedOwners(repo.git, repo.identity.owner ?? repo.identity.account);
|
|
131
|
+
if (!repo.owner || allowed.length === 0 || allowed.includes(repo.owner.toLowerCase()))
|
|
132
|
+
return;
|
|
133
|
+
out.warn('origin', 'origin belongs to "' + repo.owner + '", which is not an owner this clone pushes to.');
|
|
134
|
+
out.detail('if that is an organisation you belong to:');
|
|
135
|
+
out.detail(' git config --local --add repown.allowOwner ' + shellWord(repo.owner));
|
|
136
|
+
}
|
|
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
|
+
function ghWarning(account, auth) {
|
|
143
|
+
if (auth.ghPresent && !auth.gh.ok) {
|
|
144
|
+
out.warn('gh', 'could not be queried, so who `gh pr create` would act as is UNVERIFIED.');
|
|
145
|
+
out.detail('check it yourself: gh auth status');
|
|
146
|
+
}
|
|
147
|
+
else if (auth.gh.ok && auth.gh.value.active && account &&
|
|
148
|
+
auth.gh.value.active.toLowerCase() !== account.toLowerCase()) {
|
|
149
|
+
out.warn('gh', 'active as "' + auth.gh.value.active + '", so `gh pr create` here would act as that account.');
|
|
150
|
+
out.detail('fix: gh auth switch -u ' + account);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function guardWarning(repo) {
|
|
154
|
+
if (repo.guard === 'on')
|
|
155
|
+
return;
|
|
156
|
+
if (repo.hook?.redirected) {
|
|
157
|
+
out.warn('guard', 'core.hooksPath makes git run hooks from another tool\'s directory; repown does not install there.');
|
|
158
|
+
out.detail('guard this clone from that tool\'s pre-push hook: repown guard check --remote="$1" --url="$2"');
|
|
159
|
+
out.detail('(that hook must pass its stdin through: the refs being pushed arrive there)');
|
|
160
|
+
}
|
|
161
|
+
else if (repo.guard === 'off') {
|
|
162
|
+
out.warn('guard', 'off -- pushes are not checked. Enable it: repown guard on');
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
out.warn('guard', 'a pre-push hook repown did not write is installed; it was left alone.');
|
|
166
|
+
out.detail('read it first; if it is safe to drop, delete ' + (repo.hook?.path ?? 'it') + ', then run: repown guard on');
|
|
167
|
+
}
|
|
168
|
+
}
|