File size: 9,481 Bytes
e249c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import type { Dirent } from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
import { isMissingPathError } from "../../infra/errors.js";
import { createGitCommandError, requireGitCommandOutput } from "../../infra/git-exec.js";
import type { GitWorktreeOperations } from "./git-worktree-operations.js";
import {
  requireGit,
  requireGitBuffer,
  runGit,
  runGitBuffered,
  WORKTREE_CHECKOUT_TIMEOUT_MS,
} from "./git.js";

async function missingCommitObjects(repoRoot: string, commit: string): Promise<string[]> {
  const objects = (
    await requireGitBuffer(
      repoRoot,
      ["rev-list", "--objects", "--missing=print", "--no-object-names", "--max-count=1", commit],
      { env: { GIT_NO_LAZY_FETCH: "1" } },
    )
  ).toString("utf8");
  return objects
    .split("\n")
    .filter((line) => line.startsWith("?"))
    .map((line) => line.slice(1));
}

async function readOptionalGitConfig(repoRoot: string, args: string[]): Promise<string> {
  const result = await runGit(repoRoot, ["config", ...args]);
  return result.termination === "exit" && result.code === 1
    ? ""
    : requireGitCommandOutput(`git config ${args.join(" ")}`, result).trim();
}

function missingObjectsError(commit: string, count: number): Error {
  return new Error(
    `Repository is missing ${count} objects for ${commit}; fetch or repair the clone.`,
  );
}

async function resolveCommit(repoRoot: string, ref: string): Promise<string> {
  return await requireGit(repoRoot, [
    "rev-parse",
    "--verify",
    "--end-of-options",
    `${ref === "-" ? "@{-1}" : ref}^{commit}`,
  ]);
}

async function hydrateCommitObjects(repoRoot: string, commit: string): Promise<void> {
  const missing = await missingCommitObjects(repoRoot, commit);
  if (missing.length > 0) {
    const remote =
      (await readOptionalGitConfig(repoRoot, ["--get", "extensions.partialclone"])) ||
      /^remote\.(.+)\.promisor true$/m.exec(
        await readOptionalGitConfig(repoRoot, [
          "--bool",
          "--get-regexp",
          "^remote\\..*\\.promisor$",
        ]),
      )?.[1];
    if (!remote) {
      throw missingObjectsError(commit, missing.length);
    }
    // Hydrate once under the checkout budget; objectsize must never fetch one blob at a time.
    await requireGit(
      repoRoot,
      ["fetch", remote, "--no-tags", "--no-write-fetch-head", "--recurse-submodules=no", "--stdin"],
      { input: Buffer.from(`${missing.join("\n")}\n`), timeoutMs: WORKTREE_CHECKOUT_TIMEOUT_MS },
    );
  }
}

function allocatedBlobBytes(size: string): number {
  const value = Number(size);
  if (!size || !Number.isSafeInteger(value) || value < 0) {
    throw new Error(
      "Cannot estimate worktree checkout size; inspect the repository objects and retry.",
    );
  }
  return Math.max(4096, Math.ceil(value / 4096) * 4096);
}

// This projection belongs to the Git worker and disappears when that worker
// idles out or the Gateway closes it. Never retain missing-object checks here.
const checkoutSizeFacts = new Map<string, number>();
const MAX_CHECKOUT_SIZE_FACTS = 32;

async function commitObjectBytes(
  repoRoot: string,
  commit: string,
  replacementRefBase: string | undefined,
): Promise<number> {
  const replacements =
    replacementRefBase === undefined
      ? undefined
      : await requireGit(repoRoot, [
          "for-each-ref",
          "--format=%(refname)",
          "--",
          replacementRefBase,
        ]);
  let cacheKey: string | undefined;
  if (replacements === "") {
    const canonicalRoot = await fs.realpath(repoRoot);
    const identity = await fs.stat(canonicalRoot);
    cacheKey = JSON.stringify([canonicalRoot, identity.dev, identity.ino, commit]);
    const cached = checkoutSizeFacts.get(cacheKey);
    if (cached !== undefined) {
      return cached;
    }
  }

  try {
    const sizes = (
      await requireGitBuffer(repoRoot, ["ls-tree", "-r", "--format=%(objectsize)", commit, "--"], {
        env: { GIT_NO_LAZY_FETCH: "1" },
      })
    ).toString("utf8");
    let bytes = 0;
    for (const size of sizes.split("\n")) {
      if (!size || size === "-") {
        continue;
      }
      bytes += allocatedBlobBytes(size);
    }
    if (cacheKey) {
      checkoutSizeFacts.set(cacheKey, bytes);
      while (checkoutSizeFacts.size > MAX_CHECKOUT_SIZE_FACTS) {
        checkoutSizeFacts.delete(checkoutSizeFacts.keys().next().value!);
      }
    }
    return bytes;
  } catch (error) {
    const remaining = await missingCommitObjects(repoRoot, commit);
    if (remaining.length > 0) {
      throw missingObjectsError(commit, remaining.length);
    }
    throw error;
  }
}

