@osdlc/typed-git 0.10.0 → 0.11.1
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 +25 -0
- package/README.md +87 -10
- package/dist/async.d.ts +17 -1
- package/dist/async.js +21 -0
- package/dist/backend.d.ts +27 -1
- package/dist/backend.js +33 -4
- package/dist/client.d.ts +3 -1
- package/dist/client.js +2 -0
- package/dist/failure.d.ts +18 -4
- package/dist/failure.js +4 -0
- package/dist/features.d.ts +91 -0
- package/dist/features.js +98 -0
- package/dist/git-url.d.ts +3 -0
- package/dist/git-url.js +14 -2
- package/dist/git-version.d.ts +90 -0
- package/dist/git-version.js +225 -0
- package/dist/soft.d.ts +1 -0
- package/dist/soft.js +1 -0
- package/dist/sync.d.ts +33 -3
- package/dist/sync.js +61 -3
- package/dist/verbs.d.ts +63 -2
- package/dist/verbs.js +117 -42
- package/package.json +1 -1
package/dist/features.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The git feature table: the one place that says which git release each
|
|
3
|
+
* version-dependent feature needs, and which of them have a fallback.
|
|
4
|
+
*
|
|
5
|
+
* A verb that uses one of these features names it in `needs` (see `GitVerb`
|
|
6
|
+
* in `./verbs.ts`). Before such a verb runs, the client probes the git version
|
|
7
|
+
* once per runner (`./git-version.ts`) and calls {@link versionedVerb}, which
|
|
8
|
+
* picks the verb's own form, its `fallback` form, or throws the
|
|
9
|
+
* `unsupported-git` failure. No other module compares versions.
|
|
10
|
+
*
|
|
11
|
+
* Every feature at or below {@link GIT_FLOOR} is used without a check. Below
|
|
12
|
+
* the floor itself, every version-gated verb fails as `unsupported-git` with
|
|
13
|
+
* the feature `typed-git`, and no fallback is tried.
|
|
14
|
+
*
|
|
15
|
+
* The README's "Git versions" table is checked against this one by
|
|
16
|
+
* `tests/features.test.ts`.
|
|
17
|
+
*/
|
|
18
|
+
import { FailureError } from '@osdlc/result';
|
|
19
|
+
import { describeGitFailure } from "./failure.js";
|
|
20
|
+
import { compareGitVersions, formatGitVersion } from "./git-version.js";
|
|
21
|
+
/**
|
|
22
|
+
* The oldest git this package supports: 2.34, the git of Ubuntu 22.04 LTS.
|
|
23
|
+
* Every flag typed-git passes unconditionally is at least this old.
|
|
24
|
+
*/
|
|
25
|
+
export const GIT_FLOOR = Object.freeze({ major: 2, minor: 34, patch: 0 });
|
|
26
|
+
const release = (major, minor) => Object.freeze({ major, minor, patch: 0 });
|
|
27
|
+
/**
|
|
28
|
+
* Every git feature newer than {@link GIT_FLOOR} that typed-git uses.
|
|
29
|
+
*
|
|
30
|
+
* - `merge-tree --write-tree`: git 2.38 ("git merge-tree" learned a new mode
|
|
31
|
+
* that computes the merge tree, RelNotes/2.38.0). `--name-only` and `-z`
|
|
32
|
+
* arrived with it.
|
|
33
|
+
* - `merge-tree --merge-base`: git 2.40 ("merge-tree" learns a new
|
|
34
|
+
* `--merge-base` option, RelNotes/2.40.0).
|
|
35
|
+
* - `worktree list -z`: git 2.36 (NUL-terminated `--porcelain` output, because
|
|
36
|
+
* paths and lock reasons with unsafe bytes were not C-quoted correctly,
|
|
37
|
+
* RelNotes/2.36.0).
|
|
38
|
+
*/
|
|
39
|
+
export const GIT_FEATURES = Object.freeze({
|
|
40
|
+
'merge-tree --write-tree': { minimum: release(2, 38), usedBy: ['mergeTree'] },
|
|
41
|
+
'merge-tree --merge-base': { minimum: release(2, 40), usedBy: ['mergeTree({ mergeBase })'] },
|
|
42
|
+
'worktree list -z': {
|
|
43
|
+
minimum: release(2, 36),
|
|
44
|
+
usedBy: ['worktrees'],
|
|
45
|
+
fallback: 'newline-split `worktree list --porcelain`: a path with a newline breaks it, and a lock reason comes back C-quoted',
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
/** True when `have` has `feature`. */
|
|
49
|
+
export function gitSupports(feature, have) {
|
|
50
|
+
return compareGitVersions(have, GIT_FEATURES[feature].minimum) >= 0;
|
|
51
|
+
}
|
|
52
|
+
/** `2.38` — a feature minimum as the README and the messages write it. */
|
|
53
|
+
function formatMinimum(v) {
|
|
54
|
+
return v.patch === 0 ? `${v.major}.${v.minor}` : formatGitVersion(v);
|
|
55
|
+
}
|
|
56
|
+
/** The classified failure for a git too old for `feature`. */
|
|
57
|
+
export function unsupportedGitFailure(feature, needs, have, argv) {
|
|
58
|
+
const want = formatMinimum(needs);
|
|
59
|
+
return {
|
|
60
|
+
kind: 'unsupported-git',
|
|
61
|
+
message: `${feature} needs git ${want} or later; found ${formatGitVersion(have)}.`,
|
|
62
|
+
hint: `Upgrade git to ${want} or later from https://git-scm.com.`,
|
|
63
|
+
detail: `git version ${have.text}`,
|
|
64
|
+
argv,
|
|
65
|
+
exitCode: null,
|
|
66
|
+
unsupported: { feature, needs, have },
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function throwUnsupported(feature, needs, have, argv) {
|
|
70
|
+
const failure = unsupportedGitFailure(feature, needs, have, argv);
|
|
71
|
+
const thrown = new FailureError(failure);
|
|
72
|
+
thrown.message = describeGitFailure(failure);
|
|
73
|
+
throw thrown;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The form of `verb` that the git at `have` can run: the verb itself when it
|
|
77
|
+
* needs nothing or `have` has its feature, its `fallback` when the feature has
|
|
78
|
+
* one in the table, otherwise a thrown `unsupported-git` failure. Below
|
|
79
|
+
* {@link GIT_FLOOR} a gated verb always throws.
|
|
80
|
+
*
|
|
81
|
+
* `prefix` is the repository binding the client adds, so the failure names
|
|
82
|
+
* the argv that would have run.
|
|
83
|
+
*/
|
|
84
|
+
export function versionedVerb(verb, have, prefix = []) {
|
|
85
|
+
const feature = verb.needs;
|
|
86
|
+
if (feature === undefined)
|
|
87
|
+
return verb;
|
|
88
|
+
const argv = [...prefix, ...verb.argv];
|
|
89
|
+
if (compareGitVersions(have, GIT_FLOOR) < 0)
|
|
90
|
+
throwUnsupported('typed-git', GIT_FLOOR, have, argv);
|
|
91
|
+
if (gitSupports(feature, have))
|
|
92
|
+
return verb;
|
|
93
|
+
const spec = GIT_FEATURES[feature];
|
|
94
|
+
if (spec.fallback !== undefined && verb.fallback !== undefined) {
|
|
95
|
+
return versionedVerb(verb.fallback, have, prefix);
|
|
96
|
+
}
|
|
97
|
+
return throwUnsupported(feature, spec.minimum, have, argv);
|
|
98
|
+
}
|
package/dist/git-url.d.ts
CHANGED
|
@@ -37,6 +37,9 @@ export interface RemoteRepo {
|
|
|
37
37
|
* `host/owner/repo`, scp-style or `scheme://`, where `owner` is the second to
|
|
38
38
|
* last path segment.
|
|
39
39
|
*
|
|
40
|
+
* `ssh.github.com` (GitHub's SSH over port 443) and `www.github.com` read as
|
|
41
|
+
* `github.com`.
|
|
42
|
+
*
|
|
40
43
|
* Use it to read a remote into the repository it names. To decide which forge
|
|
41
44
|
* a host is, compare `host`, never the URL text: `notgithub.com` is not
|
|
42
45
|
* GitHub, and neither is a path that mentions `github.com`.
|
package/dist/git-url.js
CHANGED
|
@@ -40,6 +40,9 @@ export function gitSshUrl(ref) {
|
|
|
40
40
|
* `host/owner/repo`, scp-style or `scheme://`, where `owner` is the second to
|
|
41
41
|
* last path segment.
|
|
42
42
|
*
|
|
43
|
+
* `ssh.github.com` (GitHub's SSH over port 443) and `www.github.com` read as
|
|
44
|
+
* `github.com`.
|
|
45
|
+
*
|
|
43
46
|
* Use it to read a remote into the repository it names. To decide which forge
|
|
44
47
|
* a host is, compare `host`, never the URL text: `notgithub.com` is not
|
|
45
48
|
* GitHub, and neither is a path that mentions `github.com`.
|
|
@@ -47,12 +50,21 @@ export function gitSshUrl(ref) {
|
|
|
47
50
|
export function parseRemoteRepo(url) {
|
|
48
51
|
const info = parseGitUrl(url);
|
|
49
52
|
if (info !== null && info.user && info.project) {
|
|
50
|
-
return { host: info.domain, owner: info.user, repo: info.project };
|
|
53
|
+
return { host: canonicalHost(info.domain), owner: info.user, repo: info.project };
|
|
51
54
|
}
|
|
52
55
|
const remote = parseRemoteUrl(url);
|
|
53
56
|
if (remote === null)
|
|
54
57
|
return null;
|
|
55
|
-
return { host: remote.host
|
|
58
|
+
return { host: canonicalHost(remote.host), owner: remote.owner, repo: remote.repo };
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* GitHub's other names for itself, read as `github.com`: `ssh.github.com` is
|
|
62
|
+
* its SSH-over-443 endpoint, and `www.github.com` redirects.
|
|
63
|
+
*/
|
|
64
|
+
const GITHUB_ALIASES = new Set(['ssh.github.com', 'www.github.com']);
|
|
65
|
+
function canonicalHost(host) {
|
|
66
|
+
const lower = host.toLowerCase();
|
|
67
|
+
return GITHUB_ALIASES.has(lower) ? 'github.com' : lower;
|
|
56
68
|
}
|
|
57
69
|
const SSH_INPUT = /^(?:ssh:\/\/|[A-Za-z0-9._-]+@)/;
|
|
58
70
|
const HTTP_INPUT = /^https?:\/\//i;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The git version: `git --version` parsed, and probed at most once per runner.
|
|
3
|
+
*
|
|
4
|
+
* Only a verb that declares `needs` (see `./features.ts`) asks for it, so a
|
|
5
|
+
* client that never runs such a verb never spawns `git --version`. The answer
|
|
6
|
+
* is cached against the runner object, so every client sharing a runner (a
|
|
7
|
+
* `Git` and its `at()` copies, every `AsyncGit` over one `cliBackend`) probes
|
|
8
|
+
* once between them.
|
|
9
|
+
*
|
|
10
|
+
* The cache key is the runner and the effective `PATH`, because a runner
|
|
11
|
+
* that finds git on `PATH` runs whichever git the call's `PATH` names.
|
|
12
|
+
*
|
|
13
|
+
* A failed probe is not cached: a missing git is reported on each call, as it
|
|
14
|
+
* is for any other verb.
|
|
15
|
+
*/
|
|
16
|
+
import { type AsyncCommandRunner, type CommandRunner, type EnvOverrides } from '@osdlc/command-seam';
|
|
17
|
+
import { type GitVerb } from './verbs.ts';
|
|
18
|
+
/**
|
|
19
|
+
* A git version, read from `git --version`.
|
|
20
|
+
*
|
|
21
|
+
* `major`, `minor` and `patch` are the leading numbers, so they compare as
|
|
22
|
+
* versions. Everything after them is in `text` only. A release candidate
|
|
23
|
+
* (`2.47.0.rc1`) reads as the release it leads to (2.47.0). A vendor build
|
|
24
|
+
* (`2.39.3 (Apple Git-146)`, `2.45.1.windows.1`) reads as the git release it
|
|
25
|
+
* is built from.
|
|
26
|
+
*/
|
|
27
|
+
export interface GitVersion {
|
|
28
|
+
major: number;
|
|
29
|
+
minor: number;
|
|
30
|
+
patch: number;
|
|
31
|
+
/** What git printed after `git version `, trimmed: `2.39.3 (Apple Git-146)`. */
|
|
32
|
+
text: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The version in one `git --version` output, or `null` when it is not one.
|
|
36
|
+
* Only the first line is read, so trailing build details do not matter.
|
|
37
|
+
*/
|
|
38
|
+
export declare function parseGitVersion(output: string): GitVersion | null;
|
|
39
|
+
/** Negative when `a` is older than `b`, zero when they are the same release, positive when newer. */
|
|
40
|
+
export declare function compareGitVersions(a: Pick<GitVersion, 'major' | 'minor' | 'patch'>, b: Pick<GitVersion, 'major' | 'minor' | 'patch'>): number;
|
|
41
|
+
/** `2.34.1` — the numbers only, without a vendor suffix. */
|
|
42
|
+
export declare function formatGitVersion(v: Pick<GitVersion, 'major' | 'minor' | 'patch'>): string;
|
|
43
|
+
/**
|
|
44
|
+
* `git --version`, parsed. Output that is not a version line is a failure,
|
|
45
|
+
* not a guess: the parse raises a `CommandFailed`, which `gate` classifies.
|
|
46
|
+
*/
|
|
47
|
+
export declare function versionVerb(): GitVerb<GitVersion>;
|
|
48
|
+
/**
|
|
49
|
+
* The time limit on the `git --version` probe itself. In the async client the
|
|
50
|
+
* probe is shared between concurrent callers, so no caller's own timeout or
|
|
51
|
+
* signal applies to it; each caller races its own against it instead.
|
|
52
|
+
*/
|
|
53
|
+
export declare const PROBE_TIMEOUT_MS = 15000;
|
|
54
|
+
/**
|
|
55
|
+
* What a probe call needs from the call that asked: its environment, its
|
|
56
|
+
* time limit and signal, and the argv a probe failure reports.
|
|
57
|
+
*/
|
|
58
|
+
export interface ProbeCall {
|
|
59
|
+
env?: EnvOverrides | undefined;
|
|
60
|
+
timeoutMs?: number | undefined;
|
|
61
|
+
signal?: AbortSignal | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* The argv the failure names when the probe fails: the gated verb's, with
|
|
64
|
+
* the client's repository flags, so a missing git reads `git worktree: …`
|
|
65
|
+
* rather than `git --version: …`. `['--version']` for `version()` itself.
|
|
66
|
+
*/
|
|
67
|
+
report: string[];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The version of the git `runner` runs, for the call's `PATH`: probed on the
|
|
71
|
+
* first call, then cached. A failed probe is not cached. The probe runs under
|
|
72
|
+
* the call's `timeoutMs`, or {@link PROBE_TIMEOUT_MS} when it has none, so the
|
|
73
|
+
* first gated call can take up to the probe's time plus its own `timeoutMs`.
|
|
74
|
+
*/
|
|
75
|
+
export declare function probeGitVersion(runner: CommandRunner, call: ProbeCall): GitVersion;
|
|
76
|
+
/**
|
|
77
|
+
* {@link probeGitVersion} for an async runner. Concurrent first calls with
|
|
78
|
+
* the same `PATH` share one probe, which runs with no caller's signal and a
|
|
79
|
+
* fixed {@link PROBE_TIMEOUT_MS} limit. Each caller waits for it under its own
|
|
80
|
+
* `timeoutMs` and `signal`, so a caller that gives up rejects alone and the
|
|
81
|
+
* probe goes on for the others. Each caller reads the shared result itself,
|
|
82
|
+
* so a failure names that caller's verb. A failed probe is not cached.
|
|
83
|
+
*/
|
|
84
|
+
export declare function probeGitVersionAsync(runner: AsyncCommandRunner, call: ProbeCall): Promise<GitVersion>;
|
|
85
|
+
/**
|
|
86
|
+
* The `gitVersion` client option, parsed: `'2.55.0'`, or `git --version`'s
|
|
87
|
+
* whole line (`'git version 2.39.3 (Apple Git-146)'`). `undefined` when the
|
|
88
|
+
* option is not set. Anything else throws a `RangeError`, at construction.
|
|
89
|
+
*/
|
|
90
|
+
export declare function pinnedGitVersion(value: string | undefined): GitVersion | undefined;
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The git version: `git --version` parsed, and probed at most once per runner.
|
|
3
|
+
*
|
|
4
|
+
* Only a verb that declares `needs` (see `./features.ts`) asks for it, so a
|
|
5
|
+
* client that never runs such a verb never spawns `git --version`. The answer
|
|
6
|
+
* is cached against the runner object, so every client sharing a runner (a
|
|
7
|
+
* `Git` and its `at()` copies, every `AsyncGit` over one `cliBackend`) probes
|
|
8
|
+
* once between them.
|
|
9
|
+
*
|
|
10
|
+
* The cache key is the runner and the effective `PATH`, because a runner
|
|
11
|
+
* that finds git on `PATH` runs whichever git the call's `PATH` names.
|
|
12
|
+
*
|
|
13
|
+
* A failed probe is not cached: a missing git is reported on each call, as it
|
|
14
|
+
* is for any other verb.
|
|
15
|
+
*/
|
|
16
|
+
import { tmpdir } from 'node:os';
|
|
17
|
+
import { CommandFailed, } from '@osdlc/command-seam';
|
|
18
|
+
import { gate } from "./verbs.js";
|
|
19
|
+
const VERSION_LINE = /^git version (\d+)\.(\d+)(?:\.(\d+))?\S*(?:\s.*)?$/;
|
|
20
|
+
/**
|
|
21
|
+
* The version in one `git --version` output, or `null` when it is not one.
|
|
22
|
+
* Only the first line is read, so trailing build details do not matter.
|
|
23
|
+
*/
|
|
24
|
+
export function parseGitVersion(output) {
|
|
25
|
+
const line = output.trim().split(/\r?\n/, 1)[0]?.trim() ?? '';
|
|
26
|
+
const m = VERSION_LINE.exec(line);
|
|
27
|
+
if (m === null)
|
|
28
|
+
return null;
|
|
29
|
+
return {
|
|
30
|
+
major: Number(m[1]),
|
|
31
|
+
minor: Number(m[2]),
|
|
32
|
+
patch: m[3] === undefined ? 0 : Number(m[3]),
|
|
33
|
+
text: line.slice('git version '.length),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** Negative when `a` is older than `b`, zero when they are the same release, positive when newer. */
|
|
37
|
+
export function compareGitVersions(a, b) {
|
|
38
|
+
return a.major - b.major || a.minor - b.minor || a.patch - b.patch;
|
|
39
|
+
}
|
|
40
|
+
/** `2.34.1` — the numbers only, without a vendor suffix. */
|
|
41
|
+
export function formatGitVersion(v) {
|
|
42
|
+
return `${v.major}.${v.minor}.${v.patch}`;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* `git --version`, parsed. Output that is not a version line is a failure,
|
|
46
|
+
* not a guess: the parse raises a `CommandFailed`, which `gate` classifies.
|
|
47
|
+
*/
|
|
48
|
+
export function versionVerb() {
|
|
49
|
+
return {
|
|
50
|
+
argv: ['--version'],
|
|
51
|
+
absent: [],
|
|
52
|
+
// `git version` is not translated today. The C locale keeps it that way.
|
|
53
|
+
env: { LC_ALL: 'C' },
|
|
54
|
+
parse(r, ranArgv) {
|
|
55
|
+
const version = parseGitVersion(r.stdout);
|
|
56
|
+
if (version === null) {
|
|
57
|
+
// Exit 0 with no version line has no git error to show, so say what
|
|
58
|
+
// was wrong in the failure's own words.
|
|
59
|
+
const first = r.stdout.trim().split(/\r?\n/, 1)[0] ?? '';
|
|
60
|
+
const said = first === '' ? 'nothing' : JSON.stringify(first);
|
|
61
|
+
throw new CommandFailed(ranArgv, {
|
|
62
|
+
...r,
|
|
63
|
+
stderr: r.stderr.trim() === '' ? `git --version printed ${said}, not a version` : r.stderr,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return version;
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The time limit on the `git --version` probe itself. In the async client the
|
|
72
|
+
* probe is shared between concurrent callers, so no caller's own timeout or
|
|
73
|
+
* signal applies to it; each caller races its own against it instead.
|
|
74
|
+
*/
|
|
75
|
+
export const PROBE_TIMEOUT_MS = 15_000;
|
|
76
|
+
/**
|
|
77
|
+
* Where the probe runs: the OS temp directory, which always exists. The
|
|
78
|
+
* answer is cached per runner and `PATH`, not per directory, so a caller's
|
|
79
|
+
* cwd must not decide it, and a missing cwd must not fail it for others. The
|
|
80
|
+
* verb that asked still runs in its own cwd, and reports a bad one itself.
|
|
81
|
+
*/
|
|
82
|
+
function probeCwd() {
|
|
83
|
+
return tmpdir();
|
|
84
|
+
}
|
|
85
|
+
/** The probe's run options: the caller's environment, the C locale, the fixed cwd and time limit. */
|
|
86
|
+
function probeOptions(env, verb, timeoutMs) {
|
|
87
|
+
return { cwd: probeCwd(), env: { ...env, ...verb.env }, timeoutMs };
|
|
88
|
+
}
|
|
89
|
+
/** `env[name]` from the environment the runner sees, without case on Windows (`Path`). */
|
|
90
|
+
function effective(env, name) {
|
|
91
|
+
const merged = { ...process.env, ...env };
|
|
92
|
+
if (process.platform !== 'win32')
|
|
93
|
+
return merged[name];
|
|
94
|
+
// The last matching key wins, as a later spread would.
|
|
95
|
+
let value;
|
|
96
|
+
for (const [key, v] of Object.entries(merged))
|
|
97
|
+
if (key.toUpperCase() === name)
|
|
98
|
+
value = v;
|
|
99
|
+
return value;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The cache key within one runner: the effective `PATH` (and `PATHEXT` on
|
|
103
|
+
* Windows), the call's `env` over the process's. An absolute-path-only runner
|
|
104
|
+
* looks `git` up on that `PATH`, so one runner can run two gits.
|
|
105
|
+
*/
|
|
106
|
+
function pathKey(env) {
|
|
107
|
+
const path = effective(env, 'PATH') ?? '\u0000unset';
|
|
108
|
+
return process.platform === 'win32' ? `${path}\u0000${effective(env, 'PATHEXT') ?? ''}` : path;
|
|
109
|
+
}
|
|
110
|
+
/** Per runner, per `PATH`: the version once known. */
|
|
111
|
+
const known = new WeakMap();
|
|
112
|
+
/** Per async runner, per `PATH`: the probe in flight, shared by concurrent callers. */
|
|
113
|
+
const inFlight = new WeakMap();
|
|
114
|
+
function knownFor(runner, key) {
|
|
115
|
+
return known.get(runner)?.get(key);
|
|
116
|
+
}
|
|
117
|
+
function remember(runner, key, version) {
|
|
118
|
+
let byPath = known.get(runner);
|
|
119
|
+
if (byPath === undefined)
|
|
120
|
+
known.set(runner, (byPath = new Map()));
|
|
121
|
+
byPath.set(key, version);
|
|
122
|
+
return version;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The version of the git `runner` runs, for the call's `PATH`: probed on the
|
|
126
|
+
* first call, then cached. A failed probe is not cached. The probe runs under
|
|
127
|
+
* the call's `timeoutMs`, or {@link PROBE_TIMEOUT_MS} when it has none, so the
|
|
128
|
+
* first gated call can take up to the probe's time plus its own `timeoutMs`.
|
|
129
|
+
*/
|
|
130
|
+
export function probeGitVersion(runner, call) {
|
|
131
|
+
const key = pathKey(call.env);
|
|
132
|
+
const cached = knownFor(runner, key);
|
|
133
|
+
if (cached !== undefined)
|
|
134
|
+
return cached;
|
|
135
|
+
const verb = versionVerb();
|
|
136
|
+
const timeoutMs = Math.min(call.timeoutMs ?? PROBE_TIMEOUT_MS, PROBE_TIMEOUT_MS);
|
|
137
|
+
const result = runner.run(verb.argv, probeOptions(call.env, verb, timeoutMs));
|
|
138
|
+
return remember(runner, key, gate(verb, result, call.report));
|
|
139
|
+
}
|
|
140
|
+
/** A result standing in for a probe this caller stopped waiting for. */
|
|
141
|
+
function stopped(reason) {
|
|
142
|
+
return {
|
|
143
|
+
exitCode: 1,
|
|
144
|
+
stdout: '',
|
|
145
|
+
stderr: '',
|
|
146
|
+
neverExited: true,
|
|
147
|
+
reason,
|
|
148
|
+
error: reason === 'abort'
|
|
149
|
+
? 'The operation was aborted'
|
|
150
|
+
: 'timed out waiting for the git --version probe',
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/** `probe`, or a stand-in result when the caller's own timeout or signal ends first. */
|
|
154
|
+
function race(probe, timeoutMs, signal) {
|
|
155
|
+
if (signal?.aborted === true)
|
|
156
|
+
return Promise.resolve(stopped('abort'));
|
|
157
|
+
const usable = typeof timeoutMs === 'number' && Number.isFinite(timeoutMs) && timeoutMs > 0
|
|
158
|
+
? Math.min(timeoutMs, 2 ** 31 - 1)
|
|
159
|
+
: undefined;
|
|
160
|
+
if (usable === undefined && signal === undefined)
|
|
161
|
+
return probe;
|
|
162
|
+
return new Promise((resolve, reject) => {
|
|
163
|
+
const timer = usable === undefined ? undefined : setTimeout(() => done(stopped('timeout')), usable);
|
|
164
|
+
const onAbort = () => done(stopped('abort'));
|
|
165
|
+
function done(result) {
|
|
166
|
+
if (timer !== undefined)
|
|
167
|
+
clearTimeout(timer);
|
|
168
|
+
signal?.removeEventListener('abort', onAbort);
|
|
169
|
+
resolve(result);
|
|
170
|
+
}
|
|
171
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
172
|
+
probe.then(done, (error) => {
|
|
173
|
+
if (timer !== undefined)
|
|
174
|
+
clearTimeout(timer);
|
|
175
|
+
signal?.removeEventListener('abort', onAbort);
|
|
176
|
+
reject(error);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* {@link probeGitVersion} for an async runner. Concurrent first calls with
|
|
182
|
+
* the same `PATH` share one probe, which runs with no caller's signal and a
|
|
183
|
+
* fixed {@link PROBE_TIMEOUT_MS} limit. Each caller waits for it under its own
|
|
184
|
+
* `timeoutMs` and `signal`, so a caller that gives up rejects alone and the
|
|
185
|
+
* probe goes on for the others. Each caller reads the shared result itself,
|
|
186
|
+
* so a failure names that caller's verb. A failed probe is not cached.
|
|
187
|
+
*/
|
|
188
|
+
export async function probeGitVersionAsync(runner, call) {
|
|
189
|
+
const key = pathKey(call.env);
|
|
190
|
+
const cached = knownFor(runner, key);
|
|
191
|
+
if (cached !== undefined)
|
|
192
|
+
return cached;
|
|
193
|
+
const verb = versionVerb();
|
|
194
|
+
let byPath = inFlight.get(runner);
|
|
195
|
+
if (byPath === undefined)
|
|
196
|
+
inFlight.set(runner, (byPath = new Map()));
|
|
197
|
+
let probe = byPath.get(key);
|
|
198
|
+
if (probe === undefined) {
|
|
199
|
+
const started = runner.run(verb.argv, probeOptions(call.env, verb, PROBE_TIMEOUT_MS));
|
|
200
|
+
probe = started;
|
|
201
|
+
byPath.set(key, started);
|
|
202
|
+
const settle = () => {
|
|
203
|
+
if (byPath.get(key) === started)
|
|
204
|
+
byPath.delete(key);
|
|
205
|
+
};
|
|
206
|
+
started.then(settle, settle);
|
|
207
|
+
}
|
|
208
|
+
const result = await race(probe, call.timeoutMs, call.signal);
|
|
209
|
+
return knownFor(runner, key) ?? remember(runner, key, gate(verb, result, call.report));
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* The `gitVersion` client option, parsed: `'2.55.0'`, or `git --version`'s
|
|
213
|
+
* whole line (`'git version 2.39.3 (Apple Git-146)'`). `undefined` when the
|
|
214
|
+
* option is not set. Anything else throws a `RangeError`, at construction.
|
|
215
|
+
*/
|
|
216
|
+
export function pinnedGitVersion(value) {
|
|
217
|
+
if (value === undefined)
|
|
218
|
+
return undefined;
|
|
219
|
+
const text = value.trim();
|
|
220
|
+
const version = parseGitVersion(text.startsWith('git version ') ? text : `git version ${text}`);
|
|
221
|
+
if (version === null) {
|
|
222
|
+
throw new RangeError(`gitVersion must be a git version such as '2.55.0'; got '${value}'`);
|
|
223
|
+
}
|
|
224
|
+
return version;
|
|
225
|
+
}
|
package/dist/soft.d.ts
CHANGED
package/dist/soft.js
CHANGED
package/dist/sync.d.ts
CHANGED
|
@@ -16,9 +16,10 @@
|
|
|
16
16
|
* service's compare-and-swap ref operations. That is not general git.
|
|
17
17
|
*/
|
|
18
18
|
import { type CommandResult, type CommandRunner } from '@osdlc/command-seam';
|
|
19
|
+
import { type GitVersion } from './git-version.ts';
|
|
19
20
|
import { type Soft, type TriedSync } from './outcome.ts';
|
|
20
21
|
import { type RemoteCoordinate } from './remote-url.ts';
|
|
21
|
-
import { type AddOptions, type AheadBehind, type CommitOptions, type CheckoutOptions, type ConfigGetOptions, type CloneOptions, type ConflictSide, type MergeOptions, type MergeOutcome, type MergeTreeOptions, type MergeTreeOutcome, type RebaseOptions, type RebaseOutcome, type RemoteGetUrlOptions, type TaggedFile, type UnmergedEntry, type ConfigSetOptions, type DiffEntry, type DiffNameOnlyOptions, type DiffStatOptions, type DiffNameStatusOptions, type DiffTextOptions, type DiffTreeOptions, type FetchOptions, type ForEachRefOptions, type GrepOptions, type InitOptions, type LogOptions, type IgnoreMatch, type LsFilesOptions, type LsRemoteOptions, type LsTreeOptions, type NumstatEntry, type PushPorcelainOptions, type PushReport, type RemoteRef, type CleanOptions, type DiffCheckOptions, type DiffCheckResult, type StatusEntry, type StatusPorcelainOptions, type TreeEntry, type Worktree, type WorktreeAddOptions, type WorktreeMoveOptions, type GcOptions, type PushOptions, type RemoteSetBranchesOptions, type RemoteSetHeadOptions } from './verbs.ts';
|
|
22
|
+
import { type AddOptions, type AheadBehind, type CherryEntry, type CommitTreeOptions, type CommitOptions, type CheckoutOptions, type ConfigGetOptions, type CloneOptions, type ConflictSide, type MergeOptions, type MergeOutcome, type MergeTreeOptions, type MergeTreeOutcome, type RebaseOptions, type RebaseOutcome, type RemoteGetUrlOptions, type TaggedFile, type UnmergedEntry, type ConfigSetOptions, type DiffEntry, type DiffNameOnlyOptions, type DiffStatOptions, type DiffNameStatusOptions, type DiffTextOptions, type DiffTreeOptions, type FetchOptions, type ForEachRefOptions, type GrepOptions, type InitOptions, type LogOptions, type IgnoreMatch, type LsFilesOptions, type LsRemoteOptions, type LsTreeOptions, type NumstatEntry, type PushPorcelainOptions, type PushReport, type RemoteRef, type CleanOptions, type DiffCheckOptions, type DiffCheckResult, type StatusEntry, type StatusPorcelainOptions, type TreeEntry, type Worktree, type WorktreeAddOptions, type WorktreeMoveOptions, type GcOptions, type PushOptions, type RemoteSetBranchesOptions, type RemoteSetHeadOptions } from './verbs.ts';
|
|
22
23
|
/**
|
|
23
24
|
* Per-call concerns that are not part of the operation itself.
|
|
24
25
|
*
|
|
@@ -50,6 +51,8 @@ export declare class Git {
|
|
|
50
51
|
private readonly boundGitDir;
|
|
51
52
|
private readonly workTree;
|
|
52
53
|
private readonly env;
|
|
54
|
+
private readonly gitVersionOption;
|
|
55
|
+
private readonly pinnedVersion;
|
|
53
56
|
private tried;
|
|
54
57
|
private softened;
|
|
55
58
|
/**
|
|
@@ -75,16 +78,23 @@ export declare class Git {
|
|
|
75
78
|
* Neither client has a default timeout. This client kills a child whose
|
|
76
79
|
* output on one stream passes 256 MB; `AsyncGit` has no output cap. The
|
|
77
80
|
* `maxOutputBytes` call option sets the cap per call on both.
|
|
81
|
+
*
|
|
82
|
+
* `gitVersion` pins the git version: `'2.55.0'`, or a whole
|
|
83
|
+
* `git version …` line. Version-gated verbs then use it and never run
|
|
84
|
+
* `git --version`, and {@link Git.version} returns it. Set it with a fake
|
|
85
|
+
* runner, so the fake answers only the verbs under test. An unparsable
|
|
86
|
+
* value throws a `RangeError` here.
|
|
78
87
|
*/
|
|
79
88
|
constructor(cwd: string, opts?: {
|
|
80
89
|
runner?: CommandRunner | undefined;
|
|
81
90
|
gitDir?: string | undefined;
|
|
82
91
|
workTree?: string | undefined;
|
|
83
92
|
env?: Record<string, string> | undefined;
|
|
93
|
+
gitVersion?: string | undefined;
|
|
84
94
|
});
|
|
85
95
|
/**
|
|
86
96
|
* The same client in a different directory, with the same runner,
|
|
87
|
-
* repository options
|
|
97
|
+
* repository options, client-level `env` and `gitVersion`.
|
|
88
98
|
*/
|
|
89
99
|
at(cwd: string): Git;
|
|
90
100
|
/**
|
|
@@ -127,8 +137,21 @@ export declare class Git {
|
|
|
127
137
|
/** The verb's argv, after this client's `--git-dir` / `--work-tree` flags. */
|
|
128
138
|
private argv;
|
|
129
139
|
private runOpts;
|
|
130
|
-
/**
|
|
140
|
+
/**
|
|
141
|
+
* Run one verb from the table and check its result with `gate`. A verb that
|
|
142
|
+
* `needs` a git feature first gets the form this runner's git can run (see
|
|
143
|
+
* `versionedVerb` in `features.ts`), which probes the version once.
|
|
144
|
+
*/
|
|
131
145
|
private exec;
|
|
146
|
+
/**
|
|
147
|
+
* The git this client's runner runs, from `git --version`. Probed on the
|
|
148
|
+
* first call (or the first version-gated verb) and cached for the runner,
|
|
149
|
+
* so a client that never asks never spawns it. Throws the classified
|
|
150
|
+
* failure when git cannot run (`not-installed`), and an `exit` failure when
|
|
151
|
+
* the output is not a version line. With the `gitVersion` option it is that
|
|
152
|
+
* version, and nothing runs.
|
|
153
|
+
*/
|
|
154
|
+
version(opts?: GitSyncCallOptions): GitVersion;
|
|
132
155
|
/** A file's content at a rev (`git show <rev>:<relPath>`), null if absent. */
|
|
133
156
|
showAtRev(rev: string, relPath: string, opts?: GitSyncCallOptions): string | null;
|
|
134
157
|
/**
|
|
@@ -318,6 +341,13 @@ export declare class Git {
|
|
|
318
341
|
isAncestor(a: string, b: string, opts?: GitSyncCallOptions): boolean;
|
|
319
342
|
/** How far `tip` has diverged from `base`. See {@link AheadBehind}. */
|
|
320
343
|
aheadBehind(base: string, tip: string, opts?: GitSyncCallOptions): AheadBehind;
|
|
344
|
+
/**
|
|
345
|
+
* Commits on `head` (default `HEAD`) that `upstream` lacks, each marked by
|
|
346
|
+
* whether `upstream` has the same patch. See {@link CherryEntry}.
|
|
347
|
+
*/
|
|
348
|
+
cherry(upstream: string, head?: string, opts?: GitSyncCallOptions): CherryEntry[];
|
|
349
|
+
/** Write a commit for `tree` and return its sha; no ref moves. Needs a git identity. */
|
|
350
|
+
commitTree(tree: string, commitOpts: CommitTreeOptions, opts?: GitSyncCallOptions): string;
|
|
321
351
|
/** Configured remote names (`git remote`); `[]` when there are none. */
|
|
322
352
|
remotes(opts?: GitSyncCallOptions): string[];
|
|
323
353
|
/**
|