git-colabor 0.1.6 → 0.1.8

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);
@@ -1060,21 +1178,9 @@ async function applyResolvedIdentity(args) {
1060
1178
  const state = await readState(opts.cwd);
1061
1179
  state.activeIdentity = identity?.id;
1062
1180
  state.heldBy = nowHeldBy(session, opts.source);
1063
- if (state.signing === true && identity?.sshKeyPath && sshCommand) {
1064
- await setConfig("commit.gpgsign", "true", "local", opts.cwd);
1065
- await setConfig("gpg.format", "ssh", "local", opts.cwd);
1066
- const pub = `${identity.sshKeyPath}.pub`;
1067
- const viaAgent = !!identity.sshKeyFingerprint && await keyInAgent(identity.sshKeyFingerprint) && fs.existsSync(pub);
1068
- await setConfig("user.signingKey", viaAgent ? pub : identity.sshKeyPath, "local", opts.cwd);
1069
- if (opts.askpassScriptPath) {
1070
- await setConfig("gpg.ssh.program", await ensureSignWrapper(opts.askpassScriptPath), "local", opts.cwd);
1071
- }
1072
- } else if (state.signing === true && identity && !identity.sshKeyPath) {
1181
+ if (state.signing === true && identity && !await writeIdentitySigning(identity, opts.cwd, opts.askpassScriptPath)) {
1073
1182
  state.signing = false;
1074
- await unsetConfig("commit.gpgsign", "local", opts.cwd);
1075
- await unsetConfig("gpg.format", "local", opts.cwd);
1076
- await unsetConfig("user.signingKey", "local", opts.cwd);
1077
- await unsetConfig("gpg.ssh.program", "local", opts.cwd);
1183
+ await disableSigning(opts.cwd);
1078
1184
  }
1079
1185
  await writeState(state, opts.cwd);
1080
1186
  let agent;
@@ -1223,10 +1329,7 @@ async function logoutIdentity(opts) {
1223
1329
  state.heldBy = void 0;
1224
1330
  if (state.signing === true) {
1225
1331
  state.signing = false;
1226
- await unsetConfig("commit.gpgsign", "local", opts.cwd);
1227
- await unsetConfig("gpg.format", "local", opts.cwd);
1228
- await unsetConfig("user.signingKey", "local", opts.cwd);
1229
- await unsetConfig("gpg.ssh.program", "local", opts.cwd);
1332
+ await disableSigning(opts.cwd);
1230
1333
  }
1231
1334
  await writeState(state, opts.cwd);
1232
1335
  }
@@ -1291,7 +1394,7 @@ var ADD_SPEC = {
1291
1394
  };
1292
1395
  var AUDIT_SPEC = { valueFlags: ["--repo", "--since", "--tail"] };
1293
1396
  var APPLY_SPEC = { valueFlags: ["--name", "--email", "--ssh-command", "--source"] };
1294
- 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"] };
1295
1398
  var SIGN_SPEC = { valueFlags: [], boolFlags: ["--off"] };
1296
1399
  var AGENT_SPEC = { valueFlags: [], boolFlags: ["--remove", "--verify"] };
1297
1400
  function identityToJson(i, defaultId, agentPrints) {
@@ -1311,6 +1414,7 @@ function identityToJson(i, defaultId, agentPrints) {
1311
1414
  disabled: !!i.disabled,
1312
1415
  scope: i.scope ?? (i.imported ? "project" : "machine"),
1313
1416
  remoteKeys: i.remoteKeys,
1417
+ gpgKey: i.gpgKey,
1314
1418
  isDefault: i.id === defaultId
1315
1419
  };
1316
1420
  }
@@ -1336,9 +1440,11 @@ async function dispatch(command, tokens, ctx) {
1336
1440
  case "import":
1337
1441
  return importFromHistory(ctx);
1338
1442
  case "set":
1339
- return setIdentity(parseCommandArgs(tokens, SET_SPEC));
1443
+ return setIdentity(parseCommandArgs(tokens, SET_SPEC), ctx);
1340
1444
  case "sign":
1341
1445
  return sign(parseCommandArgs(tokens, SIGN_SPEC), ctx);
1446
+ case "gpg-keys":
1447
+ return gpgKeys(ctx);
1342
1448
  case "disable":
1343
1449
  return disable(parseCommandArgs(tokens), ctx);
1344
1450
  case "unhide":
@@ -1579,9 +1685,13 @@ async function rm2(p, ctx) {
1579
1685
  if (wasImported && email) await hideIdentityEmail(email);
1580
1686
  return ok({ removed: id, hidden: wasImported ? (email ?? "").toLowerCase() : void 0 });
1581
1687
  }
1582
- async function setIdentity(p) {
1688
+ async function setIdentity(p, ctx) {
1583
1689
  const id = p.positionals[0];
1584
- 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
+ }
1585
1695
  await getIdentity(id);
1586
1696
  const patch = {};
1587
1697
  const warnings = [];
@@ -1609,8 +1719,15 @@ async function setIdentity(p) {
1609
1719
  }
1610
1720
  patch.scope = s;
1611
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
+ }
1612
1729
  if (Object.keys(patch).length === 0) {
1613
- 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");
1614
1731
  }
1615
1732
  const next = await updateIdentity(id, patch);
1616
1733
  await appendAudit({
@@ -1650,16 +1767,20 @@ async function hiddenLs() {
1650
1767
  }
1651
1768
  async function sign(p, ctx) {
1652
1769
  const id = p.positionals[0];
1653
- 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");
1654
1772
  const r = await setCommitSigning({
1655
1773
  source: "cli",
1656
1774
  cwd: ctx.cwd,
1657
1775
  id,
1658
- on: !p.bools.has("--off"),
1776
+ on,
1659
1777
  askpassScriptPath: ctx.askpassScriptPath
1660
1778
  });
1661
1779
  return ok(r);
1662
1780
  }
1781
+ async function gpgKeys(ctx) {
1782
+ return ok({ keys: await listSigningKeys(ctx.cwd) });
1783
+ }
1663
1784
  async function disable(p, ctx) {
1664
1785
  const id = p.positionals[0];
1665
1786
  if (!id) throw Errors.usage("git colabor identity disable <id>");
@@ -1673,10 +1794,7 @@ async function disable(p, ctx) {
1673
1794
  state.heldBy = void 0;
1674
1795
  if (state.signing === true) {
1675
1796
  state.signing = false;
1676
- await unsetConfig("commit.gpgsign", "local", ctx.cwd);
1677
- await unsetConfig("gpg.format", "local", ctx.cwd);
1678
- await unsetConfig("user.signingKey", "local", ctx.cwd);
1679
- await unsetConfig("gpg.ssh.program", "local", ctx.cwd);
1797
+ await disableSigning(ctx.cwd);
1680
1798
  }
1681
1799
  await writeState(state, ctx.cwd);
1682
1800
  deactivated = true;
@@ -1716,7 +1834,6 @@ async function status(ctx) {
1716
1834
  const rs = await repoStatus(ctx.cwd);
1717
1835
  const active = rs.activeIdentityId ? identities.find((i) => i.id === rs.activeIdentityId) : void 0;
1718
1836
  const st = ctx.cwd ? await readState(ctx.cwd) : void 0;
1719
- const signingKey = await getConfig("user.signingKey", "local", ctx.cwd);
1720
1837
  const prints = await agentFingerprints();
1721
1838
  const hidden = new Set(await listHiddenEmails());
1722
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 }));
@@ -1726,7 +1843,8 @@ async function status(ctx) {
1726
1843
  managed: rs.managed,
1727
1844
  managedBy: rs.managedBy,
1728
1845
  heldBy: rs.heldBy,
1729
- 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),
1730
1848
  activeIdentity: active ? identityToJson(active, defaultIdentity, prints) : null,
1731
1849
  identities: rows,
1732
1850
  selected: rs.selected,
@@ -1795,7 +1913,9 @@ function topHelp() {
1795
1913
  " git colabor identity add --name <n> --email <e> [--key <path>]",
1796
1914
  " git colabor identity import (add all history committers as identities)",
1797
1915
  " git colabor identity set <id> --name <n> | --email <e> | --key <path> | --no-key",
1798
- " 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)",
1799
1919
  " git colabor identity disable <id> (deactivate on key/passphrase failure)",
1800
1920
  " git colabor identity rm <id>",
1801
1921
  " git colabor identity logout [id]",
@@ -1955,7 +2075,12 @@ function identityHuman(command, d) {
1955
2075
  }
1956
2076
  if (command === "sign") {
1957
2077
  const data = d;
1958
- 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");
1959
2084
  }
1960
2085
  if (command === "import") {
1961
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);
@@ -939,21 +1057,9 @@ async function applyResolvedIdentity(args) {
939
1057
  const state = await readState(opts.cwd);
940
1058
  state.activeIdentity = identity?.id;
941
1059
  state.heldBy = nowHeldBy(session, opts.source);
942
- if (state.signing === true && identity?.sshKeyPath && sshCommand) {
943
- await setConfig("commit.gpgsign", "true", "local", opts.cwd);
944
- await setConfig("gpg.format", "ssh", "local", opts.cwd);
945
- const pub = `${identity.sshKeyPath}.pub`;
946
- const viaAgent = !!identity.sshKeyFingerprint && await keyInAgent(identity.sshKeyFingerprint) && fs.existsSync(pub);
947
- await setConfig("user.signingKey", viaAgent ? pub : identity.sshKeyPath, "local", opts.cwd);
948
- if (opts.askpassScriptPath) {
949
- await setConfig("gpg.ssh.program", await ensureSignWrapper(opts.askpassScriptPath), "local", opts.cwd);
950
- }
951
- } else if (state.signing === true && identity && !identity.sshKeyPath) {
1060
+ if (state.signing === true && identity && !await writeIdentitySigning(identity, opts.cwd, opts.askpassScriptPath)) {
952
1061
  state.signing = false;
953
- await unsetConfig("commit.gpgsign", "local", opts.cwd);
954
- await unsetConfig("gpg.format", "local", opts.cwd);
955
- await unsetConfig("user.signingKey", "local", opts.cwd);
956
- await unsetConfig("gpg.ssh.program", "local", opts.cwd);
1062
+ await disableSigning(opts.cwd);
957
1063
  }
958
1064
  await writeState(state, opts.cwd);
959
1065
  let agent;
@@ -1102,10 +1208,7 @@ async function logoutIdentity(opts) {
1102
1208
  state.heldBy = void 0;
1103
1209
  if (state.signing === true) {
1104
1210
  state.signing = false;
1105
- await unsetConfig("commit.gpgsign", "local", opts.cwd);
1106
- await unsetConfig("gpg.format", "local", opts.cwd);
1107
- await unsetConfig("user.signingKey", "local", opts.cwd);
1108
- await unsetConfig("gpg.ssh.program", "local", opts.cwd);
1211
+ await disableSigning(opts.cwd);
1109
1212
  }
1110
1213
  await writeState(state, opts.cwd);
1111
1214
  }
@@ -1201,6 +1304,7 @@ exports.createLogger = createLogger;
1201
1304
  exports.dataDir = dataDir;
1202
1305
  exports.defaultLogFile = defaultLogFile;
1203
1306
  exports.detectConflict = detectConflict;
1307
+ exports.disableSigning = disableSigning;
1204
1308
  exports.ensureAskpassWrapper = ensureAskpassWrapper;
1205
1309
  exports.ensureCommitTemplate = ensureCommitTemplate;
1206
1310
  exports.ensureSignWrapper = ensureSignWrapper;
@@ -1210,6 +1314,7 @@ exports.genId = genId;
1210
1314
  exports.genKey = genKey;
1211
1315
  exports.getAllConfig = getAllConfig;
1212
1316
  exports.getConfig = getConfig;
1317
+ exports.getEffectiveConfig = getEffectiveConfig;
1213
1318
  exports.getIdentity = getIdentity;
1214
1319
  exports.getSelected = getSelected;
1215
1320
  exports.git = git;
@@ -1255,6 +1360,7 @@ exports.setCommitSigning = setCommitSigning;
1255
1360
  exports.setConfig = setConfig;
1256
1361
  exports.setDefault = setDefault;
1257
1362
  exports.setSelected = setSelected;
1363
+ exports.signingStatus = signingStatus;
1258
1364
  exports.statePath = statePath;
1259
1365
  exports.toErrorPayload = toErrorPayload;
1260
1366
  exports.topLevel = topLevel;
@@ -1264,5 +1370,6 @@ exports.unsetConfig = unsetConfig;
1264
1370
  exports.updateIdentity = updateIdentity;
1265
1371
  exports.verifyKey = verifyKey;
1266
1372
  exports.writeCoAuthorsToTemplate = writeCoAuthorsToTemplate;
1373
+ exports.writeIdentitySigning = writeIdentitySigning;
1267
1374
  exports.writeMap = writeMap;
1268
1375
  exports.writeState = writeState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "git-colabor",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Git co-author + multi-identity + SSH key management CLI",
5
5
  "repository": {
6
6
  "type": "git",