git-colabor 0.1.10 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -297,41 +297,43 @@ function signingKeyOwned(configured, keys) {
297
297
  }
298
298
  async function clearIdentityConfigLayer(identity, opts = {}) {
299
299
  const forceKeys = opts.forceKeys === true;
300
+ const scope = opts.scope === "global" ? "global" : "local";
300
301
  const sshPaths = [
301
302
  ...identity.pushKeyPath ? [identity.pushKeyPath] : [],
302
303
  ...identity.signKey?.format === "ssh" ? [identity.signKey.path] : []
303
304
  ];
304
305
  const gpgFingerprint = identity.signKey?.format === "openpgp" ? identity.signKey.key : void 0;
305
306
  const [n, e, ssh, signKey, sshProgram] = await Promise.all([
306
- getConfig("user.name", "local", opts.cwd),
307
- getConfig("user.email", "local", opts.cwd),
308
- getConfig("core.sshCommand", "local", opts.cwd),
309
- getConfig("user.signingKey", "local", opts.cwd),
310
- getConfig("gpg.ssh.program", "local", opts.cwd)
307
+ getConfig("user.name", scope, opts.cwd),
308
+ getConfig("user.email", scope, opts.cwd),
309
+ getConfig("core.sshCommand", scope, opts.cwd),
310
+ getConfig("user.signingKey", scope, opts.cwd),
311
+ getConfig("gpg.ssh.program", scope, opts.cwd)
311
312
  ]);
312
313
  const eq = (a, b) => !!a && a.trim() === b.trim();
313
314
  const result = { identityLayer: false, sshCommand: false, signing: false };
314
- if (eq(n, identity.name)) {
315
- await unsetConfig("user.name", "local", opts.cwd);
315
+ const emailIsThis = eq(e, identity.email);
316
+ if (emailIsThis || eq(n, identity.name)) {
317
+ await unsetConfig("user.name", scope, opts.cwd);
316
318
  result.identityLayer = true;
317
319
  }
318
- if (eq(e, identity.email)) {
319
- await unsetConfig("user.email", "local", opts.cwd);
320
+ if (emailIsThis) {
321
+ await unsetConfig("user.email", scope, opts.cwd);
320
322
  result.identityLayer = true;
321
323
  }
322
324
  if (ssh && (forceKeys || sshPaths.some((p) => sshCommandUses(ssh, p)))) {
323
- await unsetConfig("core.sshCommand", "local", opts.cwd);
325
+ await unsetConfig("core.sshCommand", scope, opts.cwd);
324
326
  result.sshCommand = true;
325
327
  }
326
328
  const wrapperIsOurs = !!sshProgram && sshProgram.includes("git-colabor");
327
329
  if (signKey && (forceKeys || signingKeyOwned(signKey, { sshPaths, gpgFingerprint }))) {
328
- await unsetConfig("user.signingKey", "local", opts.cwd);
330
+ await unsetConfig("user.signingKey", scope, opts.cwd);
329
331
  result.signing = true;
330
332
  }
331
333
  if (result.signing || wrapperIsOurs || forceKeys) {
332
- await unsetConfig("gpg.format", "local", opts.cwd);
333
- await unsetConfig("gpg.ssh.program", "local", opts.cwd);
334
- await unsetConfig("commit.gpgsign", "local", opts.cwd);
334
+ await unsetConfig("gpg.format", scope, opts.cwd);
335
+ await unsetConfig("gpg.ssh.program", scope, opts.cwd);
336
+ await unsetConfig("commit.gpgsign", scope, opts.cwd);
335
337
  }
336
338
  return result;
337
339
  }
@@ -387,38 +389,67 @@ init_rev();
387
389
  init_exec();
388
390
  init_types();
389
391
  var SHORTLOG_LINE = /^\s*\d+\t(.+)\s<(.+)>$/;
392
+ var AUTHOR_LINE = /^(.*)\s<([^<>]+)>$/;
390
393
  async function repoAuthors(filter, cwd) {
391
394
  const args = ["shortlog", "-seni", "HEAD"];
392
395
  if (filter) args.splice(1, 0, `--author=${filter}`);
393
- const r = await gitRaw(args, { cwd });
394
- if (r.exitCode !== 0) return [];
395
396
  const out = [];
396
- for (const line of r.stdout.split(/\r?\n/)) {
397
- const m = line.match(SHORTLOG_LINE);
398
- if (!m) continue;
399
- const name = m[1].trim();
400
- const email = m[2].trim();
397
+ const seen = /* @__PURE__ */ new Set();
398
+ const add = (name, email) => {
399
+ if (!name || !email) return;
400
+ const key = email.toLowerCase();
401
+ if (seen.has(key)) return;
402
+ seen.add(key);
401
403
  out.push(new exports.Author(genKey(name, email), name, email));
404
+ };
405
+ const r = await gitRaw(args, { cwd });
406
+ if (r.exitCode === 0) {
407
+ for (const line of r.stdout.split(/\r?\n/)) {
408
+ const m = line.match(SHORTLOG_LINE);
409
+ if (m) add(m[1].trim(), m[2].trim());
410
+ }
411
+ }
412
+ const t = await gitRaw(["log", "--format=%(trailers:key=Co-authored-by,valueonly)"], { cwd });
413
+ if (t.exitCode === 0) {
414
+ for (const line of t.stdout.split(/\r?\n/)) {
415
+ const m = line.match(AUTHOR_LINE);
416
+ if (!m) continue;
417
+ if (filter && !`${m[1]} <${m[2]}>`.toLowerCase().includes(filter.toLowerCase())) continue;
418
+ add(m[1].trim(), m[2].trim());
419
+ }
402
420
  }
403
421
  return out;
404
422
  }
405
423
 
406
424
  // src/core/git/committers.ts
407
425
  init_exec();
426
+ var AUTHOR_LINE2 = /^(.*)\s<([^<>]+)>$/;
427
+ function bump(counts, name, email) {
428
+ const key = email.toLowerCase();
429
+ const cur = counts.get(key);
430
+ if (cur) cur.count++;
431
+ else counts.set(key, { name, email, count: 1 });
432
+ }
408
433
  async function historyCommitters(cwd) {
409
- const r = await gitRaw(["log", "--format=%cn%x1f%ce"], { cwd });
410
- if (r.exitCode !== 0) return [];
411
434
  const counts = /* @__PURE__ */ new Map();
412
- for (const line of r.stdout.split("\n")) {
413
- const sep = line.indexOf("");
414
- if (sep < 0) continue;
415
- const name = line.slice(0, sep).trim();
416
- const email = line.slice(sep + 1).trim();
417
- if (!name || !email) continue;
418
- const key = email.toLowerCase();
419
- const cur = counts.get(key);
420
- if (cur) cur.count++;
421
- else counts.set(key, { name, email, count: 1 });
435
+ const r = await gitRaw(["log", "--format=%cn%x1f%ce"], { cwd });
436
+ if (r.exitCode === 0) {
437
+ for (const line of r.stdout.split("\n")) {
438
+ const sep = line.indexOf("");
439
+ if (sep < 0) continue;
440
+ const name = line.slice(0, sep).trim();
441
+ const email = line.slice(sep + 1).trim();
442
+ if (!name || !email) continue;
443
+ bump(counts, name, email);
444
+ }
445
+ }
446
+ const t = await gitRaw(["log", "--format=%(trailers:key=Co-authored-by,valueonly)"], { cwd });
447
+ if (t.exitCode === 0) {
448
+ for (const line of t.stdout.split("\n")) {
449
+ const m = line.match(AUTHOR_LINE2);
450
+ if (!m) continue;
451
+ bump(counts, m[1].trim(), m[2].trim());
452
+ }
422
453
  }
423
454
  return [...counts.values()].sort((a, b) => b.count - a.count).map(({ name, email }) => ({ name, email }));
424
455
  }
@@ -681,112 +712,185 @@ function toErrorPayload(e) {
681
712
  }
682
713
 
683
714
  // src/core/identity/map.ts
684
- function identityId(name, email) {
685
- return "id_" + crypto.createHash("sha256").update(`${name}
686
- ${email}`).digest("hex").slice(0, 12);
715
+ var norm = (s) => s.trim().toLowerCase();
716
+ var refOf = (i) => `${norm(i.name)} <${norm(i.email)}>`;
717
+ var idOf = (name, email) => `${name.trim()} <${email.trim()}>`;
718
+ function parseAuthorRef(ref) {
719
+ const m = ref.match(/^(.*) <([^<>]+)>$/);
720
+ return m ? { name: m[1], email: m[2] } : void 0;
721
+ }
722
+ function resolveRow(map, ref) {
723
+ const author = parseAuthorRef(ref);
724
+ if (author) {
725
+ const hit = map.identities.find((i) => norm(i.name) === norm(author.name) && norm(i.email) === norm(author.email));
726
+ if (hit) return hit;
727
+ }
728
+ const byAlias = map.identities.find((i) => i.alias !== void 0 && i.alias.toLowerCase() === ref.trim().toLowerCase());
729
+ if (byAlias) return byAlias;
730
+ if (/^\d+$/.test(ref.trim())) {
731
+ const row = map.identities[Number(ref.trim()) - 1];
732
+ if (row) return row;
733
+ throw Errors.usage(
734
+ `identity index ${ref.trim()} out of range \u2014 the list has ${map.identities.length} entries (see \`git colabor identity ls\`)`
735
+ );
736
+ }
737
+ if (!author) {
738
+ const byEmail = map.identities.filter((i) => norm(i.email) === norm(ref));
739
+ if (byEmail.length === 1) return byEmail[0];
740
+ if (byEmail.length > 1) {
741
+ const list = byEmail.map((i) => `"${idOf(i.name, i.email)}"`).join(", ");
742
+ throw Errors.usage(`email "${ref}" matches several identities \u2014 use the full form: ${list}`);
743
+ }
744
+ }
745
+ throw Errors.usage(`identity "${ref}" not found`);
746
+ }
747
+ function foldRow(rows, raw) {
748
+ let keyBindings = raw.keyBindings;
749
+ if (raw.sshKeyPath && raw.sshKeyFingerprint && !keyBindings?.pushKey) {
750
+ const ref = { path: raw.sshKeyPath, fingerprint: raw.sshKeyFingerprint };
751
+ if (raw.sshKeyEncrypted) ref.encrypted = true;
752
+ keyBindings = { ...keyBindings, pushKey: ref };
753
+ }
754
+ if (raw.gpgKey && !keyBindings?.signKey) {
755
+ keyBindings = { ...keyBindings, signKey: { format: "openpgp", key: raw.gpgKey } };
756
+ }
757
+ const scope = raw.scope === "user" || raw.scope === "vscode" ? "machine" : raw.scope ?? (raw.imported ? "project" : "machine");
758
+ const row = {
759
+ name: raw.name,
760
+ email: raw.email,
761
+ createdAt: raw.createdAt,
762
+ ...raw.host !== void 0 ? { host: raw.host } : {},
763
+ ...raw.alias !== void 0 && raw.alias !== "" ? { alias: raw.alias } : {},
764
+ ...scope !== void 0 ? { scope } : {},
765
+ ...raw.fromConfig !== void 0 ? { fromConfig: raw.fromConfig } : {},
766
+ ...keyBindings ? { keyBindings } : {}
767
+ };
768
+ const dup = rows.find((i) => norm(i.name) === norm(row.name) && norm(i.email) === norm(row.email));
769
+ if (dup) {
770
+ dup.keyBindings = { ...row.keyBindings ?? {}, ...dup.keyBindings ?? {} };
771
+ return;
772
+ }
773
+ rows.push(row);
687
774
  }
688
775
  async function readMap() {
689
776
  try {
690
777
  const txt = await promises.readFile(mapPath(), "utf8");
691
778
  const parsed = JSON.parse(txt);
692
- const identities = { ...parsed.identities };
693
- const keyBindings = { ...parsed.keyBindings ?? {} };
694
- for (const id of Object.keys(identities)) {
695
- const row = identities[id];
696
- if (row.sshKeyPath && row.sshKeyFingerprint && !keyBindings[id]?.pushKey) {
697
- const ref = { path: row.sshKeyPath, fingerprint: row.sshKeyFingerprint };
698
- if (row.sshKeyEncrypted) ref.encrypted = true;
699
- const existing = keyBindings[id] ?? {};
700
- keyBindings[id] = { ...existing, pushKey: ref };
701
- }
702
- if (row.gpgKey && !keyBindings[id]?.signKey) {
703
- const existing = keyBindings[id] ?? {};
704
- keyBindings[id] = { ...existing, signKey: { format: "openpgp", key: row.gpgKey } };
779
+ const raws = Array.isArray(parsed.identities) ? parsed.identities : Object.entries(parsed.identities ?? {}).map(([k, row]) => ({
780
+ ...row,
781
+ keyBindings: row.keyBindings ?? parsed.keyBindings?.[k]
782
+ }));
783
+ const identities = [];
784
+ for (const raw of raws) foldRow(identities, raw);
785
+ let defaultIdentity;
786
+ if (parsed.defaultIdentity) {
787
+ try {
788
+ defaultIdentity = refOf(resolveRow({ schemaVersion: 2, identities }, parsed.defaultIdentity));
789
+ } catch {
790
+ defaultIdentity = void 0;
705
791
  }
706
- delete row.sshKeyPath;
707
- delete row.sshKeyFingerprint;
708
- delete row.sshKeyEncrypted;
709
- delete row.gpgKey;
710
- delete row.passphraseCommand;
711
- delete row.disabled;
712
- delete row.remoteKeys;
713
- const legacyScope = row.scope;
714
- if (legacyScope === "user" || legacyScope === "vscode") row.scope = "machine";
715
792
  }
716
793
  const migrated = {
717
- schemaVersion: 1,
794
+ schemaVersion: 2,
795
+ // any v1 shape on disk folds into the v2 list above
718
796
  identities,
719
- keyBindings,
720
- defaultIdentity: parsed.defaultIdentity,
721
- hidden: parsed.hidden
797
+ ...defaultIdentity ? { defaultIdentity } : {},
798
+ ...parsed.hidden ? { hidden: parsed.hidden } : {},
799
+ ...parsed.standingDown ? { standingDown: parsed.standingDown } : {}
722
800
  };
723
801
  if (JSON.stringify(parsed) !== JSON.stringify(migrated)) {
724
802
  await writeMap(migrated);
725
803
  }
726
804
  return migrated;
727
805
  } catch {
728
- return { schemaVersion: 1, identities: {} };
806
+ return { schemaVersion: 2, identities: [] };
729
807
  }
730
808
  }
731
809
  async function writeMap(map) {
732
810
  await atomicWriteJson(mapPath(), map, 384);
733
811
  }
734
- async function getIdentity(id) {
735
- const map = await readMap();
736
- const found = map.identities[id];
737
- if (!found) throw Errors.usage(`identity "${id}" not found`);
738
- return found;
812
+ async function getIdentity(ref) {
813
+ return resolveRow(await readMap(), ref);
814
+ }
815
+ async function findByIdentityEmail(email) {
816
+ const { identities } = await readMap();
817
+ return identities.filter((i) => norm(i.email) === norm(email));
739
818
  }
740
819
  async function findIdentity(name, email) {
741
- const map = await readMap();
742
- return Object.values(map.identities).find(
743
- (i) => i.email.toLowerCase() === email.toLowerCase() && i.name.trim() === name.trim()
744
- );
820
+ const { identities } = await readMap();
821
+ return identities.find((i) => norm(i.email) === norm(email) && norm(i.name) === norm(name));
745
822
  }
746
823
  async function addIdentity(input) {
747
824
  const map = await readMap();
748
- const id = input.id ?? identityId(input.name, input.email);
749
- const identity = { ...input, id, createdAt: (/* @__PURE__ */ new Date()).toISOString() };
750
- if (map.identities[id]) return map.identities[id];
751
- map.identities[id] = identity;
752
- if (map.hidden?.[identity.email.toLowerCase()]) delete map.hidden[identity.email.toLowerCase()];
825
+ const dup = map.identities.find((i) => norm(i.name) === norm(input.name) && norm(i.email) === norm(input.email));
826
+ if (dup) return dup;
827
+ const identity = { ...input, createdAt: (/* @__PURE__ */ new Date()).toISOString() };
828
+ map.identities.push(identity);
829
+ const hiddenKey = norm(input.email);
830
+ if (map.hidden?.[hiddenKey]) delete map.hidden[hiddenKey];
753
831
  await writeMap(map);
754
832
  return identity;
755
833
  }
756
- async function updateIdentity(id, patch) {
834
+ async function updateIdentity(ref, patch, opts = {}) {
757
835
  const map = await readMap();
758
- const identity = map.identities[id];
759
- if (!identity) throw Errors.usage(`identity "${id}" not found`);
760
- map.identities[id] = { ...identity, ...patch };
836
+ const row = resolveRow(map, ref);
837
+ const before = refOf(row);
838
+ Object.assign(row, patch);
839
+ if (opts?.clearAlias) delete row.alias;
840
+ const after = refOf(row);
841
+ const aliasClash = patch.alias && map.identities.find((i) => i !== row && i.alias === patch.alias);
842
+ if (aliasClash) throw Errors.usage(`alias "${patch.alias}" already belongs to ${idOf(aliasClash.name, aliasClash.email)}`);
843
+ const clash = map.identities.find((i) => i !== row && refOf(i) === after);
844
+ if (clash) throw Errors.usage(`"${idOf(row.name, row.email)}" already exists`);
845
+ if (map.defaultIdentity === before) map.defaultIdentity = after;
761
846
  await writeMap(map);
762
- return map.identities[id];
847
+ return row;
763
848
  }
764
- async function getKeyBinding(id) {
765
- const map = await readMap();
766
- return map.keyBindings?.[id] ?? {};
849
+ async function getKeyBinding(ref) {
850
+ return (await getIdentity(ref)).keyBindings ?? {};
767
851
  }
768
- async function setKeyBinding(id, binding) {
852
+ async function setKeyBinding(ref, binding) {
769
853
  const map = await readMap();
770
- if (!map.identities[id]) throw Errors.usage(`identity "${id}" not found`);
771
- map.keyBindings = { ...map.keyBindings, [id]: binding };
854
+ resolveRow(map, ref).keyBindings = binding;
772
855
  await writeMap(map);
773
856
  }
774
- async function setPushKey(id, ref) {
775
- const b = await getKeyBinding(id);
776
- await setKeyBinding(id, { ...b, pushKey: ref });
857
+ async function setPushKey(ref, ref_) {
858
+ const b = await getKeyBinding(ref);
859
+ await setKeyBinding(ref, { ...b, pushKey: ref_ });
860
+ }
861
+ async function setSignKey(ref, key) {
862
+ const b = await getKeyBinding(ref);
863
+ await setKeyBinding(ref, { ...b, signKey: key });
777
864
  }
778
- async function setSignKey(id, ref) {
779
- const b = await getKeyBinding(id);
780
- await setKeyBinding(id, { ...b, signKey: ref });
865
+ async function setSignCommits(ref, on) {
866
+ const b = await getKeyBinding(ref);
867
+ await setKeyBinding(ref, { ...b, signCommits: on ? true : void 0 });
781
868
  }
782
869
  async function hideIdentityEmail(email) {
783
870
  const map = await readMap();
784
- map.hidden = { ...map.hidden, [email.toLowerCase()]: true };
871
+ map.hidden = { ...map.hidden, [norm(email)]: true };
872
+ await writeMap(map);
873
+ }
874
+ async function markStandingDown(root) {
875
+ const map = await readMap();
876
+ const roots = new Set(map.standingDown ?? []);
877
+ roots.add(root);
878
+ map.standingDown = [...roots];
785
879
  await writeMap(map);
786
880
  }
881
+ async function clearStandingDown(root) {
882
+ const map = await readMap();
883
+ if (!map.standingDown?.includes(root)) return;
884
+ map.standingDown = map.standingDown.filter((r) => r !== root);
885
+ await writeMap(map);
886
+ }
887
+ async function isStandingDown(root) {
888
+ const map = await readMap();
889
+ return (map.standingDown ?? []).includes(root);
890
+ }
787
891
  async function unhideIdentityEmail(email) {
788
892
  const map = await readMap();
789
- const key = email.toLowerCase();
893
+ const key = norm(email);
790
894
  if (!map.hidden?.[key]) return false;
791
895
  delete map.hidden[key];
792
896
  await writeMap(map);
@@ -796,23 +900,21 @@ async function listHiddenEmails() {
796
900
  const map = await readMap();
797
901
  return Object.keys(map.hidden ?? {});
798
902
  }
799
- async function removeIdentity(id) {
903
+ async function removeIdentity(ref) {
800
904
  const map = await readMap();
801
- if (!map.identities[id]) throw Errors.usage(`identity "${id}" not found`);
802
- delete map.identities[id];
803
- if (map.keyBindings?.[id]) delete map.keyBindings[id];
804
- if (map.defaultIdentity === id) delete map.defaultIdentity;
905
+ const row = resolveRow(map, ref);
906
+ map.identities = map.identities.filter((i) => i !== row);
907
+ if (map.defaultIdentity === refOf(row)) delete map.defaultIdentity;
805
908
  await writeMap(map);
806
909
  }
807
- async function setDefault(id) {
910
+ async function setDefault(ref) {
808
911
  const map = await readMap();
809
- if (!map.identities[id]) throw Errors.usage(`identity "${id}" not found`);
810
- map.defaultIdentity = id;
912
+ map.defaultIdentity = refOf(resolveRow(map, ref));
811
913
  await writeMap(map);
812
914
  }
813
915
  async function listIdentities() {
814
916
  const map = await readMap();
815
- return { identities: Object.values(map.identities), defaultIdentity: map.defaultIdentity };
917
+ return { identities: map.identities, ...map.defaultIdentity ? { defaultIdentity: map.defaultIdentity } : {} };
816
918
  }
817
919
 
818
920
  // src/core/repo/status.ts
@@ -842,7 +944,10 @@ async function repoStatus(cwd) {
842
944
  const available = identityMap.identities.filter((i) => !selectedEmails.has(i.email)).map((i) => ({ key: genKey(i.name, i.email), name: i.name, email: i.email }));
843
945
  return {
844
946
  inRepo: true,
845
- repo: cwd,
947
+ // canonical repo root (topLevel resolves symlinks, e.g. /var →
948
+ // /private/var on macOS) — the standing-down store and every per-repo
949
+ // comparison key off this exact form
950
+ repo: await topLevel(cwd),
846
951
  managed: managed === "true",
847
952
  managedBy: managedBy ?? null,
848
953
  activeIdentityId: state.activeIdentity ?? null,
@@ -1133,7 +1238,7 @@ async function signingKeyPath(sshKeyPath, fingerprint) {
1133
1238
  return sshKeyPath;
1134
1239
  }
1135
1240
  async function signingConfigFor(identity) {
1136
- const binding = await getKeyBinding(identity.id);
1241
+ const binding = await getKeyBinding(idOf(identity.name, identity.email));
1137
1242
  if (binding.signKey) {
1138
1243
  return binding.signKey.format === "openpgp" ? { format: "openpgp", key: binding.signKey.key } : { format: "ssh", key: await signingKeyPath(binding.signKey.path, binding.signKey.fingerprint) };
1139
1244
  }
@@ -1142,32 +1247,33 @@ async function signingConfigFor(identity) {
1142
1247
  }
1143
1248
  return void 0;
1144
1249
  }
1145
- async function writeIdentitySigning(identity, cwd, askpassScriptPath) {
1250
+ async function writeIdentitySigning(identity, cwd, askpassScriptPath, scope = "local") {
1146
1251
  const cfg = await signingConfigFor(identity);
1147
1252
  if (!cfg) return void 0;
1148
- await setConfig("commit.gpgsign", "true", "local", cwd);
1149
- await setConfig("gpg.format", cfg.format, "local", cwd);
1150
- await setConfig("user.signingKey", cfg.key, "local", cwd);
1253
+ await setConfig("commit.gpgsign", "true", scope, cwd);
1254
+ await setConfig("gpg.format", cfg.format, scope, cwd);
1255
+ await setConfig("user.signingKey", cfg.key, scope, cwd);
1151
1256
  if (cfg.format === "ssh" && askpassScriptPath) {
1152
- await setConfig("gpg.ssh.program", await ensureSignWrapper(askpassScriptPath), "local", cwd);
1257
+ await setConfig("gpg.ssh.program", await ensureSignWrapper(askpassScriptPath), scope, cwd);
1153
1258
  } else if (cfg.format === "openpgp") {
1154
- await unsetConfig("gpg.ssh.program", "local", cwd);
1259
+ await unsetConfig("gpg.ssh.program", scope, cwd);
1155
1260
  }
1156
1261
  return cfg;
1157
1262
  }
1158
- async function disableSigning(cwd, _managed = true) {
1159
- await unsetConfig("gpg.format", "local", cwd);
1160
- await unsetConfig("user.signingKey", "local", cwd);
1161
- await unsetConfig("gpg.ssh.program", "local", cwd);
1162
- await unsetConfig("commit.gpgsign", "local", cwd);
1263
+ async function disableSigning(cwd, scope = "local") {
1264
+ await unsetConfig("gpg.format", scope, cwd);
1265
+ await unsetConfig("user.signingKey", scope, cwd);
1266
+ await unsetConfig("gpg.ssh.program", scope, cwd);
1267
+ await unsetConfig("commit.gpgsign", scope, cwd);
1163
1268
  if ((await getEffectiveConfig("commit.gpgsign", cwd, "bool"))?.value === "true") {
1164
- await setConfig("commit.gpgsign", "false", "local", cwd);
1269
+ await setConfig("commit.gpgsign", "false", scope, cwd);
1165
1270
  }
1166
1271
  }
1167
1272
  async function setCommitSigning(opts) {
1168
1273
  if (!await insideWorkTree(opts.cwd)) throw Errors.notARepo(opts.cwd);
1169
1274
  if (!opts.on) {
1170
- await disableSigning(opts.cwd, false);
1275
+ await disableSigning(opts.cwd);
1276
+ if (opts.id) await setSignCommits(opts.id, false);
1171
1277
  await appendAudit({ action: "identity.sign", source: opts.source, identity: opts.id, repo: await safeTopLevel(opts.cwd), result: "ok", message: "signing off" });
1172
1278
  return { signing: false };
1173
1279
  }
@@ -1175,6 +1281,7 @@ async function setCommitSigning(opts) {
1175
1281
  const identity = await getIdentity(opts.id);
1176
1282
  const cfg = await writeIdentitySigning(identity, opts.cwd, opts.askpassScriptPath);
1177
1283
  if (!cfg) throw Errors.usage(`identity "${identity.name}" has no usable SSH or GPG key to sign with`);
1284
+ await setSignCommits(opts.id, true);
1178
1285
  await appendAudit({
1179
1286
  action: "identity.sign",
1180
1287
  source: opts.source,
@@ -1207,6 +1314,15 @@ async function signingStatus(cwd) {
1207
1314
  }
1208
1315
  return status;
1209
1316
  }
1317
+ function pidIsAlive(pid) {
1318
+ if (typeof pid !== "number" || !Number.isInteger(pid) || pid <= 0) return false;
1319
+ try {
1320
+ process.kill(pid, 0);
1321
+ return true;
1322
+ } catch (e) {
1323
+ return e.code === "EPERM";
1324
+ }
1325
+ }
1210
1326
  function discoverSessionBridges() {
1211
1327
  try {
1212
1328
  const dir = dataDir();
@@ -1215,7 +1331,23 @@ function discoverSessionBridges() {
1215
1331
  for (const s of sessions) {
1216
1332
  try {
1217
1333
  const parsed = JSON.parse(fs.readFileSync(path.join(dir, s.f), "utf8"));
1218
- if (parsed.socketPath && parsed.token) out.push({ socketPath: parsed.socketPath, token: parsed.token });
1334
+ if (!parsed.socketPath || !parsed.token) continue;
1335
+ const ourSock = /^askpass-[\w.-]+\.sock$/.test(path.basename(parsed.socketPath)) && path.join(dir, path.basename(parsed.socketPath)) === parsed.socketPath;
1336
+ const stale = parsed.pid !== void 0 && !pidIsAlive(parsed.pid) || !ourSock || !fs.existsSync(parsed.socketPath);
1337
+ if (stale) {
1338
+ if (ourSock && fs.existsSync(parsed.socketPath)) {
1339
+ try {
1340
+ fs.rmSync(parsed.socketPath);
1341
+ } catch {
1342
+ }
1343
+ }
1344
+ try {
1345
+ fs.unlinkSync(path.join(dir, s.f));
1346
+ } catch {
1347
+ }
1348
+ continue;
1349
+ }
1350
+ out.push({ socketPath: parsed.socketPath, token: parsed.token });
1219
1351
  } catch {
1220
1352
  }
1221
1353
  }
@@ -1239,24 +1371,29 @@ async function safeTopLevel2(cwd) {
1239
1371
  }
1240
1372
  async function applyResolvedIdentity(args) {
1241
1373
  const { name, email, sshCommand, identity, opts } = args;
1242
- await setConfig("user.name", name, "local", opts.cwd);
1243
- await setConfig("user.email", email, "local", opts.cwd);
1374
+ const scope = opts.configScope === "global" ? "global" : "local";
1375
+ await setConfig("user.name", name, scope, opts.cwd);
1376
+ await setConfig("user.email", email, scope, opts.cwd);
1244
1377
  if (sshCommand) {
1245
- await setConfig("core.sshCommand", sshCommand, "local", opts.cwd);
1378
+ await setConfig("core.sshCommand", sshCommand, scope, opts.cwd);
1246
1379
  } else if (identity) {
1247
- await unsetConfig("core.sshCommand", "local", opts.cwd);
1248
- const gpgsign = await getConfig("commit.gpgsign", "local", opts.cwd);
1249
- if (gpgsign === "true") await disableSigning(opts.cwd);
1380
+ await unsetConfig("core.sshCommand", scope, opts.cwd);
1381
+ const eff = await getEffectiveConfig("commit.gpgsign", opts.cwd, "bool");
1382
+ if (eff?.value === "true") await disableSigning(opts.cwd, scope);
1250
1383
  }
1251
- const binding = identity ? await getKeyBinding(identity.id) : void 0;
1384
+ const binding = identity ? await getKeyBinding(idOf(identity.name, identity.email)) : void 0;
1252
1385
  let agent;
1253
1386
  if (binding?.pushKey) {
1254
1387
  agent = { inAgent: await keyInAgent(binding.pushKey.fingerprint) };
1255
1388
  }
1389
+ if (scope === "local") {
1390
+ const tl = await safeTopLevel2(opts.cwd);
1391
+ if (tl) await clearStandingDown(tl);
1392
+ }
1256
1393
  await appendAudit({
1257
1394
  action: "identity.use",
1258
1395
  source: opts.source,
1259
- identity: identity?.id,
1396
+ identity: identity ? idOf(identity.name, identity.email) : void 0,
1260
1397
  identityName: identity?.name ?? name,
1261
1398
  fingerprint: binding?.pushKey?.fingerprint,
1262
1399
  repo: await safeTopLevel2(opts.cwd)
@@ -1313,10 +1450,11 @@ async function applyIdentity(id, opts) {
1313
1450
  result.keyMissing = keyMissing;
1314
1451
  result.keyDegraded = keyDegraded;
1315
1452
  if (identity) {
1316
- const gpgsign = await getConfig("commit.gpgsign", "local", opts.cwd);
1317
- if (gpgsign === "true") {
1318
- const okSign = await writeIdentitySigning(identity, opts.cwd, opts.askpassScriptPath);
1319
- if (!okSign) await disableSigning(opts.cwd);
1453
+ const scope = opts.configScope === "global" ? "global" : "local";
1454
+ const gpgsign = await getConfig("commit.gpgsign", scope, opts.cwd);
1455
+ if (gpgsign === "true" || binding.signCommits) {
1456
+ const okSign = await writeIdentitySigning(identity, opts.cwd, opts.askpassScriptPath, scope);
1457
+ if (!okSign && gpgsign === "true") await disableSigning(opts.cwd, scope);
1320
1458
  }
1321
1459
  }
1322
1460
  return { identity, result };
@@ -1330,7 +1468,7 @@ init_rev();
1330
1468
 
1331
1469
  // src/core/identity/resolve.ts
1332
1470
  init_config();
1333
- var norm = (s) => s.trim().toLowerCase();
1471
+ var norm2 = (s) => s.trim().toLowerCase();
1334
1472
  function keyPathFromSshCommand(cmd) {
1335
1473
  if (!cmd) return void 0;
1336
1474
  const m = cmd.match(/(?:^|\s)-i\s+(\S+)/);
@@ -1352,8 +1490,9 @@ async function resolveActive(cwd, opts = {}) {
1352
1490
  const name = n.value;
1353
1491
  const email = e.value;
1354
1492
  const { identities } = await listIdentities();
1355
- const byEmail = identities.filter((i) => norm(i.email) === norm(email));
1356
- let match = byEmail.find((i) => norm(i.name) === norm(name));
1493
+ let match = identities.find(
1494
+ (i) => norm2(i.email) === norm2(email) && norm2(i.name) === norm2(name)
1495
+ );
1357
1496
  let enrolled = false;
1358
1497
  if (!match) {
1359
1498
  if (!opts.enroll) return void 0;
@@ -1364,30 +1503,30 @@ async function resolveActive(cwd, opts = {}) {
1364
1503
  const cfgPushPath = keyPathFromSshCommand(sshCmd?.value);
1365
1504
  if (cfgPushPath) {
1366
1505
  const fp = await fpOf(cfgPushPath);
1367
- const binding = await getKeyBinding(match.id);
1506
+ const binding = await getKeyBinding(idOf(match.name, match.email));
1368
1507
  if (!binding.pushKey || binding.pushKey.path !== cfgPushPath || fp && binding.pushKey.fingerprint !== fp) {
1369
1508
  const ref = { path: cfgPushPath, fingerprint: fp ?? binding.pushKey?.fingerprint ?? "" };
1370
- await setPushKey(match.id, ref);
1509
+ await setPushKey(idOf(match.name, match.email), ref);
1371
1510
  }
1372
1511
  }
1373
1512
  const signKeyCfg = await getEffectiveConfig("user.signingKey", cwd);
1374
1513
  const gpgFormat = await getEffectiveConfig("gpg.format", cwd);
1375
1514
  if (signKeyCfg?.value) {
1376
- const binding = await getKeyBinding(match.id);
1515
+ const binding = await getKeyBinding(idOf(match.name, match.email));
1377
1516
  const cur = binding.signKey;
1378
1517
  if (gpgFormat?.value === "openpgp") {
1379
1518
  if (!cur || cur.format !== "openpgp" || cur.key !== signKeyCfg.value) {
1380
- await setSignKey(match.id, { format: "openpgp", key: signKeyCfg.value });
1519
+ await setSignKey(idOf(match.name, match.email), { format: "openpgp", key: signKeyCfg.value });
1381
1520
  }
1382
1521
  } else {
1383
1522
  const path = signKeyCfg.value.replace(/\.pub$/, "");
1384
1523
  if (!cur || cur.format !== "ssh" || cur.path !== path) {
1385
1524
  const fp = await fpOf(path);
1386
- if (fp) await setSignKey(match.id, { format: "ssh", path, fingerprint: fp });
1525
+ if (fp) await setSignKey(idOf(match.name, match.email), { format: "ssh", path, fingerprint: fp });
1387
1526
  }
1388
1527
  }
1389
1528
  }
1390
- const finalBinding = await getKeyBinding(match.id);
1529
+ const finalBinding = await getKeyBinding(idOf(match.name, match.email));
1391
1530
  return {
1392
1531
  identity: match,
1393
1532
  enrolled,
@@ -1405,26 +1544,28 @@ async function safeTopLevel3(cwd) {
1405
1544
  }
1406
1545
  async function logoutIdentity(opts) {
1407
1546
  const resolved = await resolveActive(opts.cwd);
1408
- let id = opts.id ?? resolved?.identity.id;
1547
+ let id = opts.id ?? (resolved ? idOf(resolved.identity.name, resolved.identity.email) : void 0);
1409
1548
  if (!id) {
1410
1549
  const { identities } = await listIdentities();
1411
- if (identities.length === 1) id = identities[0].id;
1550
+ if (identities.length === 1) id = idOf(identities[0].name, identities[0].email);
1412
1551
  else throw Errors.usage("no active identity \u2014 specify one: git colabor identity logout <id>");
1413
1552
  }
1414
1553
  const identity = await getIdentity(id);
1415
1554
  const binding = await getKeyBinding(id);
1416
- const agentRemoved = binding.pushKey ? await removeKey(binding.pushKey.path) : false;
1555
+ const agentRemoved = opts.removeAgentKey === false ? false : binding.pushKey ? await removeKey(binding.pushKey.path) : false;
1417
1556
  const { clearIdentityConfigLayer: clearIdentityConfigLayer2 } = await Promise.resolve().then(() => (init_configClear(), configClear_exports));
1418
- const cleared = await clearIdentityConfigLayer2(
1419
- {
1420
- name: identity.name,
1421
- email: identity.email,
1422
- pushKeyPath: binding.pushKey?.path,
1423
- signKey: binding.signKey
1424
- },
1425
- { cwd: opts.cwd, forceKeys: resolved?.identity.id === id }
1426
- );
1427
- const clearedAnything = cleared.identityLayer || cleared.sshCommand || cleared.signing;
1557
+ const layer = {
1558
+ name: identity.name,
1559
+ email: identity.email,
1560
+ pushKeyPath: binding.pushKey?.path,
1561
+ signKey: binding.signKey
1562
+ };
1563
+ const cleared = await clearIdentityConfigLayer2(layer, {
1564
+ cwd: opts.cwd,
1565
+ forceKeys: !!(resolved && idOf(resolved.identity.name, resolved.identity.email) === id)
1566
+ });
1567
+ const globalCleared = await clearIdentityConfigLayer2(layer, { scope: "global" });
1568
+ const clearedAnything = cleared.identityLayer || cleared.sshCommand || cleared.signing || globalCleared.identityLayer || globalCleared.sshCommand || globalCleared.signing;
1428
1569
  if (cleared.identityLayer) {
1429
1570
  const { unsetAllConfig: unsetAllConfig2 } = await Promise.resolve().then(() => (init_config(), config_exports));
1430
1571
  const { clearTemplate: clearTemplate2 } = await Promise.resolve().then(() => (init_template(), template_exports));
@@ -1432,17 +1573,21 @@ async function logoutIdentity(opts) {
1432
1573
  await clearTemplate2(opts.cwd).catch(() => {
1433
1574
  });
1434
1575
  }
1576
+ if (cleared.identityLayer || cleared.sshCommand || cleared.signing) {
1577
+ const tl = await safeTopLevel3(opts.cwd);
1578
+ if (tl) await markStandingDown(tl);
1579
+ }
1435
1580
  await appendAudit({
1436
1581
  action: "identity.logout",
1437
1582
  source: opts.source,
1438
- identity: identity.id,
1583
+ identity: id,
1439
1584
  identityName: identity.name,
1440
1585
  fingerprint: binding.pushKey?.fingerprint,
1441
1586
  repo: await safeTopLevel3(opts.cwd),
1442
1587
  message: clearedAnything ? `cleared ${[cleared.identityLayer ? "config" : null, cleared.sshCommand ? "ssh key" : null, cleared.signing ? "sign key" : null].filter(Boolean).join(" + ")} + agent` : "agent only"
1443
1588
  });
1444
1589
  return {
1445
- identity: { id: identity.id, name: identity.name, fingerprint: binding.pushKey?.fingerprint },
1590
+ identity: { id, name: identity.name, fingerprint: binding.pushKey?.fingerprint },
1446
1591
  agentRemoved,
1447
1592
  cleared: {
1448
1593
  agent: agentRemoved,
@@ -1523,6 +1668,7 @@ exports.auditLogPath = auditLogPath;
1523
1668
  exports.captureBackupIfFirstTouch = captureBackupIfFirstTouch;
1524
1669
  exports.clearIdentityConfigLayer = clearIdentityConfigLayer;
1525
1670
  exports.clearSelected = clearSelected;
1671
+ exports.clearStandingDown = clearStandingDown;
1526
1672
  exports.clearTemplate = clearTemplate;
1527
1673
  exports.cliSessionId = cliSessionId;
1528
1674
  exports.createLogger = createLogger;
@@ -1533,6 +1679,7 @@ exports.disableSigning = disableSigning;
1533
1679
  exports.ensureAskpassWrapper = ensureAskpassWrapper;
1534
1680
  exports.ensureCommitTemplate = ensureCommitTemplate;
1535
1681
  exports.ensureSignWrapper = ensureSignWrapper;
1682
+ exports.findByIdentityEmail = findByIdentityEmail;
1536
1683
  exports.findIdentity = findIdentity;
1537
1684
  exports.fingerprintOfFile = fingerprintOfFile;
1538
1685
  exports.formatMessage = formatMessage;
@@ -1550,12 +1697,13 @@ exports.gitRaw = gitRaw;
1550
1697
  exports.headSha = headSha;
1551
1698
  exports.hideIdentityEmail = hideIdentityEmail;
1552
1699
  exports.historyCommitters = historyCommitters;
1553
- exports.identityId = identityId;
1700
+ exports.idOf = idOf;
1554
1701
  exports.importKey = importKey;
1555
1702
  exports.insideWorkTree = insideWorkTree;
1556
1703
  exports.isEncrypted = isEncrypted;
1557
1704
  exports.isRepo = isRepo;
1558
1705
  exports.isStale = isStale;
1706
+ exports.isStandingDown = isStandingDown;
1559
1707
  exports.keyFileName = keyFileName;
1560
1708
  exports.keyFilePath = keyFilePath;
1561
1709
  exports.keyInAgent = keyInAgent;
@@ -1567,6 +1715,7 @@ exports.listIdentities = listIdentities;
1567
1715
  exports.loadKey = loadKey;
1568
1716
  exports.logoutIdentity = logoutIdentity;
1569
1717
  exports.mapPath = mapPath;
1718
+ exports.markStandingDown = markStandingDown;
1570
1719
  exports.nowHeldBy = nowHeldBy;
1571
1720
  exports.printTrailers = printTrailers;
1572
1721
  exports.readAudit = readAudit;
@@ -1588,6 +1737,7 @@ exports.setDefault = setDefault;
1588
1737
  exports.setKeyBinding = setKeyBinding;
1589
1738
  exports.setPushKey = setPushKey;
1590
1739
  exports.setSelected = setSelected;
1740
+ exports.setSignCommits = setSignCommits;
1591
1741
  exports.setSignKey = setSignKey;
1592
1742
  exports.signingStatus = signingStatus;
1593
1743
  exports.statePath = statePath;