Skip to content

Commit 24cdbbc

Browse files
committed
fix(server): git fast path reads only the refs a for-each-ref names
Answering for-each-ref walked every loose ref directory in the namespace, so one remote-tracking lookup cost 250 ms to 1 s in repositories with many remote branches, several times what git takes. The walk now covers only the refs under the requested name, as git's own iteration does. Wildcard patterns and listings of every remote-tracking ref, which make git walk the whole namespace too, are left to git.
1 parent 8c45eef commit 24cdbbc

2 files changed

Lines changed: 55 additions & 26 deletions

File tree

‎apps/server/src/vcs/GitMetadataFastPath.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,35 @@ describe("GitMetadataFastPath on repositories git treats differently", () => {
802802
});
803803
});
804804

805+
it("reads only the refs under the name asked for", async () => {
806+
const cwd = makeRepo("targetedRefs", (dir) => {
807+
git(dir, "update-ref", "refs/remotes/origin/main", "HEAD");
808+
git(dir, "update-ref", "refs/remotes/origin/mainline", "HEAD");
809+
git(dir, "update-ref", "refs/remotes/origin/main-old/nested", "HEAD");
810+
git(dir, "update-ref", "refs/remotes/other/main", "HEAD");
811+
});
812+
NodeFS.writeFileSync(NodePath.join(cwd, ".git", "refs", "remotes", "other", "stray.lock"), "");
813+
for (const args of [
814+
["for-each-ref", "--format=%(refname)", "refs/remotes/origin/main"],
815+
[
816+
"for-each-ref",
817+
"--format=%(refname)",
818+
"refs/remotes/origin/main-old",
819+
"refs/remotes/origin/main-old/nested",
820+
],
821+
["for-each-ref", "--format=%(refname)", "refs/heads", "refs/heads/main"],
822+
]) {
823+
const real = NodeChildProcess.spawnSync("git", args, { cwd, encoding: "utf8" });
824+
expect({ args, answer: await tryAnswerGitCommand({ cwd, args }) }).toEqual({
825+
args,
826+
answer: { exitCode: 0, stdout: real.stdout, stderr: "" },
827+
});
828+
}
829+
await declines(cwd, "for-each-ref", "--count=1", "--format=%(refname)", "refs/remotes/*/main");
830+
await declines(cwd, "for-each-ref", "--format=%(refname)", "refs/remotes");
831+
await declines(cwd, "for-each-ref", "--format=%(refname)", "refs/remotes/origin");
832+
});
833+
805834
it("leaves symbolic ref chains and remote HEAD upstreams to git", async () => {
806835
const chained = makeRepo("symrefChain", (dir) => {
807836
git(dir, "update-ref", "refs/remotes/origin/main", "HEAD");

‎apps/server/src/vcs/GitMetadataFastPath.ts‎

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,23 +1002,27 @@ const UPSTREAM_FORMAT =
10021002
const byteOrder = (left: string, right: string) =>
10031003
Buffer.compare(Buffer.from(left), Buffer.from(right));
10041004

1005-
/** Every ref below `refs/heads` or `refs/remotes`, loose and packed, that resolves to an object. */
1006-
async function listBranchRefs(repo: Repository, namespace: string): Promise<ReadonlyArray<string>> {
1005+
/**
1006+
* Every ref whose name starts with `prefix`, loose and packed, that resolves to
1007+
* an object: the refs git iterates for a pattern without wildcards.
1008+
*/
1009+
async function listBranchRefs(repo: Repository, prefix: string): Promise<ReadonlyArray<string>> {
10071010
const packed = await readPackedRefs(repo.commonDir);
10081011
const refs = new Set<string>();
1009-
for (const ref of packed.keys()) if (ref.startsWith(`${namespace}/`)) refs.add(ref);
1012+
for (const ref of packed.keys()) if (ref.startsWith(prefix)) refs.add(ref);
10101013

1011-
const walk = async (ref: string): Promise<void> => {
1012-
const dir = NodePath.join(repo.commonDir, ...ref.split("/"));
1014+
const visit = async (parent: string, keep: (name: string) => boolean): Promise<void> => {
1015+
const dir = NodePath.join(repo.commonDir, ...parent.split("/"));
10131016
await assertNoSymlinks(dir);
10141017
const entries = await NodeFSP.readdir(dir, { withFileTypes: true }).catch(
10151018
(error: NodeJS.ErrnoException) =>
10161019
error.code === "ENOENT" ? [] : unsure("unreadable refs directory"),
10171020
);
10181021
for (const entry of entries) {
1019-
const child = `${ref}/${entry.name}`;
1022+
if (!keep(entry.name)) continue;
1023+
const child = `${parent}/${entry.name}`;
10201024
if (entry.isDirectory()) {
1021-
await walk(child);
1025+
await visit(child, () => true);
10221026
continue;
10231027
}
10241028
// Lock files, odd names and non-files are git's to judge.
@@ -1039,21 +1043,22 @@ async function listBranchRefs(repo: Repository, namespace: string): Promise<Read
10391043
refs.add(child);
10401044
}
10411045
};
1042-
await walk(namespace);
1046+
const slash = prefix.lastIndexOf("/");
1047+
const base = prefix.slice(slash + 1);
1048+
await visit(prefix.slice(0, slash), (name) => name.startsWith(base));
10431049
return [...refs];
10441050
}
10451051

1046-
/** git's pattern rule: the whole ref, a leading directory of it, or a glob where `*` stays within one level. */
1052+
/**
1053+
* git's pattern rule for a pattern without wildcards: the whole ref or a leading
1054+
* directory of it. Wildcards make git walk the whole namespace, and so does a
1055+
* pattern naming no single remote; those are git's to answer.
1056+
*/
10471057
function refPatternMatcher(pattern: string): (ref: string) => boolean {
1048-
if (!isSafeRefName(pattern.replaceAll("*", "x")) || pattern.includes("**")) unsure("ref pattern");
1049-
if (!pattern.includes("*")) return (ref) => ref === pattern || ref.startsWith(`${pattern}/`);
1050-
const glob = new RegExp(
1051-
`^${pattern
1052-
.split("*")
1053-
.map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
1054-
.join("[^/]*")}$`,
1055-
);
1056-
return (ref) => glob.test(ref);
1058+
if (!isSafeRefName(pattern) || pattern.includes("*")) unsure("ref pattern");
1059+
if (pattern.startsWith("refs/remotes") && pattern.split("/").length < 4)
1060+
unsure("every remote-tracking ref");
1061+
return (ref) => ref === pattern || ref.startsWith(`${pattern}/`);
10571062
}
10581063

10591064
/** Tracking ref for `branch.<name>.merge` under the remote's fetch refspecs, or `null` when none maps it. */
@@ -1168,17 +1173,12 @@ async function answerForEachRef(repo: Repository, args: ReadonlyArray<string>) {
11681173
if ((format !== REFNAME_FORMAT && format !== UPSTREAM_FORMAT) || patterns.length === 0)
11691174
unsure("for-each-ref arguments");
11701175

1171-
const namespaces = new Set<string>();
11721176
for (const pattern of patterns) {
1173-
const namespace = /^(refs\/(?:heads|remotes))(?:\/|$)/.exec(pattern)?.[1];
1174-
namespaces.add(namespace ?? unsure("for-each-ref outside branches"));
1177+
if (!/^refs\/(?:heads|remotes)(?:\/|$)/.test(pattern)) unsure("for-each-ref outside branches");
11751178
}
11761179
const matchers = patterns.map(refPatternMatcher);
1177-
const listed = await Promise.all(
1178-
[...namespaces].map((namespace) => listBranchRefs(repo, namespace)),
1179-
);
1180-
const refs = listed
1181-
.flat()
1180+
const listed = await Promise.all(patterns.map((pattern) => listBranchRefs(repo, pattern)));
1181+
const refs = [...new Set(listed.flat())]
11821182
.filter((ref) => matchers.some((matches) => matches(ref)))
11831183
.toSorted(byteOrder)
11841184
.slice(0, count);

0 commit comments

Comments
 (0)