export async function estimateCheckoutObjectBytes(
  repoRoot: string,
  ref: string,
  replacementRefBase?: string,
): Promise<number> {
  const commit = await resolveCommit(repoRoot, ref);
  await hydrateCommitObjects(repoRoot, commit);
  return await commitObjectBytes(repoRoot, commit, replacementRefBase);
}

export async function estimateCheckoutTransitionBytes(
  repoRoot: string,
  baseRef: string,
  targetRef: string,
  replacementRefBase?: string,
): Promise<GitWorktreeOperations["worktree.checkout-transition-size"]["output"]> {
  const base = await resolveCommit(repoRoot, baseRef);
  const target = await resolveCommit(repoRoot, targetRef);
  // Template validation can need blobs that the target deletes. Hydrate both
  // histories, while sharing identical commits within this admitted operation.
  await hydrateCommitObjects(repoRoot, base);
  if (target !== base) {
    await hydrateCommitObjects(repoRoot, target);
  }
  const targetBytes = await commitObjectBytes(repoRoot, target, replacementRefBase);
  if (target === base) {
    return { targetBytes, changedBytes: 0, requiresFullCheckout: false };
  }
  const diff = await runGitBuffered(
    repoRoot,
    [
      "diff-tree",
      "--no-commit-id",
      "--raw",
      "-z",
      "--no-renames",
      "--no-abbrev",
      "-r",
      base,
      target,
      "--",
    ],
    { env: { GIT_NO_LAZY_FETCH: "1" } },
  );
  if (diff.termination === "output-limit") {
    // A large diff cannot justify a partial allocation estimate or rule out
    // attribute changes. Keep the buffer bounded and materialize the full target.
    return { targetBytes, changedBytes: targetBytes, requiresFullCheckout: true };
  }
  if (diff.termination !== "exit" || diff.code !== 0) {
    throw createGitCommandError("git diff-tree", diff);
  }
  const changes = diff.stdout.toString("utf8").split("\0");
  if (changes.at(-1) !== "" || changes.length % 2 !== 1) {
    throw new Error(
      "Cannot estimate worktree overlay size; inspect the repository diff and retry.",
    );
  }
  const blobs: string[] = [];
  let checkoutAttributesChanged = false;
  for (let offset = 0; offset < changes.length - 1; offset += 2) {
    // --no-renames gives one metadata record and one raw path per change.
    // Count every destination path, even when multiple paths share one blob.
    const metadata = /^:[0-7]{6} ([0-7]{6}) [a-f0-9]+ ([a-f0-9]+) [AMDT]$/u.exec(changes[offset]!);
    if (!metadata || changes[offset + 1] === undefined) {
      throw new Error(
        "Cannot estimate worktree overlay size; inspect the repository diff and retry.",
      );
    }
    const changedPath = changes[offset + 1]!;
    checkoutAttributesChanged ||=
      changedPath === ".gitattributes" || changedPath.endsWith("/.gitattributes");
    if (metadata[1] !== "000000" && metadata[1] !== "160000") {
      blobs.push(metadata[2]!);
    }
  }
  // read-tree does not rewrite unchanged paths when attributes change. The
  // caller must rematerialize the target so its checkout transforms apply.
  if (checkoutAttributesChanged) {
    return {
      targetBytes,
      changedBytes: targetBytes,
      requiresFullCheckout: true,
    };
  }
  if (blobs.length === 0) {
    return { targetBytes, changedBytes: 0, requiresFullCheckout: false };
  }
  const sizes = (
    await requireGitBuffer(repoRoot, ["cat-file", "--batch-check=%(objecttype) %(objectsize)"], {
      input: Buffer.from(`${blobs.join("\n")}\n`),
      env: { GIT_NO_LAZY_FETCH: "1" },
    })
  )
    .toString("utf8")
    .trimEnd()
    .split("\n");
  if (sizes.length !== blobs.length || sizes.some((size) => !size.startsWith("blob "))) {
    throw new Error(
      "Cannot estimate worktree overlay size; inspect the repository objects and retry.",
    );
  }
  return {
    targetBytes,
    changedBytes: sizes.reduce((bytes, size) => bytes + allocatedBlobBytes(size.slice(5)), 0),
    requiresFullCheckout: false,
  };
}

/** Measure without following links; unreadable trees must never be counted as empty. */
export async function measureDirectoryTreeBytes(root: string, excludeGit = false): Promise<number> {
  let entries: Dirent[];
  try {
    entries = await fs.readdir(root, { withFileTypes: true });
  } catch (error) {
    if (isMissingPathError(error)) {
      return 0;
    }
    throw error;
  }
  let total = 0;
  for (const entry of entries) {
    if (excludeGit && entry.name === ".git") {
      continue;
    }
    const child = path.join(root, entry.name);
    if (entry.isDirectory() && !entry.isSymbolicLink()) {
      total += await measureDirectoryTreeBytes(child, excludeGit);
    } else {
      try {
        total += (await fs.lstat(child)).size;
      } catch (error) {
        if (!isMissingPathError(error)) {
          throw error;
        }
      }
    }
  }
  return total;
}