repown 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/CHANGELOG.md +27 -0
- package/LICENSE +21 -0
- package/README.md +217 -0
- package/dist/cli.js +33 -0
- package/dist/commands/accounts.js +146 -0
- package/dist/commands/doctor.js +100 -0
- package/dist/commands/fix.js +90 -0
- package/dist/commands/guard.js +103 -0
- package/dist/commands/off.js +58 -0
- package/dist/commands/scan.js +224 -0
- package/dist/commands/status.js +168 -0
- package/dist/commands/use.js +164 -0
- package/dist/core/credential/gcm.js +83 -0
- package/dist/core/credential/gh.js +78 -0
- package/dist/core/credential/repair.js +84 -0
- package/dist/core/exec.js +82 -0
- package/dist/core/git.js +226 -0
- package/dist/core/guard/check.js +245 -0
- package/dist/core/guard/hook.js +184 -0
- package/dist/core/hosts/azdo.js +59 -0
- package/dist/core/hosts/generic.js +16 -0
- package/dist/core/hosts/github.js +35 -0
- package/dist/core/hosts/index.js +14 -0
- package/dist/core/hosts/types.js +9 -0
- package/dist/core/identity.js +96 -0
- package/dist/core/inspect.js +74 -0
- package/dist/core/registry.js +122 -0
- package/dist/core/result.js +13 -0
- package/dist/core/url.js +82 -0
- package/dist/ui/args.js +145 -0
- package/dist/ui/command.js +25 -0
- package/dist/ui/dispatch.js +142 -0
- package/dist/ui/format.js +84 -0
- package/dist/ui/help.js +97 -0
- package/dist/ui/prompt.js +43 -0
- package/dist/ui/suggest.js +33 -0
- package/package.json +59 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
Every release of repown, newest first. The format follows
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
|
|
5
|
+
[Semantic Versioning](https://semver.org/). Before 1.0, a minor version may change
|
|
6
|
+
commands. How a release is cut: [docs/RELEASING.md](docs/RELEASING.md).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0] - 2026-09-25
|
|
11
|
+
|
|
12
|
+
- First release on npm: `npm install -g repown`.
|
|
13
|
+
- `repown use <account>` pins a clone to one account: repo-local `user.name`,
|
|
14
|
+
`user.email`, `repown.account`, `user.useConfigOnly`, and the credential username
|
|
15
|
+
where it was measured (GitHub; not Azure DevOps).
|
|
16
|
+
- `repown guard on | off | status` manages a `pre-push` hook that refuses commits
|
|
17
|
+
authored or committed by anyone else, pushes to a foreign destination owner, and
|
|
18
|
+
pushes with identity-overriding environment variables set.
|
|
19
|
+
- `repown status`, `repown doctor` and `repown fix` show and repair what serves
|
|
20
|
+
credentials on the machine.
|
|
21
|
+
- `repown accounts list | add | remove` keeps the per-machine account registry.
|
|
22
|
+
- `repown scan` audits every clone under a directory; `--format json` is a stable
|
|
23
|
+
contract for scripts.
|
|
24
|
+
- Runs on Node 20+ on Windows, macOS and Linux, with zero runtime dependencies.
|
|
25
|
+
|
|
26
|
+
[Unreleased]: https://github.com/makubexD/repown/compare/v0.1.0...HEAD
|
|
27
|
+
[0.1.0]: https://github.com/makubexD/repown/releases/tag/v0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 makubexD
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# repown
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/repown)
|
|
4
|
+
[](https://github.com/makubexD/repown/actions/workflows/ci.yml)
|
|
5
|
+
[](https://github.com/makubexD/repown/actions/workflows/release.yml)
|
|
6
|
+
[](package.json)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
|
|
9
|
+
**Use several git accounts on one machine, and move between their repositories
|
|
10
|
+
with no switch command.** Each repo owns its identity (that's the name: *repo
|
|
11
|
+
own*), so you can go from a work repo to a personal one to a client one, on
|
|
12
|
+
different hosts or sign-in methods, and every commit and push goes out as the
|
|
13
|
+
right account. A push carrying the wrong identity is refused before it leaves.
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
repown doctor # once per machine: is git's credential helper ready?
|
|
17
|
+
repown use octocat # once per clone: this clone is octocat's
|
|
18
|
+
repown guard on # refuse any push that carries another identity
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
> **Status:** pre-1.0: the commands may still change. What changed in each version is in
|
|
22
|
+
> [CHANGELOG.md](CHANGELOG.md).
|
|
23
|
+
|
|
24
|
+
## The problem
|
|
25
|
+
|
|
26
|
+
- **Every clone inherits the identity in your global git config.** A commit's author
|
|
27
|
+
address becomes permanent once it's pushed to a public repository.
|
|
28
|
+
- **`gh auth switch` changes the account for the whole machine.** While `gh` is git's
|
|
29
|
+
credential helper, every switch breaks the *other* account's repositories. An
|
|
30
|
+
SSO-only account has no password to type at the prompt
|
|
31
|
+
([ADR-001](docs/decisions/ADR-001-credential-manager-not-gh.md)).
|
|
32
|
+
|
|
33
|
+
## How repown solves it
|
|
34
|
+
|
|
35
|
+
- **Pin each clone:** `repown use <account>` writes the commit identity and the push
|
|
36
|
+
account into that clone's `.git/config`. It is set once and never switched.
|
|
37
|
+
- **One credential per account:** Git Credential Manager already stores one
|
|
38
|
+
credential per account and picks it per clone.
|
|
39
|
+
- `repown doctor` checks that it is the helper.
|
|
40
|
+
- `repown fix` hands the host back to it if `gh auth setup-git` took over.
|
|
41
|
+
- `gh` stays your account store for `gh pr create` and `gh api`.
|
|
42
|
+
|
|
43
|
+
Credential pinning is GitHub-over-https only today. Commit identity and the guard
|
|
44
|
+
work on every host ([ADR-009](docs/decisions/ADR-009-hosts-claim-only-measured.md)).
|
|
45
|
+
- **Guard every push:** `repown guard on` installs a `pre-push` hook that checks the
|
|
46
|
+
commits themselves, not just today's config.
|
|
47
|
+
|
|
48
|
+
**Every scenario, with diagrams: [docs/HOW-IT-WORKS.md](docs/HOW-IT-WORKS.md).**
|
|
49
|
+
|
|
50
|
+
## Compared with the alternatives
|
|
51
|
+
|
|
52
|
+
| Approach | Right commit identity | Right push credential | Checked before push | Catch |
|
|
53
|
+
| --- | --- | --- | --- | --- |
|
|
54
|
+
| `git config user.email` by hand in each repo | yes, if you never forget | no | no | Forgetting once publishes the wrong address permanently |
|
|
55
|
+
| `includeIf "gitdir:~/work/"` in global config | yes, by folder | only if you also add the credential key per folder | no | Depends on where a repo happens to be cloned; a clone anywhere else inherits the default |
|
|
56
|
+
| SSH host aliases (`git@github-work:...`) | no | yes | no | Every remote URL has to be rewritten, and keys managed per account |
|
|
57
|
+
| `gh auth switch` | no | while gh is the helper, only the *active* account | no | Machine-wide: it breaks the other account's repos |
|
|
58
|
+
| **repown** | yes, per clone | yes, per clone (GitHub) | yes, once `repown guard on`: every commit the push would publish | `use` and `guard on` once per clone |
|
|
59
|
+
|
|
60
|
+
repown doesn't replace these tools. It writes plain repo-local git config, uses the
|
|
61
|
+
credential manager you already have, and leaves `gh` in charge of the GitHub CLI.
|
|
62
|
+
|
|
63
|
+
## Install
|
|
64
|
+
|
|
65
|
+
Needs Node 20+ and git, on Windows, macOS or Linux.
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
npm install -g repown
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
To try it without installing, run `npx repown doctor`. The package has no runtime
|
|
72
|
+
dependencies, and each version is published from CI with
|
|
73
|
+
[provenance](https://docs.npmjs.com/generating-provenance-statements), so npm shows the
|
|
74
|
+
commit and workflow that built it. Update with `npm install -g repown@latest`; a guarded
|
|
75
|
+
clone keeps working, because its hook calls the installed CLI
|
|
76
|
+
([ADR-003](docs/decisions/ADR-003-hook-calls-installed-cli.md)).
|
|
77
|
+
|
|
78
|
+
To uninstall, run `repown guard off` in each guarded clone **first**: a guard that can't
|
|
79
|
+
find repown refuses every push
|
|
80
|
+
([the steps](docs/HOW-IT-WORKS.md#12-uninstall-or-repown-missing)).
|
|
81
|
+
|
|
82
|
+
## Quickstart
|
|
83
|
+
|
|
84
|
+
**Once per machine:** `repown doctor`, then `repown fix` only if doctor says so. `fix`
|
|
85
|
+
shows what it removes, and the undo, before changing anything.
|
|
86
|
+
|
|
87
|
+
**Once per clone:**
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
$ repown use octocat
|
|
91
|
+
OK identity Octo Cat <octocat@users.noreply.github.com> push-as:octocat
|
|
92
|
+
|
|
93
|
+
No stored credential for "octocat" yet -- the first push signs in
|
|
94
|
+
once, then never again. Verify it afterwards: repown doctor
|
|
95
|
+
|
|
96
|
+
Next: repown guard on (check every push before it leaves)
|
|
97
|
+
|
|
98
|
+
$ repown guard on
|
|
99
|
+
OK guard on -- every push is checked before it leaves
|
|
100
|
+
/path/to/clone/.git/hooks/pre-push
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
The first time, `repown use` asks for the account's commit name and email and records
|
|
104
|
+
them for every other clone. Without a terminal it can't ask, so record the account
|
|
105
|
+
first: `repown accounts add octocat --name "Octo Cat" --email octocat@users.noreply.github.com`.
|
|
106
|
+
|
|
107
|
+
It writes these repo-local keys and nothing else:
|
|
108
|
+
|
|
109
|
+
| Key | What it decides |
|
|
110
|
+
| --- | --- |
|
|
111
|
+
| `user.name`, `user.email` | who **authored** the commit |
|
|
112
|
+
| `repown.account` | whose clone this is; the guard checks the destination against it |
|
|
113
|
+
| `credential.<host>.username` | which stored credential serves the **push** (GitHub over https only) |
|
|
114
|
+
| `user.useConfigOnly` | git refuses to invent an identity from the hostname |
|
|
115
|
+
|
|
116
|
+
`.git/config` and `.git/hooks` are never pushed. No name or address reaches the
|
|
117
|
+
repository, and teammates see nothing.
|
|
118
|
+
|
|
119
|
+
## Day to day
|
|
120
|
+
|
|
121
|
+
**Moving between accounts needs no command.** `cd` into any pinned clone, then commit
|
|
122
|
+
and push. To see who a clone is, run `repown`:
|
|
123
|
+
|
|
124
|
+
```
|
|
125
|
+
$ repown
|
|
126
|
+
|
|
127
|
+
commits as Octo Cat <octocat@users.noreply.github.com>
|
|
128
|
+
pushes as octocat
|
|
129
|
+
origin octocat (GitHub)
|
|
130
|
+
helper manager
|
|
131
|
+
gh active octo-work
|
|
132
|
+
push guard on
|
|
133
|
+
|
|
134
|
+
WARN gh active as "octo-work", so `gh pr create` here would act as that account.
|
|
135
|
+
fix: gh auth switch -u octocat
|
|
136
|
+
OK identity this clone is pinned, and its credential mechanism honours it
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
It prints all three things that decide who you are: the commit identity, the push
|
|
140
|
+
credential, and gh's active account. The one you can't see is the one that catches
|
|
141
|
+
you out.
|
|
142
|
+
|
|
143
|
+
**Audit every clone you have:** `repown scan ~/code ~/work` lists each clone's owner,
|
|
144
|
+
host, identity and guard. It also shows which email domains appear in its history, and
|
|
145
|
+
how often. It never changes anything.
|
|
146
|
+
|
|
147
|
+
**Something went wrong?**
|
|
148
|
+
|
|
149
|
+
| What happened | Where to look |
|
|
150
|
+
| --- | --- |
|
|
151
|
+
| A push asked for a password | [card 1](docs/HOW-IT-WORKS.md#1-set-up-the-machine), then [card 6](docs/HOW-IT-WORKS.md#6-commit-and-first-push) |
|
|
152
|
+
| A push failed after signing in (SSO) | [card 6](docs/HOW-IT-WORKS.md#6-commit-and-first-push) |
|
|
153
|
+
| The guard refused a push | [card 8](docs/HOW-IT-WORKS.md#8-push-refused-and-the-fix) |
|
|
154
|
+
| A teammate doesn't use repown | [card 9](docs/HOW-IT-WORKS.md#9-a-teammate-without-repown) |
|
|
155
|
+
| husky or another hook tool | [card 10](docs/HOW-IT-WORKS.md#10-other-hook-tools) |
|
|
156
|
+
|
|
157
|
+
## Commands
|
|
158
|
+
|
|
159
|
+
| Command | What it does |
|
|
160
|
+
| --- | --- |
|
|
161
|
+
| `repown status` (or just `repown`) | the state of this repository and this machine |
|
|
162
|
+
| `repown use <account> [--gh]` | pin this clone to an account (`--name` and `--email` together skip the registry) |
|
|
163
|
+
| `repown off` | unpin this clone, leaving global config alone |
|
|
164
|
+
| `repown doctor` | what serves credentials on this machine, and to whom |
|
|
165
|
+
| `repown fix [--dry-run] [--yes]` | undo `gh auth setup-git` so per-clone pins work again |
|
|
166
|
+
| `repown guard on \| off \| status` | install, remove or show the pre-push hook |
|
|
167
|
+
| `repown accounts list \| add \| remove` | the accounts this machine knows (`add` takes `--name`, `--email`, `--host github\|azdo\|generic`) |
|
|
168
|
+
| `repown scan [dir...] [--emails] [--depth <n>] [--format json]` | audit every clone under the given directories (default: the current one) |
|
|
169
|
+
|
|
170
|
+
- **Help:** `repown --help` lists the commands. `repown help <command>` (or
|
|
171
|
+
`repown <command> --help`) shows a command's options, and `repown help guard on` goes
|
|
172
|
+
one level deeper. Asking for help never changes anything.
|
|
173
|
+
- **Every command:** `--cwd <dir>` works on all of them.
|
|
174
|
+
- **Scripts:** `repown scan --format json` and `repown accounts list --format json` print
|
|
175
|
+
one JSON document on stdout. Those fields are stable; the text layout isn't
|
|
176
|
+
([ADR-014](docs/decisions/ADR-014-json-for-scripts.md)).
|
|
177
|
+
- **Version:** `repown --version`.
|
|
178
|
+
- **Exit codes:** `0` success, `1` failure or refusal, `2` usage error.
|
|
179
|
+
|
|
180
|
+
## Development
|
|
181
|
+
|
|
182
|
+
From a clone (`npm link` puts your working copy on PATH as `repown`):
|
|
183
|
+
|
|
184
|
+
```
|
|
185
|
+
git clone https://github.com/makubexD/repown.git
|
|
186
|
+
cd repown && npm install && npm run build && npm link
|
|
187
|
+
npm test # node's own test runner, no framework
|
|
188
|
+
npm run build # tsc, also the typecheck
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Cutting a version (maintainers):
|
|
192
|
+
|
|
193
|
+
```
|
|
194
|
+
npm run changelog # draft CHANGELOG.md's Unreleased section from git history, then edit it
|
|
195
|
+
npm run release:patch # or release:minor | release:major | release:beta
|
|
196
|
+
git push --follow-tags # CI publishes to npm (trusted publishing) and creates the GitHub Release
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
`release:*` runs `npm version`, which refuses unless the repository is ready
|
|
200
|
+
(`npm run release:check`), the tests and build pass, and the tarball holds only what
|
|
201
|
+
it should (`npm run pack:check`). It then dates the changelog, commits and tags
|
|
202
|
+
`v<version>` locally. The whole routine is in [docs/RELEASING.md](docs/RELEASING.md);
|
|
203
|
+
what changed in each version is in [CHANGELOG.md](CHANGELOG.md).
|
|
204
|
+
|
|
205
|
+
- **Node:** developing needs Node 22.18+, though running needs only 20
|
|
206
|
+
([ADR-010](docs/decisions/ADR-010-typescript-on-node.md)).
|
|
207
|
+
- **Dependencies:** zero at runtime, by choice.
|
|
208
|
+
- **Demo:** `vhs demo/demo.tape` runs a [VHS](https://github.com/charmbracelet/vhs)
|
|
209
|
+
script with placeholder identities in a throwaway sandbox. No GIF is committed yet,
|
|
210
|
+
because VHS hasn't rendered on the machines tried so far.
|
|
211
|
+
- **Further reading:**
|
|
212
|
+
- [CLAUDE.md](CLAUDE.md) has the contributor rules.
|
|
213
|
+
- [docs/decisions/](docs/decisions/README.md) explains every non-obvious choice.
|
|
214
|
+
|
|
215
|
+
## License
|
|
216
|
+
|
|
217
|
+
[MIT](LICENSE)
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// repown -- pin a git clone to one account, and refuse to push commits that carry
|
|
3
|
+
// another identity.
|
|
4
|
+
//
|
|
5
|
+
// The grammar is short on purpose. `repown use <account>` has to be as quick to
|
|
6
|
+
// type as `gh auth switch`, or it will not be typed. Everything longer than a
|
|
7
|
+
// word is a command you run once per clone or once per machine.
|
|
8
|
+
//
|
|
9
|
+
// This file only DECLARES the program: its commands, its version and its top
|
|
10
|
+
// help. Dispatch itself -- resolving a command, intercepting `--help`/`-h`/`help`
|
|
11
|
+
// before a command's own `run()` ever sees the arguments, and exiting 2 on a
|
|
12
|
+
// usage error -- lives in ui/dispatch.ts. Parsing the arguments lives in
|
|
13
|
+
// ui/args.ts, which a command can import without pulling in this file --
|
|
14
|
+
// importing this module runs repown.
|
|
15
|
+
import { readFileSync } from 'node:fs';
|
|
16
|
+
import { fileURLToPath } from 'node:url';
|
|
17
|
+
import { renderTopHelp } from "./ui/help.js";
|
|
18
|
+
import { start } from "./ui/dispatch.js";
|
|
19
|
+
const COMMANDS = {
|
|
20
|
+
status: async () => (await import("./commands/status.js")).default,
|
|
21
|
+
use: async () => (await import("./commands/use.js")).default,
|
|
22
|
+
off: async () => (await import("./commands/off.js")).default,
|
|
23
|
+
doctor: async () => (await import("./commands/doctor.js")).default,
|
|
24
|
+
fix: async () => (await import("./commands/fix.js")).default,
|
|
25
|
+
guard: async () => (await import("./commands/guard.js")).default,
|
|
26
|
+
accounts: async () => (await import("./commands/accounts.js")).default,
|
|
27
|
+
scan: async () => (await import("./commands/scan.js")).default,
|
|
28
|
+
};
|
|
29
|
+
function version() {
|
|
30
|
+
const path = fileURLToPath(new URL('../package.json', import.meta.url));
|
|
31
|
+
return JSON.parse(readFileSync(path, 'utf8')).version;
|
|
32
|
+
}
|
|
33
|
+
start({ name: 'repown', commands: COMMANDS, defaultCommand: 'status', topHelp: renderTopHelp, version });
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// The per-machine account registry.
|
|
2
|
+
//
|
|
3
|
+
// Recording an account once is what makes `repown use <account>` a one-word command
|
|
4
|
+
// afterwards, in every repository on the machine.
|
|
5
|
+
import { loadRegistry, saveAccount, removeAccount, registryPath } from "../core/registry.js";
|
|
6
|
+
import { providers } from "../core/hosts/index.js";
|
|
7
|
+
import { ask, interactive } from "../ui/prompt.js";
|
|
8
|
+
import { flagString, wantsJson, FORMAT_OPTION } from "../ui/args.js";
|
|
9
|
+
import * as out from "../ui/format.js";
|
|
10
|
+
async function list(args) {
|
|
11
|
+
const loaded = await loadRegistry();
|
|
12
|
+
if (!loaded.ok) {
|
|
13
|
+
out.fail('accounts', loaded.error);
|
|
14
|
+
return 1;
|
|
15
|
+
}
|
|
16
|
+
const names = Object.keys(loaded.value.accounts).sort();
|
|
17
|
+
if (wantsJson(args))
|
|
18
|
+
return listJson(names, loaded.value.accounts, loaded.value.unreadable);
|
|
19
|
+
out.line();
|
|
20
|
+
if (names.length === 0) {
|
|
21
|
+
printNoneYet();
|
|
22
|
+
return 0;
|
|
23
|
+
}
|
|
24
|
+
for (const name of names) {
|
|
25
|
+
const entry = loaded.value.accounts[name];
|
|
26
|
+
out.field(name, entry.name + ' <' + entry.email + '>' + hostSuffix(entry), 24);
|
|
27
|
+
}
|
|
28
|
+
for (const key of loaded.value.unreadable) {
|
|
29
|
+
out.warn('accounts', key + ': an entry repown cannot read; it is kept, and blocks saving until fixed.');
|
|
30
|
+
}
|
|
31
|
+
out.line();
|
|
32
|
+
out.line(out.dim(' ' + registryPath()));
|
|
33
|
+
out.line();
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
/** One object per account, `[]` when none; an unreadable entry is still a warning on stderr. */
|
|
37
|
+
function listJson(names, accounts, unreadable) {
|
|
38
|
+
out.json(names.map((account) => {
|
|
39
|
+
const entry = accounts[account];
|
|
40
|
+
return { account, name: entry.name, email: entry.email, host: entry.host ?? 'github' };
|
|
41
|
+
}));
|
|
42
|
+
for (const key of unreadable)
|
|
43
|
+
out.warn('accounts', key + ': an entry repown cannot read; it is kept, and blocks saving until fixed.');
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
function printNoneYet() {
|
|
47
|
+
out.line(' No accounts recorded yet.');
|
|
48
|
+
out.line();
|
|
49
|
+
out.line(' Record one so `repown use <account>` never has to ask again:');
|
|
50
|
+
out.line(' repown accounts add <account>');
|
|
51
|
+
out.line();
|
|
52
|
+
}
|
|
53
|
+
function hostSuffix(entry) {
|
|
54
|
+
return entry.host && entry.host !== 'github' ? ' (' + entry.host + ')' : '';
|
|
55
|
+
}
|
|
56
|
+
async function add(args) {
|
|
57
|
+
const account = args.positional[0];
|
|
58
|
+
const hostId = flagString(args, 'host') ?? 'github';
|
|
59
|
+
// Only when something is left to ask, and someone is there to answer: the
|
|
60
|
+
// suggestion is a network call.
|
|
61
|
+
const complete = flagString(args, 'name') !== null && flagString(args, 'email') !== null;
|
|
62
|
+
const suggested = complete || !interactive() ? {} : await suggestProfile(hostId, account);
|
|
63
|
+
const name = flagString(args, 'name') ?? await askFor('Commit name', suggested.name ?? account);
|
|
64
|
+
if (name === null)
|
|
65
|
+
return 1;
|
|
66
|
+
const email = flagString(args, 'email') ?? await askFor('Commit email', suggested.email);
|
|
67
|
+
if (email === null)
|
|
68
|
+
return 1;
|
|
69
|
+
const written = await saveAccount(account, { name, email, host: hostId });
|
|
70
|
+
if (!written.ok) {
|
|
71
|
+
out.fail('accounts', written.error);
|
|
72
|
+
return 1;
|
|
73
|
+
}
|
|
74
|
+
out.pass('accounts', account + ' ' + name + ' <' + email + '>');
|
|
75
|
+
out.line();
|
|
76
|
+
out.line(' Use it in any clone: repown use ' + account);
|
|
77
|
+
out.line();
|
|
78
|
+
return 0;
|
|
79
|
+
}
|
|
80
|
+
/** Best effort. A host that cannot be asked simply contributes no suggestion. */
|
|
81
|
+
async function suggestProfile(hostId, account) {
|
|
82
|
+
const provider = providers().find((candidate) => candidate.id === hostId);
|
|
83
|
+
if (!provider?.resolveProfile)
|
|
84
|
+
return {};
|
|
85
|
+
return (await provider.resolveProfile(account)) ?? {};
|
|
86
|
+
}
|
|
87
|
+
async function askFor(label, suggestion) {
|
|
88
|
+
const answer = await ask(label, suggestion);
|
|
89
|
+
if (answer.ok)
|
|
90
|
+
return answer.value;
|
|
91
|
+
out.fail('accounts', answer.error);
|
|
92
|
+
out.detail('pass it directly: repown accounts add <account> --name "..." --email "..."');
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
async function remove(args) {
|
|
96
|
+
const account = args.positional[0];
|
|
97
|
+
const removed = await removeAccount(account);
|
|
98
|
+
if (!removed.ok) {
|
|
99
|
+
out.fail('accounts', removed.error);
|
|
100
|
+
return 1;
|
|
101
|
+
}
|
|
102
|
+
if (!removed.value) {
|
|
103
|
+
out.warn('accounts', account + ' was not recorded.');
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
|
106
|
+
out.pass('accounts', 'removed ' + account);
|
|
107
|
+
out.line();
|
|
108
|
+
out.line(' Clones already pinned to it are unchanged -- their identity is in');
|
|
109
|
+
out.line(' their own .git/config. Unpin one with: repown off');
|
|
110
|
+
out.line();
|
|
111
|
+
return 0;
|
|
112
|
+
}
|
|
113
|
+
const listAction = {
|
|
114
|
+
summary: 'list the accounts this machine knows',
|
|
115
|
+
options: [FORMAT_OPTION],
|
|
116
|
+
examples: ['repown accounts list', 'repown accounts list --format json'],
|
|
117
|
+
run: list,
|
|
118
|
+
};
|
|
119
|
+
const addAction = {
|
|
120
|
+
summary: 'record an account (asks for name/email once, then remembers)',
|
|
121
|
+
positionals: { min: 1, max: 1, label: '<account>' },
|
|
122
|
+
options: [
|
|
123
|
+
{ name: 'name', kind: 'string', help: 'the commit author name' },
|
|
124
|
+
{ name: 'email', kind: 'string', help: 'the commit author email' },
|
|
125
|
+
{
|
|
126
|
+
name: 'host', kind: 'string', default: 'github',
|
|
127
|
+
choices: providers().map((provider) => provider.id),
|
|
128
|
+
help: 'which host this account belongs to',
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
examples: ['repown accounts add octocat'],
|
|
132
|
+
run: add,
|
|
133
|
+
};
|
|
134
|
+
const removeAction = {
|
|
135
|
+
summary: 'forget a recorded account (clones already pinned to it are unaffected)',
|
|
136
|
+
positionals: { min: 1, max: 1, label: '<account>' },
|
|
137
|
+
examples: ['repown accounts remove octocat'],
|
|
138
|
+
run: remove,
|
|
139
|
+
};
|
|
140
|
+
export default {
|
|
141
|
+
summary: 'list or record the accounts this machine knows',
|
|
142
|
+
defaultAction: 'list',
|
|
143
|
+
actions: { list: listAction, add: addAction, remove: removeAction },
|
|
144
|
+
// `rm` was the advertised name first; it keeps working, unadvertised.
|
|
145
|
+
aliases: { rm: 'remove' },
|
|
146
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// What actually supplies a credential on this machine.
|
|
2
|
+
//
|
|
3
|
+
// This is the answer to "why am I being asked for a password". That failure
|
|
4
|
+
// otherwise looks identical to a bad token:
|
|
5
|
+
//
|
|
6
|
+
// remote: Invalid username or token. Password authentication is not supported
|
|
7
|
+
// fatal: Authentication failed for 'https://github.com/...'
|
|
8
|
+
//
|
|
9
|
+
// which appears when the GitHub CLI is the credential helper and its active
|
|
10
|
+
// account is not the one the repository asked for. gh serves only its active
|
|
11
|
+
// account and returns nothing for any other, so the fix is not a new token -- it
|
|
12
|
+
// is to stop gh being the helper.
|
|
13
|
+
import { inspectRepo, inspectAuth, activeAccountLabel, storedAccountsLabel } from "../core/inspect.js";
|
|
14
|
+
import { gitFor } from "../ui/args.js";
|
|
15
|
+
import * as out from "../ui/format.js";
|
|
16
|
+
export default {
|
|
17
|
+
summary: 'what serves credentials on this machine, and to whom',
|
|
18
|
+
examples: ['repown doctor'],
|
|
19
|
+
async run(args) {
|
|
20
|
+
const git = gitFor(args);
|
|
21
|
+
const repo = await inspectRepo(git);
|
|
22
|
+
const auth = await inspectAuth(git, repo.originUrl ?? undefined);
|
|
23
|
+
out.line();
|
|
24
|
+
out.field('helper', auth.helper ?? 'none configured', 18);
|
|
25
|
+
out.field('stored accounts', storedAccountsLabel(auth), 18);
|
|
26
|
+
out.field('gh accounts', ghAccountsLabel(auth), 18);
|
|
27
|
+
out.field('gh active', activeAccountLabel(auth), 18);
|
|
28
|
+
out.field('GCM', auth.gcmPath ?? 'not found', 18);
|
|
29
|
+
out.line();
|
|
30
|
+
if (auth.ghIsHelper)
|
|
31
|
+
return diagnoseGhHelper(auth);
|
|
32
|
+
if (!auth.helperIsGcm)
|
|
33
|
+
return diagnoseUnknownHelper(auth);
|
|
34
|
+
return diagnoseHealthy(auth, repo.provider.label);
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
function ghAccountsLabel(auth) {
|
|
38
|
+
if (!auth.ghPresent)
|
|
39
|
+
return 'not installed';
|
|
40
|
+
if (!auth.gh.ok)
|
|
41
|
+
return 'unknown -- gh could not be queried';
|
|
42
|
+
const logins = auth.gh.value.accounts.map((account) => account.login);
|
|
43
|
+
return logins.length > 0 ? logins.join(', ') : 'none';
|
|
44
|
+
}
|
|
45
|
+
function diagnoseGhHelper(auth) {
|
|
46
|
+
out.fail('helper', 'gh is the credential helper for github.com.');
|
|
47
|
+
out.detail('It serves ONLY its active account and returns nothing for any');
|
|
48
|
+
out.detail('other, so every repository pinned to a different account is');
|
|
49
|
+
out.detail('prompted for a password -- and `gh auth switch` moves the problem');
|
|
50
|
+
out.detail('rather than fixing it.');
|
|
51
|
+
// On its own line, and only when known: interpolating it inline printed an
|
|
52
|
+
// empty "()" whenever gh could not be queried, which reads as a bug.
|
|
53
|
+
if (auth.gh.ok && auth.gh.value.active)
|
|
54
|
+
out.detail('Active right now: ' + auth.gh.value.active);
|
|
55
|
+
const files = [...new Set(auth.ghHelperOrigins.map((entry) => entry.file))];
|
|
56
|
+
if (files.length > 0) {
|
|
57
|
+
out.detail('');
|
|
58
|
+
out.detail('Installed by `gh auth setup-git`, in:');
|
|
59
|
+
for (const file of files)
|
|
60
|
+
out.detail(' ' + file);
|
|
61
|
+
}
|
|
62
|
+
out.line();
|
|
63
|
+
out.line(' fix: repown fix');
|
|
64
|
+
out.line();
|
|
65
|
+
return 1;
|
|
66
|
+
}
|
|
67
|
+
function diagnoseUnknownHelper(auth) {
|
|
68
|
+
out.warn('helper', 'github.com is served by "' + (auth.helper ?? 'nothing') +
|
|
69
|
+
'", which repown has no opinion about.');
|
|
70
|
+
out.detail('The per-repository pin (credential.<url>.username) only works if');
|
|
71
|
+
out.detail('that helper honours it.');
|
|
72
|
+
out.line();
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
function diagnoseHealthy(auth, providerLabel) {
|
|
76
|
+
out.line(' Credentials come from Git Credential Manager, which stores one per');
|
|
77
|
+
out.line(' account and picks per repository from credential.<url>.username. No');
|
|
78
|
+
out.line(' switching is needed for git, and `gh auth switch` affects the CLI only.');
|
|
79
|
+
out.line();
|
|
80
|
+
if (auth.stored.ok && auth.stored.value.length === 0) {
|
|
81
|
+
out.warn('store', 'no accounts stored yet -- the first push will sign in once.');
|
|
82
|
+
out.line();
|
|
83
|
+
}
|
|
84
|
+
ssoNote(providerLabel);
|
|
85
|
+
return 0;
|
|
86
|
+
}
|
|
87
|
+
// Not a check -- repown cannot see this coming (ADR-013, "Deliberately
|
|
88
|
+
// not done"). A credential that is otherwise healthy still fails the moment
|
|
89
|
+
// it touches an org it is not SSO-authorized for, and that failure looks
|
|
90
|
+
// identical to a bad token. Printed unconditionally so it is there before it
|
|
91
|
+
// is needed. The exact fix command is gh-specific, so it only names `gh` for
|
|
92
|
+
// a GitHub origin -- gh does not manage auth for any other host.
|
|
93
|
+
function ssoNote(providerLabel) {
|
|
94
|
+
out.line(' If a push or fetch still fails right after this, the credential may');
|
|
95
|
+
out.line(' be valid but not yet SSO-authorized for that organisation. Re-authorize it:');
|
|
96
|
+
out.line(providerLabel === 'GitHub'
|
|
97
|
+
? ' gh auth refresh -h <host>, or via the org\'s SSO settings.'
|
|
98
|
+
: " check your git host's SSO / conditional-access settings.");
|
|
99
|
+
out.line();
|
|
100
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Hand credential serving back to whatever was configured before
|
|
2
|
+
// `gh auth setup-git` took it over.
|
|
3
|
+
//
|
|
4
|
+
// It edits local, global and possibly system config -- only gh's own helper
|
|
5
|
+
// values and the reset gh wrote with them -- so it shows exactly what it will
|
|
6
|
+
// remove, and the undo, BEFORE asking. A change to machine-wide config should be
|
|
7
|
+
// reviewable rather than magic.
|
|
8
|
+
import { inspectAuth, storedAccountsLabel } from "../core/inspect.js";
|
|
9
|
+
import { planRepair, repair, describeValue } from "../core/credential/repair.js";
|
|
10
|
+
import { confirm, interactive } from "../ui/prompt.js";
|
|
11
|
+
import { flagBool, gitFor } from "../ui/args.js";
|
|
12
|
+
import * as out from "../ui/format.js";
|
|
13
|
+
export default {
|
|
14
|
+
summary: 'undo `gh auth setup-git` so per-repo account pins work again',
|
|
15
|
+
options: [
|
|
16
|
+
{ name: 'yes', kind: 'boolean', help: 'remove the entries without asking to confirm' },
|
|
17
|
+
{ name: 'dry-run', kind: 'boolean', help: 'show what would be removed and stop there' },
|
|
18
|
+
],
|
|
19
|
+
examples: ['repown fix', 'repown fix --dry-run'],
|
|
20
|
+
async run(args) {
|
|
21
|
+
const git = gitFor(args);
|
|
22
|
+
const planned = await planRepair(git);
|
|
23
|
+
if (planned.length === 0) {
|
|
24
|
+
out.pass('fix', 'nothing to undo -- gh is not installed as a credential helper.');
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
preview(planned);
|
|
28
|
+
if (flagBool(args, 'dry-run')) {
|
|
29
|
+
out.line(' (dry run -- nothing was changed)');
|
|
30
|
+
out.line();
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
if (!await confirmed(args))
|
|
34
|
+
return 1;
|
|
35
|
+
return apply(await repair(git), git);
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
async function confirmed(args) {
|
|
39
|
+
if (flagBool(args, 'yes'))
|
|
40
|
+
return true;
|
|
41
|
+
if (!interactive()) {
|
|
42
|
+
out.fail('fix', 'this changes global config, so it needs confirmation.');
|
|
43
|
+
out.detail('re-run with --yes to proceed without being asked.');
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
if (await confirm('Remove these entries?'))
|
|
47
|
+
return true;
|
|
48
|
+
out.warn('fix', 'declined; nothing was changed.');
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
function preview(planned) {
|
|
52
|
+
out.line();
|
|
53
|
+
out.line(' These entries make gh the credential helper, and will be removed:');
|
|
54
|
+
out.line();
|
|
55
|
+
for (const entry of planned) {
|
|
56
|
+
out.line(' ' + entry.scope + ': ' + entry.key);
|
|
57
|
+
for (const value of entry.values)
|
|
58
|
+
out.line(' = ' + describeValue(value));
|
|
59
|
+
}
|
|
60
|
+
out.line();
|
|
61
|
+
out.line(' Nothing else in your config is touched.');
|
|
62
|
+
out.line(' Undo at any time: gh auth setup-git');
|
|
63
|
+
out.line();
|
|
64
|
+
}
|
|
65
|
+
async function apply(results, git) {
|
|
66
|
+
const failed = reportRemovals(results);
|
|
67
|
+
out.line();
|
|
68
|
+
const auth = await inspectAuth(git);
|
|
69
|
+
out.field('helper now', auth.helper ?? 'none', 16);
|
|
70
|
+
out.field('accounts', storedAccountsLabel(auth), 16);
|
|
71
|
+
out.line();
|
|
72
|
+
if (failed > 0)
|
|
73
|
+
return 1;
|
|
74
|
+
out.line(' Each clone now authenticates as its own pinned account. Check one: repown');
|
|
75
|
+
out.line();
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
/** One line per entry, removed or not. Returns how many could not be removed. */
|
|
79
|
+
function reportRemovals(results) {
|
|
80
|
+
for (const entry of results) {
|
|
81
|
+
if (entry.removed) {
|
|
82
|
+
out.pass('fix', 'removed ' + entry.scope + ': ' + entry.key);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
out.warn('fix', 'could NOT remove ' + entry.scope + ': ' + entry.key);
|
|
86
|
+
if (entry.scope === 'system')
|
|
87
|
+
out.detail('the system scope is machine-wide; re-run this in an ELEVATED shell.');
|
|
88
|
+
}
|
|
89
|
+
return results.filter((entry) => !entry.removed).length;
|
|
90
|
+
}
|