git-colabor 0.1.0 → 0.1.4
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 +201 -0
- package/README.md +18 -15
- package/dist/askpass.cjs +72 -22
- package/dist/cli.cjs +911 -410
- package/dist/index.cjs +461 -313
- package/package.json +7 -3
package/dist/index.cjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var os = require('os');
|
|
3
4
|
var promises = require('fs/promises');
|
|
4
|
-
var fs = require('fs');
|
|
5
5
|
var path = require('path');
|
|
6
|
-
var os = require('os');
|
|
7
6
|
var child_process = require('child_process');
|
|
8
7
|
var crypto = require('crypto');
|
|
8
|
+
var fs = require('fs');
|
|
9
9
|
var net = require('net');
|
|
10
10
|
var promises$1 = require('readline/promises');
|
|
11
11
|
|
|
@@ -33,6 +33,21 @@ function genKey(name, email) {
|
|
|
33
33
|
const initials = name.toLowerCase().split(/\s+/).filter(Boolean).map((w) => w[0] ?? "").join("");
|
|
34
34
|
return `${initials}${email.slice(0, 2)}`;
|
|
35
35
|
}
|
|
36
|
+
function formatMessage(txt, authors) {
|
|
37
|
+
const stripRe = new RegExp(`(\r
|
|
38
|
+
|\r|
|
|
39
|
+
){0,2}${CO_AUTHOR_TRAILER}.*`, "g");
|
|
40
|
+
const message = txt.replace(stripRe, "");
|
|
41
|
+
if (authors && authors.length > 0) {
|
|
42
|
+
const trailerText = authors.map((a) => a.format()).join(os.EOL);
|
|
43
|
+
return [message, os.EOL, os.EOL, trailerText].join("");
|
|
44
|
+
}
|
|
45
|
+
return message;
|
|
46
|
+
}
|
|
47
|
+
function printTrailers(authors) {
|
|
48
|
+
if (authors.length === 0) return "";
|
|
49
|
+
return os.EOL + os.EOL + authors.map((a) => a.format()).join(os.EOL);
|
|
50
|
+
}
|
|
36
51
|
var GitError = class extends Error {
|
|
37
52
|
cmd;
|
|
38
53
|
exitCode;
|
|
@@ -98,169 +113,6 @@ async function runBin(cmd, args, opts = {}) {
|
|
|
98
113
|
});
|
|
99
114
|
}
|
|
100
115
|
|
|
101
|
-
// src/core/git/rev.ts
|
|
102
|
-
async function insideWorkTree(cwd) {
|
|
103
|
-
const r = await gitRaw(["rev-parse", "--is-inside-work-tree"], { cwd });
|
|
104
|
-
return r.exitCode === 0 && r.stdout.trim() === "true";
|
|
105
|
-
}
|
|
106
|
-
async function topLevel(cwd) {
|
|
107
|
-
const r = await git(["rev-parse", "--show-toplevel"], { cwd });
|
|
108
|
-
return r.stdout.trim();
|
|
109
|
-
}
|
|
110
|
-
async function gitDir(cwd) {
|
|
111
|
-
const r = await git(["rev-parse", "--git-dir"], { cwd });
|
|
112
|
-
return path.resolve(cwd ?? process.cwd(), r.stdout.trim());
|
|
113
|
-
}
|
|
114
|
-
async function isRepo(cwd) {
|
|
115
|
-
return insideWorkTree(cwd);
|
|
116
|
-
}
|
|
117
|
-
async function headSha(cwd) {
|
|
118
|
-
const r = await gitRaw(["rev-parse", "HEAD"], { cwd });
|
|
119
|
-
if (r.exitCode !== 0) return void 0;
|
|
120
|
-
const sha = r.stdout.trim();
|
|
121
|
-
return sha.length > 0 ? sha : void 0;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// src/core/errors.ts
|
|
125
|
-
var EXIT_CODES = {
|
|
126
|
-
OK: 0,
|
|
127
|
-
RUNTIME: 1,
|
|
128
|
-
USAGE: 2,
|
|
129
|
-
NOT_A_REPO: 3,
|
|
130
|
-
SECRET_UNAVAILABLE: 4,
|
|
131
|
-
NOT_FOUND: 5,
|
|
132
|
-
CONFLICT_BLOCKED: 6
|
|
133
|
-
};
|
|
134
|
-
var AppError = class extends Error {
|
|
135
|
-
code;
|
|
136
|
-
exitCode;
|
|
137
|
-
hints;
|
|
138
|
-
constructor(init) {
|
|
139
|
-
super(init.message);
|
|
140
|
-
this.name = "AppError";
|
|
141
|
-
this.code = init.code;
|
|
142
|
-
this.exitCode = init.exitCode ?? EXIT_CODES.RUNTIME;
|
|
143
|
-
this.hints = init.hints;
|
|
144
|
-
}
|
|
145
|
-
toPayload() {
|
|
146
|
-
return { code: this.code, message: this.message, hints: this.hints, exitCode: this.exitCode };
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
var Errors = {
|
|
150
|
-
notARepo: (cwd) => new AppError({
|
|
151
|
-
code: "NOT_A_REPO",
|
|
152
|
-
message: cwd ? `not a git repository: ${cwd}` : "not a git repository",
|
|
153
|
-
exitCode: EXIT_CODES.NOT_A_REPO
|
|
154
|
-
}),
|
|
155
|
-
authorNotFound: (key) => new AppError({
|
|
156
|
-
code: "AUTHOR_NOT_FOUND",
|
|
157
|
-
message: `Author with initials "${key}" not found in .git-coauthors`,
|
|
158
|
-
exitCode: EXIT_CODES.NOT_FOUND,
|
|
159
|
-
hints: [`Add it with: git colabor coauthor add ${key} "Full Name" email@example.com`]
|
|
160
|
-
}),
|
|
161
|
-
duplicateKey: (key) => new AppError({
|
|
162
|
-
code: "DUPLICATE_KEY",
|
|
163
|
-
message: `Key "${key}" already exists in .git-coauthors`,
|
|
164
|
-
exitCode: EXIT_CODES.NOT_FOUND,
|
|
165
|
-
hints: ["Edit .git-coauthors directly to change an existing entry."]
|
|
166
|
-
}),
|
|
167
|
-
invalidEmail: (email) => new AppError({ code: "INVALID_EMAIL", message: `Invalid email: ${email}`, exitCode: EXIT_CODES.USAGE }),
|
|
168
|
-
usage: (message, hints) => new AppError({ code: "USAGE", message, exitCode: EXIT_CODES.USAGE, hints }),
|
|
169
|
-
secretUnavailable: (message, hints) => new AppError({ code: "SECRET_UNAVAILABLE", message, exitCode: EXIT_CODES.SECRET_UNAVAILABLE, hints })
|
|
170
|
-
};
|
|
171
|
-
function toErrorPayload(e) {
|
|
172
|
-
if (e instanceof AppError) return e.toPayload();
|
|
173
|
-
const message = e instanceof Error ? e.message : String(e);
|
|
174
|
-
return { code: "INTERNAL", message, exitCode: EXIT_CODES.RUNTIME };
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// src/core/authors/store.ts
|
|
178
|
-
var COAUTHORS_FILENAME = ".git-coauthors";
|
|
179
|
-
var EMAIL_RE = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|((\w+\.)+[a-zA-Z]{2,}))$/;
|
|
180
|
-
function validateEmail(email) {
|
|
181
|
-
return EMAIL_RE.test(email);
|
|
182
|
-
}
|
|
183
|
-
function homeCoAuthorsPath() {
|
|
184
|
-
return path.join(os.homedir(), COAUTHORS_FILENAME);
|
|
185
|
-
}
|
|
186
|
-
async function coAuthorsPath(cwd) {
|
|
187
|
-
const envPath = process.env.GIT_COLABOR_COAUTHORS_PATH;
|
|
188
|
-
if (envPath) return path.resolve(envPath);
|
|
189
|
-
try {
|
|
190
|
-
const root = await topLevel(cwd);
|
|
191
|
-
const local = path.join(root, COAUTHORS_FILENAME);
|
|
192
|
-
if (fs.existsSync(local)) return local;
|
|
193
|
-
} catch {
|
|
194
|
-
}
|
|
195
|
-
return homeCoAuthorsPath();
|
|
196
|
-
}
|
|
197
|
-
async function readAuthors(cwd) {
|
|
198
|
-
const p = await coAuthorsPath(cwd);
|
|
199
|
-
const txt = await promises.readFile(p, "utf8");
|
|
200
|
-
const parsed = JSON.parse(txt);
|
|
201
|
-
if (!parsed || typeof parsed !== "object" || !parsed.coauthors) {
|
|
202
|
-
throw Errors.usage(`Invalid .git-coauthors at ${p}: missing "coauthors" object`);
|
|
203
|
-
}
|
|
204
|
-
return { coauthors: parsed.coauthors };
|
|
205
|
-
}
|
|
206
|
-
async function writeAuthors(schema, cwd) {
|
|
207
|
-
const p = await coAuthorsPath(cwd);
|
|
208
|
-
await promises.mkdir(path.dirname(p), { recursive: true });
|
|
209
|
-
await promises.writeFile(p, JSON.stringify(schema, null, 2) + "\n", "utf8");
|
|
210
|
-
}
|
|
211
|
-
function schemaToAuthors(schema) {
|
|
212
|
-
return Object.entries(schema.coauthors).map(([key, v]) => new Author(key, v.name, v.email));
|
|
213
|
-
}
|
|
214
|
-
async function listAuthors(filter, cwd) {
|
|
215
|
-
let schema;
|
|
216
|
-
try {
|
|
217
|
-
schema = await readAuthors(cwd);
|
|
218
|
-
} catch {
|
|
219
|
-
return [];
|
|
220
|
-
}
|
|
221
|
-
const all = schemaToAuthors(schema);
|
|
222
|
-
if (!filter) return all;
|
|
223
|
-
const f = filter.toLowerCase();
|
|
224
|
-
return all.filter(
|
|
225
|
-
(a) => a.key.toLowerCase().includes(f) || a.name.toLowerCase().includes(f) || a.email.toLowerCase().includes(f)
|
|
226
|
-
);
|
|
227
|
-
}
|
|
228
|
-
async function getAuthor(key, cwd) {
|
|
229
|
-
const all = await listAuthors(void 0, cwd);
|
|
230
|
-
const found = all.find((a) => a.key === key);
|
|
231
|
-
if (!found) throw Errors.authorNotFound(key);
|
|
232
|
-
return found;
|
|
233
|
-
}
|
|
234
|
-
async function addCoAuthor(author, cwd) {
|
|
235
|
-
if (!validateEmail(author.email)) throw Errors.invalidEmail(author.email);
|
|
236
|
-
let schema;
|
|
237
|
-
try {
|
|
238
|
-
schema = await readAuthors(cwd);
|
|
239
|
-
} catch {
|
|
240
|
-
schema = { coauthors: {} };
|
|
241
|
-
}
|
|
242
|
-
if (Object.prototype.hasOwnProperty.call(schema.coauthors, author.key)) {
|
|
243
|
-
throw Errors.duplicateKey(author.key);
|
|
244
|
-
}
|
|
245
|
-
schema.coauthors[author.key] = { name: author.name, email: author.email };
|
|
246
|
-
await writeAuthors(schema, cwd);
|
|
247
|
-
}
|
|
248
|
-
function formatMessage(txt, authors) {
|
|
249
|
-
const stripRe = new RegExp(`(\r
|
|
250
|
-
|\r|
|
|
251
|
-
){0,2}${CO_AUTHOR_TRAILER}.*`, "g");
|
|
252
|
-
const message = txt.replace(stripRe, "");
|
|
253
|
-
if (authors && authors.length > 0) {
|
|
254
|
-
const trailerText = authors.map((a) => a.format()).join(os.EOL);
|
|
255
|
-
return [message, os.EOL, os.EOL, trailerText].join("");
|
|
256
|
-
}
|
|
257
|
-
return message;
|
|
258
|
-
}
|
|
259
|
-
function printTrailers(authors) {
|
|
260
|
-
if (authors.length === 0) return "";
|
|
261
|
-
return os.EOL + os.EOL + authors.map((a) => a.format()).join(os.EOL);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
116
|
// src/core/git/config.ts
|
|
265
117
|
function scopeArgs(scope) {
|
|
266
118
|
return scope ? [`--${scope}`] : [];
|
|
@@ -290,6 +142,27 @@ async function unsetAllConfig(key, scope, cwd) {
|
|
|
290
142
|
async function removeSection(section, scope, cwd) {
|
|
291
143
|
await gitRaw(["config", ...scopeArgs(scope), "--remove-section", section], { cwd });
|
|
292
144
|
}
|
|
145
|
+
async function insideWorkTree(cwd) {
|
|
146
|
+
const r = await gitRaw(["rev-parse", "--is-inside-work-tree"], { cwd });
|
|
147
|
+
return r.exitCode === 0 && r.stdout.trim() === "true";
|
|
148
|
+
}
|
|
149
|
+
async function topLevel(cwd) {
|
|
150
|
+
const r = await git(["rev-parse", "--show-toplevel"], { cwd });
|
|
151
|
+
return r.stdout.trim();
|
|
152
|
+
}
|
|
153
|
+
async function gitDir(cwd) {
|
|
154
|
+
const r = await git(["rev-parse", "--git-dir"], { cwd });
|
|
155
|
+
return path.resolve(cwd ?? process.cwd(), r.stdout.trim());
|
|
156
|
+
}
|
|
157
|
+
async function isRepo(cwd) {
|
|
158
|
+
return insideWorkTree(cwd);
|
|
159
|
+
}
|
|
160
|
+
async function headSha(cwd) {
|
|
161
|
+
const r = await gitRaw(["rev-parse", "HEAD"], { cwd });
|
|
162
|
+
if (r.exitCode !== 0) return void 0;
|
|
163
|
+
const sha = r.stdout.trim();
|
|
164
|
+
return sha.length > 0 ? sha : void 0;
|
|
165
|
+
}
|
|
293
166
|
|
|
294
167
|
// src/core/message/template.ts
|
|
295
168
|
function gitMessagePath() {
|
|
@@ -341,15 +214,11 @@ var LINE_RE = /^(.*)\s<([^>]+)>$/;
|
|
|
341
214
|
async function getSelected(cwd) {
|
|
342
215
|
const raw = (await getAllConfig(KEY, "local", cwd)).split(/\r?\n/).filter(Boolean);
|
|
343
216
|
if (raw.length === 0) return [];
|
|
344
|
-
const all = await listAuthors(void 0, cwd);
|
|
345
217
|
const out = [];
|
|
346
218
|
for (const line of raw) {
|
|
347
219
|
const m = line.match(LINE_RE);
|
|
348
220
|
if (!m) continue;
|
|
349
|
-
|
|
350
|
-
const email = m[2].trim();
|
|
351
|
-
const found = all.find((a) => a.email === email);
|
|
352
|
-
out.push(found ?? new Author(email.split("@")[0] || "co", name, email));
|
|
221
|
+
out.push(new Author(m[2].split("@")[0] || "co", m[1].trim(), m[2].trim()));
|
|
353
222
|
}
|
|
354
223
|
return out;
|
|
355
224
|
}
|
|
@@ -366,7 +235,7 @@ async function clearSelected(cwd) {
|
|
|
366
235
|
}
|
|
367
236
|
|
|
368
237
|
// src/core/git/shortlog.ts
|
|
369
|
-
var SHORTLOG_LINE = /^\d+\t(.+)\s<(.+)>$/;
|
|
238
|
+
var SHORTLOG_LINE = /^\s*\d+\t(.+)\s<(.+)>$/;
|
|
370
239
|
async function repoAuthors(filter, cwd) {
|
|
371
240
|
const args = ["shortlog", "-seni", "HEAD"];
|
|
372
241
|
if (filter) args.splice(1, 0, `--author=${filter}`);
|
|
@@ -382,6 +251,25 @@ async function repoAuthors(filter, cwd) {
|
|
|
382
251
|
}
|
|
383
252
|
return out;
|
|
384
253
|
}
|
|
254
|
+
|
|
255
|
+
// src/core/git/committers.ts
|
|
256
|
+
async function historyCommitters(cwd) {
|
|
257
|
+
const r = await gitRaw(["log", "--format=%cn%x1f%ce"], { cwd });
|
|
258
|
+
if (r.exitCode !== 0) return [];
|
|
259
|
+
const counts = /* @__PURE__ */ new Map();
|
|
260
|
+
for (const line of r.stdout.split("\n")) {
|
|
261
|
+
const sep = line.indexOf("");
|
|
262
|
+
if (sep < 0) continue;
|
|
263
|
+
const name = line.slice(0, sep).trim();
|
|
264
|
+
const email = line.slice(sep + 1).trim();
|
|
265
|
+
if (!name || !email) continue;
|
|
266
|
+
const key = email.toLowerCase();
|
|
267
|
+
const cur = counts.get(key);
|
|
268
|
+
if (cur) cur.count++;
|
|
269
|
+
else counts.set(key, { name, email, count: 1 });
|
|
270
|
+
}
|
|
271
|
+
return [...counts.values()].sort((a, b) => b.count - a.count).map(({ name, email }) => ({ name, email }));
|
|
272
|
+
}
|
|
385
273
|
var ORDER = { trace: 10, debug: 20, info: 30, warn: 40, error: 50 };
|
|
386
274
|
var VALID = /* @__PURE__ */ new Set(["trace", "debug", "info", "warn", "error"]);
|
|
387
275
|
var MAX_BYTES = 2 * 1024 * 1024;
|
|
@@ -536,13 +424,16 @@ async function writeState(state, cwd) {
|
|
|
536
424
|
await atomicWriteJson(p, state, 384);
|
|
537
425
|
}
|
|
538
426
|
async function readCurrentIdentityConfig(cwd) {
|
|
539
|
-
const [userName, userEmail, sshCommand, commitTemplate] = await Promise.all([
|
|
427
|
+
const [userName, userEmail, sshCommand, commitTemplate, commitGpgsign, gpgFormat, signingKey] = await Promise.all([
|
|
540
428
|
getConfig("user.name", "local", cwd),
|
|
541
429
|
getConfig("user.email", "local", cwd),
|
|
542
430
|
getConfig("core.sshCommand", "local", cwd),
|
|
543
|
-
getConfig("commit.template", "local", cwd)
|
|
431
|
+
getConfig("commit.template", "local", cwd),
|
|
432
|
+
getConfig("commit.gpgsign", "local", cwd),
|
|
433
|
+
getConfig("gpg.format", "local", cwd),
|
|
434
|
+
getConfig("user.signingKey", "local", cwd)
|
|
544
435
|
]);
|
|
545
|
-
return { userName, userEmail, sshCommand, commitTemplate };
|
|
436
|
+
return { userName, userEmail, sshCommand, commitTemplate, commitGpgsign, gpgFormat, signingKey };
|
|
546
437
|
}
|
|
547
438
|
async function captureBackupIfFirstTouch(cwd) {
|
|
548
439
|
const managed = await getConfig("colabor.managed", "local", cwd);
|
|
@@ -576,44 +467,60 @@ function nowHeldBy(session, source) {
|
|
|
576
467
|
};
|
|
577
468
|
}
|
|
578
469
|
|
|
579
|
-
// src/core/
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
470
|
+
// src/core/errors.ts
|
|
471
|
+
var EXIT_CODES = {
|
|
472
|
+
OK: 0,
|
|
473
|
+
RUNTIME: 1,
|
|
474
|
+
USAGE: 2,
|
|
475
|
+
NOT_A_REPO: 3,
|
|
476
|
+
SECRET_UNAVAILABLE: 4,
|
|
477
|
+
NOT_FOUND: 5,
|
|
478
|
+
CONFLICT_BLOCKED: 6
|
|
479
|
+
};
|
|
480
|
+
var AppError = class extends Error {
|
|
481
|
+
code;
|
|
482
|
+
exitCode;
|
|
483
|
+
hints;
|
|
484
|
+
constructor(init) {
|
|
485
|
+
super(init.message);
|
|
486
|
+
this.name = "AppError";
|
|
487
|
+
this.code = init.code;
|
|
488
|
+
this.exitCode = init.exitCode ?? EXIT_CODES.RUNTIME;
|
|
489
|
+
this.hints = init.hints;
|
|
593
490
|
}
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
491
|
+
toPayload() {
|
|
492
|
+
return { code: this.code, message: this.message, hints: this.hints, exitCode: this.exitCode };
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
var Errors = {
|
|
496
|
+
notARepo: (cwd) => new AppError({
|
|
497
|
+
code: "NOT_A_REPO",
|
|
498
|
+
message: cwd ? `not a git repository: ${cwd}` : "not a git repository",
|
|
499
|
+
exitCode: EXIT_CODES.NOT_A_REPO
|
|
500
|
+
}),
|
|
501
|
+
authorNotFound: (key) => new AppError({
|
|
502
|
+
code: "AUTHOR_NOT_FOUND",
|
|
503
|
+
message: `Author with initials "${key}" not found in identities`,
|
|
504
|
+
exitCode: EXIT_CODES.NOT_FOUND,
|
|
505
|
+
hints: [`Add it with: git colabor coauthor add ${key} "Full Name" email@example.com`]
|
|
506
|
+
}),
|
|
507
|
+
duplicateKey: (key) => new AppError({
|
|
508
|
+
code: "DUPLICATE_KEY",
|
|
509
|
+
message: `Key "${key}" already exists`,
|
|
510
|
+
exitCode: EXIT_CODES.NOT_FOUND,
|
|
511
|
+
hints: []
|
|
512
|
+
}),
|
|
513
|
+
invalidEmail: (email) => new AppError({ code: "INVALID_EMAIL", message: `Invalid email: ${email}`, exitCode: EXIT_CODES.USAGE }),
|
|
514
|
+
usage: (message, hints) => new AppError({ code: "USAGE", message, exitCode: EXIT_CODES.USAGE, hints }),
|
|
515
|
+
secretUnavailable: (message, hints) => new AppError({ code: "SECRET_UNAVAILABLE", message, exitCode: EXIT_CODES.SECRET_UNAVAILABLE, hints })
|
|
516
|
+
};
|
|
517
|
+
function toErrorPayload(e) {
|
|
518
|
+
if (e instanceof AppError) return e.toPayload();
|
|
519
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
520
|
+
return { code: "INTERNAL", message, exitCode: EXIT_CODES.RUNTIME };
|
|
616
521
|
}
|
|
522
|
+
|
|
523
|
+
// src/core/identity/map.ts
|
|
617
524
|
function genId() {
|
|
618
525
|
return "id_" + crypto.randomBytes(4).toString("hex");
|
|
619
526
|
}
|
|
@@ -621,10 +528,17 @@ async function readMap() {
|
|
|
621
528
|
try {
|
|
622
529
|
const txt = await promises.readFile(mapPath(), "utf8");
|
|
623
530
|
const parsed = JSON.parse(txt);
|
|
531
|
+
const identities = { ...parsed.identities };
|
|
532
|
+
for (const id of Object.keys(identities)) {
|
|
533
|
+
const row = identities[id];
|
|
534
|
+
delete row.passphraseCommand;
|
|
535
|
+
if (row.scope === "user" || row.scope === "vscode") row.scope = "machine";
|
|
536
|
+
}
|
|
624
537
|
return {
|
|
625
538
|
schemaVersion: 1,
|
|
626
|
-
identities
|
|
627
|
-
defaultIdentity: parsed.defaultIdentity
|
|
539
|
+
identities,
|
|
540
|
+
defaultIdentity: parsed.defaultIdentity,
|
|
541
|
+
hidden: parsed.hidden
|
|
628
542
|
};
|
|
629
543
|
} catch {
|
|
630
544
|
return { schemaVersion: 1, identities: {} };
|
|
@@ -645,9 +559,41 @@ async function addIdentity(input) {
|
|
|
645
559
|
if (map.identities[id]) throw Errors.usage(`identity "${id}" already exists`);
|
|
646
560
|
const identity = { ...input, id, createdAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
647
561
|
map.identities[id] = identity;
|
|
562
|
+
if (map.hidden?.[identity.email.toLowerCase()]) delete map.hidden[identity.email.toLowerCase()];
|
|
648
563
|
await writeMap(map);
|
|
649
564
|
return identity;
|
|
650
565
|
}
|
|
566
|
+
async function updateIdentity(id, patch) {
|
|
567
|
+
const map = await readMap();
|
|
568
|
+
const identity = map.identities[id];
|
|
569
|
+
if (!identity) throw Errors.usage(`identity "${id}" not found`);
|
|
570
|
+
const next = { ...identity, ...patch };
|
|
571
|
+
if (patch.sshKeyPath === void 0 && Object.keys(patch).includes("sshKeyPath")) {
|
|
572
|
+
delete next.sshKeyPath;
|
|
573
|
+
delete next.sshKeyFingerprint;
|
|
574
|
+
delete next.sshKeyEncrypted;
|
|
575
|
+
}
|
|
576
|
+
map.identities[id] = next;
|
|
577
|
+
await writeMap(map);
|
|
578
|
+
return next;
|
|
579
|
+
}
|
|
580
|
+
async function hideIdentityEmail(email) {
|
|
581
|
+
const map = await readMap();
|
|
582
|
+
map.hidden = { ...map.hidden, [email.toLowerCase()]: true };
|
|
583
|
+
await writeMap(map);
|
|
584
|
+
}
|
|
585
|
+
async function unhideIdentityEmail(email) {
|
|
586
|
+
const map = await readMap();
|
|
587
|
+
const key = email.toLowerCase();
|
|
588
|
+
if (!map.hidden?.[key]) return false;
|
|
589
|
+
delete map.hidden[key];
|
|
590
|
+
await writeMap(map);
|
|
591
|
+
return true;
|
|
592
|
+
}
|
|
593
|
+
async function listHiddenEmails() {
|
|
594
|
+
const map = await readMap();
|
|
595
|
+
return Object.keys(map.hidden ?? {});
|
|
596
|
+
}
|
|
651
597
|
async function removeIdentity(id) {
|
|
652
598
|
const map = await readMap();
|
|
653
599
|
if (!map.identities[id]) throw Errors.usage(`identity "${id}" not found`);
|
|
@@ -665,6 +611,45 @@ async function listIdentities() {
|
|
|
665
611
|
const map = await readMap();
|
|
666
612
|
return { identities: Object.values(map.identities), defaultIdentity: map.defaultIdentity };
|
|
667
613
|
}
|
|
614
|
+
|
|
615
|
+
// src/core/repo/status.ts
|
|
616
|
+
async function repoStatus(cwd) {
|
|
617
|
+
const inRepo = cwd ? await insideWorkTree(cwd) : false;
|
|
618
|
+
if (!inRepo) {
|
|
619
|
+
return {
|
|
620
|
+
inRepo: false,
|
|
621
|
+
repo: cwd ?? null,
|
|
622
|
+
managed: false,
|
|
623
|
+
managedBy: null,
|
|
624
|
+
activeIdentityId: null,
|
|
625
|
+
heldBy: null,
|
|
626
|
+
selected: [],
|
|
627
|
+
available: []
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
const [managed, managedBy, state, selected, identityMap] = await Promise.all([
|
|
631
|
+
getConfig("colabor.managed", "local", cwd),
|
|
632
|
+
getConfig("colabor.managed-by", "local", cwd),
|
|
633
|
+
readState(cwd),
|
|
634
|
+
getSelected(cwd),
|
|
635
|
+
listIdentities()
|
|
636
|
+
]);
|
|
637
|
+
const selectedEmails = new Set(selected.map((a) => a.email));
|
|
638
|
+
const available = identityMap.identities.filter((i) => !selectedEmails.has(i.email)).map((i) => ({ key: genKey(i.name, i.email), name: i.name, email: i.email }));
|
|
639
|
+
return {
|
|
640
|
+
inRepo: true,
|
|
641
|
+
repo: cwd,
|
|
642
|
+
managed: managed === "true",
|
|
643
|
+
managedBy: managedBy ?? null,
|
|
644
|
+
activeIdentityId: state.activeIdentity ?? null,
|
|
645
|
+
heldBy: state.heldBy ?? null,
|
|
646
|
+
selected: selected.map(toBrief),
|
|
647
|
+
available
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
function toBrief(a) {
|
|
651
|
+
return { key: a.key, name: a.name, email: a.email };
|
|
652
|
+
}
|
|
668
653
|
var FP_RE = /SHA256:[A-Za-z0-9+/=]+/;
|
|
669
654
|
async function fingerprintOfFile(path) {
|
|
670
655
|
const r = await runBin("ssh-keygen", ["-lf", path]);
|
|
@@ -682,44 +667,45 @@ async function isEncrypted(path) {
|
|
|
682
667
|
const r = await runBin("ssh-keygen", ["-y", "-P", "", "-f", path]);
|
|
683
668
|
return r.exitCode !== 0;
|
|
684
669
|
}
|
|
685
|
-
async function
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
}
|
|
692
|
-
return;
|
|
670
|
+
async function keyUsable(path) {
|
|
671
|
+
try {
|
|
672
|
+
await fingerprintOfFile(path);
|
|
673
|
+
return true;
|
|
674
|
+
} catch {
|
|
675
|
+
return false;
|
|
693
676
|
}
|
|
694
|
-
await promises.chmod(path, 384).catch(() => {
|
|
695
|
-
});
|
|
696
|
-
}
|
|
697
|
-
async function materializeKey(privateKeyPem) {
|
|
698
|
-
await promises.mkdir(keysDir(), { recursive: true });
|
|
699
|
-
const tmp = path.join(keysDir(), `.import.${crypto.randomBytes(4).toString("hex")}`);
|
|
700
|
-
await promises.writeFile(tmp, privateKeyPem.endsWith("\n") ? privateKeyPem : privateKeyPem + "\n", "utf8");
|
|
701
|
-
await restrictKeyPerms(tmp);
|
|
702
|
-
const fingerprint = await fingerprintOfFile(tmp);
|
|
703
|
-
const dest = keyFilePath(fingerprint);
|
|
704
|
-
await promises.rename(tmp, dest);
|
|
705
|
-
await restrictKeyPerms(dest);
|
|
706
|
-
return { path: dest, fingerprint };
|
|
707
677
|
}
|
|
708
678
|
async function importKey(sourcePath) {
|
|
709
|
-
const
|
|
710
|
-
const
|
|
711
|
-
|
|
712
|
-
return { path, fingerprint, encrypted };
|
|
679
|
+
const fingerprint = await fingerprintOfFile(sourcePath);
|
|
680
|
+
const encrypted = await isEncrypted(sourcePath);
|
|
681
|
+
return { path: path.resolve(sourcePath), fingerprint, encrypted };
|
|
713
682
|
}
|
|
714
683
|
function runInherit(cmdArgs, env, stdinNull = false) {
|
|
715
684
|
return new Promise((resolve4) => {
|
|
716
685
|
const [cmd, ...args] = cmdArgs;
|
|
717
686
|
const child = child_process.spawn(cmd, args, {
|
|
718
687
|
env: env ?? process.env,
|
|
719
|
-
stdio: [stdinNull ? "ignore" : "inherit", "inherit", "pipe"]
|
|
688
|
+
stdio: [stdinNull ? "ignore" : "inherit", "inherit", "pipe"],
|
|
689
|
+
detached: false
|
|
690
|
+
// keep setsid children in our process group so the kill timer can reach them
|
|
691
|
+
});
|
|
692
|
+
const killTimer = setTimeout(() => {
|
|
693
|
+
child.kill("SIGKILL");
|
|
694
|
+
if (child.pid) {
|
|
695
|
+
try {
|
|
696
|
+
process.kill(-child.pid, "SIGKILL");
|
|
697
|
+
} catch {
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
}, 1e4);
|
|
701
|
+
child.on("error", () => {
|
|
702
|
+
clearTimeout(killTimer);
|
|
703
|
+
resolve4(127);
|
|
704
|
+
});
|
|
705
|
+
child.on("close", (c) => {
|
|
706
|
+
clearTimeout(killTimer);
|
|
707
|
+
resolve4(c ?? 127);
|
|
720
708
|
});
|
|
721
|
-
child.on("error", () => resolve4(127));
|
|
722
|
-
child.on("close", (c) => resolve4(c ?? 127));
|
|
723
709
|
});
|
|
724
710
|
}
|
|
725
711
|
function hasBin(bin) {
|
|
@@ -752,7 +738,7 @@ async function loadKey(opts) {
|
|
|
752
738
|
code = await runInherit(["ssh-add", "--apple-use-keychain", opts.keyPath]);
|
|
753
739
|
if (code === 0) return { loaded: true, via: "apple-keychain" };
|
|
754
740
|
}
|
|
755
|
-
const haveSource = opts.socketPath && opts.token
|
|
741
|
+
const haveSource = opts.socketPath && opts.token;
|
|
756
742
|
if (opts.askpassScriptPath && haveSource) {
|
|
757
743
|
const env = { ...process.env };
|
|
758
744
|
env.SSH_ASKPASS = opts.askpassScriptPath;
|
|
@@ -760,12 +746,18 @@ async function loadKey(opts) {
|
|
|
760
746
|
env.DISPLAY = env.DISPLAY ?? ":0";
|
|
761
747
|
if (opts.socketPath) env.GIT_COLABOR_ASKPASS_SOCK = opts.socketPath;
|
|
762
748
|
if (opts.token) env.GIT_COLABOR_ASKPASS_TOKEN = opts.token;
|
|
763
|
-
if (opts.passphraseCommand) env.GIT_COLABOR_PASSPHRASE_COMMAND = opts.passphraseCommand;
|
|
764
749
|
env.GIT_COLABOR_FINGERPRINT = opts.fingerprint;
|
|
765
750
|
const useSetsid = process.platform !== "win32" && hasBin("setsid");
|
|
766
751
|
code = await runInherit(useSetsid ? ["setsid", "ssh-add", opts.keyPath] : ["ssh-add", opts.keyPath], env, true);
|
|
767
752
|
if (code === 0) return { loaded: true, via: "askpass" };
|
|
768
|
-
|
|
753
|
+
if (await verifyKey(opts)) {
|
|
754
|
+
return {
|
|
755
|
+
loaded: true,
|
|
756
|
+
via: "keygen-verify",
|
|
757
|
+
message: "passphrase verified (no ssh-agent \u2014 the key unlocks at ssh time via SSH_ASKPASS)"
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
return { loaded: false, via: "askpass", message: "passphrase rejected" };
|
|
769
761
|
}
|
|
770
762
|
if (process.stdin.isTTY) {
|
|
771
763
|
code = await runInherit(["ssh-add", opts.keyPath]);
|
|
@@ -777,12 +769,48 @@ async function loadKey(opts) {
|
|
|
777
769
|
async function listAgent() {
|
|
778
770
|
return runBinQuiet("ssh-add", ["-l"]);
|
|
779
771
|
}
|
|
772
|
+
async function agentFingerprints() {
|
|
773
|
+
const out = await listAgent();
|
|
774
|
+
const prints = /* @__PURE__ */ new Set();
|
|
775
|
+
for (const line of out.split("\n")) {
|
|
776
|
+
const tok = line.trim().split(/\s+/)[1];
|
|
777
|
+
if (tok?.startsWith("SHA256:")) prints.add(tok);
|
|
778
|
+
}
|
|
779
|
+
return prints;
|
|
780
|
+
}
|
|
781
|
+
async function keyInAgent(fingerprint) {
|
|
782
|
+
return (await agentFingerprints()).has(fingerprint);
|
|
783
|
+
}
|
|
780
784
|
async function removeKey(keyPath) {
|
|
781
785
|
const code = await runInherit(["ssh-add", "-d", keyPath], void 0, true);
|
|
782
786
|
return code === 0;
|
|
783
787
|
}
|
|
784
|
-
|
|
785
|
-
|
|
788
|
+
async function verifyKey(opts) {
|
|
789
|
+
if (!opts.askpassScriptPath || !(opts.socketPath && opts.token)) return false;
|
|
790
|
+
const env = { ...process.env };
|
|
791
|
+
env.SSH_ASKPASS = opts.askpassScriptPath;
|
|
792
|
+
env.SSH_ASKPASS_REQUIRE = "force";
|
|
793
|
+
env.DISPLAY = env.DISPLAY ?? ":0";
|
|
794
|
+
env.GIT_COLABOR_ASKPASS_SOCK = opts.socketPath;
|
|
795
|
+
env.GIT_COLABOR_ASKPASS_TOKEN = opts.token;
|
|
796
|
+
env.GIT_COLABOR_FINGERPRINT = opts.fingerprint;
|
|
797
|
+
const verifyQuiet = (args) => new Promise((resolve4) => {
|
|
798
|
+
const child = child_process.spawn("ssh-keygen", args, { env, stdio: ["ignore", "pipe", "pipe"] });
|
|
799
|
+
const killTimer = setTimeout(() => child.kill("SIGKILL"), 1e4);
|
|
800
|
+
child.on("error", () => {
|
|
801
|
+
clearTimeout(killTimer);
|
|
802
|
+
resolve4(127);
|
|
803
|
+
});
|
|
804
|
+
child.on("close", (c) => {
|
|
805
|
+
clearTimeout(killTimer);
|
|
806
|
+
resolve4(c ?? 127);
|
|
807
|
+
});
|
|
808
|
+
});
|
|
809
|
+
const verify = await verifyQuiet(["-y", "-P", "", "-f", opts.keyPath]).then(
|
|
810
|
+
(c) => c === 0 ? Promise.resolve(0) : verifyQuiet(["-y", "-f", opts.keyPath])
|
|
811
|
+
);
|
|
812
|
+
return verify === 0;
|
|
813
|
+
}
|
|
786
814
|
async function safeTopLevel(cwd) {
|
|
787
815
|
try {
|
|
788
816
|
return await topLevel(cwd);
|
|
@@ -790,6 +818,102 @@ async function safeTopLevel(cwd) {
|
|
|
790
818
|
return void 0;
|
|
791
819
|
}
|
|
792
820
|
}
|
|
821
|
+
async function ensureSignWrapper(askpassScriptPath) {
|
|
822
|
+
const wrapperPath = path.join(dataDir(), "sign-wrapper.sh");
|
|
823
|
+
const askpass = askpassScriptPath ? await ensureAskpassWrapper(askpassScriptPath) : path.join(dataDir(), "askpass-wrapper.sh");
|
|
824
|
+
const content = [
|
|
825
|
+
"#!/bin/sh",
|
|
826
|
+
"# extract -f <key> so the askpass helper can fingerprint it even when",
|
|
827
|
+
'# the prompt carries no key path (bare "Enter passphrase:" on some OpenSSH)',
|
|
828
|
+
"signkey=",
|
|
829
|
+
"prev=",
|
|
830
|
+
'for a in "$@"; do',
|
|
831
|
+
' if [ "$prev" = "-f" ]; then signkey="$a"; break; fi',
|
|
832
|
+
' prev="$a"',
|
|
833
|
+
"done",
|
|
834
|
+
`SSH_ASKPASS="${askpass}" SSH_ASKPASS_REQUIRE=force DISPLAY=:0 GIT_COLABOR_SIGNING_KEY="$signkey" exec ssh-keygen "$@"`,
|
|
835
|
+
""
|
|
836
|
+
].join("\n");
|
|
837
|
+
if (fs.existsSync(wrapperPath)) {
|
|
838
|
+
const existing = await promises.readFile(wrapperPath, "utf8");
|
|
839
|
+
if (existing === content) return wrapperPath;
|
|
840
|
+
}
|
|
841
|
+
await promises.writeFile(wrapperPath, content, { mode: 448 });
|
|
842
|
+
await promises.chmod(wrapperPath, 448);
|
|
843
|
+
return wrapperPath;
|
|
844
|
+
}
|
|
845
|
+
async function signingKeyPath(sshKeyPath, fingerprint) {
|
|
846
|
+
if (fingerprint && await keyInAgent(fingerprint)) {
|
|
847
|
+
const pub = `${sshKeyPath}.pub`;
|
|
848
|
+
if (fs.existsSync(pub)) return pub;
|
|
849
|
+
}
|
|
850
|
+
return sshKeyPath;
|
|
851
|
+
}
|
|
852
|
+
async function setCommitSigning(opts) {
|
|
853
|
+
if (!await insideWorkTree(opts.cwd)) throw Errors.notARepo(opts.cwd);
|
|
854
|
+
const state = await readState(opts.cwd);
|
|
855
|
+
if (!opts.on) {
|
|
856
|
+
state.signing = false;
|
|
857
|
+
await writeState(state, opts.cwd);
|
|
858
|
+
await unsetConfig("commit.gpgsign", "local", opts.cwd);
|
|
859
|
+
await unsetConfig("gpg.format", "local", opts.cwd);
|
|
860
|
+
await unsetConfig("user.signingKey", "local", opts.cwd);
|
|
861
|
+
await unsetConfig("gpg.ssh.program", "local", opts.cwd);
|
|
862
|
+
await appendAudit({ action: "identity.sign", source: opts.source, identity: opts.id, repo: await safeTopLevel(opts.cwd), result: "ok", message: "signing off" });
|
|
863
|
+
return { signing: false };
|
|
864
|
+
}
|
|
865
|
+
const identity = await getIdentity(opts.id);
|
|
866
|
+
if (!identity.sshKeyPath || !await keyUsable(identity.sshKeyPath)) {
|
|
867
|
+
throw Errors.usage(`identity "${identity.name}" has no usable key to sign with`);
|
|
868
|
+
}
|
|
869
|
+
state.signing = true;
|
|
870
|
+
await writeState(state, opts.cwd);
|
|
871
|
+
await setConfig("commit.gpgsign", "true", "local", opts.cwd);
|
|
872
|
+
await setConfig("gpg.format", "ssh", "local", opts.cwd);
|
|
873
|
+
await setConfig("user.signingKey", await signingKeyPath(identity.sshKeyPath, identity.sshKeyFingerprint), "local", opts.cwd);
|
|
874
|
+
if (opts.askpassScriptPath) {
|
|
875
|
+
const signWrapper = await ensureSignWrapper(opts.askpassScriptPath);
|
|
876
|
+
await setConfig("gpg.ssh.program", signWrapper, "local", opts.cwd);
|
|
877
|
+
}
|
|
878
|
+
await appendAudit({
|
|
879
|
+
action: "identity.sign",
|
|
880
|
+
source: opts.source,
|
|
881
|
+
identity: opts.id,
|
|
882
|
+
identityName: identity.name,
|
|
883
|
+
fingerprint: identity.sshKeyFingerprint,
|
|
884
|
+
repo: await safeTopLevel(opts.cwd),
|
|
885
|
+
result: "ok",
|
|
886
|
+
message: "signing on"
|
|
887
|
+
});
|
|
888
|
+
return { signing: true, key: identity.sshKeyPath };
|
|
889
|
+
}
|
|
890
|
+
function discoverSessionBridges() {
|
|
891
|
+
try {
|
|
892
|
+
const dir = dataDir();
|
|
893
|
+
const sessions = fs.readdirSync(dir).filter((f) => /^session-.*\.json$/.test(f)).map((f) => ({ f, mtime: fs.statSync(path.join(dir, f)).mtimeMs })).sort((a, b) => b.mtime - a.mtime);
|
|
894
|
+
const out = [];
|
|
895
|
+
for (const s of sessions) {
|
|
896
|
+
try {
|
|
897
|
+
const parsed = JSON.parse(fs.readFileSync(path.join(dir, s.f), "utf8"));
|
|
898
|
+
if (parsed.socketPath && parsed.token) out.push({ socketPath: parsed.socketPath, token: parsed.token });
|
|
899
|
+
} catch {
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
return out;
|
|
903
|
+
} catch {
|
|
904
|
+
return [];
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
function discoverSessionBridge() {
|
|
908
|
+
return discoverSessionBridges()[0];
|
|
909
|
+
}
|
|
910
|
+
async function safeTopLevel2(cwd) {
|
|
911
|
+
try {
|
|
912
|
+
return await topLevel(cwd);
|
|
913
|
+
} catch {
|
|
914
|
+
return void 0;
|
|
915
|
+
}
|
|
916
|
+
}
|
|
793
917
|
async function applyResolvedIdentity(args) {
|
|
794
918
|
const { name, email, sshCommand, identity, opts } = args;
|
|
795
919
|
const session = opts.session ?? (opts.source === "cli" ? cliSessionId() : "ext:unknown");
|
|
@@ -811,18 +935,20 @@ async function applyResolvedIdentity(args) {
|
|
|
811
935
|
const state = await readState(opts.cwd);
|
|
812
936
|
state.activeIdentity = identity?.id;
|
|
813
937
|
state.heldBy = nowHeldBy(session, opts.source);
|
|
938
|
+
if (state.signing === true && identity?.sshKeyPath && sshCommand) {
|
|
939
|
+
await setConfig("commit.gpgsign", "true", "local", opts.cwd);
|
|
940
|
+
await setConfig("gpg.format", "ssh", "local", opts.cwd);
|
|
941
|
+
const pub = `${identity.sshKeyPath}.pub`;
|
|
942
|
+
const viaAgent = !!identity.sshKeyFingerprint && await keyInAgent(identity.sshKeyFingerprint) && fs.existsSync(pub);
|
|
943
|
+
await setConfig("user.signingKey", viaAgent ? pub : identity.sshKeyPath, "local", opts.cwd);
|
|
944
|
+
if (opts.askpassScriptPath) {
|
|
945
|
+
await setConfig("gpg.ssh.program", await ensureSignWrapper(opts.askpassScriptPath), "local", opts.cwd);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
814
948
|
await writeState(state, opts.cwd);
|
|
815
|
-
let
|
|
816
|
-
if (identity?.
|
|
817
|
-
|
|
818
|
-
keyPath: identity.sshKeyPath,
|
|
819
|
-
fingerprint: identity.sshKeyFingerprint,
|
|
820
|
-
askpassScriptPath: opts.askpassScriptPath,
|
|
821
|
-
socketPath: opts.socketPath,
|
|
822
|
-
token: opts.token,
|
|
823
|
-
passphraseCommand: identity.passphraseCommand,
|
|
824
|
-
useAppleKeychain: true
|
|
825
|
-
});
|
|
949
|
+
let agent;
|
|
950
|
+
if (identity?.sshKeyFingerprint) {
|
|
951
|
+
agent = { inAgent: await keyInAgent(identity.sshKeyFingerprint) };
|
|
826
952
|
}
|
|
827
953
|
await appendAudit({
|
|
828
954
|
action: "identity.use",
|
|
@@ -830,27 +956,70 @@ async function applyResolvedIdentity(args) {
|
|
|
830
956
|
identity: identity?.id,
|
|
831
957
|
identityName: identity?.name ?? name,
|
|
832
958
|
fingerprint: identity?.sshKeyFingerprint,
|
|
833
|
-
repo: await
|
|
959
|
+
repo: await safeTopLevel2(opts.cwd),
|
|
834
960
|
result: conflict ? "warn" : "ok",
|
|
835
961
|
message: conflict ? `overrode ${conflict.heldBy.session}` : void 0
|
|
836
962
|
});
|
|
837
|
-
return { name, email, sshCommand, conflict,
|
|
963
|
+
return { name, email, sshCommand, conflict, agent };
|
|
964
|
+
}
|
|
965
|
+
async function ensureAskpassWrapper(askpassScriptPath) {
|
|
966
|
+
const wrapperPath = path.join(dataDir(), "askpass-wrapper.sh");
|
|
967
|
+
const content = `#!/bin/sh
|
|
968
|
+
exec "${process.execPath}" "${askpassScriptPath}" "$@"
|
|
969
|
+
`;
|
|
970
|
+
try {
|
|
971
|
+
const existing = await promises.readFile(wrapperPath, "utf8");
|
|
972
|
+
if (existing === content) return wrapperPath;
|
|
973
|
+
} catch {
|
|
974
|
+
}
|
|
975
|
+
await promises.writeFile(wrapperPath, content, { mode: 448 });
|
|
976
|
+
await promises.chmod(wrapperPath, 448);
|
|
977
|
+
return wrapperPath;
|
|
838
978
|
}
|
|
839
979
|
async function applyIdentity(id, opts) {
|
|
840
|
-
|
|
841
|
-
|
|
980
|
+
let identity = await getIdentity(id);
|
|
981
|
+
let sshCommand;
|
|
982
|
+
let bridgeUsed = false;
|
|
983
|
+
let keyMissing = false;
|
|
984
|
+
if (identity.sshKeyPath) {
|
|
985
|
+
let imp;
|
|
986
|
+
try {
|
|
987
|
+
imp = await importKey(identity.sshKeyPath);
|
|
988
|
+
} catch {
|
|
989
|
+
imp = void 0;
|
|
990
|
+
}
|
|
991
|
+
if (imp) {
|
|
992
|
+
if (imp.fingerprint !== identity.sshKeyFingerprint || imp.encrypted !== !!identity.sshKeyEncrypted) {
|
|
993
|
+
await updateIdentity(id, { sshKeyFingerprint: imp.fingerprint, sshKeyEncrypted: imp.encrypted });
|
|
994
|
+
identity = { ...identity, sshKeyFingerprint: imp.fingerprint, sshKeyEncrypted: imp.encrypted };
|
|
995
|
+
}
|
|
996
|
+
const bare = `ssh -i ${identity.sshKeyPath} -o IdentitiesOnly=yes`;
|
|
997
|
+
const bridge = opts.socketPath && opts.token || discoverSessionBridge();
|
|
998
|
+
if (opts.askpassScriptPath && bridge) {
|
|
999
|
+
const wrapper = await ensureAskpassWrapper(opts.askpassScriptPath);
|
|
1000
|
+
sshCommand = `SSH_ASKPASS="${wrapper}" SSH_ASKPASS_REQUIRE=force DISPLAY=:0 ${bare}`;
|
|
1001
|
+
bridgeUsed = true;
|
|
1002
|
+
} else {
|
|
1003
|
+
sshCommand = bare;
|
|
1004
|
+
}
|
|
1005
|
+
} else {
|
|
1006
|
+
keyMissing = true;
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
842
1009
|
const result = await applyResolvedIdentity({
|
|
843
1010
|
name: opts.asName ?? identity.name,
|
|
844
1011
|
email: opts.asEmail ?? identity.email,
|
|
845
1012
|
sshCommand,
|
|
846
|
-
identity,
|
|
1013
|
+
identity: keyMissing ? { ...identity, sshKeyPath: void 0 } : identity,
|
|
847
1014
|
opts
|
|
848
1015
|
});
|
|
1016
|
+
result.keyMissing = keyMissing;
|
|
1017
|
+
result.bridgeUsed = bridgeUsed;
|
|
849
1018
|
return { identity, result };
|
|
850
1019
|
}
|
|
851
1020
|
|
|
852
1021
|
// src/core/identity/revert.ts
|
|
853
|
-
async function
|
|
1022
|
+
async function safeTopLevel3(cwd) {
|
|
854
1023
|
try {
|
|
855
1024
|
return await topLevel(cwd);
|
|
856
1025
|
} catch {
|
|
@@ -870,6 +1039,9 @@ async function revertRepo(opts) {
|
|
|
870
1039
|
await restoreKey("user.email", backups.userEmail, opts.cwd);
|
|
871
1040
|
await restoreKey("core.sshCommand", backups.sshCommand, opts.cwd);
|
|
872
1041
|
await restoreKey("commit.template", backups.commitTemplate, opts.cwd);
|
|
1042
|
+
await restoreKey("commit.gpgsign", backups.commitGpgsign, opts.cwd);
|
|
1043
|
+
await restoreKey("gpg.format", backups.gpgFormat, opts.cwd);
|
|
1044
|
+
await restoreKey("user.signingKey", backups.signingKey, opts.cwd);
|
|
873
1045
|
}
|
|
874
1046
|
if (state.activeIdentity) {
|
|
875
1047
|
try {
|
|
@@ -890,31 +1062,21 @@ async function revertRepo(opts) {
|
|
|
890
1062
|
await appendAudit({
|
|
891
1063
|
action: "identity.revert",
|
|
892
1064
|
source: opts.source,
|
|
893
|
-
repo: await
|
|
1065
|
+
repo: await safeTopLevel3(opts.cwd),
|
|
894
1066
|
result: managed === "true" ? "ok" : "warn",
|
|
895
1067
|
message: managed === "true" ? void 0 : "repo was not managed"
|
|
896
1068
|
});
|
|
897
1069
|
return { restored: backups, hadBackup: !!backups };
|
|
898
1070
|
}
|
|
899
|
-
|
|
1071
|
+
|
|
1072
|
+
// src/core/identity/logout.ts
|
|
1073
|
+
async function safeTopLevel4(cwd) {
|
|
900
1074
|
try {
|
|
901
1075
|
return await topLevel(cwd);
|
|
902
1076
|
} catch {
|
|
903
1077
|
return void 0;
|
|
904
1078
|
}
|
|
905
1079
|
}
|
|
906
|
-
async function shred(path) {
|
|
907
|
-
if (process.platform !== "win32") {
|
|
908
|
-
const r = child_process.spawnSync("shred", ["-u", path]);
|
|
909
|
-
if (r.status === 0) return true;
|
|
910
|
-
}
|
|
911
|
-
try {
|
|
912
|
-
await promises.unlink(path);
|
|
913
|
-
return true;
|
|
914
|
-
} catch {
|
|
915
|
-
return false;
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
1080
|
async function logoutIdentity(opts) {
|
|
919
1081
|
const state = await readState(opts.cwd);
|
|
920
1082
|
let id = opts.id ?? state.activeIdentity;
|
|
@@ -924,15 +1086,17 @@ async function logoutIdentity(opts) {
|
|
|
924
1086
|
else throw Errors.usage("no active identity \u2014 specify one: git colabor identity logout <id>");
|
|
925
1087
|
}
|
|
926
1088
|
const identity = await getIdentity(id);
|
|
927
|
-
|
|
928
|
-
let keyfileShredded = false;
|
|
929
|
-
if (identity.sshKeyPath) {
|
|
930
|
-
agentRemoved = await removeKey(identity.sshKeyPath);
|
|
931
|
-
keyfileShredded = await shred(identity.sshKeyPath);
|
|
932
|
-
}
|
|
1089
|
+
const agentRemoved = identity.sshKeyPath ? await removeKey(identity.sshKeyPath) : false;
|
|
933
1090
|
if (state.activeIdentity === identity.id) {
|
|
934
1091
|
state.activeIdentity = void 0;
|
|
935
1092
|
state.heldBy = void 0;
|
|
1093
|
+
if (state.signing === true) {
|
|
1094
|
+
state.signing = false;
|
|
1095
|
+
await unsetConfig("commit.gpgsign", "local", opts.cwd);
|
|
1096
|
+
await unsetConfig("gpg.format", "local", opts.cwd);
|
|
1097
|
+
await unsetConfig("user.signingKey", "local", opts.cwd);
|
|
1098
|
+
await unsetConfig("gpg.ssh.program", "local", opts.cwd);
|
|
1099
|
+
}
|
|
936
1100
|
await writeState(state, opts.cwd);
|
|
937
1101
|
}
|
|
938
1102
|
await appendAudit({
|
|
@@ -941,12 +1105,11 @@ async function logoutIdentity(opts) {
|
|
|
941
1105
|
identity: identity.id,
|
|
942
1106
|
identityName: identity.name,
|
|
943
1107
|
fingerprint: identity.sshKeyFingerprint,
|
|
944
|
-
repo: await
|
|
1108
|
+
repo: await safeTopLevel4(opts.cwd)
|
|
945
1109
|
});
|
|
946
1110
|
return {
|
|
947
1111
|
identity: { id: identity.id, name: identity.name, fingerprint: identity.sshKeyFingerprint },
|
|
948
|
-
agentRemoved
|
|
949
|
-
keyfileShredded
|
|
1112
|
+
agentRemoved
|
|
950
1113
|
};
|
|
951
1114
|
}
|
|
952
1115
|
var SOCKET_TIMEOUT_MS = 3e3;
|
|
@@ -974,32 +1137,17 @@ function askSocket(socketPath, token, fingerprint) {
|
|
|
974
1137
|
}, SOCKET_TIMEOUT_MS).unref?.();
|
|
975
1138
|
});
|
|
976
1139
|
}
|
|
977
|
-
function runPassphraseCommand(command) {
|
|
978
|
-
return new Promise((resolve4) => {
|
|
979
|
-
const child = child_process.spawn(command, { shell: true, stdio: ["ignore", "pipe", "pipe"] });
|
|
980
|
-
let out = "";
|
|
981
|
-
child.stdout.on("data", (d) => {
|
|
982
|
-
out += d.toString("utf8");
|
|
983
|
-
});
|
|
984
|
-
child.on("error", () => resolve4(void 0));
|
|
985
|
-
child.on("close", () => resolve4(out.replace(/\r?\n$/, "") || void 0));
|
|
986
|
-
});
|
|
987
|
-
}
|
|
988
1140
|
async function resolvePassphrase(fingerprint, opts = {}) {
|
|
989
1141
|
if (opts.socketPath && opts.token) {
|
|
990
1142
|
const got = await askSocket(opts.socketPath, opts.token, fingerprint);
|
|
991
1143
|
if (got) return got;
|
|
992
1144
|
}
|
|
993
|
-
if (opts.passphraseCommand) {
|
|
994
|
-
const got = await runPassphraseCommand(opts.passphraseCommand);
|
|
995
|
-
if (got) return got;
|
|
996
|
-
}
|
|
997
1145
|
if (opts.interactive && process.stdin.isTTY) {
|
|
998
1146
|
return await promptPassphraseTty(`Passphrase for ${fingerprint}: `);
|
|
999
1147
|
}
|
|
1000
1148
|
throw Errors.secretUnavailable("no passphrase source available", [
|
|
1001
|
-
"use --passphrase-command <cmd>",
|
|
1002
1149
|
"run within the VS Code extension (provides the askpass bridge)",
|
|
1150
|
+
"load the key into ssh-agent first (ssh-add)",
|
|
1003
1151
|
"import a passphraseless key"
|
|
1004
1152
|
]);
|
|
1005
1153
|
}
|
|
@@ -1021,15 +1169,14 @@ async function promptPassphraseTty(prompt) {
|
|
|
1021
1169
|
|
|
1022
1170
|
exports.AppError = AppError;
|
|
1023
1171
|
exports.Author = Author;
|
|
1024
|
-
exports.COAUTHORS_FILENAME = COAUTHORS_FILENAME;
|
|
1025
1172
|
exports.CO_AUTHOR_TRAILER = CO_AUTHOR_TRAILER;
|
|
1026
1173
|
exports.EXIT_CODES = EXIT_CODES;
|
|
1027
1174
|
exports.Errors = Errors;
|
|
1028
1175
|
exports.GitError = GitError;
|
|
1029
|
-
exports.addCoAuthor = addCoAuthor;
|
|
1030
1176
|
exports.addConfigValue = addConfigValue;
|
|
1031
1177
|
exports.addIdentity = addIdentity;
|
|
1032
1178
|
exports.addRedactable = addRedactable;
|
|
1179
|
+
exports.agentFingerprints = agentFingerprints;
|
|
1033
1180
|
exports.appendAudit = appendAudit;
|
|
1034
1181
|
exports.applyIdentity = applyIdentity;
|
|
1035
1182
|
exports.applyResolvedIdentity = applyResolvedIdentity;
|
|
@@ -1040,18 +1187,18 @@ exports.captureBackupIfFirstTouch = captureBackupIfFirstTouch;
|
|
|
1040
1187
|
exports.clearSelected = clearSelected;
|
|
1041
1188
|
exports.clearTemplate = clearTemplate;
|
|
1042
1189
|
exports.cliSessionId = cliSessionId;
|
|
1043
|
-
exports.coAuthorsPath = coAuthorsPath;
|
|
1044
1190
|
exports.createLogger = createLogger;
|
|
1045
1191
|
exports.dataDir = dataDir;
|
|
1046
1192
|
exports.defaultLogFile = defaultLogFile;
|
|
1047
1193
|
exports.detectConflict = detectConflict;
|
|
1194
|
+
exports.ensureAskpassWrapper = ensureAskpassWrapper;
|
|
1048
1195
|
exports.ensureCommitTemplate = ensureCommitTemplate;
|
|
1196
|
+
exports.ensureSignWrapper = ensureSignWrapper;
|
|
1049
1197
|
exports.fingerprintOfFile = fingerprintOfFile;
|
|
1050
1198
|
exports.formatMessage = formatMessage;
|
|
1051
1199
|
exports.genId = genId;
|
|
1052
1200
|
exports.genKey = genKey;
|
|
1053
1201
|
exports.getAllConfig = getAllConfig;
|
|
1054
|
-
exports.getAuthor = getAuthor;
|
|
1055
1202
|
exports.getConfig = getConfig;
|
|
1056
1203
|
exports.getIdentity = getIdentity;
|
|
1057
1204
|
exports.getSelected = getSelected;
|
|
@@ -1060,7 +1207,8 @@ exports.gitDir = gitDir;
|
|
|
1060
1207
|
exports.gitMessagePath = gitMessagePath;
|
|
1061
1208
|
exports.gitRaw = gitRaw;
|
|
1062
1209
|
exports.headSha = headSha;
|
|
1063
|
-
exports.
|
|
1210
|
+
exports.hideIdentityEmail = hideIdentityEmail;
|
|
1211
|
+
exports.historyCommitters = historyCommitters;
|
|
1064
1212
|
exports.importKey = importKey;
|
|
1065
1213
|
exports.insideWorkTree = insideWorkTree;
|
|
1066
1214
|
exports.isEncrypted = isEncrypted;
|
|
@@ -1068,18 +1216,18 @@ exports.isRepo = isRepo;
|
|
|
1068
1216
|
exports.isStale = isStale;
|
|
1069
1217
|
exports.keyFileName = keyFileName;
|
|
1070
1218
|
exports.keyFilePath = keyFilePath;
|
|
1219
|
+
exports.keyInAgent = keyInAgent;
|
|
1220
|
+
exports.keyUsable = keyUsable;
|
|
1071
1221
|
exports.keysDir = keysDir;
|
|
1072
1222
|
exports.listAgent = listAgent;
|
|
1073
|
-
exports.
|
|
1223
|
+
exports.listHiddenEmails = listHiddenEmails;
|
|
1074
1224
|
exports.listIdentities = listIdentities;
|
|
1075
1225
|
exports.loadKey = loadKey;
|
|
1076
1226
|
exports.logoutIdentity = logoutIdentity;
|
|
1077
1227
|
exports.mapPath = mapPath;
|
|
1078
|
-
exports.materializeKey = materializeKey;
|
|
1079
1228
|
exports.nowHeldBy = nowHeldBy;
|
|
1080
1229
|
exports.printTrailers = printTrailers;
|
|
1081
1230
|
exports.readAudit = readAudit;
|
|
1082
|
-
exports.readAuthors = readAuthors;
|
|
1083
1231
|
exports.readCurrentIdentityConfig = readCurrentIdentityConfig;
|
|
1084
1232
|
exports.readMap = readMap;
|
|
1085
1233
|
exports.readState = readState;
|
|
@@ -1093,18 +1241,18 @@ exports.resolvePassphrase = resolvePassphrase;
|
|
|
1093
1241
|
exports.resolveTemplatePath = resolveTemplatePath;
|
|
1094
1242
|
exports.revertRepo = revertRepo;
|
|
1095
1243
|
exports.runBin = runBin;
|
|
1096
|
-
exports.
|
|
1097
|
-
exports.schemaToAuthors = schemaToAuthors;
|
|
1244
|
+
exports.setCommitSigning = setCommitSigning;
|
|
1098
1245
|
exports.setConfig = setConfig;
|
|
1099
1246
|
exports.setDefault = setDefault;
|
|
1100
1247
|
exports.setSelected = setSelected;
|
|
1101
1248
|
exports.statePath = statePath;
|
|
1102
1249
|
exports.toErrorPayload = toErrorPayload;
|
|
1103
1250
|
exports.topLevel = topLevel;
|
|
1251
|
+
exports.unhideIdentityEmail = unhideIdentityEmail;
|
|
1104
1252
|
exports.unsetAllConfig = unsetAllConfig;
|
|
1105
1253
|
exports.unsetConfig = unsetConfig;
|
|
1106
|
-
exports.
|
|
1107
|
-
exports.
|
|
1254
|
+
exports.updateIdentity = updateIdentity;
|
|
1255
|
+
exports.verifyKey = verifyKey;
|
|
1108
1256
|
exports.writeCoAuthorsToTemplate = writeCoAuthorsToTemplate;
|
|
1109
1257
|
exports.writeMap = writeMap;
|
|
1110
1258
|
exports.writeState = writeState;
|