@osdlc/typed-git 0.8.1 → 0.10.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 +45 -0
- package/README.md +42 -9
- package/SECURITY.md +4 -2
- package/dist/async.d.ts +18 -12
- package/dist/async.js +45 -13
- package/dist/backend.d.ts +34 -12
- package/dist/backend.js +18 -6
- package/dist/checkout-root.d.ts +2 -1
- package/dist/checkout-root.js +7 -1
- package/dist/client.d.ts +1 -1
- package/dist/failure.js +6 -17
- package/dist/git-url.d.ts +24 -0
- package/dist/git-url.js +43 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +4 -3
- package/dist/outcome.d.ts +7 -2
- package/dist/outcome.js +9 -45
- package/dist/remote-url.d.ts +2 -2
- package/dist/remote-url.js +7 -3
- package/dist/soft.d.ts +1 -0
- package/dist/soft.js +1 -0
- package/dist/sync.d.ts +40 -15
- package/dist/sync.js +63 -10
- package/dist/verbs.d.ts +136 -8
- package/dist/verbs.js +146 -20
- package/package.json +2 -2
package/dist/failure.js
CHANGED
|
@@ -8,20 +8,15 @@
|
|
|
8
8
|
* kind, so an unrecognised failure is still an `exit` with git's first stderr
|
|
9
9
|
* line as its message, never a wrong git kind.
|
|
10
10
|
*/
|
|
11
|
-
import {
|
|
11
|
+
import { classifyFailed, isCommandFailed, } from '@osdlc/command-seam';
|
|
12
12
|
import { describe, FailureError, isFailure } from '@osdlc/result';
|
|
13
13
|
const INSTALL_HINT = 'Install git from https://git-scm.com.';
|
|
14
14
|
/** The git failure for a failed `git` run. */
|
|
15
15
|
export function classifyGitFailure(failed) {
|
|
16
16
|
// The client's runner may not name its binary; this is git either way, so
|
|
17
17
|
// the messages say `git` rather than "The command".
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
: failed.failure;
|
|
21
|
-
const base = { ...f, cause: failed };
|
|
22
|
-
if (f.kind === 'not-installed')
|
|
23
|
-
return { ...base, hint: INSTALL_HINT };
|
|
24
|
-
if (f.kind !== 'exit')
|
|
18
|
+
const base = classifyFailed(failed, { binary: 'git', installHint: INSTALL_HINT });
|
|
19
|
+
if (base.kind !== 'exit')
|
|
25
20
|
return base;
|
|
26
21
|
// git's own words: stderr, then the stdout some commands explain with.
|
|
27
22
|
const text = `${failed.result.stderr}\n${failed.result.stdout}`;
|
|
@@ -45,18 +40,12 @@ export function classifyGitFailure(failed) {
|
|
|
45
40
|
hint: 'List the remotes with `git remote -v`.',
|
|
46
41
|
};
|
|
47
42
|
}
|
|
48
|
-
if (/Authentication failed|Permission denied \(publickey\)|could not read (Username|Password)/i.test(text)) {
|
|
49
|
-
return {
|
|
50
|
-
...base,
|
|
51
|
-
kind: 'not-authenticated',
|
|
52
|
-
message: 'The remote refused the credentials.',
|
|
53
|
-
hint: 'Check that you are signed in to the host, or that your SSH key is added.',
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
43
|
// "unable to access" with a status means the host answered. A refusal or a
|
|
57
44
|
// wrong URL will not clear up by retrying, so it must not read as unreachable.
|
|
58
45
|
const status = /unable to access[^\n]*returned error: (\d{3})/.exec(text)?.[1];
|
|
59
|
-
if (
|
|
46
|
+
if (/Authentication failed|Permission denied \(publickey\)|could not read (Username|Password)/i.test(text) ||
|
|
47
|
+
status === '401' ||
|
|
48
|
+
status === '403') {
|
|
60
49
|
return {
|
|
61
50
|
...base,
|
|
62
51
|
kind: 'not-authenticated',
|
package/dist/git-url.d.ts
CHANGED
|
@@ -18,6 +18,30 @@ export interface GitHostInfo {
|
|
|
18
18
|
export declare function parseGitUrl(ref: string): GitHostInfo | null;
|
|
19
19
|
/** `ref` as a scp-style SSH clone URL (`git@github.com:owner/repo.git`), or null. */
|
|
20
20
|
export declare function gitSshUrl(ref: string): string | null;
|
|
21
|
+
/** The repository a git remote names, and the host it is on. */
|
|
22
|
+
export interface RemoteRepo {
|
|
23
|
+
/** The host, lower-cased and without a port: `github.com`, `git.example.com`. */
|
|
24
|
+
host: string;
|
|
25
|
+
owner: string;
|
|
26
|
+
repo: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* `{host, owner, repo}` from a git remote reference, or null when it names no
|
|
30
|
+
* project (a local path, a single path segment).
|
|
31
|
+
*
|
|
32
|
+
* It reads a known host (GitHub, GitLab, Bitbucket, Sourcehut) with
|
|
33
|
+
* {@link parseGitUrl} first, so `owner/repo` and `github:owner/repo`
|
|
34
|
+
* shorthand read as GitHub and a GitLab nested group stays whole in `owner`
|
|
35
|
+
* (`group/sub`). Any other clone URL, such as a GitHub Enterprise or a
|
|
36
|
+
* self-hosted Forgejo remote, is read host-agnostically by `parseRemoteUrl`:
|
|
37
|
+
* `host/owner/repo`, scp-style or `scheme://`, where `owner` is the second to
|
|
38
|
+
* last path segment.
|
|
39
|
+
*
|
|
40
|
+
* Use it to read a remote into the repository it names. To decide which forge
|
|
41
|
+
* a host is, compare `host`, never the URL text: `notgithub.com` is not
|
|
42
|
+
* GitHub, and neither is a path that mentions `github.com`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function parseRemoteRepo(url: string): RemoteRepo | null;
|
|
21
45
|
export type CloneProtocol = 'https' | 'ssh';
|
|
22
46
|
/**
|
|
23
47
|
* A hosted repository named by any reference a person might paste: one
|
package/dist/git-url.js
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
// oxlint-disable-next-line typescript/triple-slash-reference
|
|
11
11
|
/// <reference path="./hosted-git-info.d.ts" />
|
|
12
12
|
import hostedGitInfo from 'hosted-git-info';
|
|
13
|
+
import { parseRemoteUrl } from "./remote-url.js";
|
|
13
14
|
/** Parse `ref`, or null when it doesn't match a known git host. */
|
|
14
15
|
export function parseGitUrl(ref) {
|
|
15
16
|
const info = hostedGitInfo.fromUrl(ref);
|
|
@@ -27,6 +28,32 @@ export function parseGitUrl(ref) {
|
|
|
27
28
|
export function gitSshUrl(ref) {
|
|
28
29
|
return hostedGitInfo.fromUrl(ref)?.ssh() ?? null;
|
|
29
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* `{host, owner, repo}` from a git remote reference, or null when it names no
|
|
33
|
+
* project (a local path, a single path segment).
|
|
34
|
+
*
|
|
35
|
+
* It reads a known host (GitHub, GitLab, Bitbucket, Sourcehut) with
|
|
36
|
+
* {@link parseGitUrl} first, so `owner/repo` and `github:owner/repo`
|
|
37
|
+
* shorthand read as GitHub and a GitLab nested group stays whole in `owner`
|
|
38
|
+
* (`group/sub`). Any other clone URL, such as a GitHub Enterprise or a
|
|
39
|
+
* self-hosted Forgejo remote, is read host-agnostically by `parseRemoteUrl`:
|
|
40
|
+
* `host/owner/repo`, scp-style or `scheme://`, where `owner` is the second to
|
|
41
|
+
* last path segment.
|
|
42
|
+
*
|
|
43
|
+
* Use it to read a remote into the repository it names. To decide which forge
|
|
44
|
+
* a host is, compare `host`, never the URL text: `notgithub.com` is not
|
|
45
|
+
* GitHub, and neither is a path that mentions `github.com`.
|
|
46
|
+
*/
|
|
47
|
+
export function parseRemoteRepo(url) {
|
|
48
|
+
const info = parseGitUrl(url);
|
|
49
|
+
if (info !== null && info.user && info.project) {
|
|
50
|
+
return { host: info.domain, owner: info.user, repo: info.project };
|
|
51
|
+
}
|
|
52
|
+
const remote = parseRemoteUrl(url);
|
|
53
|
+
if (remote === null)
|
|
54
|
+
return null;
|
|
55
|
+
return { host: remote.host.toLowerCase(), owner: remote.owner, repo: remote.repo };
|
|
56
|
+
}
|
|
30
57
|
const SSH_INPUT = /^(?:ssh:\/\/|[A-Za-z0-9._-]+@)/;
|
|
31
58
|
const HTTP_INPUT = /^https?:\/\//i;
|
|
32
59
|
/**
|
|
@@ -47,7 +74,7 @@ export function parseRepoRef(input, protocol = 'https') {
|
|
|
47
74
|
const text = input.trim();
|
|
48
75
|
if (text === '')
|
|
49
76
|
return null;
|
|
50
|
-
const info = parseGitUrl(text) ??
|
|
77
|
+
const info = parseGitUrl(text) ?? parseWebPage(text);
|
|
51
78
|
if (info === null || !info.user || !info.project)
|
|
52
79
|
return null;
|
|
53
80
|
const { domain: host, user: owner, project: repo } = info;
|
|
@@ -59,6 +86,21 @@ export function parseRepoRef(input, protocol = 'https') {
|
|
|
59
86
|
const cloneUrl = kind === 'ssh' ? `git@${host}:${owner}/${repo}.git` : `https://${host}/${owner}/${repo}.git`;
|
|
60
87
|
return { host, owner, repo, cloneUrl, committish: info.committish };
|
|
61
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* A web page under a project, read as the project. A GitLab page
|
|
91
|
+
* (`https://host/group/sub/repo/-/…`) is first cut at its `/-/`, which keeps a
|
|
92
|
+
* nested group whole. When that names no project (a GitHub path can hold a `-`
|
|
93
|
+
* segment too), the page is cut to its first two path segments instead.
|
|
94
|
+
*/
|
|
95
|
+
function parseWebPage(text) {
|
|
96
|
+
const gitlab = /^(https?:\/\/[^/?#]+(?:\/[^/?#]+){2,}?)\/-(?:[/?#].*)?$/i.exec(text);
|
|
97
|
+
if (gitlab !== null) {
|
|
98
|
+
const info = parseGitUrl(gitlab[1]);
|
|
99
|
+
if (info !== null && info.user && info.project)
|
|
100
|
+
return info;
|
|
101
|
+
}
|
|
102
|
+
return parseGitUrl(trimWebPath(text));
|
|
103
|
+
}
|
|
62
104
|
/** `https://host/a/b/anything…` → `https://host/a/b`; other inputs unchanged. */
|
|
63
105
|
function trimWebPath(text) {
|
|
64
106
|
const m = /^(https?:\/\/[^/?#]+\/[^/?#]+\/[^/?#]+)(?:[/?#].*)?$/i.exec(text);
|
package/dist/index.d.ts
CHANGED
|
@@ -24,14 +24,15 @@
|
|
|
24
24
|
*
|
|
25
25
|
* The verb table in `./verbs.ts` is the single definition of every argv,
|
|
26
26
|
* every absence code and every parser, so the two clients cannot drift apart.
|
|
27
|
-
* The
|
|
28
|
-
* `
|
|
27
|
+
* The exceptions are helpers that compose other verbs (`remoteOwnerRepo`,
|
|
28
|
+
* `hasHistory`, `mainCheckout`, `isDirty`, `branchExists`, `refs`,
|
|
29
|
+
* `logLines`). Each client writes them, so a change must be made in both.
|
|
29
30
|
*
|
|
30
31
|
* Subpath exports: `./client` is everything here except the git-reference
|
|
31
32
|
* parsers and the checkout walks (and so without `hosted-git-info` on the
|
|
32
33
|
* module graph); `./testing` is the temp-repo fixture builder.
|
|
33
34
|
*/
|
|
34
35
|
export * from './client.ts';
|
|
35
|
-
export { parseGitUrl, gitSshUrl, parseRepoRef, type GitHostInfo, type RepoRef, type CloneProtocol, } from './git-url.ts';
|
|
36
|
+
export { parseGitUrl, gitSshUrl, parseRemoteRepo, parseRepoRef, type GitHostInfo, type RemoteRepo, type RepoRef, type CloneProtocol, } from './git-url.ts';
|
|
36
37
|
export { checkoutRoot, discoverCheckouts, scanForCheckouts, DEFAULT_MAX_DEPTH, DEFAULT_WARN_AFTER, } from './checkout-root.ts';
|
|
37
38
|
export type { CheckoutScan, DiscoverCheckoutsOptions } from './checkout-root.ts';
|
package/dist/index.js
CHANGED
|
@@ -24,13 +24,14 @@
|
|
|
24
24
|
*
|
|
25
25
|
* The verb table in `./verbs.ts` is the single definition of every argv,
|
|
26
26
|
* every absence code and every parser, so the two clients cannot drift apart.
|
|
27
|
-
* The
|
|
28
|
-
* `
|
|
27
|
+
* The exceptions are helpers that compose other verbs (`remoteOwnerRepo`,
|
|
28
|
+
* `hasHistory`, `mainCheckout`, `isDirty`, `branchExists`, `refs`,
|
|
29
|
+
* `logLines`). Each client writes them, so a change must be made in both.
|
|
29
30
|
*
|
|
30
31
|
* Subpath exports: `./client` is everything here except the git-reference
|
|
31
32
|
* parsers and the checkout walks (and so without `hosted-git-info` on the
|
|
32
33
|
* module graph); `./testing` is the temp-repo fixture builder.
|
|
33
34
|
*/
|
|
34
35
|
export * from "./client.js";
|
|
35
|
-
export { parseGitUrl, gitSshUrl, parseRepoRef, } from "./git-url.js";
|
|
36
|
+
export { parseGitUrl, gitSshUrl, parseRemoteRepo, parseRepoRef, } from "./git-url.js";
|
|
36
37
|
export { checkoutRoot, discoverCheckouts, scanForCheckouts, DEFAULT_MAX_DEPTH, DEFAULT_WARN_AFTER, } from "./checkout-root.js";
|
package/dist/outcome.d.ts
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* is `{ ok: true, value: null }`, because absence is an answer, not a
|
|
18
18
|
* failure.
|
|
19
19
|
*/
|
|
20
|
-
import {
|
|
20
|
+
import type { Result } from '@osdlc/result';
|
|
21
21
|
import { type GitFailure } from './failure.ts';
|
|
22
22
|
/**
|
|
23
23
|
* The methods `try` mirrors: everything except `raw`, `rawChecked`, `at`,
|
|
@@ -35,7 +35,12 @@ export type TriedSync<C> = {
|
|
|
35
35
|
export type TriedAsync<C> = {
|
|
36
36
|
[K in keyof Triable<C>]: Triable<C>[K] extends (...a: infer A) => Promise<infer R> ? (...a: A) => Promise<Result<R, GitFailure>> : never;
|
|
37
37
|
};
|
|
38
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* Wrap a synchronous client so every method returns a `Result<T, GitFailure>`.
|
|
40
|
+
* The proxy is command-seam's `tryMethods`: it mirrors whatever the client
|
|
41
|
+
* has, with no per-verb entry to keep in step, and runs each method on the
|
|
42
|
+
* client itself, so private state stays reachable.
|
|
43
|
+
*/
|
|
39
44
|
export declare function triedSync<C extends object>(client: C): TriedSync<C>;
|
|
40
45
|
/** Wrap an awaitable client so every method promises a `Result<T, GitFailure>`. */
|
|
41
46
|
export declare function triedAsync<C extends object>(client: C): TriedAsync<C>;
|
package/dist/outcome.js
CHANGED
|
@@ -17,56 +17,20 @@
|
|
|
17
17
|
* is `{ ok: true, value: null }`, because absence is an answer, not a
|
|
18
18
|
* failure.
|
|
19
19
|
*/
|
|
20
|
-
import {
|
|
20
|
+
import { methodFacade, tryMethods, tryMethodsAsync } from '@osdlc/command-seam';
|
|
21
21
|
import { thrownGitFailure } from "./failure.js";
|
|
22
|
-
/** A thrown git failure becomes an `err`; anything else is thrown again. */
|
|
23
|
-
function asFailure(error) {
|
|
24
|
-
const failure = thrownGitFailure(error);
|
|
25
|
-
if (failure !== null)
|
|
26
|
-
return err(failure);
|
|
27
|
-
throw error;
|
|
28
|
-
}
|
|
29
22
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* per-verb entry to keep in step
|
|
33
|
-
* client itself,
|
|
23
|
+
* Wrap a synchronous client so every method returns a `Result<T, GitFailure>`.
|
|
24
|
+
* The proxy is command-seam's `tryMethods`: it mirrors whatever the client
|
|
25
|
+
* has, with no per-verb entry to keep in step, and runs each method on the
|
|
26
|
+
* client itself, so private state stays reachable.
|
|
34
27
|
*/
|
|
35
|
-
function facade(client, wrap) {
|
|
36
|
-
return new Proxy(client, {
|
|
37
|
-
get(target, prop) {
|
|
38
|
-
// Read from the target, not the proxy: a getter (`try` itself) must run
|
|
39
|
-
// against the real client rather than recursing through the proxy.
|
|
40
|
-
const value = Reflect.get(target, prop);
|
|
41
|
-
if (typeof value !== 'function')
|
|
42
|
-
return value;
|
|
43
|
-
const method = value;
|
|
44
|
-
const name = String(prop);
|
|
45
|
-
return (...args) => wrap(() => method.apply(target, args), name);
|
|
46
|
-
},
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
/** Wrap a synchronous client so every method returns a `Result<T, GitFailure>`. */
|
|
50
28
|
export function triedSync(client) {
|
|
51
|
-
return
|
|
52
|
-
try {
|
|
53
|
-
return ok(call());
|
|
54
|
-
}
|
|
55
|
-
catch (error) {
|
|
56
|
-
return asFailure(error);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
29
|
+
return tryMethods(client, thrownGitFailure);
|
|
59
30
|
}
|
|
60
31
|
/** Wrap an awaitable client so every method promises a `Result<T, GitFailure>`. */
|
|
61
32
|
export function triedAsync(client) {
|
|
62
|
-
return
|
|
63
|
-
try {
|
|
64
|
-
return ok(await call());
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
return asFailure(error);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
33
|
+
return tryMethodsAsync(client, thrownGitFailure);
|
|
70
34
|
}
|
|
71
35
|
function fallbackFor(table, name) {
|
|
72
36
|
const kind = table[name];
|
|
@@ -77,7 +41,7 @@ function fallbackFor(table, name) {
|
|
|
77
41
|
}
|
|
78
42
|
/** Wrap a synchronous client so a failed call returns the verb's absence value. */
|
|
79
43
|
export function softSync(client, table) {
|
|
80
|
-
return
|
|
44
|
+
return methodFacade(client, (call, name) => {
|
|
81
45
|
try {
|
|
82
46
|
return call();
|
|
83
47
|
}
|
|
@@ -90,7 +54,7 @@ export function softSync(client, table) {
|
|
|
90
54
|
}
|
|
91
55
|
/** Wrap an awaitable client so a failed call resolves to the verb's absence value. */
|
|
92
56
|
export function softAsync(client, table) {
|
|
93
|
-
return
|
|
57
|
+
return methodFacade(client, async (call, name) => {
|
|
94
58
|
try {
|
|
95
59
|
return await call();
|
|
96
60
|
}
|
package/dist/remote-url.d.ts
CHANGED
|
@@ -20,8 +20,8 @@ export interface RemoteCoordinate {
|
|
|
20
20
|
* Parses `git@host:owner/repo.git` (scp-style) and
|
|
21
21
|
* `scheme://[user@]host[:port]/owner/repo[.git]`. The last two path segments
|
|
22
22
|
* are the owner and repo, so a nested group (`group/sub/repo`) answers
|
|
23
|
-
* `owner: "sub"`. Returns null for anything else (a local path
|
|
24
|
-
* path segment).
|
|
23
|
+
* `owner: "sub"`. Returns null for anything else (a local path or a
|
|
24
|
+
* `file://` URL, a single path segment).
|
|
25
25
|
*
|
|
26
26
|
* Only an `http(s)://` port is read. An `ssh://` port is dropped, so `port` is
|
|
27
27
|
* null for `ssh://git@host:2222/owner/repo.git`.
|
package/dist/remote-url.js
CHANGED
|
@@ -12,14 +12,17 @@
|
|
|
12
12
|
* Parses `git@host:owner/repo.git` (scp-style) and
|
|
13
13
|
* `scheme://[user@]host[:port]/owner/repo[.git]`. The last two path segments
|
|
14
14
|
* are the owner and repo, so a nested group (`group/sub/repo`) answers
|
|
15
|
-
* `owner: "sub"`. Returns null for anything else (a local path
|
|
16
|
-
* path segment).
|
|
15
|
+
* `owner: "sub"`. Returns null for anything else (a local path or a
|
|
16
|
+
* `file://` URL, a single path segment).
|
|
17
17
|
*
|
|
18
18
|
* Only an `http(s)://` port is read. An `ssh://` port is dropped, so `port` is
|
|
19
19
|
* null for `ssh://git@host:2222/owner/repo.git`.
|
|
20
20
|
*/
|
|
21
21
|
export function parseRemoteUrl(url) {
|
|
22
22
|
const trimmed = url.trim();
|
|
23
|
+
// `file://localhost/srv/git/a/b.git` is a local path, not a forge coordinate.
|
|
24
|
+
if (/^file:/i.test(trimmed))
|
|
25
|
+
return null;
|
|
23
26
|
const scp = /^[\w.-]+@([^:/]+):(.+)$/.exec(trimmed);
|
|
24
27
|
const uri = /^[a-z][a-z0-9+.-]*:\/\/(?:[^@/]+@)?([^/:]+)(?::(\d+))?\/(.+)$/i.exec(trimmed);
|
|
25
28
|
const match = scp ?? uri;
|
|
@@ -30,7 +33,8 @@ export function parseRemoteUrl(url) {
|
|
|
30
33
|
const isHttp = match === uri && /^https?:/i.test(trimmed);
|
|
31
34
|
const port = isHttp && match[2] !== undefined ? Number(match[2]) : null;
|
|
32
35
|
const rest = match === uri ? match[3] : match[2];
|
|
33
|
-
|
|
36
|
+
// Trim the slashes first, so `repo.git/` loses its `.git` too.
|
|
37
|
+
const path = rest.replace(/^\/+|\/+$/g, '').replace(/\.git$/, '');
|
|
34
38
|
const parts = path.split('/').filter((p) => p !== '');
|
|
35
39
|
if (parts.length < 2)
|
|
36
40
|
return null;
|
package/dist/soft.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export declare const SOFT_TABLE: {
|
|
|
22
22
|
readonly statusPorcelain: "empty";
|
|
23
23
|
readonly statusEntries: "empty";
|
|
24
24
|
readonly isDirty: "false";
|
|
25
|
+
readonly hasStagedChanges: "false";
|
|
25
26
|
readonly branches: "empty";
|
|
26
27
|
readonly branchExists: "false";
|
|
27
28
|
readonly worktrees: "empty";
|
package/dist/soft.js
CHANGED
package/dist/sync.d.ts
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
import { type CommandResult, type CommandRunner } from '@osdlc/command-seam';
|
|
19
19
|
import { type Soft, type TriedSync } from './outcome.ts';
|
|
20
20
|
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
|
|
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
22
|
/**
|
|
23
23
|
* Per-call concerns that are not part of the operation itself.
|
|
24
24
|
*
|
|
@@ -68,7 +68,9 @@ export declare class Git {
|
|
|
68
68
|
* `backend.ts`.
|
|
69
69
|
*
|
|
70
70
|
* This client takes a `runner`, where `AsyncGit` takes a `backend`. The
|
|
71
|
-
* default runner spawns `git
|
|
71
|
+
* default runner spawns the `git` found on an absolute `PATH` entry, so a
|
|
72
|
+
* `git` file in the checkout cannot run in its place. Tests pass a fake
|
|
73
|
+
* `CommandRunner`.
|
|
72
74
|
*
|
|
73
75
|
* Neither client has a default timeout. This client kills a child whose
|
|
74
76
|
* output on one stream passes 256 MB; `AsyncGit` has no output cap. The
|
|
@@ -129,6 +131,11 @@ export declare class Git {
|
|
|
129
131
|
private exec;
|
|
130
132
|
/** A file's content at a rev (`git show <rev>:<relPath>`), null if absent. */
|
|
131
133
|
showAtRev(rev: string, relPath: string, opts?: GitSyncCallOptions): string | null;
|
|
134
|
+
/**
|
|
135
|
+
* A file's content at a rev, throwing when the path is missing. Use it when
|
|
136
|
+
* a missing file is an error. See `showAtRevChecked` in `verbs.ts`.
|
|
137
|
+
*/
|
|
138
|
+
showAtRevChecked(rev: string, relPath: string, opts?: GitSyncCallOptions): string;
|
|
132
139
|
/**
|
|
133
140
|
* Trimmed `git rev-parse <args>` output, for a query no other verb covers. See `revParse` in `verbs.ts`; for a ref, prefer
|
|
134
141
|
* {@link Git.resolveRef}.
|
|
@@ -179,9 +186,12 @@ export declare class Git {
|
|
|
179
186
|
* The primary checkout that owns this working directory, or null when it is
|
|
180
187
|
* not inside a repository at all.
|
|
181
188
|
*
|
|
182
|
-
* It is correct when the cwd is a linked worktree too
|
|
183
|
-
* worktree-aware and absolute
|
|
184
|
-
*
|
|
189
|
+
* It is correct when the cwd is a linked worktree too: `gitCommonDir()` is
|
|
190
|
+
* worktree-aware and absolute. When that store is `<checkout>/.git`, its
|
|
191
|
+
* parent is the answer. Otherwise (a submodule's `.git/modules/<name>`, a
|
|
192
|
+
* `--separate-git-dir` clone, a bare store) it is `core.worktree`, this
|
|
193
|
+
* checkout when the cwd is the main worktree, or the first non-bare
|
|
194
|
+
* `worktree list` entry. A bare store with no main checkout answers null.
|
|
185
195
|
*/
|
|
186
196
|
mainCheckout(opts?: GitSyncCallOptions): string | null;
|
|
187
197
|
/** Current branch shortname, or null when detached. Throws outside a repo. */
|
|
@@ -193,6 +203,12 @@ export declare class Git {
|
|
|
193
203
|
statusPorcelain(opts?: GitSyncCallOptions & StatusPorcelainOptions): string[];
|
|
194
204
|
/** Every changed or untracked file, from `status --porcelain -z`. */
|
|
195
205
|
statusEntries(opts?: GitSyncCallOptions): StatusEntry[];
|
|
206
|
+
/**
|
|
207
|
+
* True iff anything is staged (`diff --cached --quiet` exits 1). On an
|
|
208
|
+
* unborn HEAD the index is compared against the empty tree. See
|
|
209
|
+
* `hasStagedChanges` in `verbs.ts`.
|
|
210
|
+
*/
|
|
211
|
+
hasStagedChanges(opts?: GitSyncCallOptions): boolean;
|
|
196
212
|
/** True iff the working tree has any staged/unstaged/untracked change. */
|
|
197
213
|
isDirty(opts?: GitSyncCallOptions & StatusPorcelainOptions): boolean;
|
|
198
214
|
/** Local branch shortnames matching `pattern` (all when omitted). */
|
|
@@ -358,11 +374,10 @@ export declare class Git {
|
|
|
358
374
|
* "push only if the remote ref is still where I saw it". `{forceWithLease: true}` adds the
|
|
359
375
|
* bare flag, which is the unsafe form: it compares against the
|
|
360
376
|
* remote-tracking ref, which a fetch in this same process may have just
|
|
361
|
-
* updated. See `push` in
|
|
377
|
+
* updated. `{delete: true}` deletes `refspec` on the remote. See `push` in
|
|
378
|
+
* `verbs.ts`.
|
|
362
379
|
*/
|
|
363
|
-
push(remote: string, refspec: string, opts?: GitSyncCallOptions &
|
|
364
|
-
forceWithLease?: boolean | ForceLease | undefined;
|
|
365
|
-
}): void;
|
|
380
|
+
push(remote: string, refspec: string, opts?: GitSyncCallOptions & PushOptions): void;
|
|
366
381
|
/**
|
|
367
382
|
* `git push --porcelain`, read into one result per ref. A rejected ref comes
|
|
368
383
|
* back with a typed `rejection` rather than a throw; a failure that reaches
|
|
@@ -387,22 +402,32 @@ export declare class Git {
|
|
|
387
402
|
deleteRef(ref: string, opts: GitSyncCallOptions & {
|
|
388
403
|
expected: string;
|
|
389
404
|
}): void;
|
|
390
|
-
/**
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
}): void;
|
|
405
|
+
/**
|
|
406
|
+
* `git worktree add [--detach] [-b|-B branch] <path> [startPoint]`.
|
|
407
|
+
* `resetBranch` picks `-B`: create `branch`, or reset it if it exists.
|
|
408
|
+
*/
|
|
409
|
+
worktreeAdd(path: string, opts?: GitSyncCallOptions & WorktreeAddOptions): void;
|
|
396
410
|
/** `git worktree remove [--force] <path>`. */
|
|
397
411
|
worktreeRemove(path: string, opts?: GitSyncCallOptions & {
|
|
398
412
|
force?: boolean | undefined;
|
|
399
413
|
}): void;
|
|
400
414
|
/** `git worktree move [--force] <from> <to>`. */
|
|
401
415
|
worktreeMove(from: string, to: string, opts?: GitSyncCallOptions & WorktreeMoveOptions): void;
|
|
416
|
+
/** `git worktree unlock <path>`. */
|
|
417
|
+
worktreeUnlock(path: string, opts?: GitSyncCallOptions): void;
|
|
402
418
|
/** `git worktree prune`. */
|
|
403
419
|
worktreePrune(opts?: GitSyncCallOptions): void;
|
|
404
420
|
/** `git remote add <name> <url>`. */
|
|
405
421
|
remoteAdd(name: string, url: string, opts?: GitSyncCallOptions): void;
|
|
422
|
+
/**
|
|
423
|
+
* `git remote set-head <remote> <branch> | --auto | --delete` — exactly one
|
|
424
|
+
* of `branch`, `auto` or `delete`. See `remoteSetHead` in `verbs.ts`.
|
|
425
|
+
*/
|
|
426
|
+
remoteSetHead(remote: string, opts: GitSyncCallOptions & RemoteSetHeadOptions): void;
|
|
427
|
+
/** `git remote set-branches [--add] <remote> <branch…>`; an empty list throws. */
|
|
428
|
+
remoteSetBranches(remote: string, branches: string[], opts?: GitSyncCallOptions & RemoteSetBranchesOptions): void;
|
|
429
|
+
/** `git gc [--auto] [--aggressive] [--prune=<date> | --no-prune] [--quiet]`. */
|
|
430
|
+
gc(opts?: GitSyncCallOptions & GcOptions): void;
|
|
406
431
|
/**
|
|
407
432
|
* Merge `ref` into the current branch. A conflict is a value, not a throw:
|
|
408
433
|
* the outcome is `clean` or `conflict`. Git refusing to merge at all still
|
package/dist/sync.js
CHANGED
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
* Not here: any protocol with its own conflict rules, such as a lease
|
|
16
16
|
* service's compare-and-swap ref operations. That is not general git.
|
|
17
17
|
*/
|
|
18
|
-
import { dirname } from 'node:path';
|
|
19
|
-
import {
|
|
18
|
+
import { basename, dirname, resolve } from 'node:path';
|
|
19
|
+
import { spawnRunner, } from '@osdlc/command-seam';
|
|
20
20
|
import { softSync, triedSync } from "./outcome.js";
|
|
21
21
|
import { parseRemoteUrl } from "./remote-url.js";
|
|
22
22
|
import { SOFT_TABLE } from "./soft.js";
|
|
@@ -47,7 +47,9 @@ export class Git {
|
|
|
47
47
|
* `backend.ts`.
|
|
48
48
|
*
|
|
49
49
|
* This client takes a `runner`, where `AsyncGit` takes a `backend`. The
|
|
50
|
-
* default runner spawns `git
|
|
50
|
+
* default runner spawns the `git` found on an absolute `PATH` entry, so a
|
|
51
|
+
* `git` file in the checkout cannot run in its place. Tests pass a fake
|
|
52
|
+
* `CommandRunner`.
|
|
51
53
|
*
|
|
52
54
|
* Neither client has a default timeout. This client kills a child whose
|
|
53
55
|
* output on one stream passes 256 MB; `AsyncGit` has no output cap. The
|
|
@@ -55,7 +57,7 @@ export class Git {
|
|
|
55
57
|
*/
|
|
56
58
|
constructor(cwd, opts = {}) {
|
|
57
59
|
this.cwd = cwd;
|
|
58
|
-
this.runner =
|
|
60
|
+
this.runner = opts.runner ?? spawnRunner('git', { absolutePathOnly: true });
|
|
59
61
|
this.boundGitDir = opts.gitDir;
|
|
60
62
|
this.workTree = opts.workTree;
|
|
61
63
|
this.env = opts.env;
|
|
@@ -156,6 +158,13 @@ export class Git {
|
|
|
156
158
|
showAtRev(rev, relPath, opts = {}) {
|
|
157
159
|
return this.exec(v.showAtRev(rev, relPath), opts);
|
|
158
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* A file's content at a rev, throwing when the path is missing. Use it when
|
|
163
|
+
* a missing file is an error. See `showAtRevChecked` in `verbs.ts`.
|
|
164
|
+
*/
|
|
165
|
+
showAtRevChecked(rev, relPath, opts = {}) {
|
|
166
|
+
return this.exec(v.showAtRevChecked(rev, relPath), opts);
|
|
167
|
+
}
|
|
159
168
|
/**
|
|
160
169
|
* Trimmed `git rev-parse <args>` output, for a query no other verb covers. See `revParse` in `verbs.ts`; for a ref, prefer
|
|
161
170
|
* {@link Git.resolveRef}.
|
|
@@ -223,13 +232,26 @@ export class Git {
|
|
|
223
232
|
* The primary checkout that owns this working directory, or null when it is
|
|
224
233
|
* not inside a repository at all.
|
|
225
234
|
*
|
|
226
|
-
* It is correct when the cwd is a linked worktree too
|
|
227
|
-
* worktree-aware and absolute
|
|
228
|
-
*
|
|
235
|
+
* It is correct when the cwd is a linked worktree too: `gitCommonDir()` is
|
|
236
|
+
* worktree-aware and absolute. When that store is `<checkout>/.git`, its
|
|
237
|
+
* parent is the answer. Otherwise (a submodule's `.git/modules/<name>`, a
|
|
238
|
+
* `--separate-git-dir` clone, a bare store) it is `core.worktree`, this
|
|
239
|
+
* checkout when the cwd is the main worktree, or the first non-bare
|
|
240
|
+
* `worktree list` entry. A bare store with no main checkout answers null.
|
|
229
241
|
*/
|
|
230
242
|
mainCheckout(opts = {}) {
|
|
231
243
|
const common = this.gitCommonDir(opts);
|
|
232
|
-
|
|
244
|
+
if (common === null)
|
|
245
|
+
return null;
|
|
246
|
+
if (basename(common) === '.git')
|
|
247
|
+
return dirname(common);
|
|
248
|
+
const configured = this.configGet('core.worktree', opts);
|
|
249
|
+
if (configured !== null)
|
|
250
|
+
return resolve(common, configured);
|
|
251
|
+
if (this.gitDir(opts) === common)
|
|
252
|
+
return this.topLevel(opts);
|
|
253
|
+
const main = this.worktrees(opts)[0];
|
|
254
|
+
return main === undefined || main.bare ? null : main.path;
|
|
233
255
|
}
|
|
234
256
|
/** Current branch shortname, or null when detached. Throws outside a repo. */
|
|
235
257
|
currentBranch(opts = {}) {
|
|
@@ -246,6 +268,14 @@ export class Git {
|
|
|
246
268
|
statusEntries(opts = {}) {
|
|
247
269
|
return this.exec(v.statusEntries(), opts);
|
|
248
270
|
}
|
|
271
|
+
/**
|
|
272
|
+
* True iff anything is staged (`diff --cached --quiet` exits 1). On an
|
|
273
|
+
* unborn HEAD the index is compared against the empty tree. See
|
|
274
|
+
* `hasStagedChanges` in `verbs.ts`.
|
|
275
|
+
*/
|
|
276
|
+
hasStagedChanges(opts = {}) {
|
|
277
|
+
return this.exec(v.hasStagedChanges(), opts);
|
|
278
|
+
}
|
|
249
279
|
/** True iff the working tree has any staged/unstaged/untracked change. */
|
|
250
280
|
isDirty(opts = {}) {
|
|
251
281
|
return this.statusPorcelain(opts).length > 0;
|
|
@@ -503,7 +533,8 @@ export class Git {
|
|
|
503
533
|
* "push only if the remote ref is still where I saw it". `{forceWithLease: true}` adds the
|
|
504
534
|
* bare flag, which is the unsafe form: it compares against the
|
|
505
535
|
* remote-tracking ref, which a fetch in this same process may have just
|
|
506
|
-
* updated. See `push` in
|
|
536
|
+
* updated. `{delete: true}` deletes `refspec` on the remote. See `push` in
|
|
537
|
+
* `verbs.ts`.
|
|
507
538
|
*/
|
|
508
539
|
push(remote, refspec, opts = {}) {
|
|
509
540
|
this.exec(v.push(remote, refspec, opts), opts);
|
|
@@ -540,7 +571,10 @@ export class Git {
|
|
|
540
571
|
deleteRef(ref, opts) {
|
|
541
572
|
this.exec(v.deleteRef(ref, opts), opts);
|
|
542
573
|
}
|
|
543
|
-
/**
|
|
574
|
+
/**
|
|
575
|
+
* `git worktree add [--detach] [-b|-B branch] <path> [startPoint]`.
|
|
576
|
+
* `resetBranch` picks `-B`: create `branch`, or reset it if it exists.
|
|
577
|
+
*/
|
|
544
578
|
worktreeAdd(path, opts = {}) {
|
|
545
579
|
this.exec(v.worktreeAdd(path, opts), opts);
|
|
546
580
|
}
|
|
@@ -552,6 +586,10 @@ export class Git {
|
|
|
552
586
|
worktreeMove(from, to, opts = {}) {
|
|
553
587
|
this.exec(v.worktreeMove(from, to, opts), opts);
|
|
554
588
|
}
|
|
589
|
+
/** `git worktree unlock <path>`. */
|
|
590
|
+
worktreeUnlock(path, opts = {}) {
|
|
591
|
+
this.exec(v.worktreeUnlock(path), opts);
|
|
592
|
+
}
|
|
555
593
|
/** `git worktree prune`. */
|
|
556
594
|
worktreePrune(opts = {}) {
|
|
557
595
|
this.exec(v.worktreePrune(), opts);
|
|
@@ -560,6 +598,21 @@ export class Git {
|
|
|
560
598
|
remoteAdd(name, url, opts = {}) {
|
|
561
599
|
this.exec(v.remoteAdd(name, url), opts);
|
|
562
600
|
}
|
|
601
|
+
/**
|
|
602
|
+
* `git remote set-head <remote> <branch> | --auto | --delete` — exactly one
|
|
603
|
+
* of `branch`, `auto` or `delete`. See `remoteSetHead` in `verbs.ts`.
|
|
604
|
+
*/
|
|
605
|
+
remoteSetHead(remote, opts) {
|
|
606
|
+
this.exec(v.remoteSetHead(remote, opts), opts);
|
|
607
|
+
}
|
|
608
|
+
/** `git remote set-branches [--add] <remote> <branch…>`; an empty list throws. */
|
|
609
|
+
remoteSetBranches(remote, branches, opts = {}) {
|
|
610
|
+
this.exec(v.remoteSetBranches(remote, branches, opts), opts);
|
|
611
|
+
}
|
|
612
|
+
/** `git gc [--auto] [--aggressive] [--prune=<date> | --no-prune] [--quiet]`. */
|
|
613
|
+
gc(opts = {}) {
|
|
614
|
+
this.exec(v.gc(opts), opts);
|
|
615
|
+
}
|
|
563
616
|
// ── integration ───────────────────────────────────────────────────────────
|
|
564
617
|
/**
|
|
565
618
|
* Merge `ref` into the current branch. A conflict is a value, not a throw:
|