git-colabor 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,179 @@
1
+ # git-colabor
2
+
3
+ **`git colabor`** — switch the Git **committer + pusher identity** and **SSH key** per repository, and manage git-mob-style **co-authors**, from one CLI.
4
+
5
+ This is the CLI half of Git Colabor. A VS Code extension ([`vscode-git-colabor`](https://github.com/hnrobert/vscode-git-colabor)) bundles this binary and drives it over its `--json` interface — but the CLI is fully standalone and scriptable.
6
+
7
+ ---
8
+
9
+ ## Why (requirements in one paragraph)
10
+
11
+ You share a machine — a lab workstation, a pair-programming box, a Remote-SSH server, a Codespace. Multiple people (or one person with work / personal / student accounts) commit and push from it. Git's `user.name` / `user.email` / `core.sshCommand` are global-or-manual, so identity bleed is one forgotten `git config` away: commits attributed to the wrong person, pushes with the wrong key, and no trace of *who set what, when*. Meanwhile, pairing sessions need `Co-authored-by:` trailers, which git has no workflow for at all. `git-colabor` treats identity as explicit, per-repo, auditable state — and makes co-author selection a one-liner.
12
+
13
+ ## Features
14
+
15
+ - **Identities** — named `{ name, email, SSH key }` profiles; `identity use` writes `user.name`, `user.email`, and `core.sshCommand` for the current repo, loads the key into `ssh-agent`, and snapshots prior config so `revert` can restore it.
16
+ - **Co-authors** — select people from a `.git-coauthors` catalogue (git-mob-compatible JSON); trailers are seeded into the commit template and stay in sync.
17
+ - **Safe key handling** — private keys are imported to a `0600` directory, passphrases never appear on `argv` / `ps` (SSH_ASKPASS bridge, optional `--passphrase-command`), `logout` shreds the key file.
18
+ - **Coordination** — advisory `heldBy` session locking warns when a second session (another terminal, another VS Code window) is about to override the repo identity; nothing is ever silently clobbered.
19
+ - **Audit trail** — every identity action is appended to a local JSONL audit log (fingerprint only — never key bodies or passphrases).
20
+ - **Machine-friendly** — every command supports `--json` with a stable envelope and documented exit codes.
21
+
22
+ ## Install
23
+
24
+ Requires **Node.js >= 24**.
25
+
26
+ ```bash
27
+ npm install -g git-colabor # once published (M6)
28
+ # or from source:
29
+ git clone git@github.com:hnrobert/git-colabor.git && cd git-colabor
30
+ pnpm install && pnpm build && npm link
31
+ ```
32
+
33
+ Git finds `git-colabor` on your `PATH`, so the command becomes **`git colabor …`**.
34
+
35
+ ## Quick start
36
+
37
+ ```bash
38
+ # 1. Create identities (work + personal, one with an SSH key)
39
+ git colabor identity add --name "Robert He" --email robert@work.io --default
40
+ git colabor identity add --name "Robert" --email me@personal.io \
41
+ --key ~/.ssh/id_ed25519_personal
42
+
43
+ # 2. Use one in the current repo
44
+ git colabor identity use id_a1b2c3d4 # from `identity ls`
45
+
46
+ # 3. Pairing: add a co-author and select them for this repo
47
+ git colabor coauthor add jd "Jamie Doe" jamie@example.com
48
+ git colabor coauthor use jd # trailers go into .gitmessage
49
+
50
+ git commit # Co-authored-by: trailer included
51
+
52
+ # 4. Done pairing / leaving the repo
53
+ git colabor coauthor solo
54
+ git colabor identity revert # restore pre-tool git config
55
+ ```
56
+
57
+ ## Command reference
58
+
59
+ ### `git colabor coauthor …`
60
+
61
+ | Command | Effect |
62
+ | --- | --- |
63
+ | `coauthor ls [filter]` | List catalogue entries (key / name / email); optional substring filter. |
64
+ | `coauthor use <key…>` | Select co-authors for the current repo (no args: print current selection). |
65
+ | `coauthor solo` | Clear the selection. |
66
+ | `coauthor print [-i]` | Print `Co-authored-by:` trailer blob (`-i`: comma-joined keys instead). |
67
+ | `coauthor add <key> "Name" <email>` | Add to `.git-coauthors`. |
68
+ | `coauthor suggest [filter]` | Suggest co-authors from `git shortlog` history; interactive pick without `--json`. |
69
+
70
+ Selecting co-authors rewrites the commit template (`commit.template` / `~/.gitmessage`): all existing `Co-authored-by:` trailers are stripped and the current selection re-appended — the same behavior as git-mob's message formatter.
71
+
72
+ ### `git colabor identity …`
73
+
74
+ | Command | Effect |
75
+ | --- | --- |
76
+ | `identity ls` | List identities; `*` marks the default; shows key fingerprint. |
77
+ | `identity use <id>` | Apply identity to the current repo (config + agent + state). Flags: `--source cli\|ext`, `--as-name`, `--as-email` (override committer fields), `--no-override` (refuse to take over a repo held by another session → exit 6). |
78
+ | `identity add` | `--name`, `--email` (required); `--key <path>` (import), `--passphrase-command <cmd>`, `--host`, `--default`. |
79
+ | `identity rm <id>` | Remove from the identity map (logs out first, best-effort). |
80
+ | `identity logout [id]` | Remove key from `ssh-agent` and **shred** the key file; repo stays configured (use `revert` for that). |
81
+ | `identity revert` | Restore the repo's pre-tool `user.*` / `core.sshCommand` / `commit.template` from the first-touch backup, clear markers. |
82
+ | `identity status` | Per-repo snapshot: active identity, managed markers, `heldBy`, selected + available co-authors. |
83
+ | `identity audit` | `--repo <path>`, `--since <date>`, `--tail <n>` filters over the audit log. |
84
+ | `identity doctor` | Self-check: binaries, identity map, key dir, agent, askpass bundle, repo markers. |
85
+
86
+ ### Global flags & exit codes
87
+
88
+ | Flag | Effect |
89
+ | --- | --- |
90
+ | `--json` | Machine-readable output (see below). |
91
+ | `-C <path>` | Run as if in `<path>`. |
92
+ | `--log-level <level>` | CLI debug log level (also env `GIT_COLABOR_LOG_LEVEL`). |
93
+ | `--no-color` | Accepted; output is color-free by default. |
94
+ | `-h` / `-v` | Help / version. |
95
+
96
+ | Exit code | Meaning |
97
+ | --- | --- |
98
+ | 0 | success |
99
+ | 1 | runtime / internal error |
100
+ | 2 | usage error, invalid email |
101
+ | 3 | not inside a git repository |
102
+ | 4 | secret unavailable (no passphrase could be obtained) |
103
+ | 5 | not found (author / identity), duplicate key |
104
+ | 6 | conflict blocked (`--no-override` with a live `heldBy`) |
105
+
106
+ ### JSON envelope
107
+
108
+ Every command accepts `--json` and prints one compact line:
109
+
110
+ ```json
111
+ {"ok":true,"data":{…},"warnings":[{"code":"…","message":"…"}]}
112
+ {"ok":false,"error":{"code":"NOT_FOUND","message":"…","hints":[],"exitCode":5},"data":null}
113
+ ```
114
+
115
+ This is the contract the VS Code extension consumes.
116
+
117
+ ## What it writes on disk
118
+
119
+ | Path | Purpose | Mode |
120
+ | --- | --- | --- |
121
+ | `~/.config/git-colabor/identities.json` | Identity map (env `GIT_COLABOR_MAP`) | `0600` |
122
+ | `~/.config/git-colabor/keys/<fingerprint>.key` | Imported private keys | `0600` (`icacls` on Windows) |
123
+ | `~/.config/git-colabor/audit.log` | JSONL audit trail | `0600` |
124
+ | `<git-dir>/colabor/state.json` | Per-repo state: active identity, `heldBy`, config backups | `0600` |
125
+ | `.git-coauthors` (repo, else `~/.git-coauthors`) | Co-author catalogue, git-mob-compatible JSON | — |
126
+ | `~/.gitmessage` (or `commit.template`) | Commit template seeded with trailers | — |
127
+
128
+ (Windows: the data dir is `%APPDATA%\git-colabor`.)
129
+
130
+ 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`), `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.
131
+
132
+ ## git-mob compatibility
133
+
134
+ - Same `.git-coauthors` **JSON** format (`{"coauthors": {"jd": {"name": …, "email": …}}}`) and same 3-tier resolution (env → repo → home).
135
+ - Same trailer format `Co-authored-by: NAME <EMAIL>` and template rewrite semantics; same shortlog-derived key generation (`rkrk` for "Richard Kotze" `<rkotze@…>`).
136
+ - Does **not** read or write git-mob's `git-mob.co-author` config — the two tools can coexist.
137
+
138
+ ## Security notes
139
+
140
+ - Private keys are **copied** into a `0600` keys dir (`icacls /inheritance:r` on Windows); the source file is left untouched. Encrypt-on-import is planned post-0.1.0; importing an unencrypted key raises a warning.
141
+ - Passphrases are resolved via, in order: the extension's askpass socket bridge → `passphrase-command` → interactive tty prompt. They never appear on `argv`, in `ps`, or in any log (redaction is unit- and e2e-tested).
142
+ - The audit log records fingerprints and session metadata only.
143
+
144
+ Full threat model: [docs/SECURITY.md](../docs/SECURITY.md) in the extension repo.
145
+
146
+ ## Environment variables
147
+
148
+ | Variable | Overrides |
149
+ | --- | --- |
150
+ | `GIT_COLABOR_MAP` | identity map path |
151
+ | `GIT_COLABOR_AUDIT_FILE` | audit log path |
152
+ | `GIT_COLABOR_COAUTHORS_PATH` | `.git-coauthors` path |
153
+ | `GIT_COLABOR_MESSAGE_PATH` | commit template path |
154
+ | `GIT_COLABOR_LOG_FILE` / `GIT_COLABOR_LOG_LEVEL` | CLI debug log |
155
+ | `GIT_COLABOR_ASKPASS_SOCK` / `GIT_COLABOR_ASKPASS_TOKEN` | askpass bridge endpoint (set by the extension) |
156
+
157
+ ## Library
158
+
159
+ `dist/index.cjs` exports the full core (26 modules): author store, co-author state, identity map/keys/agent/apply/revert/logout, repo state & coordination, git exec/config, message formatter, audit, paths, errors — everything except the CLI layer.
160
+
161
+ ```js
162
+ const { loadIdentityMap, applyIdentity, setSelected } = require('git-colabor');
163
+ ```
164
+
165
+ ## Development
166
+
167
+ ```bash
168
+ pnpm install
169
+ pnpm typecheck && pnpm lint
170
+ pnpm test # 40 unit tests
171
+ pnpm test:e2e # 15 e2e tests against real git + ssh-keygen in tmp dirs
172
+ pnpm build # tsup → dist/{cli,index,askpass}.cjs (node24, zero deps)
173
+ ```
174
+
175
+ Contributions follow the AIM standards — see [CONTRIBUTING.md](../CONTRIBUTING.md) in the extension repo.
176
+
177
+ ## License
178
+
179
+ MIT — see the [extension repo](https://github.com/hnrobert/vscode-git-colabor).
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+ // CJS on purpose: the package is "type": "module", so the bin stub must be .cjs
4
+ // for `require` to exist. It loads the bundled CLI (tsup -> dist/cli.cjs).
5
+ require('../dist/cli.cjs');
@@ -0,0 +1,71 @@
1
+ 'use strict';
2
+
3
+ var net = require('net');
4
+ var child_process = require('child_process');
5
+
6
+ // src/core/secrets/askpass-protocol.ts
7
+ var SOCKET_TIMEOUT_MS = 3e3;
8
+ function askSocket(socketPath, token, fingerprint) {
9
+ return new Promise((resolve) => {
10
+ let settled = false;
11
+ const done = (v) => {
12
+ if (!settled) {
13
+ settled = true;
14
+ resolve(v);
15
+ }
16
+ };
17
+ const sock = net.connect(socketPath, () => {
18
+ sock.write(JSON.stringify({ token, fingerprint }) + "\n");
19
+ });
20
+ let data = "";
21
+ sock.on("data", (d) => {
22
+ data += d.toString("utf8");
23
+ });
24
+ sock.on("close", () => done(data.replace(/\r?\n$/, "") || void 0));
25
+ sock.on("error", () => done(void 0));
26
+ setTimeout(() => {
27
+ sock.destroy();
28
+ done(void 0);
29
+ }, SOCKET_TIMEOUT_MS).unref?.();
30
+ });
31
+ }
32
+ function runPassphraseCommand(command) {
33
+ return new Promise((resolve) => {
34
+ const child = child_process.spawn(command, { shell: true, stdio: ["ignore", "pipe", "pipe"] });
35
+ let out = "";
36
+ child.stdout.on("data", (d) => {
37
+ out += d.toString("utf8");
38
+ });
39
+ child.on("error", () => resolve(void 0));
40
+ child.on("close", () => resolve(out.replace(/\r?\n$/, "") || void 0));
41
+ });
42
+ }
43
+
44
+ // src/askpass/index.ts
45
+ async function main() {
46
+ const socketPath = process.env.GIT_COLABOR_ASKPASS_SOCK;
47
+ const token = process.env.GIT_COLABOR_ASKPASS_TOKEN;
48
+ const fingerprint = process.env.GIT_COLABOR_FINGERPRINT;
49
+ const passphraseCommand = process.env.GIT_COLABOR_PASSPHRASE_COMMAND;
50
+ if (socketPath && token && fingerprint) {
51
+ const got = await askSocket(socketPath, token, fingerprint);
52
+ if (got) {
53
+ process.stdout.write(got);
54
+ process.exit(0);
55
+ }
56
+ }
57
+ if (passphraseCommand) {
58
+ const got = await runPassphraseCommand(passphraseCommand);
59
+ if (got) {
60
+ process.stdout.write(got);
61
+ process.exit(0);
62
+ }
63
+ }
64
+ process.stderr.write("git-colabor askpass: no passphrase available\n");
65
+ process.exit(1);
66
+ }
67
+ main().catch((e) => {
68
+ process.stderr.write(`git-colabor askpass error: ${e instanceof Error ? e.message : String(e)}
69
+ `);
70
+ process.exit(1);
71
+ });