The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 14:45 UTC
fix(server): PR checkout respects restricted fetch mappings (#27853)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Alex Martani committed
Nov 14, 2026 at 03:07 UTC
3d3275b067c3ee7d4676e470fe26d14e7d978751
2 files changed
+147
-9
apps/server/src/vcs/GitVcsDriverCore.test.ts
+106
@@ -6385,6 +6385,112 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
6385
});
6386
6387
describe("remote operations", () => {
6388
+ it.effect.each([
6389
+ {
6390
+ configuration: "ordinary",
6391
+ fetchRefspecs: ["+refs/heads/*:refs/remotes/origin/*"],
6392
+ addsMapping: false,
6393
+ },
6394
+ {
6395
+ configuration: "restricted",
6396
+ fetchRefspecs: [
6397
+ "+refs/heads/main:refs/remotes/origin/main",
6398
+ "+refs/heads/team/*:refs/remotes/origin/team/*",
6399
+ "+refs/heads/release/release-*:refs/remotes/origin/release/release-*",
6400
+ ],
6401
+ addsMapping: true,
6402
+ },
6403
+ ])(
6404
+ "sets a fetched branch's upstream with $configuration fetch configuration",
6405
+ ({ fetchRefspecs, addsMapping }) =>
6406
+ Effect.gen(function* () {
6407
+ const cwd = yield* makeTmpDir();
6408
+ yield* initRepoWithCommit(cwd);
6409
+ const remote = yield* makeTmpDir("git-remote-");
6410
+ yield* git(remote, ["init", "--bare"]);
6411
+ yield* git(cwd, ["remote", "add", "origin", remote]);
6412
+ yield* git(cwd, ["config", "--unset-all", "remote.origin.fetch"]);
6413
+ for (const refspec of fetchRefspecs) {
6414
+ yield* git(cwd, ["config", "--add", "remote.origin.fetch", refspec]);
6415
+ }
6416
+ const remoteBranch = "feature/example-change";
6417
+ const branch = "review/example-change";
6418
+ const mapping = `+refs/heads/${remoteBranch}*:refs/remotes/origin/${remoteBranch}*`;
6419
+ yield* git(cwd, [
6420
+ "push",
6421
+ remote,
6422
+ "HEAD:refs/heads/main",
6423
+ `HEAD:refs/heads/${remoteBranch}`,
6424
+ ]);
6425
+ const driver = yield* GitVcsDriver.GitVcsDriver;
6426
+ yield* driver.fetchRemoteTrackingBranch({ cwd, remoteName: "origin", remoteBranch });
6427
+ yield* git(cwd, ["checkout", "--no-track", "-b", branch, `origin/${remoteBranch}`]);
6428
+
6429
+ const input = { cwd, branch, remoteName: "origin", remoteBranch };
6430
+ yield* driver.setBranchUpstream(input);
6431
+ yield* driver.setBranchUpstream(input);
6432
+
6433
+ assert.equal(
6434
+ yield* git(cwd, ["rev-parse", "--abbrev-ref", `${branch}@{upstream}`]),
6435
+ `origin/${remoteBranch}`,
6436
+ );
6437
+ assert.equal(yield* git(cwd, ["config", `branch.${branch}.remote`]), "origin");
6438
+ assert.equal(
6439
+ yield* git(cwd, ["config", `branch.${branch}.merge`]),
6440
+ `refs/heads/${remoteBranch}`,
6441
+ );
6442
+ assert.deepEqual(
6443
+ (yield* git(cwd, ["config", "--get-all", "remote.origin.fetch"])).split("\n"),
6444
+ addsMapping ? [...fetchRefspecs, mapping] : fetchRefspecs,
6445
+ );
6446
+ // A merged PR's head is deleted; later fetches of the remote must keep working.
6447
+ yield* git(remote, ["--git-dir", remote, "branch", "-D", remoteBranch]);
6448
+ yield* git(cwd, ["fetch", "origin"]);
6449
+ }),
6450
+ );
6451
+
6452
+ it.effect.each(["ordinary", "restricted"] as const)(
6453
+ "retries upstream configuration with a config lock and %s fetch mappings",
6454
+ (configuration) =>
6455
+ Effect.gen(function* () {
6456
+ const cwd = yield* makeTmpDir();
6457
+ const { initialBranch } = yield* initRepoWithCommit(cwd);
6458
+ yield* git(cwd, ["remote", "add", "origin", "https://example.invalid/repo.git"]);
6459
+ if (configuration === "restricted") {
6460
+ yield* git(cwd, [
6461
+ "config",
6462
+ "remote.origin.fetch",
6463
+ `+refs/heads/${initialBranch}:refs/remotes/origin/${initialBranch}`,
6464
+ ]);
6465
+ }
6466
+ yield* git(cwd, ["branch", "review/pr-head"]);
6467
+ yield* git(cwd, ["update-ref", "refs/remotes/origin/pr-head", "HEAD"]);
6468
+ const lockPath = (yield* Path.Path).join(cwd, ".git", "config.lock");
6469
+ yield* (yield* FileSystem.FileSystem).writeFileString(lockPath, "");
6470
+ const { driver, failures } = yield* makeDriverReleasingLock(
6471
+ configuration === "restricted"
6472
+ ? (args) => args[0] === "config" && args[1] === "--add"
6473
+ : (args) => args.includes("--set-upstream-to"),
6474
+ lockPath,
6475
+ );
6476
+
6477
+ yield* TestClock.withLive(
6478
+ driver.setBranchUpstream({
6479
+ cwd,
6480
+ branch: "review/pr-head",
6481
+ remoteName: "origin",
6482
+ remoteBranch: "pr-head",
6483
+ }),
6484
+ );
6485
+
6486
+ assert.equal(yield* Ref.get(failures), 1);
6487
+ assert.equal(
6488
+ yield* git(cwd, ["rev-parse", "--abbrev-ref", "review/pr-head@{upstream}"]),
6489
+ "origin/pr-head",
6490
+ );
6491
+ }),
6492
+ );
6493
+
6494
it.effect("ensureRemote reuses a partial clone remote and avoids its name", () =>
6495
Effect.gen(function* () {
6496
const cwd = yield* makeTmpDir();
apps/server/src/vcs/GitVcsDriverCore.ts
+41
-9
@@ -5201,19 +5201,51 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
5201
]);
5202
});
5203
5204
- const setBranchUpstream: GitVcsDriver.GitVcsDriver["Service"]["setBranchUpstream"] = (input) =>
5205
- executeGitWithStableDiagnostics(
5206
- "GitVcsDriver.setBranchUpstream",
5207
- input.cwd,
5208
- [
5204
+ const setBranchUpstream: GitVcsDriver.GitVcsDriver["Service"]["setBranchUpstream"] = Effect.fn(
5205
+ "setBranchUpstream",
5206
+ )(
5207
+ function* (input) {
5208
+ const args = [
5209
"branch",
5210
"--set-upstream-to",
5211
- `${input.remoteName}/${input.remoteBranch}`,
5211
+ `refs/remotes/${input.remoteName}/${input.remoteBranch}`,
5212
"--",
5213
input.branch,
5214
- ],
5215
- { writesSharedConfig: true },
5216
- ).pipe(retryWhileConfigLocked, Effect.asVoid);
5214
+ ];
5215
+ const result = yield* executeGitWithStableDiagnostics(
5216
+ "GitVcsDriver.setBranchUpstream",
5217
+ input.cwd,
5218
+ args,
5219
+ { allowNonZeroExit: true },
5220
+ );
5221
+ if (result.exitCode === 0) return;
5222
+ // Git requires a configured fetch mapping even when the tracking ref already exists. The
5223
+ // mapping is a glob because an exact source fails every later fetch once the branch is deleted.
5224
+ if (
5225
+ /^fatal: cannot set up tracking information; starting point '.+' is not a branch$/m.test(
5226
+ result.stderr,
5227
+ )
5228
+ ) {
5229
+ yield* executeGitWithStableDiagnostics(
5230
+ "GitVcsDriver.setBranchUpstream.addFetchMapping",
5231
+ input.cwd,
5232
+ [
5233
+ "config",
5234
+ "--add",
5235
+ `remote.${input.remoteName}.fetch`,
5236
+ `+refs/heads/${input.remoteBranch}*:refs/remotes/${input.remoteName}/${input.remoteBranch}*`,
5237
+ ],
5238
+ ).pipe(retryWhileConfigLocked);
5239
+ }
5240
+ yield* executeGitWithStableDiagnostics(
5241
+ "GitVcsDriver.setBranchUpstream",
5242
+ input.cwd,
5243
+ args,
5244
+ ).pipe(retryWhileConfigLocked);
5245
+ },
5246
+ // Keep mapping detection and both writes under the same lock across sibling worktrees.
5247
+ (effect, input) => withSharedConfigLock(input.cwd, effect),
5248
+ );
5249
5250
/**
5251
* Unlinks links under `target` without following them and removes the