Skip to content

Commit 7ac63b6

Browse files
only21milclaude
andcommitted
perf(server): read branch base config in one git process
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1d626d0 commit 7ac63b6

2 files changed

Lines changed: 58 additions & 7 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5664,6 +5664,27 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
56645664
}),
56655665
);
56665666

5667+
it.effect("reads a recorded branch base as git config --get would", () =>
5668+
Effect.gen(function* () {
5669+
const cwd = yield* makeTmpDir();
5670+
yield* initRepoWithCommit(cwd);
5671+
const driver = yield* GitVcsDriver.GitVcsDriver;
5672+
const branch = "feature/a.b+";
5673+
yield* git(cwd, ["config", "branch.feature/aXbb.t3-merge-base", "regex-match"]);
5674+
assert.isNull(yield* driver.readBranchBase(cwd, branch));
5675+
5676+
yield* git(cwd, ["config", `branch.${branch}.gh-merge-base`, "legacy"]);
5677+
assert.equal(yield* driver.readBranchBase(cwd, branch), "legacy");
5678+
5679+
yield* git(cwd, ["config", `branch.${branch}.t3-merge-base`, "first"]);
5680+
yield* git(cwd, ["config", "--add", `branch.${branch}.t3-merge-base`, "last"]);
5681+
assert.equal(yield* driver.readBranchBase(cwd, branch), "last");
5682+
5683+
yield* git(cwd, ["config", "--add", `branch.${branch}.t3-merge-base`, ""]);
5684+
assert.equal(yield* driver.readBranchBase(cwd, branch), "legacy");
5685+
}),
5686+
);
5687+
56675688
it.effect("keeps a recorded merge base when publishing a tracked branch", () =>
56685689
Effect.gen(function* () {
56695690
const cwd = yield* makeTmpDir();

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

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ const DEFAULT_TIMEOUT_MS = 30_000;
5959
/** T3's record of the branch, named as on the host, that a branch was cut from. */
6060
const branchBaseConfigKey = (branch: string) => `branch.${branch}.t3-merge-base`;
6161
/**
62-
* Where T3 kept that record before it had a key of its own, for every host alike: the GitHub
63-
* CLI's key, which branches cut before then still carry instead.
62+
* T3's keys plus the GitHub CLI's `gh-merge-base`, where T3 kept the record for every host alike
63+
* before it had a key of its own; branches cut before then still carry it instead.
6464
*/
65-
const legacyBranchBaseConfigKey = (branch: string) => `branch.${branch}.gh-merge-base`;
65+
const branchBaseConfigPattern = (branch: string) => {
66+
const escapedBranch = branch.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
67+
return `^branch\\.${escapedBranch}\\.(t3-merge-base-ref|t3-merge-base|gh-merge-base)$`;
68+
};
6669
const gitProcesses = Semaphore.makeUnsafe(8);
6770
const WORKTREE_REMOVE_TIMEOUT_MS = Duration.toMillis(Duration.minutes(5));
6871
const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000;
@@ -1848,7 +1851,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
18481851
readonly knownOriginDefaultBranch?: string | null;
18491852
},
18501853
) {
1851-
const configuredBaseRef = yield* readConfigValue(cwd, `branch.${refName}.t3-merge-base-ref`);
1854+
const { baseRef: configuredBaseRef, base: recordedBase } = yield* readBranchBaseConfig(
1855+
cwd,
1856+
refName,
1857+
);
18521858
if (
18531859
configuredBaseRef?.startsWith("refs/heads/") ||
18541860
configuredBaseRef?.startsWith("refs/remotes/")
@@ -1863,7 +1869,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
18631869
}
18641870
}
18651871

1866-
const configuredBaseBranch = (yield* readBranchBase(cwd, refName)) ?? "";
1872+
const configuredBaseBranch = recordedBase ?? "";
18671873
const configuredBase = yield* resolveComparisonBaseRef(cwd, configuredBaseBranch);
18681874
if (
18691875
configuredBase !== null &&
@@ -3747,11 +3753,35 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
37473753
yield* createBranchAt("GitVcsDriver.createBranch", input, "git branch create failed");
37483754
});
37493755

3756+
// The last value of a repeated key wins, as with `git config --get`.
3757+
const readBranchBaseConfig = Effect.fn("GitVcsDriver.readBranchBaseConfig")(function* (
3758+
cwd: string,
3759+
branch: string,
3760+
) {
3761+
const stdout = yield* runGitStdout(
3762+
"GitVcsDriver.readBranchBaseConfig",
3763+
cwd,
3764+
["config", "-z", "--get-regexp", branchBaseConfigPattern(branch)],
3765+
true,
3766+
);
3767+
const values = new Map<string, string | null>();
3768+
for (const entry of stdout.split("\0")) {
3769+
if (entry.length === 0) continue;
3770+
const newline = entry.indexOf("\n");
3771+
const key = newline < 0 ? entry : entry.slice(0, newline);
3772+
const value = newline < 0 ? "" : entry.slice(newline + 1).trim();
3773+
values.set(key.slice(key.lastIndexOf(".") + 1), value.length > 0 ? value : null);
3774+
}
3775+
return {
3776+
baseRef: values.get("t3-merge-base-ref") ?? null,
3777+
base: values.get("t3-merge-base") ?? values.get("gh-merge-base") ?? null,
3778+
};
3779+
});
3780+
37503781
const readBranchBase: GitVcsDriver.GitVcsDriver["Service"]["readBranchBase"] = Effect.fn(
37513782
"GitVcsDriver.readBranchBase",
37523783
)(function* (cwd, branch) {
3753-
const base = yield* readConfigValue(cwd, branchBaseConfigKey(branch));
3754-
return base ?? (yield* readConfigValue(cwd, legacyBranchBaseConfigKey(branch)));
3784+
return (yield* readBranchBaseConfig(cwd, branch)).base;
37553785
});
37563786

37573787
const resolveBranchBase = Effect.fn("GitVcsDriver.resolveBranchBase")(function* (

0 commit comments

Comments
 (0)