@osdlc/typed-git 0.8.2 → 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/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) ?? parseGitUrl(trimWebPath(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 one exception is `remoteOwnerRepo`, which calls `remoteGetUrl` and then
28
- * `parseRemoteUrl`, and is written in each client.
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 one exception is `remoteOwnerRepo`, which calls `remoteGetUrl` and then
28
- * `parseRemoteUrl`, and is written in each client.
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 { type Result } from '@osdlc/result';
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
- /** Wrap a synchronous client so every method returns a `Result<T, GitFailure>`. */
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 { err, ok } from '@osdlc/result';
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
- * A `Proxy` rather than a generated object: it mirrors whatever the client
31
- * has, including methods like `isDirty` that call other verbs, with no
32
- * per-verb entry to keep in step. Methods are invoked on the
33
- * client itself, not on the proxy, so private state stays reachable.
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 facade(client, (call) => {
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 facade(client, async (call) => {
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 facade(client, (call, name) => {
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 facade(client, async (call, name) => {
57
+ return methodFacade(client, async (call, name) => {
94
58
  try {
95
59
  return await call();
96
60
  }
@@ -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, a single
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`.
@@ -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, a single
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
- const path = rest.replace(/\.git$/, '').replace(/^\/+|\/+$/g, '');
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
@@ -22,6 +22,7 @@ export const SOFT_TABLE = {
22
22
  statusPorcelain: 'empty',
23
23
  statusEntries: 'empty',
24
24
  isDirty: 'false',
25
+ hasStagedChanges: 'false',
25
26
  branches: 'empty',
26
27
  branchExists: 'false',
27
28
  worktrees: 'empty',
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 ForceLease, 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 WorktreeMoveOptions } from './verbs.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
22
  /**
23
23
  * Per-call concerns that are not part of the operation itself.
24
24
  *
@@ -131,6 +131,11 @@ export declare class Git {
131
131
  private exec;
132
132
  /** A file's content at a rev (`git show <rev>:<relPath>`), null if absent. */
133
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;
134
139
  /**
135
140
  * Trimmed `git rev-parse <args>` output, for a query no other verb covers. See `revParse` in `verbs.ts`; for a ref, prefer
136
141
  * {@link Git.resolveRef}.
@@ -181,9 +186,12 @@ export declare class Git {
181
186
  * The primary checkout that owns this working directory, or null when it is
182
187
  * not inside a repository at all.
183
188
  *
184
- * It is correct when the cwd is a linked worktree too. `gitCommonDir()` is
185
- * worktree-aware and absolute, so its parent is the primary checkout in
186
- * both cases.
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.
187
195
  */
188
196
  mainCheckout(opts?: GitSyncCallOptions): string | null;
189
197
  /** Current branch shortname, or null when detached. Throws outside a repo. */
@@ -195,6 +203,12 @@ export declare class Git {
195
203
  statusPorcelain(opts?: GitSyncCallOptions & StatusPorcelainOptions): string[];
196
204
  /** Every changed or untracked file, from `status --porcelain -z`. */
197
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;
198
212
  /** True iff the working tree has any staged/unstaged/untracked change. */
199
213
  isDirty(opts?: GitSyncCallOptions & StatusPorcelainOptions): boolean;
200
214
  /** Local branch shortnames matching `pattern` (all when omitted). */
@@ -360,11 +374,10 @@ export declare class Git {
360
374
  * "push only if the remote ref is still where I saw it". `{forceWithLease: true}` adds the
361
375
  * bare flag, which is the unsafe form: it compares against the
362
376
  * remote-tracking ref, which a fetch in this same process may have just
363
- * updated. See `push` in `verbs.ts`.
377
+ * updated. `{delete: true}` deletes `refspec` on the remote. See `push` in
378
+ * `verbs.ts`.
364
379
  */
365
- push(remote: string, refspec: string, opts?: GitSyncCallOptions & {
366
- forceWithLease?: boolean | ForceLease | undefined;
367
- }): void;
380
+ push(remote: string, refspec: string, opts?: GitSyncCallOptions & PushOptions): void;
368
381
  /**
369
382
  * `git push --porcelain`, read into one result per ref. A rejected ref comes
370
383
  * back with a typed `rejection` rather than a throw; a failure that reaches
@@ -389,12 +402,11 @@ export declare class Git {
389
402
  deleteRef(ref: string, opts: GitSyncCallOptions & {
390
403
  expected: string;
391
404
  }): void;
392
- /** `git worktree add [--detach] [-b branch] <path> [startPoint]`. */
393
- worktreeAdd(path: string, opts?: GitSyncCallOptions & {
394
- branch?: string | undefined;
395
- startPoint?: string | undefined;
396
- detach?: boolean | undefined;
397
- }): 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;
398
410
  /** `git worktree remove [--force] <path>`. */
399
411
  worktreeRemove(path: string, opts?: GitSyncCallOptions & {
400
412
  force?: boolean | undefined;
@@ -407,6 +419,15 @@ export declare class Git {
407
419
  worktreePrune(opts?: GitSyncCallOptions): void;
408
420
  /** `git remote add <name> <url>`. */
409
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;
410
431
  /**
411
432
  * Merge `ref` into the current branch. A conflict is a value, not a throw:
412
433
  * the outcome is `clean` or `conflict`. Git refusing to merge at all still
package/dist/sync.js CHANGED
@@ -15,7 +15,7 @@
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';
18
+ import { basename, dirname, resolve } from 'node:path';
19
19
  import { spawnRunner, } from '@osdlc/command-seam';
20
20
  import { softSync, triedSync } from "./outcome.js";
21
21
  import { parseRemoteUrl } from "./remote-url.js";
@@ -158,6 +158,13 @@ export class Git {
158
158
  showAtRev(rev, relPath, opts = {}) {
159
159
  return this.exec(v.showAtRev(rev, relPath), opts);
160
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
+ }
161
168
  /**
162
169
  * Trimmed `git rev-parse <args>` output, for a query no other verb covers. See `revParse` in `verbs.ts`; for a ref, prefer
163
170
  * {@link Git.resolveRef}.
@@ -225,13 +232,26 @@ export class Git {
225
232
  * The primary checkout that owns this working directory, or null when it is
226
233
  * not inside a repository at all.
227
234
  *
228
- * It is correct when the cwd is a linked worktree too. `gitCommonDir()` is
229
- * worktree-aware and absolute, so its parent is the primary checkout in
230
- * both cases.
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.
231
241
  */
232
242
  mainCheckout(opts = {}) {
233
243
  const common = this.gitCommonDir(opts);
234
- return common === null ? null : dirname(common);
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;
235
255
  }
236
256
  /** Current branch shortname, or null when detached. Throws outside a repo. */
237
257
  currentBranch(opts = {}) {
@@ -248,6 +268,14 @@ export class Git {
248
268
  statusEntries(opts = {}) {
249
269
  return this.exec(v.statusEntries(), opts);
250
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
+ }
251
279
  /** True iff the working tree has any staged/unstaged/untracked change. */
252
280
  isDirty(opts = {}) {
253
281
  return this.statusPorcelain(opts).length > 0;
@@ -505,7 +533,8 @@ export class Git {
505
533
  * "push only if the remote ref is still where I saw it". `{forceWithLease: true}` adds the
506
534
  * bare flag, which is the unsafe form: it compares against the
507
535
  * remote-tracking ref, which a fetch in this same process may have just
508
- * updated. See `push` in `verbs.ts`.
536
+ * updated. `{delete: true}` deletes `refspec` on the remote. See `push` in
537
+ * `verbs.ts`.
509
538
  */
510
539
  push(remote, refspec, opts = {}) {
511
540
  this.exec(v.push(remote, refspec, opts), opts);
@@ -542,7 +571,10 @@ export class Git {
542
571
  deleteRef(ref, opts) {
543
572
  this.exec(v.deleteRef(ref, opts), opts);
544
573
  }
545
- /** `git worktree add [--detach] [-b branch] <path> [startPoint]`. */
574
+ /**
575
+ * `git worktree add [--detach] [-b|-B branch] <path> [startPoint]`.
576
+ * `resetBranch` picks `-B`: create `branch`, or reset it if it exists.
577
+ */
546
578
  worktreeAdd(path, opts = {}) {
547
579
  this.exec(v.worktreeAdd(path, opts), opts);
548
580
  }
@@ -566,6 +598,21 @@ export class Git {
566
598
  remoteAdd(name, url, opts = {}) {
567
599
  this.exec(v.remoteAdd(name, url), opts);
568
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
+ }
569
616
  // ── integration ───────────────────────────────────────────────────────────
570
617
  /**
571
618
  * Merge `ref` into the current branch. A conflict is a value, not a throw: