@statewalker/vcs-commands 0.13.0 → 0.15.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/dist/commands/add-command.d.ts.map +1 -1
- package/dist/commands/add-patch/patch-backend.d.ts +1 -1
- package/dist/commands/add-patch/patch-backend.d.ts.map +1 -1
- package/dist/commands/archive/archive-command.d.ts +6 -2
- package/dist/commands/archive/archive-command.d.ts.map +1 -1
- package/dist/commands/archive/remote-archive-command.d.ts +27 -2
- package/dist/commands/archive/remote-archive-command.d.ts.map +1 -1
- package/dist/commands/archive/upload-archive-writer.d.ts +11 -5
- package/dist/commands/archive/upload-archive-writer.d.ts.map +1 -1
- package/dist/commands/branch-command.d.ts +14 -2
- package/dist/commands/branch-command.d.ts.map +1 -1
- package/dist/commands/bundle/bundle-format.d.ts +6 -1
- package/dist/commands/bundle/bundle-format.d.ts.map +1 -1
- package/dist/commands/bundle/bundle-uri-host.d.ts +12 -1
- package/dist/commands/bundle/bundle-uri-host.d.ts.map +1 -1
- package/dist/commands/bundle/bundle-uri.d.ts +7 -1
- package/dist/commands/bundle/bundle-uri.d.ts.map +1 -1
- package/dist/commands/bundle/bundle-verify.d.ts +4 -1
- package/dist/commands/bundle/bundle-verify.d.ts.map +1 -1
- package/dist/commands/cat-file/batch-format.d.ts +8 -12
- package/dist/commands/cat-file/batch-format.d.ts.map +1 -1
- package/dist/commands/cat-file/cat-file-command.d.ts +39 -5
- package/dist/commands/cat-file/cat-file-command.d.ts.map +1 -1
- package/dist/commands/check-ignore-command.d.ts.map +1 -1
- package/dist/commands/checkout-command.d.ts +4 -0
- package/dist/commands/checkout-command.d.ts.map +1 -1
- package/dist/commands/checkout-paths.d.ts +9 -0
- package/dist/commands/checkout-paths.d.ts.map +1 -1
- package/dist/commands/cherry-pick-command.d.ts +3 -0
- package/dist/commands/cherry-pick-command.d.ts.map +1 -1
- package/dist/commands/clean-interactive.d.ts +0 -9
- package/dist/commands/clean-interactive.d.ts.map +1 -1
- package/dist/commands/clone-command.d.ts +17 -0
- package/dist/commands/clone-command.d.ts.map +1 -1
- package/dist/commands/column/column-config.d.ts +23 -0
- package/dist/commands/column/column-config.d.ts.map +1 -0
- package/dist/commands/column/column-layout.d.ts +129 -0
- package/dist/commands/column/column-layout.d.ts.map +1 -0
- package/dist/commands/column/index.d.ts +3 -0
- package/dist/commands/column/index.d.ts.map +1 -0
- package/dist/commands/column-command.d.ts +59 -0
- package/dist/commands/column-command.d.ts.map +1 -0
- package/dist/commands/comment-char-config.d.ts +15 -0
- package/dist/commands/comment-char-config.d.ts.map +1 -1
- package/dist/commands/commit-command.d.ts +30 -2
- package/dist/commands/commit-command.d.ts.map +1 -1
- package/dist/commands/config-command.d.ts +12 -0
- package/dist/commands/config-command.d.ts.map +1 -1
- package/dist/commands/credential-helper.d.ts +214 -0
- package/dist/commands/credential-helper.d.ts.map +1 -0
- package/dist/commands/default-branch.d.ts +33 -3
- package/dist/commands/default-branch.d.ts.map +1 -1
- package/dist/commands/diff-color-config.d.ts +4 -0
- package/dist/commands/diff-color-config.d.ts.map +1 -1
- package/dist/commands/diff-command.d.ts +12 -0
- package/dist/commands/diff-command.d.ts.map +1 -1
- package/dist/commands/fast-export/fast-export-command.d.ts +89 -2
- package/dist/commands/fast-export/fast-export-command.d.ts.map +1 -1
- package/dist/commands/fast-export/fast-import-command.d.ts +113 -8
- package/dist/commands/fast-export/fast-import-command.d.ts.map +1 -1
- package/dist/commands/fast-export/fast-stream-support.d.ts +0 -9
- package/dist/commands/fast-export/fast-stream-support.d.ts.map +1 -1
- package/dist/commands/fetch-command.d.ts +23 -1
- package/dist/commands/fetch-command.d.ts.map +1 -1
- package/dist/commands/fsck/fsck-object-checks.d.ts.map +1 -1
- package/dist/commands/git-hooks.d.ts +31 -3
- package/dist/commands/git-hooks.d.ts.map +1 -1
- package/dist/commands/history-command.d.ts +173 -7
- package/dist/commands/history-command.d.ts.map +1 -1
- package/dist/commands/index-host.d.ts +19 -0
- package/dist/commands/index-host.d.ts.map +1 -1
- package/dist/commands/index-refresh.d.ts +36 -0
- package/dist/commands/index-refresh.d.ts.map +1 -0
- package/dist/commands/index.d.ts +9 -0
- package/dist/commands/index.d.ts.map +1 -1
- package/dist/commands/index.js +7 -6
- package/dist/commands/init-command.d.ts +20 -0
- package/dist/commands/init-command.d.ts.map +1 -1
- package/dist/commands/local-transport.d.ts +40 -2
- package/dist/commands/local-transport.d.ts.map +1 -1
- package/dist/commands/log-command.d.ts +7 -0
- package/dist/commands/log-command.d.ts.map +1 -1
- package/dist/commands/log-output.d.ts +11 -0
- package/dist/commands/log-output.d.ts.map +1 -1
- package/dist/commands/ls-tree-command.d.ts +10 -0
- package/dist/commands/ls-tree-command.d.ts.map +1 -1
- package/dist/commands/maintenance-schedule-command.d.ts +122 -0
- package/dist/commands/maintenance-schedule-command.d.ts.map +1 -0
- package/dist/commands/merge-command.d.ts +13 -1
- package/dist/commands/merge-command.d.ts.map +1 -1
- package/dist/commands/merge-index-command.d.ts +42 -0
- package/dist/commands/merge-index-command.d.ts.map +1 -0
- package/dist/commands/merge-one-file.d.ts +129 -0
- package/dist/commands/merge-one-file.d.ts.map +1 -0
- package/dist/commands/merge-support.d.ts +40 -4
- package/dist/commands/merge-support.d.ts.map +1 -1
- package/dist/commands/object-disk-info.d.ts +35 -0
- package/dist/commands/object-disk-info.d.ts.map +1 -0
- package/dist/commands/octopus-merge.d.ts +3 -3
- package/dist/commands/octopus-merge.d.ts.map +1 -1
- package/dist/commands/plumbing-input.d.ts.map +1 -1
- package/dist/commands/promisor-remote-reply.d.ts +31 -4
- package/dist/commands/promisor-remote-reply.d.ts.map +1 -1
- package/dist/commands/promisor-remote.d.ts +8 -1
- package/dist/commands/promisor-remote.d.ts.map +1 -1
- package/dist/commands/pull-command.d.ts +55 -7
- package/dist/commands/pull-command.d.ts.map +1 -1
- package/dist/commands/push-command.d.ts +18 -1
- package/dist/commands/push-command.d.ts.map +1 -1
- package/dist/commands/range-diff/range-diff-command.d.ts +37 -1
- package/dist/commands/range-diff/range-diff-command.d.ts.map +1 -1
- package/dist/commands/rebase-command.d.ts +13 -1
- package/dist/commands/rebase-command.d.ts.map +1 -1
- package/dist/commands/ref-filter/for-each-ref-command.d.ts +3 -2
- package/dist/commands/ref-filter/for-each-ref-command.d.ts.map +1 -1
- package/dist/commands/ref-filter/index.d.ts +1 -0
- package/dist/commands/ref-filter/index.d.ts.map +1 -1
- package/dist/commands/ref-filter/peel-limit.d.ts +8 -0
- package/dist/commands/ref-filter/peel-limit.d.ts.map +1 -0
- package/dist/commands/ref-filter/ref-filter.d.ts.map +1 -1
- package/dist/commands/ref-filter/ref-format.d.ts +26 -1
- package/dist/commands/ref-filter/ref-format.d.ts.map +1 -1
- package/dist/commands/ref-filter/ref-listing-command.d.ts +9 -0
- package/dist/commands/ref-filter/ref-listing-command.d.ts.map +1 -1
- package/dist/commands/remote-transport.d.ts +27 -0
- package/dist/commands/remote-transport.d.ts.map +1 -1
- package/dist/commands/repack-command.d.ts +18 -2
- package/dist/commands/repack-command.d.ts.map +1 -1
- package/dist/commands/repo-command.d.ts +2 -7
- package/dist/commands/repo-command.d.ts.map +1 -1
- package/dist/commands/reset-command.d.ts +4 -1
- package/dist/commands/reset-command.d.ts.map +1 -1
- package/dist/commands/restore-command.d.ts.map +1 -1
- package/dist/commands/rev-list-command.d.ts +65 -0
- package/dist/commands/rev-list-command.d.ts.map +1 -1
- package/dist/commands/revert-command.d.ts +3 -0
- package/dist/commands/revert-command.d.ts.map +1 -1
- package/dist/commands/revision-filenames.d.ts.map +1 -1
- package/dist/commands/revision-range.d.ts +17 -0
- package/dist/commands/revision-range.d.ts.map +1 -1
- package/dist/commands/revision-resolver.d.ts +12 -0
- package/dist/commands/revision-resolver.d.ts.map +1 -1
- package/dist/commands/serve-repository.d.ts +21 -4
- package/dist/commands/serve-repository.d.ts.map +1 -1
- package/dist/commands/served-lazy-fetch.d.ts +61 -0
- package/dist/commands/served-lazy-fetch.d.ts.map +1 -0
- package/dist/commands/signing.d.ts +37 -4
- package/dist/commands/signing.d.ts.map +1 -1
- package/dist/commands/split-cmdline.d.ts +6 -0
- package/dist/commands/split-cmdline.d.ts.map +1 -0
- package/dist/commands/status-color-config.d.ts +33 -0
- package/dist/commands/status-color-config.d.ts.map +1 -0
- package/dist/commands/status-command.d.ts +15 -0
- package/dist/commands/status-command.d.ts.map +1 -1
- package/dist/commands/status-format.d.ts +46 -4
- package/dist/commands/status-format.d.ts.map +1 -1
- package/dist/commands/tag-command.d.ts +15 -1
- package/dist/commands/tag-command.d.ts.map +1 -1
- package/dist/commands/tree-restore.d.ts +14 -0
- package/dist/commands/tree-restore.d.ts.map +1 -1
- package/dist/commands/verify-command.d.ts +22 -2
- package/dist/commands/verify-command.d.ts.map +1 -1
- package/dist/commands/worktree-add-command.d.ts.map +1 -1
- package/dist/commands/worktree-overwrite-checks.d.ts +31 -6
- package/dist/commands/worktree-overwrite-checks.d.ts.map +1 -1
- package/dist/{commands-CPceUCP2.js → commands-DT3VEv50.js} +15168 -16842
- package/dist/errors/commit-errors.d.ts +37 -1
- package/dist/errors/commit-errors.d.ts.map +1 -1
- package/dist/errors/git-api-error.d.ts +6 -0
- package/dist/errors/git-api-error.d.ts.map +1 -1
- package/dist/errors/index.js +44 -3
- package/dist/{fsck-tbrPZX2q.js → fsck-BZ508Elg.js} +2 -2
- package/dist/git-command.d.ts +33 -1
- package/dist/git-command.d.ts.map +1 -1
- package/dist/git.d.ts +104 -1
- package/dist/git.d.ts.map +1 -1
- package/dist/index.js +8 -7
- package/dist/{remote-transport-C0OSJtor.js → local-transport-D98U6p4d.js} +228 -692
- package/dist/{maintenance-run-command-UiSIaeP8.js → maintenance-run-command-DZFnbnbo.js} +2366 -108
- package/dist/remote-config/remote-config-store.d.ts +6 -0
- package/dist/remote-config/remote-config-store.d.ts.map +1 -1
- package/dist/remote-transport-zvSqwpOB.js +4865 -0
- package/dist/results/cherry-pick-result.d.ts +4 -3
- package/dist/results/cherry-pick-result.d.ts.map +1 -1
- package/dist/results/diff-output.d.ts +7 -0
- package/dist/results/diff-output.d.ts.map +1 -1
- package/dist/results/diff-words.d.ts +14 -3
- package/dist/results/diff-words.d.ts.map +1 -1
- package/dist/results/index.js +1 -1
- package/dist/results/line-prefix-sink.d.ts +50 -0
- package/dist/results/line-prefix-sink.d.ts.map +1 -0
- package/dist/results/merge-result.d.ts +4 -3
- package/dist/results/merge-result.d.ts.map +1 -1
- package/dist/results/rebase-result.d.ts +5 -0
- package/dist/results/rebase-result.d.ts.map +1 -1
- package/dist/results/revert-result.d.ts +4 -3
- package/dist/results/revert-result.d.ts.map +1 -1
- package/dist/{results-CkS1p1CH.js → results-rF81cQse.js} +1788 -247
- package/dist/transport-command.d.ts +13 -0
- package/dist/transport-command.d.ts.map +1 -1
- package/package.json +21 -13
- package/src/commands/add-command.ts +15 -2
- package/src/commands/add-patch/patch-backend.ts +6 -44
- package/src/commands/archive/archive-command.ts +7 -3
- package/src/commands/archive/remote-archive-command.ts +45 -4
- package/src/commands/archive/upload-archive-writer.ts +34 -22
- package/src/commands/branch-command.ts +57 -4
- package/src/commands/bundle/bundle-format.ts +10 -2
- package/src/commands/bundle/bundle-uri-host.ts +71 -11
- package/src/commands/bundle/bundle-uri.ts +11 -4
- package/src/commands/bundle/bundle-verify.ts +4 -1
- package/src/commands/cat-file/batch-format.ts +17 -18
- package/src/commands/cat-file/cat-file-command.ts +182 -48
- package/src/commands/check-ignore-command.ts +110 -21
- package/src/commands/checkout-command.ts +149 -21
- package/src/commands/checkout-index-command.ts +2 -1
- package/src/commands/checkout-paths.ts +13 -0
- package/src/commands/cherry-pick-command.ts +9 -0
- package/src/commands/clean-interactive.ts +20 -74
- package/src/commands/clone-command.ts +102 -11
- package/src/commands/column/column-config.ts +63 -0
- package/src/commands/column/column-layout.ts +396 -0
- package/src/commands/column/index.ts +2 -0
- package/src/commands/column-command.ts +171 -0
- package/src/commands/comment-char-config.ts +42 -0
- package/src/commands/commit-command.ts +93 -18
- package/src/commands/config-command.ts +28 -0
- package/src/commands/credential-helper.ts +807 -0
- package/src/commands/default-branch.ts +57 -5
- package/src/commands/diff-color-config.ts +23 -10
- package/src/commands/diff-command.ts +25 -2
- package/src/commands/fast-export/fast-export-command.ts +661 -35
- package/src/commands/fast-export/fast-import-command.ts +694 -94
- package/src/commands/fast-export/fast-stream-support.ts +18 -1
- package/src/commands/fetch-command.ts +116 -16
- package/src/commands/fsck/fsck-object-checks.ts +3 -1
- package/src/commands/git-hooks.ts +96 -5
- package/src/commands/history-command.ts +952 -20
- package/src/commands/index-host.ts +51 -0
- package/src/commands/index-refresh.ts +121 -0
- package/src/commands/index.ts +9 -0
- package/src/commands/init-command.ts +57 -4
- package/src/commands/legacy-remotes.ts +4 -20
- package/src/commands/local-transport.ts +183 -18
- package/src/commands/log-command.ts +39 -1
- package/src/commands/log-output.ts +31 -4
- package/src/commands/ls-tree-command.ts +20 -12
- package/src/commands/maintenance-schedule-command.ts +418 -0
- package/src/commands/merge-command.ts +42 -8
- package/src/commands/merge-index-command.ts +202 -0
- package/src/commands/merge-one-file.ts +451 -0
- package/src/commands/merge-support.ts +169 -15
- package/src/commands/object-disk-info.ts +169 -0
- package/src/commands/octopus-merge.ts +37 -128
- package/src/commands/plumbing-input.ts +3 -1
- package/src/commands/promisor-remote-reply.ts +338 -56
- package/src/commands/promisor-remote.ts +39 -6
- package/src/commands/pull-command.ts +173 -27
- package/src/commands/push-command.ts +55 -6
- package/src/commands/range-diff/range-diff-command.ts +86 -3
- package/src/commands/read-tree-command.ts +2 -2
- package/src/commands/rebase-command.ts +46 -1
- package/src/commands/ref-filter/for-each-ref-command.ts +4 -3
- package/src/commands/ref-filter/index.ts +1 -0
- package/src/commands/ref-filter/peel-limit.ts +7 -0
- package/src/commands/ref-filter/ref-filter.ts +68 -7
- package/src/commands/ref-filter/ref-format.ts +85 -11
- package/src/commands/ref-filter/ref-listing-command.ts +30 -0
- package/src/commands/remote-command.ts +2 -2
- package/src/commands/remote-transport.ts +66 -3
- package/src/commands/repack-command.ts +75 -9
- package/src/commands/repo-command.ts +6 -70
- package/src/commands/reset-command.ts +11 -1
- package/src/commands/restore-command.ts +1 -0
- package/src/commands/rev-list-command.ts +205 -3
- package/src/commands/revert-command.ts +9 -0
- package/src/commands/revision-filenames.ts +32 -4
- package/src/commands/revision-range.ts +2 -2
- package/src/commands/revision-resolver.ts +21 -1
- package/src/commands/serve-repository.ts +42 -11
- package/src/commands/served-lazy-fetch.ts +164 -0
- package/src/commands/signing.ts +103 -12
- package/src/commands/split-cmdline.ts +32 -0
- package/src/commands/status-color-config.ts +92 -0
- package/src/commands/status-command.ts +50 -2
- package/src/commands/status-format.ts +198 -50
- package/src/commands/tag-command.ts +57 -2
- package/src/commands/tree-restore.ts +17 -0
- package/src/commands/verify-command.ts +84 -12
- package/src/commands/worktree-add-command.ts +2 -0
- package/src/commands/worktree-overwrite-checks.ts +75 -14
- package/src/errors/commit-errors.ts +52 -2
- package/src/errors/git-api-error.ts +7 -0
- package/src/git-command.ts +105 -4
- package/src/git.ts +153 -0
- package/src/remote-config/remote-config-store.ts +16 -0
- package/src/results/cherry-pick-result.ts +4 -3
- package/src/results/diff-output.ts +8 -0
- package/src/results/diff-words.ts +160 -32
- package/src/results/line-prefix-sink.ts +102 -0
- package/src/results/merge-result.ts +4 -3
- package/src/results/rebase-result.ts +5 -0
- package/src/results/revert-result.ts +4 -3
- package/src/transport-command.ts +79 -3
|
@@ -0,0 +1,807 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* git's credential helpers (credential.c): `credential.helper` programs asked for HTTP
|
|
3
|
+
* credentials with the key=value text protocol, run through the host's hook runner.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* // what `git -c credential.helper=store fetch` asks after a 401
|
|
7
|
+
* const helper = credentialHelperOf(workingCopy);
|
|
8
|
+
* await git.fetch().setCredentialHelper(helper).call();
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* A {@link GitCredential} is git's `struct credential`; {@link CredentialHelpers} runs
|
|
12
|
+
* `credential_fill`, `credential_approve` and `credential_reject` over one, and
|
|
13
|
+
* {@link createConfigCredentialHelper} adapts them to the transports' `HttpCredentialHelper`
|
|
14
|
+
* as http.c drives its `http_auth`. Nothing runs without a hook runner.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type {
|
|
18
|
+
CredentialRequest,
|
|
19
|
+
Credentials,
|
|
20
|
+
HttpCredentialHelper,
|
|
21
|
+
} from "@statewalker/vcs-transport";
|
|
22
|
+
import { isUrlNormalizeError, normalizeUrl, urlMatchedEntries } from "@statewalker/vcs-utils";
|
|
23
|
+
import { parseGitBool } from "@statewalker/vcs-utils/encoding";
|
|
24
|
+
import type { WorkingCopy } from "@statewalker/vcs-working-tree";
|
|
25
|
+
import { InvalidConfigError } from "../errors/index.js";
|
|
26
|
+
import { RemoteConfigStore } from "../remote-config/index.js";
|
|
27
|
+
import { HookSession, hookRunnerOf } from "./git-hooks.js";
|
|
28
|
+
|
|
29
|
+
/** The git version whose credential protocol is spoken (2.46 added capabilities). */
|
|
30
|
+
export type CredentialCompatibility = "2.43" | "2.56";
|
|
31
|
+
|
|
32
|
+
/** One of git's credential capabilities (`struct credential_capability`). */
|
|
33
|
+
interface CredentialCapability {
|
|
34
|
+
requestInitial: boolean;
|
|
35
|
+
requestHelper: boolean;
|
|
36
|
+
response: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The step a credential is read or written for (`enum credential_op_type`). */
|
|
40
|
+
export type CredentialOp = "initial" | "helper" | "response";
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* git's `struct credential`: what is known about one remote's credentials, as handed to and
|
|
44
|
+
* read from the helpers.
|
|
45
|
+
*/
|
|
46
|
+
export interface GitCredential {
|
|
47
|
+
protocol?: string;
|
|
48
|
+
host?: string;
|
|
49
|
+
path?: string;
|
|
50
|
+
username?: string;
|
|
51
|
+
password?: string;
|
|
52
|
+
/** 2.46+: the ready credential of {@link authtype} */
|
|
53
|
+
credential?: string;
|
|
54
|
+
authtype?: string;
|
|
55
|
+
ephemeral: boolean;
|
|
56
|
+
oauthRefreshToken?: string;
|
|
57
|
+
/** Seconds since the epoch; undefined is git's TIME_MAX */
|
|
58
|
+
passwordExpiryUtc?: number;
|
|
59
|
+
/** The `WWW-Authenticate` challenges (`wwwauth[]`) */
|
|
60
|
+
wwwauth: string[];
|
|
61
|
+
/** `state[]` a helper gave, sent back on the next step */
|
|
62
|
+
stateHeaders: string[];
|
|
63
|
+
stateHeadersToSend: string[];
|
|
64
|
+
multistage: boolean;
|
|
65
|
+
quit: boolean;
|
|
66
|
+
approved: boolean;
|
|
67
|
+
configured: boolean;
|
|
68
|
+
useHttpPath: boolean;
|
|
69
|
+
usernameFromProto: boolean;
|
|
70
|
+
sanitizePrompt: boolean;
|
|
71
|
+
protectProtocol: boolean;
|
|
72
|
+
/** `credential.helper` values in config order, after resets */
|
|
73
|
+
helpers: string[];
|
|
74
|
+
capaAuthtype: CredentialCapability;
|
|
75
|
+
capaState: CredentialCapability;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** git's `CREDENTIAL_INIT`. */
|
|
79
|
+
export function newCredential(): GitCredential {
|
|
80
|
+
return {
|
|
81
|
+
ephemeral: false,
|
|
82
|
+
wwwauth: [],
|
|
83
|
+
stateHeaders: [],
|
|
84
|
+
stateHeadersToSend: [],
|
|
85
|
+
multistage: false,
|
|
86
|
+
quit: false,
|
|
87
|
+
approved: false,
|
|
88
|
+
configured: false,
|
|
89
|
+
useHttpPath: false,
|
|
90
|
+
usernameFromProto: false,
|
|
91
|
+
sanitizePrompt: true,
|
|
92
|
+
protectProtocol: true,
|
|
93
|
+
helpers: [],
|
|
94
|
+
capaAuthtype: { requestInitial: false, requestHelper: false, response: false },
|
|
95
|
+
capaState: { requestInitial: false, requestHelper: false, response: false },
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** A death of git's credential code (`die()`): its `fatal:` line. */
|
|
100
|
+
export class CredentialError extends Error {
|
|
101
|
+
constructor(message: string) {
|
|
102
|
+
super(`fatal: ${message}`);
|
|
103
|
+
this.name = "CredentialError";
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function setCapability(capa: CredentialCapability, op: CredentialOp): void {
|
|
108
|
+
if (op === "initial") capa.requestInitial = true;
|
|
109
|
+
else if (op === "helper") capa.requestHelper = true;
|
|
110
|
+
else capa.response = true;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** `credential_set_all_capabilities` */
|
|
114
|
+
export function setAllCapabilities(c: GitCredential, op: CredentialOp): void {
|
|
115
|
+
setCapability(c.capaAuthtype, op);
|
|
116
|
+
setCapability(c.capaState, op);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function hasCapability(capa: CredentialCapability, op: CredentialOp): boolean {
|
|
120
|
+
if (op === "helper") return capa.requestInitial;
|
|
121
|
+
if (op === "response") return capa.requestInitial && capa.requestHelper;
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function clearSecrets(c: GitCredential): void {
|
|
126
|
+
delete c.password;
|
|
127
|
+
delete c.credential;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** `credential_next_state`: the `state[]` a helper gave become the ones sent next. */
|
|
131
|
+
export function nextState(c: GitCredential): void {
|
|
132
|
+
c.stateHeadersToSend = c.stateHeaders;
|
|
133
|
+
c.stateHeaders = [];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** git_config_bool without a key to die for: a bad value reads as false here. */
|
|
137
|
+
function bool(value: string): boolean {
|
|
138
|
+
return parseGitBool(value) ?? false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* `credential_read`: applies the key=value lines of `text` up to an empty line (or its end)
|
|
143
|
+
* to `c`. A line without `=` stops the reading with git's warning (false is returned); keys
|
|
144
|
+
* git does not know are ignored. Under 2.43 the keys 2.46 added are unknown too.
|
|
145
|
+
*/
|
|
146
|
+
export function readCredential(
|
|
147
|
+
c: GitCredential,
|
|
148
|
+
text: string,
|
|
149
|
+
op: CredentialOp,
|
|
150
|
+
compatibility: CredentialCompatibility = "2.56",
|
|
151
|
+
warn: (line: string) => void = () => {},
|
|
152
|
+
): boolean {
|
|
153
|
+
const modern = compatibility !== "2.43";
|
|
154
|
+
const lines = text.split("\n");
|
|
155
|
+
// strbuf_getline: a final line without its newline still counts, an empty tail does not
|
|
156
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
157
|
+
for (const raw of lines) {
|
|
158
|
+
// strbuf_getline drops a CR before the LF
|
|
159
|
+
const line = raw.endsWith("\r") ? raw.slice(0, -1) : raw;
|
|
160
|
+
if (line === "") break;
|
|
161
|
+
const eq = line.indexOf("=");
|
|
162
|
+
if (eq < 0) {
|
|
163
|
+
warn(`warning: invalid credential line: ${line}\n`);
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
const key = line.slice(0, eq);
|
|
167
|
+
const value = line.slice(eq + 1);
|
|
168
|
+
switch (key) {
|
|
169
|
+
case "username":
|
|
170
|
+
c.username = value;
|
|
171
|
+
c.usernameFromProto = true;
|
|
172
|
+
break;
|
|
173
|
+
case "password":
|
|
174
|
+
c.password = value;
|
|
175
|
+
break;
|
|
176
|
+
case "protocol":
|
|
177
|
+
c.protocol = value;
|
|
178
|
+
break;
|
|
179
|
+
case "host":
|
|
180
|
+
c.host = value;
|
|
181
|
+
break;
|
|
182
|
+
case "path":
|
|
183
|
+
c.path = value;
|
|
184
|
+
break;
|
|
185
|
+
case "wwwauth[]":
|
|
186
|
+
c.wwwauth.push(value);
|
|
187
|
+
break;
|
|
188
|
+
case "password_expiry_utc": {
|
|
189
|
+
const expiry = parseTimestamp(value);
|
|
190
|
+
c.passwordExpiryUtc = expiry === 0 || expiry === undefined ? undefined : expiry;
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
case "oauth_refresh_token":
|
|
194
|
+
c.oauthRefreshToken = value;
|
|
195
|
+
break;
|
|
196
|
+
case "url":
|
|
197
|
+
credentialFromUrl(c, value, warn);
|
|
198
|
+
break;
|
|
199
|
+
case "quit":
|
|
200
|
+
c.quit = bool(value);
|
|
201
|
+
break;
|
|
202
|
+
default:
|
|
203
|
+
if (!modern) break;
|
|
204
|
+
switch (key) {
|
|
205
|
+
case "credential":
|
|
206
|
+
c.credential = value;
|
|
207
|
+
break;
|
|
208
|
+
case "ephemeral":
|
|
209
|
+
c.ephemeral = bool(value);
|
|
210
|
+
break;
|
|
211
|
+
case "state[]":
|
|
212
|
+
c.stateHeaders.push(value);
|
|
213
|
+
break;
|
|
214
|
+
case "capability[]":
|
|
215
|
+
if (value === "authtype") setCapability(c.capaAuthtype, op);
|
|
216
|
+
else if (value === "state") setCapability(c.capaState, op);
|
|
217
|
+
break;
|
|
218
|
+
case "continue":
|
|
219
|
+
c.multistage = bool(value);
|
|
220
|
+
break;
|
|
221
|
+
case "authtype":
|
|
222
|
+
c.authtype = value;
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** parse_timestamp (strtoumax, base 10): leading digits; undefined (ERANGE) when too large. */
|
|
231
|
+
function parseTimestamp(value: string): number | undefined {
|
|
232
|
+
const m = /^\s*\+?(\d*)/.exec(value);
|
|
233
|
+
const digits = m?.[1] ?? "";
|
|
234
|
+
if (digits === "") return 0;
|
|
235
|
+
const n = Number(digits);
|
|
236
|
+
return Number.isSafeInteger(n) ? n : undefined;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* `credential_write`: the lines git hands a helper (`helper`: `get`) or hands back
|
|
241
|
+
* (`response`: `store`, `erase`, and `git credential`'s output).
|
|
242
|
+
*
|
|
243
|
+
* @throws CredentialError for a value holding a newline (or, 2.46+, a carriage return under
|
|
244
|
+
* `credential.protectProtocol`)
|
|
245
|
+
*/
|
|
246
|
+
export function writeCredential(
|
|
247
|
+
c: GitCredential,
|
|
248
|
+
op: CredentialOp,
|
|
249
|
+
compatibility: CredentialCompatibility = "2.56",
|
|
250
|
+
): string {
|
|
251
|
+
const modern = compatibility !== "2.43";
|
|
252
|
+
let out = "";
|
|
253
|
+
const item = (key: string, value: string | undefined) => {
|
|
254
|
+
if (value === undefined) return;
|
|
255
|
+
if (value.includes("\n"))
|
|
256
|
+
throw new CredentialError(`credential value for ${key} contains newline`);
|
|
257
|
+
if (modern && c.protectProtocol && value.includes("\r")) {
|
|
258
|
+
throw new CredentialError(
|
|
259
|
+
`credential value for ${key} contains carriage return\nIf this is intended, set \`credential.protectProtocol=false\``,
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
out += `${key}=${value}\n`;
|
|
263
|
+
};
|
|
264
|
+
const authtype = modern && hasCapability(c.capaAuthtype, op);
|
|
265
|
+
const state = modern && hasCapability(c.capaState, op);
|
|
266
|
+
if (authtype) item("capability[]", "authtype");
|
|
267
|
+
if (state) item("capability[]", "state");
|
|
268
|
+
if (authtype) {
|
|
269
|
+
item("authtype", c.authtype);
|
|
270
|
+
item("credential", c.credential);
|
|
271
|
+
if (c.ephemeral) item("ephemeral", "1");
|
|
272
|
+
}
|
|
273
|
+
item("protocol", c.protocol ?? "");
|
|
274
|
+
item("host", c.host ?? "");
|
|
275
|
+
item("path", c.path);
|
|
276
|
+
item("username", c.username);
|
|
277
|
+
item("password", c.password);
|
|
278
|
+
item("oauth_refresh_token", c.oauthRefreshToken);
|
|
279
|
+
if (c.passwordExpiryUtc !== undefined) item("password_expiry_utc", String(c.passwordExpiryUtc));
|
|
280
|
+
for (const header of c.wwwauth) item("wwwauth[]", header);
|
|
281
|
+
if (state) {
|
|
282
|
+
if (c.multistage) item("continue", "1");
|
|
283
|
+
for (const header of c.stateHeadersToSend) item("state[]", header);
|
|
284
|
+
}
|
|
285
|
+
return out;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const encoder = new TextEncoder();
|
|
289
|
+
const decoder = new TextDecoder();
|
|
290
|
+
|
|
291
|
+
/** url_decode_mem: `%XX` escapes decoded (no `+`), bytes read as UTF-8. */
|
|
292
|
+
function urlDecode(text: string): string {
|
|
293
|
+
const bytes = encoder.encode(text);
|
|
294
|
+
const out: number[] = [];
|
|
295
|
+
const hex = (b: number | undefined) => {
|
|
296
|
+
if (b === undefined) return -1;
|
|
297
|
+
if (b >= 0x30 && b <= 0x39) return b - 0x30;
|
|
298
|
+
if (b >= 0x41 && b <= 0x46) return b - 0x37;
|
|
299
|
+
if (b >= 0x61 && b <= 0x66) return b - 0x57;
|
|
300
|
+
return -1;
|
|
301
|
+
};
|
|
302
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
303
|
+
const b = bytes[i] as number;
|
|
304
|
+
if (b === 0x25 && bytes.length - i >= 3) {
|
|
305
|
+
const hi = hex(bytes[i + 1]);
|
|
306
|
+
const lo = hex(bytes[i + 2]);
|
|
307
|
+
// hex2chr: a value of 0 (`%00`) is not taken either
|
|
308
|
+
const val = hi < 0 || lo < 0 ? -1 : (hi << 4) | lo;
|
|
309
|
+
if (val > 0) {
|
|
310
|
+
out.push(val);
|
|
311
|
+
i += 2;
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
out.push(b);
|
|
316
|
+
}
|
|
317
|
+
return decoder.decode(new Uint8Array(out));
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* `credential_from_url_1`: `c` cleared, then filled from `url` (`proto://[user[:pass]@]host/path`,
|
|
322
|
+
* slashes around the path trimmed). A partial URL may lack any part.
|
|
323
|
+
*
|
|
324
|
+
* @returns git's warning when the URL cannot be used (no scheme, a newline in a part)
|
|
325
|
+
*/
|
|
326
|
+
function credentialFromUrlGently(
|
|
327
|
+
c: GitCredential,
|
|
328
|
+
url: string,
|
|
329
|
+
allowPartial: boolean,
|
|
330
|
+
): string | undefined {
|
|
331
|
+
Object.assign(c, newCredential());
|
|
332
|
+
// credential_clear: every field, secrets and their metadata included
|
|
333
|
+
for (const key of [
|
|
334
|
+
"protocol",
|
|
335
|
+
"host",
|
|
336
|
+
"path",
|
|
337
|
+
"username",
|
|
338
|
+
"password",
|
|
339
|
+
"credential",
|
|
340
|
+
"authtype",
|
|
341
|
+
"oauthRefreshToken",
|
|
342
|
+
"passwordExpiryUtc",
|
|
343
|
+
] as const) {
|
|
344
|
+
delete c[key];
|
|
345
|
+
}
|
|
346
|
+
const protoEnd = url.indexOf("://");
|
|
347
|
+
if (!allowPartial && protoEnd <= 0) return `warning: url has no scheme: ${url}\n`;
|
|
348
|
+
const cp = protoEnd >= 0 ? protoEnd + 3 : 0;
|
|
349
|
+
const rest = url.slice(cp);
|
|
350
|
+
const atRel = rest.indexOf("@");
|
|
351
|
+
const colonRel = rest.indexOf(":");
|
|
352
|
+
let slashRel = rest.search(/[/?#]/);
|
|
353
|
+
if (slashRel < 0) slashRel = rest.length;
|
|
354
|
+
let hostRel: number;
|
|
355
|
+
if (atRel < 0 || slashRel <= atRel) hostRel = 0;
|
|
356
|
+
else if (colonRel < 0 || atRel <= colonRel) {
|
|
357
|
+
c.username = urlDecode(rest.slice(0, atRel));
|
|
358
|
+
if (c.username) c.usernameFromProto = true;
|
|
359
|
+
hostRel = atRel + 1;
|
|
360
|
+
} else {
|
|
361
|
+
c.username = urlDecode(rest.slice(0, colonRel));
|
|
362
|
+
if (c.username) c.usernameFromProto = true;
|
|
363
|
+
c.password = urlDecode(rest.slice(colonRel + 1, atRel));
|
|
364
|
+
hostRel = atRel + 1;
|
|
365
|
+
}
|
|
366
|
+
if (protoEnd > 0) c.protocol = url.slice(0, protoEnd);
|
|
367
|
+
if (!allowPartial || slashRel - hostRel > 0) c.host = urlDecode(rest.slice(hostRel, slashRel));
|
|
368
|
+
let p = slashRel;
|
|
369
|
+
while (rest[p] === "/") p++;
|
|
370
|
+
if (p < rest.length) {
|
|
371
|
+
let path = urlDecode(rest.slice(p));
|
|
372
|
+
let end = path.length;
|
|
373
|
+
while (end > 1 && path[end - 1] === "/") end--;
|
|
374
|
+
path = path.slice(0, end);
|
|
375
|
+
c.path = path;
|
|
376
|
+
}
|
|
377
|
+
for (const key of ["username", "password", "protocol", "host", "path"] as const) {
|
|
378
|
+
if (c[key]?.includes("\n"))
|
|
379
|
+
return `warning: url contains a newline in its ${key} component: ${url}\n`;
|
|
380
|
+
}
|
|
381
|
+
return undefined;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* `credential_from_url`: `c` from a full URL.
|
|
386
|
+
*
|
|
387
|
+
* @throws CredentialError when git dies on it (`credential url cannot be parsed: <url>`),
|
|
388
|
+
* after handing its warning to `warn`
|
|
389
|
+
*/
|
|
390
|
+
export function credentialFromUrl(
|
|
391
|
+
c: GitCredential,
|
|
392
|
+
url: string,
|
|
393
|
+
warn: (line: string) => void = () => {},
|
|
394
|
+
): void {
|
|
395
|
+
const warning = credentialFromUrlGently(c, url, false);
|
|
396
|
+
if (warning !== undefined) {
|
|
397
|
+
warn(warning);
|
|
398
|
+
throw new CredentialError(`credential url cannot be parsed: ${url}`);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* The remote as git's prompt names it (`Username for '<this>': `): `credential_format`
|
|
404
|
+
* (2.46+'s `credential.sanitizePrompt`, on by default) or `credential_describe`.
|
|
405
|
+
*/
|
|
406
|
+
export function describeCredential(
|
|
407
|
+
c: GitCredential,
|
|
408
|
+
compatibility: CredentialCompatibility = "2.56",
|
|
409
|
+
): string {
|
|
410
|
+
if (compatibility !== "2.43" && c.sanitizePrompt) return credentialFormat(c, compatibility);
|
|
411
|
+
if (c.protocol === undefined) return "";
|
|
412
|
+
let out = `${c.protocol}://`;
|
|
413
|
+
if (c.username) out += `${c.username}@`;
|
|
414
|
+
if (c.host !== undefined) out += c.host;
|
|
415
|
+
if (c.path !== undefined) out += `/${c.path}`;
|
|
416
|
+
return out;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** `credential_match` (without the secrets) */
|
|
420
|
+
function credentialMatch(want: GitCredential, have: GitCredential): boolean {
|
|
421
|
+
const check = (key: "protocol" | "host" | "path" | "username") =>
|
|
422
|
+
want[key] === undefined || (have[key] !== undefined && want[key] === have[key]);
|
|
423
|
+
return check("protocol") && check("host") && check("path") && check("username");
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const URL_UNSAFE_CHARS = " <>\"%{}|\\^`:?#[]@!$&'()*+,;=";
|
|
427
|
+
|
|
428
|
+
/** strbuf_add_percentencode over the UTF-8 bytes of `text`. */
|
|
429
|
+
function percentEncode(text: string, flags: { slash?: boolean; hostAndPort?: boolean }): string {
|
|
430
|
+
let out = "";
|
|
431
|
+
for (const ch of encoder.encode(text)) {
|
|
432
|
+
const c = String.fromCharCode(ch);
|
|
433
|
+
const encode =
|
|
434
|
+
ch <= 0x1f ||
|
|
435
|
+
ch >= 0x7f ||
|
|
436
|
+
(c === "/" && flags.slash === true) ||
|
|
437
|
+
(flags.hostAndPort
|
|
438
|
+
? !/[A-Za-z0-9]/.test(c) && !"-.:[]".includes(c)
|
|
439
|
+
: URL_UNSAFE_CHARS.includes(c));
|
|
440
|
+
out += encode ? `%${ch.toString(16).toUpperCase().padStart(2, "0")}` : c;
|
|
441
|
+
}
|
|
442
|
+
return out;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** `credential_format`: the URL the configuration is matched against. */
|
|
446
|
+
function credentialFormat(c: GitCredential, compatibility: CredentialCompatibility): string {
|
|
447
|
+
if (c.protocol === undefined) return "";
|
|
448
|
+
let out = `${c.protocol}://`;
|
|
449
|
+
if (c.username) out += `${percentEncode(c.username, { slash: true })}@`;
|
|
450
|
+
if (c.host !== undefined) {
|
|
451
|
+
out += compatibility === "2.43" ? c.host : percentEncode(c.host, { hostAndPort: true });
|
|
452
|
+
}
|
|
453
|
+
if (c.path !== undefined) out += `/${percentEncode(c.path, {})}`;
|
|
454
|
+
return out;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** The `credential.*` entries matching a URL `url_normalize` refuses (see `applyConfig`). */
|
|
458
|
+
function* unnormalizedMatches(
|
|
459
|
+
entries: readonly CredentialConfigEntry[],
|
|
460
|
+
fallbackMatch: (configUrl: string) => boolean,
|
|
461
|
+
): Generator<{ name: string; entry: CredentialConfigEntry }> {
|
|
462
|
+
for (const entry of entries) {
|
|
463
|
+
if (!entry.key.startsWith("credential.")) continue;
|
|
464
|
+
const rest = entry.key.slice(11);
|
|
465
|
+
const dot = rest.lastIndexOf(".");
|
|
466
|
+
if (dot < 0) {
|
|
467
|
+
yield { name: rest, entry };
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
const configUrl = rest.slice(0, dot);
|
|
471
|
+
if (!isUrlNormalizeError(normalizeUrl(configUrl, { allowGlobs: true }))) continue;
|
|
472
|
+
if (fallbackMatch(configUrl)) yield { name: rest.slice(dot + 1), entry };
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** A configuration entry as {@link CredentialHelpers} reads it: canonical key, raw value. */
|
|
477
|
+
export interface CredentialConfigEntry {
|
|
478
|
+
key: string;
|
|
479
|
+
value: string | undefined;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** What a helper did (see {@link CredentialHelpersOptions.run}). */
|
|
483
|
+
export interface CredentialHelperRun {
|
|
484
|
+
exitCode: number;
|
|
485
|
+
stdout: Uint8Array;
|
|
486
|
+
stderr: Uint8Array;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** How {@link CredentialHelpers} reaches the configuration and the helpers. */
|
|
490
|
+
export interface CredentialHelpersOptions {
|
|
491
|
+
/** The configuration's `credential.*` entries, in config order */
|
|
492
|
+
entries(): readonly CredentialConfigEntry[];
|
|
493
|
+
/**
|
|
494
|
+
* Runs `command` (the helper and its operation) through the shell with `stdin`; undefined
|
|
495
|
+
* when nothing can run a program (no hook runner): no helper is asked
|
|
496
|
+
*/
|
|
497
|
+
run(command: string, stdin: string): Promise<CredentialHelperRun | undefined>;
|
|
498
|
+
/** What git writes to stderr meanwhile: helpers' stderr and git's warnings, in order */
|
|
499
|
+
onStderr?(text: string): void;
|
|
500
|
+
/** The protocol's version (default `2.56`) */
|
|
501
|
+
compatibility?: CredentialCompatibility;
|
|
502
|
+
/** The clock, in milliseconds (default `Date.now`) */
|
|
503
|
+
now?(): number;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* credential.c's helper sequence over a {@link GitCredential}: `credential_fill`,
|
|
508
|
+
* `credential_approve`, `credential_reject`.
|
|
509
|
+
*
|
|
510
|
+
* @example
|
|
511
|
+
* ```ts
|
|
512
|
+
* const c = newCredential();
|
|
513
|
+
* credentialFromUrl(c, "https://example.com/repo.git");
|
|
514
|
+
* if (await helpers.fill(c, true)) use(c.username, c.password);
|
|
515
|
+
* ```
|
|
516
|
+
*/
|
|
517
|
+
export class CredentialHelpers {
|
|
518
|
+
private readonly compatibility: CredentialCompatibility;
|
|
519
|
+
|
|
520
|
+
constructor(private readonly options: CredentialHelpersOptions) {
|
|
521
|
+
this.compatibility = options.compatibility ?? "2.56";
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
private stderr(text: string): void {
|
|
525
|
+
if (text) this.options.onStderr?.(text);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
private nowSeconds(): number {
|
|
529
|
+
return Math.floor((this.options.now ?? Date.now)() / 1000);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* `credential_apply_config`: the helpers, `credential.username` and `useHttpPath` of every
|
|
534
|
+
* `credential.*` / `credential.<url>.*` entry matching `c`, in config order; once per
|
|
535
|
+
* credential. The path is dropped for http(s) unless `useHttpPath`.
|
|
536
|
+
*
|
|
537
|
+
* @throws CredentialError without a host or a protocol
|
|
538
|
+
* @throws InvalidConfigError for a valueless entry (`config_error_nonbool`)
|
|
539
|
+
*/
|
|
540
|
+
applyConfig(c: GitCredential): void {
|
|
541
|
+
if (c.host === undefined) {
|
|
542
|
+
throw new CredentialError("refusing to work with credential missing host field");
|
|
543
|
+
}
|
|
544
|
+
if (c.protocol === undefined) {
|
|
545
|
+
throw new CredentialError("refusing to work with credential missing protocol field");
|
|
546
|
+
}
|
|
547
|
+
if (c.configured) return;
|
|
548
|
+
const normalized = normalizeUrl(credentialFormat(c, this.compatibility));
|
|
549
|
+
const entries = this.options.entries();
|
|
550
|
+
// match_partial_url: a subsection url_normalize refuses, matched as a partial credential
|
|
551
|
+
const fallbackMatch = (configUrl: string): boolean => {
|
|
552
|
+
const want = newCredential();
|
|
553
|
+
const warning = credentialFromUrlGently(want, configUrl, true);
|
|
554
|
+
if (warning !== undefined) {
|
|
555
|
+
this.stderr(warning);
|
|
556
|
+
this.stderr(`warning: skipping credential lookup for key: credential.${configUrl}\n`);
|
|
557
|
+
return false;
|
|
558
|
+
}
|
|
559
|
+
return credentialMatch(want, c);
|
|
560
|
+
};
|
|
561
|
+
const matched = isUrlNormalizeError(normalized)
|
|
562
|
+
? // a URL git cannot normalize matches no normalized subsection (match_urls compares
|
|
563
|
+
// its empty scheme), only the plain keys and the partial ones
|
|
564
|
+
unnormalizedMatches(entries, fallbackMatch)
|
|
565
|
+
: urlMatchedEntries(entries, "credential", normalized, undefined, {
|
|
566
|
+
selectAll: true,
|
|
567
|
+
fallbackMatch,
|
|
568
|
+
});
|
|
569
|
+
for (const { name, entry } of matched) {
|
|
570
|
+
const value = entry.value;
|
|
571
|
+
const key = `credential.${name}`;
|
|
572
|
+
if (value === undefined) {
|
|
573
|
+
throw new InvalidConfigError(key, `error: missing value for '${entry.key}'`);
|
|
574
|
+
}
|
|
575
|
+
if (name === "helper") {
|
|
576
|
+
if (value) c.helpers.push(value);
|
|
577
|
+
else c.helpers = [];
|
|
578
|
+
} else if (name === "username") {
|
|
579
|
+
if (!c.usernameFromProto) c.username = value;
|
|
580
|
+
} else if (name === "usehttppath") c.useHttpPath = bool(value);
|
|
581
|
+
else if (this.compatibility !== "2.43") {
|
|
582
|
+
if (name === "sanitizeprompt") c.sanitizePrompt = bool(value);
|
|
583
|
+
else if (name === "protectprotocol") c.protectProtocol = bool(value);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
c.configured = true;
|
|
587
|
+
if (!c.useHttpPath && (c.protocol === "http" || c.protocol === "https")) delete c.path;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/** `credential_do` + `run_credential_helper` */
|
|
591
|
+
private async runHelper(c: GitCredential, helper: string, operation: string): Promise<void> {
|
|
592
|
+
const program = helper.startsWith("!")
|
|
593
|
+
? helper.slice(1)
|
|
594
|
+
: helper.startsWith("/")
|
|
595
|
+
? helper
|
|
596
|
+
: `git credential-${helper}`;
|
|
597
|
+
const wantOutput = operation === "get";
|
|
598
|
+
const stdin = writeCredential(c, wantOutput ? "helper" : "response", this.compatibility);
|
|
599
|
+
const run = await this.options.run(`${program} ${operation}`, stdin);
|
|
600
|
+
if (!run) return;
|
|
601
|
+
// git reads the helper's stdout as it writes its stderr; ours come back after it exited
|
|
602
|
+
this.stderr(decoder.decode(run.stderr));
|
|
603
|
+
if (wantOutput) {
|
|
604
|
+
readCredential(c, decoder.decode(run.stdout), "helper", this.compatibility, (line) =>
|
|
605
|
+
this.stderr(line),
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/** A username and a password, or (2.46+) a `credential`: nothing more to ask for. */
|
|
611
|
+
private full(c: GitCredential): boolean {
|
|
612
|
+
return (
|
|
613
|
+
(c.username !== undefined && c.password !== undefined) ||
|
|
614
|
+
(this.compatibility !== "2.43" && c.credential !== undefined)
|
|
615
|
+
);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* `credential_fill` up to the prompt: asks the helpers in order until one completes the
|
|
620
|
+
* credential (a username and a password, 2.46+ also a `credential`).
|
|
621
|
+
*
|
|
622
|
+
* @param allCapabilities the caller takes every capability (http.c: true; 2.46+ only)
|
|
623
|
+
* @returns whether the credential is complete (false: git would prompt)
|
|
624
|
+
* @throws CredentialError when a helper said `quit` (`credential helper '<h>' told us to quit`)
|
|
625
|
+
*/
|
|
626
|
+
async fill(c: GitCredential, allCapabilities = false): Promise<boolean> {
|
|
627
|
+
if (this.full(c)) return true;
|
|
628
|
+
const modern = this.compatibility !== "2.43";
|
|
629
|
+
if (modern) {
|
|
630
|
+
nextState(c);
|
|
631
|
+
c.multistage = false;
|
|
632
|
+
}
|
|
633
|
+
this.applyConfig(c);
|
|
634
|
+
if (modern && allCapabilities) setAllCapabilities(c, "initial");
|
|
635
|
+
// the list as it stands after each helper (a `url=` answer clears it, as git's loop sees)
|
|
636
|
+
for (let i = 0; i < c.helpers.length; i++) {
|
|
637
|
+
const helper = c.helpers[i] as string;
|
|
638
|
+
await this.runHelper(c, helper, "get");
|
|
639
|
+
if (c.passwordExpiryUtc !== undefined && c.passwordExpiryUtc < this.nowSeconds()) {
|
|
640
|
+
if (modern) clearSecrets(c);
|
|
641
|
+
else delete c.password;
|
|
642
|
+
c.passwordExpiryUtc = undefined;
|
|
643
|
+
}
|
|
644
|
+
if (this.full(c)) {
|
|
645
|
+
if (modern) c.wwwauth = [];
|
|
646
|
+
return true;
|
|
647
|
+
}
|
|
648
|
+
if (c.quit) throw new CredentialError(`credential helper '${helper}' told us to quit`);
|
|
649
|
+
}
|
|
650
|
+
return false;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/** `credential_approve`: a complete, unexpired credential stored to every helper (`store`). */
|
|
654
|
+
async approve(c: GitCredential): Promise<void> {
|
|
655
|
+
if (c.approved) return;
|
|
656
|
+
if (!this.full(c)) return;
|
|
657
|
+
if (c.passwordExpiryUtc !== undefined && c.passwordExpiryUtc < this.nowSeconds()) return;
|
|
658
|
+
if (this.compatibility !== "2.43") nextState(c);
|
|
659
|
+
this.applyConfig(c);
|
|
660
|
+
for (let i = 0; i < c.helpers.length; i++)
|
|
661
|
+
await this.runHelper(c, c.helpers[i] as string, "store");
|
|
662
|
+
c.approved = true;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/** `credential_reject`: the credential erased from every helper (`erase`), then forgotten. */
|
|
666
|
+
async reject(c: GitCredential): Promise<void> {
|
|
667
|
+
if (this.compatibility !== "2.43") nextState(c);
|
|
668
|
+
this.applyConfig(c);
|
|
669
|
+
for (let i = 0; i < c.helpers.length; i++)
|
|
670
|
+
await this.runHelper(c, c.helpers[i] as string, "erase");
|
|
671
|
+
clearSecrets(c);
|
|
672
|
+
delete c.username;
|
|
673
|
+
delete c.oauthRefreshToken;
|
|
674
|
+
c.passwordExpiryUtc = undefined;
|
|
675
|
+
c.approved = false;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/** Options of {@link createConfigCredentialHelper}. */
|
|
680
|
+
export type ConfigCredentialHelperOptions = CredentialHelpersOptions;
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* An `HttpCredentialHelper` speaking git's helper protocol (`credential.helper`), as http.c
|
|
684
|
+
* drives its `http_auth`: a 401 starts a credential from the request's URL (its user, its
|
|
685
|
+
* `WWW-Authenticate` challenges) and fills it through the helpers (every capability, 2.46+);
|
|
686
|
+
* `approve` stores it, `reject` erases it, both with what the helpers said during the fill.
|
|
687
|
+
*/
|
|
688
|
+
export function createConfigCredentialHelper(
|
|
689
|
+
options: ConfigCredentialHelperOptions,
|
|
690
|
+
): HttpCredentialHelper {
|
|
691
|
+
const helpers = new CredentialHelpers(options);
|
|
692
|
+
const modern = (options.compatibility ?? "2.56") !== "2.43";
|
|
693
|
+
/** The credential of each base URL, as the last fill left it */
|
|
694
|
+
const states = new Map<string, GitCredential>();
|
|
695
|
+
const fresh = (request: CredentialRequest): GitCredential => {
|
|
696
|
+
const c = newCredential();
|
|
697
|
+
credentialFromUrl(c, request.url);
|
|
698
|
+
if (request.username !== undefined && c.username === undefined) {
|
|
699
|
+
c.username = request.username;
|
|
700
|
+
if (request.username) c.usernameFromProto = true;
|
|
701
|
+
}
|
|
702
|
+
c.wwwauth = [...request.wwwAuthenticate];
|
|
703
|
+
return c;
|
|
704
|
+
};
|
|
705
|
+
const withSecrets = (c: GitCredential, credentials: Credentials): GitCredential => {
|
|
706
|
+
if (credentials.username !== undefined) c.username = credentials.username;
|
|
707
|
+
if (credentials.password !== undefined) c.password = credentials.password;
|
|
708
|
+
if (credentials.token !== undefined && c.password === undefined) {
|
|
709
|
+
c.password = credentials.token;
|
|
710
|
+
c.username ??= "x-access-token";
|
|
711
|
+
}
|
|
712
|
+
if (modern && credentials.authtype !== undefined) c.authtype = credentials.authtype;
|
|
713
|
+
if (modern && credentials.credential !== undefined) c.credential = credentials.credential;
|
|
714
|
+
return c;
|
|
715
|
+
};
|
|
716
|
+
return {
|
|
717
|
+
async fill(request) {
|
|
718
|
+
const c = fresh(request);
|
|
719
|
+
states.set(request.url, c);
|
|
720
|
+
if (!(await helpers.fill(c, true))) return undefined;
|
|
721
|
+
return {
|
|
722
|
+
...(c.username !== undefined ? { username: c.username } : {}),
|
|
723
|
+
...(c.password !== undefined ? { password: c.password } : {}),
|
|
724
|
+
...(modern && c.authtype !== undefined && c.credential !== undefined
|
|
725
|
+
? { authtype: c.authtype, credential: c.credential }
|
|
726
|
+
: {}),
|
|
727
|
+
};
|
|
728
|
+
},
|
|
729
|
+
async approve(credentials, request) {
|
|
730
|
+
const c = states.get(request.url) ?? withSecrets(fresh(request), credentials);
|
|
731
|
+
// http.c's fwrite_wwwauth keeps the challenges of the last response only: the one that
|
|
732
|
+
// succeeded carries none
|
|
733
|
+
c.wwwauth = [];
|
|
734
|
+
await helpers.approve(c);
|
|
735
|
+
},
|
|
736
|
+
async reject(credentials, request) {
|
|
737
|
+
const c = states.get(request.url) ?? withSecrets(fresh(request), credentials);
|
|
738
|
+
states.delete(request.url);
|
|
739
|
+
// the challenges of the 401 that refused the credentials
|
|
740
|
+
c.wwwauth = [...request.wwwAuthenticate];
|
|
741
|
+
await helpers.reject(c);
|
|
742
|
+
},
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/** Options of {@link credentialHelpersOf} and {@link credentialHelperOf}. */
|
|
747
|
+
export interface CredentialHelperOfOptions {
|
|
748
|
+
/** The session the helpers run in (default: a new one over the working copy) */
|
|
749
|
+
hooks?: HookSession;
|
|
750
|
+
/** Where helpers' stderr and git's warnings go (default: the session's hook output) */
|
|
751
|
+
onStderr?(text: string): void;
|
|
752
|
+
/** The clock, in milliseconds (default `Date.now`) */
|
|
753
|
+
now?(): number;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* How {@link CredentialHelpers} reach a repository: its configuration's `credential.*`
|
|
758
|
+
* entries (command line included), its helpers run through the host's hook runner in the
|
|
759
|
+
* worktree's top (no runner: none runs), the protocol of the runner's compatibility.
|
|
760
|
+
*/
|
|
761
|
+
export function credentialHelpersOptionsOf(
|
|
762
|
+
workingCopy: WorkingCopy,
|
|
763
|
+
options: CredentialHelperOfOptions = {},
|
|
764
|
+
): CredentialHelpersOptions {
|
|
765
|
+
const hooks = options.hooks ?? new HookSession(workingCopy);
|
|
766
|
+
return {
|
|
767
|
+
entries: () => RemoteConfigStore.from(workingCopy).entriesWithPrefix("credential.") ?? [],
|
|
768
|
+
run: (command, stdin) => hooks.runCredentialHelper(command, stdin),
|
|
769
|
+
onStderr: options.onStderr ?? ((text) => hooks.append(text)),
|
|
770
|
+
compatibility: hookRunnerOf(workingCopy)?.compatibility ?? "2.56",
|
|
771
|
+
...(options.now ? { now: options.now } : {}),
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
/** git's credential.c over a repository (see {@link credentialHelpersOptionsOf}). */
|
|
776
|
+
export function credentialHelpersOf(
|
|
777
|
+
workingCopy: WorkingCopy,
|
|
778
|
+
options: CredentialHelperOfOptions = {},
|
|
779
|
+
): CredentialHelpers {
|
|
780
|
+
return new CredentialHelpers(credentialHelpersOptionsOf(workingCopy, options));
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
/**
|
|
784
|
+
* The credential helper git builds from a repository's configuration (`credential.helper`,
|
|
785
|
+
* `credential.<url>.*`, command line included): see {@link credentialHelpersOptionsOf} and
|
|
786
|
+
* {@link createConfigCredentialHelper}.
|
|
787
|
+
*
|
|
788
|
+
* @example
|
|
789
|
+
* ```ts
|
|
790
|
+
* await git.push().setCredentialHelper(credentialHelperOf(workingCopy)).call();
|
|
791
|
+
* ```
|
|
792
|
+
*/
|
|
793
|
+
export function credentialHelperOf(
|
|
794
|
+
workingCopy: WorkingCopy,
|
|
795
|
+
options: CredentialHelperOfOptions = {},
|
|
796
|
+
): HttpCredentialHelper {
|
|
797
|
+
return createConfigCredentialHelper(credentialHelpersOptionsOf(workingCopy, options));
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
/**
|
|
801
|
+
* Whether the configuration names any credential helper (`credential.helper` or
|
|
802
|
+
* `credential.<url>.helper`, empty resets included).
|
|
803
|
+
*/
|
|
804
|
+
export function configuresCredentialHelper(workingCopy: WorkingCopy): boolean {
|
|
805
|
+
const entries = RemoteConfigStore.from(workingCopy).entriesWithPrefix("credential.") ?? [];
|
|
806
|
+
return entries.some((e) => e.key === "credential.helper" || e.key.endsWith(".helper"));
|
|
807
|
+
}
|