git-colabor 0.1.5 → 0.1.7

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/README.md CHANGED
@@ -82,11 +82,12 @@ Selecting co-authors rewrites the commit template (`commit.template` / `~/.gitme
82
82
  | `identity add` | `--name`, `--email` (required); `--key <path>` (import), `--host`, `--default`. |
83
83
  | `identity rm <id>` | Remove from the identity map (logs out first, best-effort; imported identities are also hidden from future auto-imports). |
84
84
  | `identity import` | Add every distinct **committer** from the repo history as a key-less identity (idempotent, skips hidden emails; driven automatically by the extension on repo open). |
85
- | `identity set <id>` | Edit in place: `--name <n>`, `--email <e>`, `--key <path>` (re-reference), `--no-key` (clear), `--scope user\|machine\|project`. |
86
- | `identity sign <id> [--off]` | Opt-in SSH commit signing with the identity's key — writes `commit.gpgsign=true`, `gpg.format=ssh`, `user.signingKey`; `--off` clears them. While on, `identity use` re-binds the signing key to the newly applied identity. |
85
+ | `identity set <id>` | Edit in place: `--name <n>`, `--email <e>`, `--key <path>` (re-reference), `--no-key` (clear), `--scope user\|machine\|project`, `--gpg-key <key>` (bind a secret GPG signing key — fingerprint, key id or email; stored as the fingerprint), `--no-gpg-key` (unbind). |
86
+ | `identity sign <id>` / `identity sign [id] --off` | Opt-in commit signing with the identity's key — its bound GPG key (`gpg.format=openpgp`), else its SSH key (`gpg.format=ssh`) — writes `commit.gpgsign=true`, `gpg.format`, `user.signingKey`. While on, `identity use` re-binds to the newly applied identity's key (no key → off). `--off` drops colabor's keys and, when global/system config would still sign, pins `commit.gpgsign=false` locally — off means this repo does not sign. |
87
+ | `identity gpg-keys` | Secret GPG keys usable for signing (via `gpg.program`, default `gpg`): fingerprint + user ids. |
87
88
  | `identity logout [id]` | Remove key from `ssh-agent` — the key file itself is never touched; repo stays configured (use `revert` for that). |
88
89
  | `identity revert` | Restore the repo's pre-tool `user.*` / `core.sshCommand` / `commit.template` from the first-touch backup, clear markers. |
89
- | `identity status` | Per-repo snapshot: active identity, managed markers, `heldBy`, selected + available co-authors. |
90
+ | `identity status` | Per-repo snapshot: active identity, managed markers, `heldBy`, selected + available co-authors, and the **effective** commit signing (`signing`: `enabled`, `format`, `key`, the `scope`/`origin` file `commit.gpgsign` comes from, `managed` when colabor turned it on, and the GPG key's `keyEmails`). |
90
91
  | `identity audit` | `--repo <path>`, `--since <date>`, `--tail <n>` filters over the audit log. |
91
92
  | `identity doctor` | Self-check: binaries, identity map, key dir, agent, askpass bundle, repo markers. |
92
93
 
@@ -132,7 +133,7 @@ This is the contract the VS Code extension consumes.
132
133
 
133
134
  (Windows: the data dir is `%APPDATA%\git-colabor`.)
134
135
 
135
- Git config keys **written locally** per repo: `user.name`, `user.email`, `core.sshCommand` (only when the identity has a key: `ssh -i <key> -o IdentitiesOnly=yes`), and — with a usable key — SSH commit signing (`commit.gpgsign=true`, `gpg.format=ssh`, `user.signingKey=<key path>`; switched to a key-less identity clears them), `colabor.managed=true`, `colabor.managed-by=cli|ext`, and multi-valued `colabor.selected` (`Name <email>` per co-author). `commit.template` is set **globally** only if unset in any scope.
136
+ Git config keys **written locally** per repo: `user.name`, `user.email`, `core.sshCommand` (only when the identity has a key: `ssh -i <key> -o IdentitiesOnly=yes`), and — after an explicit `identity sign` — commit signing (`commit.gpgsign=true`, `gpg.format=openpgp|ssh`, `user.signingKey=<GPG fingerprint | SSH key path>`; switching to a key-less identity or `--off` clears them and pins `commit.gpgsign=false` when global/system config would still sign), `colabor.managed=true`, `colabor.managed-by=cli|ext`, and multi-valued `colabor.selected` (`Name <email>` per co-author). `commit.template` is set **globally** only if unset in any scope.
136
137
 
137
138
  ## git-mob compatibility
138
139
 
package/dist/cli.cjs CHANGED
@@ -379,6 +379,15 @@ async function getConfig(key, scope, cwd) {
379
379
  const v = r.stdout.trim();
380
380
  return v.length > 0 ? v : void 0;
381
381
  }
382
+ async function getEffectiveConfig(key, cwd, type) {
383
+ const r = await gitRaw(
384
+ ["config", "--show-scope", "--show-origin", ...type ? [`--type=${type}`] : [], "--get", key],
385
+ { cwd }
386
+ );
387
+ if (r.exitCode !== 0) return void 0;
388
+ const [scope = "", origin = "", ...value] = r.stdout.replace(/\n$/, "").split(" ");
389
+ return { value: value.join(" "), scope, origin: origin.replace(/^file:/, "") };
390
+ }
382
391
  async function getAllConfig(key, scope, cwd) {
383
392
  const r = await gitRaw(["config", ...scopeArgs(scope), "--get-all", key], { cwd });
384
393
  return r.exitCode === 0 ? r.stdout.replace(/\s+$/, "") : "";
@@ -932,6 +941,72 @@ async function verifyKey(opts) {
932
941
  );
933
942
  return verify === 0;
934
943
  }
944
+
945
+ // src/core/identity/gpg.ts
946
+ var UNUSABLE = /* @__PURE__ */ new Set(["i", "d", "r", "e", "n"]);
947
+ function unescape(s) {
948
+ return s.replace(/\\x([0-9a-fA-F]{2})/g, (_, h) => String.fromCharCode(parseInt(h, 16)));
949
+ }
950
+ function emailOf(uid) {
951
+ const m = /<([^<>\s]+@[^<>\s]+)>/.exec(uid) ?? /^([^<>\s]+@[^<>\s]+)$/.exec(uid.trim());
952
+ return m?.[1]?.toLowerCase();
953
+ }
954
+ function parseSecretKeys(colons) {
955
+ const keys = [];
956
+ let cur;
957
+ let usable = false;
958
+ let inSubkey = false;
959
+ const flush = () => {
960
+ if (cur && usable && cur.fingerprint) keys.push(cur);
961
+ };
962
+ for (const line of colons.split("\n")) {
963
+ const f = line.split(":");
964
+ switch (f[0]) {
965
+ case "sec": {
966
+ flush();
967
+ const caps = f[11] ?? "";
968
+ cur = { fingerprint: "", keyId: f[4] ?? "", uids: [], emails: [] };
969
+ usable = !UNUSABLE.has(f[1] ?? "") && caps.includes("S") && !caps.includes("D");
970
+ inSubkey = false;
971
+ break;
972
+ }
973
+ case "ssb":
974
+ inSubkey = true;
975
+ break;
976
+ case "fpr":
977
+ if (cur && !inSubkey && !cur.fingerprint) cur.fingerprint = f[9] ?? "";
978
+ break;
979
+ case "uid": {
980
+ if (!cur || UNUSABLE.has(f[1] ?? "")) break;
981
+ const uid = unescape(f[9] ?? "");
982
+ cur.uids.push(uid);
983
+ const email = emailOf(uid);
984
+ if (email) cur.emails.push(email);
985
+ break;
986
+ }
987
+ }
988
+ }
989
+ flush();
990
+ return keys;
991
+ }
992
+ async function gpgProgram(cwd) {
993
+ return (await getEffectiveConfig("gpg.program", cwd))?.value || "gpg";
994
+ }
995
+ async function listSigningKeys(cwd, query) {
996
+ const program = await gpgProgram(cwd);
997
+ let r;
998
+ try {
999
+ r = await runBin(program, ["--batch", "--list-secret-keys", "--with-colons", ...query ? [query] : []], { cwd });
1000
+ } catch {
1001
+ throw new AppError({
1002
+ code: "GPG_UNAVAILABLE",
1003
+ message: `cannot run "${program}"`,
1004
+ exitCode: 1,
1005
+ hints: ["install GnuPG, or point git at it: git config --global gpg.program <path>"]
1006
+ });
1007
+ }
1008
+ return r.exitCode === 0 ? parseSecretKeys(r.stdout) : [];
1009
+ }
935
1010
  async function safeTopLevel(cwd) {
936
1011
  try {
937
1012
  return await topLevel(cwd);
@@ -970,43 +1045,84 @@ async function signingKeyPath(sshKeyPath, fingerprint) {
970
1045
  }
971
1046
  return sshKeyPath;
972
1047
  }
1048
+ async function signingConfigFor(identity) {
1049
+ if (identity.gpgKey) return { format: "openpgp", key: identity.gpgKey };
1050
+ if (identity.sshKeyPath && await keyUsable(identity.sshKeyPath)) {
1051
+ return { format: "ssh", key: await signingKeyPath(identity.sshKeyPath, identity.sshKeyFingerprint) };
1052
+ }
1053
+ return void 0;
1054
+ }
1055
+ async function writeIdentitySigning(identity, cwd, askpassScriptPath) {
1056
+ const cfg = await signingConfigFor(identity);
1057
+ if (!cfg) return void 0;
1058
+ await setConfig("commit.gpgsign", "true", "local", cwd);
1059
+ await setConfig("gpg.format", cfg.format, "local", cwd);
1060
+ await setConfig("user.signingKey", cfg.key, "local", cwd);
1061
+ if (cfg.format === "ssh" && askpassScriptPath) {
1062
+ await setConfig("gpg.ssh.program", await ensureSignWrapper(askpassScriptPath), "local", cwd);
1063
+ } else if (cfg.format === "openpgp") {
1064
+ await unsetConfig("gpg.ssh.program", "local", cwd);
1065
+ }
1066
+ return cfg;
1067
+ }
1068
+ async function disableSigning(cwd, managed = true) {
1069
+ if (managed) {
1070
+ await unsetConfig("gpg.format", "local", cwd);
1071
+ await unsetConfig("user.signingKey", "local", cwd);
1072
+ await unsetConfig("gpg.ssh.program", "local", cwd);
1073
+ }
1074
+ await unsetConfig("commit.gpgsign", "local", cwd);
1075
+ if ((await getEffectiveConfig("commit.gpgsign", cwd, "bool"))?.value === "true") {
1076
+ await setConfig("commit.gpgsign", "false", "local", cwd);
1077
+ }
1078
+ }
973
1079
  async function setCommitSigning(opts) {
974
1080
  if (!await insideWorkTree(opts.cwd)) throw Errors.notARepo(opts.cwd);
975
1081
  const state = await readState(opts.cwd);
976
1082
  if (!opts.on) {
1083
+ await disableSigning(opts.cwd, state.signing === true);
977
1084
  state.signing = false;
978
1085
  await writeState(state, opts.cwd);
979
- await unsetConfig("commit.gpgsign", "local", opts.cwd);
980
- await unsetConfig("gpg.format", "local", opts.cwd);
981
- await unsetConfig("user.signingKey", "local", opts.cwd);
982
- await unsetConfig("gpg.ssh.program", "local", opts.cwd);
983
1086
  await appendAudit({ action: "identity.sign", source: opts.source, identity: opts.id, repo: await safeTopLevel(opts.cwd), result: "ok", message: "signing off" });
984
1087
  return { signing: false };
985
1088
  }
1089
+ if (!opts.id) throw Errors.usage("git colabor identity sign <id>");
986
1090
  const identity = await getIdentity(opts.id);
987
- if (!identity.sshKeyPath || !await keyUsable(identity.sshKeyPath)) {
988
- throw Errors.usage(`identity "${identity.name}" has no usable key to sign with`);
989
- }
1091
+ const cfg = await writeIdentitySigning(identity, opts.cwd, opts.askpassScriptPath);
1092
+ if (!cfg) throw Errors.usage(`identity "${identity.name}" has no usable SSH or GPG key to sign with`);
990
1093
  state.signing = true;
991
1094
  await writeState(state, opts.cwd);
992
- await setConfig("commit.gpgsign", "true", "local", opts.cwd);
993
- await setConfig("gpg.format", "ssh", "local", opts.cwd);
994
- await setConfig("user.signingKey", await signingKeyPath(identity.sshKeyPath, identity.sshKeyFingerprint), "local", opts.cwd);
995
- if (opts.askpassScriptPath) {
996
- const signWrapper = await ensureSignWrapper(opts.askpassScriptPath);
997
- await setConfig("gpg.ssh.program", signWrapper, "local", opts.cwd);
998
- }
999
1095
  await appendAudit({
1000
1096
  action: "identity.sign",
1001
1097
  source: opts.source,
1002
1098
  identity: opts.id,
1003
1099
  identityName: identity.name,
1004
- fingerprint: identity.sshKeyFingerprint,
1100
+ fingerprint: cfg.format === "ssh" ? identity.sshKeyFingerprint : cfg.key,
1005
1101
  repo: await safeTopLevel(opts.cwd),
1006
1102
  result: "ok",
1007
- message: "signing on"
1103
+ message: `signing on (${cfg.format})`
1008
1104
  });
1009
- return { signing: true, key: identity.sshKeyPath };
1105
+ return { signing: true, format: cfg.format, key: cfg.key };
1106
+ }
1107
+ async function signingStatus(cwd, managed) {
1108
+ const [sign2, format, key] = await Promise.all([
1109
+ getEffectiveConfig("commit.gpgsign", cwd, "bool"),
1110
+ getEffectiveConfig("gpg.format", cwd),
1111
+ getEffectiveConfig("user.signingKey", cwd)
1112
+ ]);
1113
+ const status2 = {
1114
+ enabled: sign2?.value === "true",
1115
+ key: key?.value || null,
1116
+ format: format?.value || "openpgp",
1117
+ scope: sign2?.scope ?? null,
1118
+ origin: sign2?.origin ?? null,
1119
+ managed
1120
+ };
1121
+ if (status2.enabled && status2.format === "openpgp" && status2.key) {
1122
+ const emails = (await listSigningKeys(cwd, status2.key).catch(() => [])).flatMap((k) => k.emails);
1123
+ if (emails.length > 0) status2.keyEmails = emails;
1124
+ }
1125
+ return status2;
1010
1126
  }
1011
1127
  function discoverSessionBridges() {
1012
1128
  try {
@@ -1028,6 +1144,8 @@ function discoverSessionBridges() {
1028
1144
  function discoverSessionBridge() {
1029
1145
  return discoverSessionBridges()[0];
1030
1146
  }
1147
+
1148
+ // src/core/identity/apply.ts
1031
1149
  async function safeTopLevel2(cwd) {
1032
1150
  try {
1033
1151
  return await topLevel(cwd);
@@ -1050,21 +1168,19 @@ async function applyResolvedIdentity(args) {
1050
1168
  await captureBackupIfFirstTouch(opts.cwd);
1051
1169
  await setConfig("user.name", name, "local", opts.cwd);
1052
1170
  await setConfig("user.email", email, "local", opts.cwd);
1053
- if (sshCommand) await setConfig("core.sshCommand", sshCommand, "local", opts.cwd);
1171
+ if (sshCommand) {
1172
+ await setConfig("core.sshCommand", sshCommand, "local", opts.cwd);
1173
+ } else if (identity && !identity.sshKeyPath) {
1174
+ await unsetConfig("core.sshCommand", "local", opts.cwd);
1175
+ }
1054
1176
  await setConfig("colabor.managed", "true", "local", opts.cwd);
1055
1177
  await setConfig("colabor.managed-by", opts.source, "local", opts.cwd);
1056
1178
  const state = await readState(opts.cwd);
1057
1179
  state.activeIdentity = identity?.id;
1058
1180
  state.heldBy = nowHeldBy(session, opts.source);
1059
- if (state.signing === true && identity?.sshKeyPath && sshCommand) {
1060
- await setConfig("commit.gpgsign", "true", "local", opts.cwd);
1061
- await setConfig("gpg.format", "ssh", "local", opts.cwd);
1062
- const pub = `${identity.sshKeyPath}.pub`;
1063
- const viaAgent = !!identity.sshKeyFingerprint && await keyInAgent(identity.sshKeyFingerprint) && fs.existsSync(pub);
1064
- await setConfig("user.signingKey", viaAgent ? pub : identity.sshKeyPath, "local", opts.cwd);
1065
- if (opts.askpassScriptPath) {
1066
- await setConfig("gpg.ssh.program", await ensureSignWrapper(opts.askpassScriptPath), "local", opts.cwd);
1067
- }
1181
+ if (state.signing === true && identity && !await writeIdentitySigning(identity, opts.cwd, opts.askpassScriptPath)) {
1182
+ state.signing = false;
1183
+ await disableSigning(opts.cwd);
1068
1184
  }
1069
1185
  await writeState(state, opts.cwd);
1070
1186
  let agent;
@@ -1213,10 +1329,7 @@ async function logoutIdentity(opts) {
1213
1329
  state.heldBy = void 0;
1214
1330
  if (state.signing === true) {
1215
1331
  state.signing = false;
1216
- await unsetConfig("commit.gpgsign", "local", opts.cwd);
1217
- await unsetConfig("gpg.format", "local", opts.cwd);
1218
- await unsetConfig("user.signingKey", "local", opts.cwd);
1219
- await unsetConfig("gpg.ssh.program", "local", opts.cwd);
1332
+ await disableSigning(opts.cwd);
1220
1333
  }
1221
1334
  await writeState(state, opts.cwd);
1222
1335
  }
@@ -1281,7 +1394,7 @@ var ADD_SPEC = {
1281
1394
  };
1282
1395
  var AUDIT_SPEC = { valueFlags: ["--repo", "--since", "--tail"] };
1283
1396
  var APPLY_SPEC = { valueFlags: ["--name", "--email", "--ssh-command", "--source"] };
1284
- var SET_SPEC = { valueFlags: ["--name", "--email", "--key", "--scope"], boolFlags: ["--no-key"] };
1397
+ var SET_SPEC = { valueFlags: ["--name", "--email", "--key", "--scope", "--gpg-key"], boolFlags: ["--no-key", "--no-gpg-key"] };
1285
1398
  var SIGN_SPEC = { valueFlags: [], boolFlags: ["--off"] };
1286
1399
  var AGENT_SPEC = { valueFlags: [], boolFlags: ["--remove", "--verify"] };
1287
1400
  function identityToJson(i, defaultId, agentPrints) {
@@ -1301,6 +1414,7 @@ function identityToJson(i, defaultId, agentPrints) {
1301
1414
  disabled: !!i.disabled,
1302
1415
  scope: i.scope ?? (i.imported ? "project" : "machine"),
1303
1416
  remoteKeys: i.remoteKeys,
1417
+ gpgKey: i.gpgKey,
1304
1418
  isDefault: i.id === defaultId
1305
1419
  };
1306
1420
  }
@@ -1326,9 +1440,11 @@ async function dispatch(command, tokens, ctx) {
1326
1440
  case "import":
1327
1441
  return importFromHistory(ctx);
1328
1442
  case "set":
1329
- return setIdentity(parseCommandArgs(tokens, SET_SPEC));
1443
+ return setIdentity(parseCommandArgs(tokens, SET_SPEC), ctx);
1330
1444
  case "sign":
1331
1445
  return sign(parseCommandArgs(tokens, SIGN_SPEC), ctx);
1446
+ case "gpg-keys":
1447
+ return gpgKeys(ctx);
1332
1448
  case "disable":
1333
1449
  return disable(parseCommandArgs(tokens), ctx);
1334
1450
  case "unhide":
@@ -1569,9 +1685,13 @@ async function rm2(p, ctx) {
1569
1685
  if (wasImported && email) await hideIdentityEmail(email);
1570
1686
  return ok({ removed: id, hidden: wasImported ? (email ?? "").toLowerCase() : void 0 });
1571
1687
  }
1572
- async function setIdentity(p) {
1688
+ async function setIdentity(p, ctx) {
1573
1689
  const id = p.positionals[0];
1574
- if (!id) throw Errors.usage("git colabor identity set <id> --name <n> | --email <e> | --key <path> | --no-key");
1690
+ if (!id) {
1691
+ throw Errors.usage(
1692
+ "git colabor identity set <id> --name <n> | --email <e> | --key <path> | --no-key | --gpg-key <key> | --no-gpg-key"
1693
+ );
1694
+ }
1575
1695
  await getIdentity(id);
1576
1696
  const patch = {};
1577
1697
  const warnings = [];
@@ -1599,8 +1719,15 @@ async function setIdentity(p) {
1599
1719
  }
1600
1720
  patch.scope = s;
1601
1721
  }
1722
+ if (p.bools.has("--no-gpg-key")) patch.gpgKey = void 0;
1723
+ if (p.values["--gpg-key"]) {
1724
+ const [key, ...more] = await listSigningKeys(ctx.cwd, p.values["--gpg-key"]);
1725
+ if (!key) throw Errors.usage(`no usable secret GPG signing key matches "${p.values["--gpg-key"]}"`);
1726
+ if (more.length > 0) throw Errors.usage(`"${p.values["--gpg-key"]}" matches several GPG keys \u2014 pass a fingerprint`);
1727
+ patch.gpgKey = key.fingerprint;
1728
+ }
1602
1729
  if (Object.keys(patch).length === 0) {
1603
- throw Errors.usage("identity set needs at least one of --name / --email / --key / --no-key");
1730
+ throw Errors.usage("identity set needs at least one of --name / --email / --key / --no-key / --gpg-key / --no-gpg-key");
1604
1731
  }
1605
1732
  const next = await updateIdentity(id, patch);
1606
1733
  await appendAudit({
@@ -1640,16 +1767,20 @@ async function hiddenLs() {
1640
1767
  }
1641
1768
  async function sign(p, ctx) {
1642
1769
  const id = p.positionals[0];
1643
- if (!id) throw Errors.usage("git colabor identity sign <id> [--off]");
1770
+ const on = !p.bools.has("--off");
1771
+ if (!id && on) throw Errors.usage("git colabor identity sign <id> | identity sign [id] --off");
1644
1772
  const r = await setCommitSigning({
1645
1773
  source: "cli",
1646
1774
  cwd: ctx.cwd,
1647
1775
  id,
1648
- on: !p.bools.has("--off"),
1776
+ on,
1649
1777
  askpassScriptPath: ctx.askpassScriptPath
1650
1778
  });
1651
1779
  return ok(r);
1652
1780
  }
1781
+ async function gpgKeys(ctx) {
1782
+ return ok({ keys: await listSigningKeys(ctx.cwd) });
1783
+ }
1653
1784
  async function disable(p, ctx) {
1654
1785
  const id = p.positionals[0];
1655
1786
  if (!id) throw Errors.usage("git colabor identity disable <id>");
@@ -1663,10 +1794,7 @@ async function disable(p, ctx) {
1663
1794
  state.heldBy = void 0;
1664
1795
  if (state.signing === true) {
1665
1796
  state.signing = false;
1666
- await unsetConfig("commit.gpgsign", "local", ctx.cwd);
1667
- await unsetConfig("gpg.format", "local", ctx.cwd);
1668
- await unsetConfig("user.signingKey", "local", ctx.cwd);
1669
- await unsetConfig("gpg.ssh.program", "local", ctx.cwd);
1797
+ await disableSigning(ctx.cwd);
1670
1798
  }
1671
1799
  await writeState(state, ctx.cwd);
1672
1800
  deactivated = true;
@@ -1706,7 +1834,6 @@ async function status(ctx) {
1706
1834
  const rs = await repoStatus(ctx.cwd);
1707
1835
  const active = rs.activeIdentityId ? identities.find((i) => i.id === rs.activeIdentityId) : void 0;
1708
1836
  const st = ctx.cwd ? await readState(ctx.cwd) : void 0;
1709
- const signingKey = await getConfig("user.signingKey", "local", ctx.cwd);
1710
1837
  const prints = await agentFingerprints();
1711
1838
  const hidden = new Set(await listHiddenEmails());
1712
1839
  const rows = identities.filter((i) => i.id === rs.activeIdentityId || !hidden.has(i.email.toLowerCase())).map((i) => ({ ...identityToJson(i, defaultIdentity, prints), active: i.id === rs.activeIdentityId }));
@@ -1716,7 +1843,8 @@ async function status(ctx) {
1716
1843
  managed: rs.managed,
1717
1844
  managedBy: rs.managedBy,
1718
1845
  heldBy: rs.heldBy,
1719
- signing: { enabled: st?.signing === true, key: signingKey ?? null },
1846
+ // what `git commit` will actually do here, and which scope decides it
1847
+ signing: await signingStatus(ctx.cwd, st?.signing === true),
1720
1848
  activeIdentity: active ? identityToJson(active, defaultIdentity, prints) : null,
1721
1849
  identities: rows,
1722
1850
  selected: rs.selected,
@@ -1785,7 +1913,9 @@ function topHelp() {
1785
1913
  " git colabor identity add --name <n> --email <e> [--key <path>]",
1786
1914
  " git colabor identity import (add all history committers as identities)",
1787
1915
  " git colabor identity set <id> --name <n> | --email <e> | --key <path> | --no-key",
1788
- " git colabor identity sign <id> [--off] (opt-in SSH commit signing)",
1916
+ " | --gpg-key <key> | --no-gpg-key (GPG key to sign with)",
1917
+ " git colabor identity sign <id> | sign [id] --off (opt-in commit signing; GPG key if bound, else SSH)",
1918
+ " git colabor identity gpg-keys (secret GPG keys usable for signing)",
1789
1919
  " git colabor identity disable <id> (deactivate on key/passphrase failure)",
1790
1920
  " git colabor identity rm <id>",
1791
1921
  " git colabor identity logout [id]",
@@ -1945,7 +2075,12 @@ function identityHuman(command, d) {
1945
2075
  }
1946
2076
  if (command === "sign") {
1947
2077
  const data = d;
1948
- return data.signing ? `SSH commit signing ON (${data.key}).` : "SSH commit signing OFF.";
2078
+ return data.signing ? `Commit signing ON (${data.format} ${data.key}).` : "Commit signing OFF (this repo does not sign, whatever global config says).";
2079
+ }
2080
+ if (command === "gpg-keys") {
2081
+ const keys = d.keys;
2082
+ if (keys.length === 0) return "No secret GPG keys usable for signing.";
2083
+ return keys.map((k) => `${k.fingerprint} ${k.uids.join(", ")}`).join("\n");
1949
2084
  }
1950
2085
  if (command === "import") {
1951
2086
  const data = d;
package/dist/index.cjs CHANGED
@@ -123,6 +123,15 @@ async function getConfig(key, scope, cwd) {
123
123
  const v = r.stdout.trim();
124
124
  return v.length > 0 ? v : void 0;
125
125
  }
126
+ async function getEffectiveConfig(key, cwd, type) {
127
+ const r = await gitRaw(
128
+ ["config", "--show-scope", "--show-origin", ...type ? [`--type=${type}`] : [], "--get", key],
129
+ { cwd }
130
+ );
131
+ if (r.exitCode !== 0) return void 0;
132
+ const [scope = "", origin = "", ...value] = r.stdout.replace(/\n$/, "").split(" ");
133
+ return { value: value.join(" "), scope, origin: origin.replace(/^file:/, "") };
134
+ }
126
135
  async function getAllConfig(key, scope, cwd) {
127
136
  const r = await gitRaw(["config", ...scopeArgs(scope), "--get-all", key], { cwd });
128
137
  return r.exitCode === 0 ? r.stdout.replace(/\s+$/, "") : "";
@@ -811,6 +820,72 @@ async function verifyKey(opts) {
811
820
  );
812
821
  return verify === 0;
813
822
  }
823
+
824
+ // src/core/identity/gpg.ts
825
+ var UNUSABLE = /* @__PURE__ */ new Set(["i", "d", "r", "e", "n"]);
826
+ function unescape(s) {
827
+ return s.replace(/\\x([0-9a-fA-F]{2})/g, (_, h) => String.fromCharCode(parseInt(h, 16)));
828
+ }
829
+ function emailOf(uid) {
830
+ const m = /<([^<>\s]+@[^<>\s]+)>/.exec(uid) ?? /^([^<>\s]+@[^<>\s]+)$/.exec(uid.trim());
831
+ return m?.[1]?.toLowerCase();
832
+ }
833
+ function parseSecretKeys(colons) {
834
+ const keys = [];
835
+ let cur;
836
+ let usable = false;
837
+ let inSubkey = false;
838
+ const flush = () => {
839
+ if (cur && usable && cur.fingerprint) keys.push(cur);
840
+ };
841
+ for (const line of colons.split("\n")) {
842
+ const f = line.split(":");
843
+ switch (f[0]) {
844
+ case "sec": {
845
+ flush();
846
+ const caps = f[11] ?? "";
847
+ cur = { fingerprint: "", keyId: f[4] ?? "", uids: [], emails: [] };
848
+ usable = !UNUSABLE.has(f[1] ?? "") && caps.includes("S") && !caps.includes("D");
849
+ inSubkey = false;
850
+ break;
851
+ }
852
+ case "ssb":
853
+ inSubkey = true;
854
+ break;
855
+ case "fpr":
856
+ if (cur && !inSubkey && !cur.fingerprint) cur.fingerprint = f[9] ?? "";
857
+ break;
858
+ case "uid": {
859
+ if (!cur || UNUSABLE.has(f[1] ?? "")) break;
860
+ const uid = unescape(f[9] ?? "");
861
+ cur.uids.push(uid);
862
+ const email = emailOf(uid);
863
+ if (email) cur.emails.push(email);
864
+ break;
865
+ }
866
+ }
867
+ }
868
+ flush();
869
+ return keys;
870
+ }
871
+ async function gpgProgram(cwd) {
872
+ return (await getEffectiveConfig("gpg.program", cwd))?.value || "gpg";
873
+ }
874
+ async function listSigningKeys(cwd, query) {
875
+ const program = await gpgProgram(cwd);
876
+ let r;
877
+ try {
878
+ r = await runBin(program, ["--batch", "--list-secret-keys", "--with-colons", ...query ? [query] : []], { cwd });
879
+ } catch {
880
+ throw new AppError({
881
+ code: "GPG_UNAVAILABLE",
882
+ message: `cannot run "${program}"`,
883
+ exitCode: 1,
884
+ hints: ["install GnuPG, or point git at it: git config --global gpg.program <path>"]
885
+ });
886
+ }
887
+ return r.exitCode === 0 ? parseSecretKeys(r.stdout) : [];
888
+ }
814
889
  async function safeTopLevel(cwd) {
815
890
  try {
816
891
  return await topLevel(cwd);
@@ -849,43 +924,84 @@ async function signingKeyPath(sshKeyPath, fingerprint) {
849
924
  }
850
925
  return sshKeyPath;
851
926
  }
927
+ async function signingConfigFor(identity) {
928
+ if (identity.gpgKey) return { format: "openpgp", key: identity.gpgKey };
929
+ if (identity.sshKeyPath && await keyUsable(identity.sshKeyPath)) {
930
+ return { format: "ssh", key: await signingKeyPath(identity.sshKeyPath, identity.sshKeyFingerprint) };
931
+ }
932
+ return void 0;
933
+ }
934
+ async function writeIdentitySigning(identity, cwd, askpassScriptPath) {
935
+ const cfg = await signingConfigFor(identity);
936
+ if (!cfg) return void 0;
937
+ await setConfig("commit.gpgsign", "true", "local", cwd);
938
+ await setConfig("gpg.format", cfg.format, "local", cwd);
939
+ await setConfig("user.signingKey", cfg.key, "local", cwd);
940
+ if (cfg.format === "ssh" && askpassScriptPath) {
941
+ await setConfig("gpg.ssh.program", await ensureSignWrapper(askpassScriptPath), "local", cwd);
942
+ } else if (cfg.format === "openpgp") {
943
+ await unsetConfig("gpg.ssh.program", "local", cwd);
944
+ }
945
+ return cfg;
946
+ }
947
+ async function disableSigning(cwd, managed = true) {
948
+ if (managed) {
949
+ await unsetConfig("gpg.format", "local", cwd);
950
+ await unsetConfig("user.signingKey", "local", cwd);
951
+ await unsetConfig("gpg.ssh.program", "local", cwd);
952
+ }
953
+ await unsetConfig("commit.gpgsign", "local", cwd);
954
+ if ((await getEffectiveConfig("commit.gpgsign", cwd, "bool"))?.value === "true") {
955
+ await setConfig("commit.gpgsign", "false", "local", cwd);
956
+ }
957
+ }
852
958
  async function setCommitSigning(opts) {
853
959
  if (!await insideWorkTree(opts.cwd)) throw Errors.notARepo(opts.cwd);
854
960
  const state = await readState(opts.cwd);
855
961
  if (!opts.on) {
962
+ await disableSigning(opts.cwd, state.signing === true);
856
963
  state.signing = false;
857
964
  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
965
  await appendAudit({ action: "identity.sign", source: opts.source, identity: opts.id, repo: await safeTopLevel(opts.cwd), result: "ok", message: "signing off" });
863
966
  return { signing: false };
864
967
  }
968
+ if (!opts.id) throw Errors.usage("git colabor identity sign <id>");
865
969
  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
- }
970
+ const cfg = await writeIdentitySigning(identity, opts.cwd, opts.askpassScriptPath);
971
+ if (!cfg) throw Errors.usage(`identity "${identity.name}" has no usable SSH or GPG key to sign with`);
869
972
  state.signing = true;
870
973
  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
974
  await appendAudit({
879
975
  action: "identity.sign",
880
976
  source: opts.source,
881
977
  identity: opts.id,
882
978
  identityName: identity.name,
883
- fingerprint: identity.sshKeyFingerprint,
979
+ fingerprint: cfg.format === "ssh" ? identity.sshKeyFingerprint : cfg.key,
884
980
  repo: await safeTopLevel(opts.cwd),
885
981
  result: "ok",
886
- message: "signing on"
982
+ message: `signing on (${cfg.format})`
887
983
  });
888
- return { signing: true, key: identity.sshKeyPath };
984
+ return { signing: true, format: cfg.format, key: cfg.key };
985
+ }
986
+ async function signingStatus(cwd, managed) {
987
+ const [sign, format, key] = await Promise.all([
988
+ getEffectiveConfig("commit.gpgsign", cwd, "bool"),
989
+ getEffectiveConfig("gpg.format", cwd),
990
+ getEffectiveConfig("user.signingKey", cwd)
991
+ ]);
992
+ const status = {
993
+ enabled: sign?.value === "true",
994
+ key: key?.value || null,
995
+ format: format?.value || "openpgp",
996
+ scope: sign?.scope ?? null,
997
+ origin: sign?.origin ?? null,
998
+ managed
999
+ };
1000
+ if (status.enabled && status.format === "openpgp" && status.key) {
1001
+ const emails = (await listSigningKeys(cwd, status.key).catch(() => [])).flatMap((k) => k.emails);
1002
+ if (emails.length > 0) status.keyEmails = emails;
1003
+ }
1004
+ return status;
889
1005
  }
890
1006
  function discoverSessionBridges() {
891
1007
  try {
@@ -907,6 +1023,8 @@ function discoverSessionBridges() {
907
1023
  function discoverSessionBridge() {
908
1024
  return discoverSessionBridges()[0];
909
1025
  }
1026
+
1027
+ // src/core/identity/apply.ts
910
1028
  async function safeTopLevel2(cwd) {
911
1029
  try {
912
1030
  return await topLevel(cwd);
@@ -929,21 +1047,19 @@ async function applyResolvedIdentity(args) {
929
1047
  await captureBackupIfFirstTouch(opts.cwd);
930
1048
  await setConfig("user.name", name, "local", opts.cwd);
931
1049
  await setConfig("user.email", email, "local", opts.cwd);
932
- if (sshCommand) await setConfig("core.sshCommand", sshCommand, "local", opts.cwd);
1050
+ if (sshCommand) {
1051
+ await setConfig("core.sshCommand", sshCommand, "local", opts.cwd);
1052
+ } else if (identity && !identity.sshKeyPath) {
1053
+ await unsetConfig("core.sshCommand", "local", opts.cwd);
1054
+ }
933
1055
  await setConfig("colabor.managed", "true", "local", opts.cwd);
934
1056
  await setConfig("colabor.managed-by", opts.source, "local", opts.cwd);
935
1057
  const state = await readState(opts.cwd);
936
1058
  state.activeIdentity = identity?.id;
937
1059
  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
- }
1060
+ if (state.signing === true && identity && !await writeIdentitySigning(identity, opts.cwd, opts.askpassScriptPath)) {
1061
+ state.signing = false;
1062
+ await disableSigning(opts.cwd);
947
1063
  }
948
1064
  await writeState(state, opts.cwd);
949
1065
  let agent;
@@ -1092,10 +1208,7 @@ async function logoutIdentity(opts) {
1092
1208
  state.heldBy = void 0;
1093
1209
  if (state.signing === true) {
1094
1210
  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);
1211
+ await disableSigning(opts.cwd);
1099
1212
  }
1100
1213
  await writeState(state, opts.cwd);
1101
1214
  }
@@ -1191,6 +1304,7 @@ exports.createLogger = createLogger;
1191
1304
  exports.dataDir = dataDir;
1192
1305
  exports.defaultLogFile = defaultLogFile;
1193
1306
  exports.detectConflict = detectConflict;
1307
+ exports.disableSigning = disableSigning;
1194
1308
  exports.ensureAskpassWrapper = ensureAskpassWrapper;
1195
1309
  exports.ensureCommitTemplate = ensureCommitTemplate;
1196
1310
  exports.ensureSignWrapper = ensureSignWrapper;
@@ -1200,6 +1314,7 @@ exports.genId = genId;
1200
1314
  exports.genKey = genKey;
1201
1315
  exports.getAllConfig = getAllConfig;
1202
1316
  exports.getConfig = getConfig;
1317
+ exports.getEffectiveConfig = getEffectiveConfig;
1203
1318
  exports.getIdentity = getIdentity;
1204
1319
  exports.getSelected = getSelected;
1205
1320
  exports.git = git;
@@ -1245,6 +1360,7 @@ exports.setCommitSigning = setCommitSigning;
1245
1360
  exports.setConfig = setConfig;
1246
1361
  exports.setDefault = setDefault;
1247
1362
  exports.setSelected = setSelected;
1363
+ exports.signingStatus = signingStatus;
1248
1364
  exports.statePath = statePath;
1249
1365
  exports.toErrorPayload = toErrorPayload;
1250
1366
  exports.topLevel = topLevel;
@@ -1254,5 +1370,6 @@ exports.unsetConfig = unsetConfig;
1254
1370
  exports.updateIdentity = updateIdentity;
1255
1371
  exports.verifyKey = verifyKey;
1256
1372
  exports.writeCoAuthorsToTemplate = writeCoAuthorsToTemplate;
1373
+ exports.writeIdentitySigning = writeIdentitySigning;
1257
1374
  exports.writeMap = writeMap;
1258
1375
  exports.writeState = writeState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "git-colabor",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Git co-author + multi-identity + SSH key management CLI",
5
5
  "repository": {
6
6
  "type": "git",