Skip to content

Commit 12a627e

Browse files
committed
fix(server): 适配当前 Git 状态刷新与对象格式
保留上游禁用自动维护的行为,仅查询当前分支,并让临时仓库匹配 SHA-256 对象格式。 停止清扫共享目录中归属不明的临时 pack,验证两小时前的其他操作文件仍保留。 验证连续超时、并发引用更新和两种对象格式;类型检查通过,特殊路径测试失败已在未修改 main 复现。
1 parent bf58feb commit 12a627e

2 files changed

Lines changed: 144 additions & 119 deletions

File tree

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

Lines changed: 125 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -2681,117 +2681,124 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
26812681
}),
26822682
);
26832683

2684-
it.effect("keeps isolated fetch refs outside the shared repository", () =>
2685-
Effect.scoped(
2686-
Effect.gen(function* () {
2687-
const cwd = yield* makeTmpDir();
2688-
const remote = yield* makeTmpDir("git-vcs-driver-remote-");
2689-
const updater = yield* makeTmpDir("git-vcs-driver-updater-");
2690-
const { initialBranch } = yield* initRepoWithCommit(cwd);
2691-
yield* git(remote, ["init", "--bare"]);
2692-
yield* git(cwd, ["remote", "add", "origin", remote]);
2693-
yield* git(cwd, ["push", "-u", "origin", initialBranch]);
2694-
2695-
yield* git(updater, ["clone", remote, "."]);
2696-
yield* git(updater, ["config", "user.email", "[email protected]"]);
2697-
yield* git(updater, ["config", "user.name", "Test"]);
2698-
yield* writeTextFile(updater, "remote.txt", "remote\n");
2699-
yield* git(updater, ["add", "remote.txt"]);
2700-
yield* git(updater, ["commit", "-m", "remote commit"]);
2701-
yield* git(updater, ["push", "origin", initialBranch]);
2702-
const concurrentCommit = yield* git(cwd, [
2703-
"commit-tree",
2704-
"HEAD^{tree}",
2705-
"-p",
2706-
"HEAD",
2707-
"-m",
2708-
"concurrent ref update",
2709-
]);
2710-
2711-
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
2712-
let isolatedFetchCount = 0;
2713-
const isolatedFetchExited = yield* Deferred.make<void>();
2714-
const releaseIsolatedFetch = yield* Deferred.make<void>();
2715-
yield* Effect.addFinalizer(() =>
2716-
Deferred.succeed(releaseIsolatedFetch, undefined).pipe(Effect.asVoid),
2717-
);
2718-
const blockingSpawner = ChildProcessSpawner.make((command) =>
2719-
Effect.gen(function* () {
2720-
const handle = yield* delegate.spawn(command);
2721-
if (
2722-
!ChildProcess.isStandardCommand(command) ||
2723-
!command.args.includes("--no-write-fetch-head") ||
2724-
command.options.env?.GIT_OBJECT_DIRECTORY === undefined
2725-
) {
2726-
return handle;
2727-
}
2728-
isolatedFetchCount += 1;
2729-
return ChildProcessSpawner.makeHandle({
2730-
pid: handle.pid,
2731-
exitCode: handle.exitCode.pipe(
2732-
Effect.tap(() => Deferred.succeed(isolatedFetchExited, undefined)),
2733-
Effect.tap(() => Deferred.await(releaseIsolatedFetch)),
2734-
),
2735-
isRunning: handle.isRunning,
2736-
kill: handle.kill,
2737-
unref: handle.unref,
2738-
stdin: handle.stdin,
2739-
stdout: handle.stdout,
2740-
stderr: handle.stderr,
2741-
all: handle.all,
2742-
getInputFd: handle.getInputFd,
2743-
getOutputFd: handle.getOutputFd,
2744-
});
2745-
}),
2746-
);
2747-
const driver = yield* makeGitVcsDriverCore().pipe(
2748-
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, blockingSpawner),
2749-
Effect.provide(layerServerConfig),
2750-
);
2751-
const refresh = yield* driver
2752-
.statusDetailsRemote(cwd)
2753-
.pipe(Effect.forkChild({ startImmediately: true }));
2754-
2755-
yield* Deferred.await(isolatedFetchExited);
2756-
const sharedTemporaryRefs = yield* git(cwd, [
2757-
"for-each-ref",
2758-
"--format=%(refname)",
2759-
"refs/t3-status-fetch",
2760-
]);
2761-
const fileSystem = yield* FileSystem.FileSystem;
2762-
const pathService = yield* Path.Path;
2763-
const gitCommonDirRaw = yield* git(cwd, ["rev-parse", "--git-common-dir"]);
2764-
const gitCommonDir = pathService.isAbsolute(gitCommonDirRaw)
2765-
? gitCommonDirRaw
2766-
: pathService.resolve(cwd, gitCommonDirRaw);
2767-
const isolatedFetchRoot = pathService.join(gitCommonDir, "objects", "t3-status-fetch");
2768-
const [isolatedGitDir] = yield* fileSystem.readDirectory(isolatedFetchRoot, {
2769-
recursive: false,
2770-
});
2771-
if (isolatedGitDir === undefined) {
2772-
return yield* Effect.die("expected an isolated Git status fetch directory");
2773-
}
2774-
const privateTemporaryRefs = yield* git(cwd, [
2775-
"--git-dir",
2776-
pathService.join(isolatedFetchRoot, isolatedGitDir),
2777-
"for-each-ref",
2778-
"--format=%(refname)",
2779-
"refs/t3-status-fetch",
2780-
]);
2781-
2782-
assert.equal(sharedTemporaryRefs, "");
2783-
assert.match(privateTemporaryRefs, /\/upstream$/u);
2784-
2785-
yield* git(cwd, ["update-ref", `refs/remotes/origin/${initialBranch}`, concurrentCommit]);
2786-
yield* Deferred.succeed(releaseIsolatedFetch, undefined);
2787-
const refreshedStatus = yield* Fiber.join(refresh);
2788-
assert.equal(refreshedStatus.behindCount, 1);
2789-
2790-
yield* TestClock.adjust("16 seconds");
2791-
yield* driver.statusDetailsRemote(cwd);
2792-
assert.equal(isolatedFetchCount, 2);
2793-
}),
2794-
),
2684+
it.effect.each(["sha1", "sha256"] as const)(
2685+
"keeps isolated fetch refs outside the shared repository (%s)",
2686+
(objectFormat) =>
2687+
Effect.scoped(
2688+
Effect.gen(function* () {
2689+
const cwd = yield* makeTmpDir();
2690+
const remote = yield* makeTmpDir("git-vcs-driver-remote-");
2691+
const updater = yield* makeTmpDir("git-vcs-driver-updater-");
2692+
yield* git(cwd, ["init", `--object-format=${objectFormat}`]);
2693+
const { initialBranch } = yield* initRepoWithCommit(cwd);
2694+
yield* git(remote, ["init", "--bare", `--object-format=${objectFormat}`]);
2695+
yield* git(cwd, ["remote", "add", "origin", remote]);
2696+
yield* git(cwd, ["push", "-u", "origin", initialBranch]);
2697+
2698+
yield* git(updater, ["clone", remote, "."]);
2699+
yield* git(updater, ["config", "user.email", "[email protected]"]);
2700+
yield* git(updater, ["config", "user.name", "Test"]);
2701+
yield* writeTextFile(updater, "remote.txt", "remote\n");
2702+
yield* git(updater, ["add", "remote.txt"]);
2703+
yield* git(updater, ["commit", "-m", "remote commit"]);
2704+
yield* git(updater, ["push", "origin", initialBranch]);
2705+
const concurrentCommit = yield* git(cwd, [
2706+
"commit-tree",
2707+
"HEAD^{tree}",
2708+
"-p",
2709+
"HEAD",
2710+
"-m",
2711+
"concurrent ref update",
2712+
]);
2713+
2714+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
2715+
let isolatedFetchCount = 0;
2716+
const isolatedFetchExited = yield* Deferred.make<void>();
2717+
const releaseIsolatedFetch = yield* Deferred.make<void>();
2718+
yield* Effect.addFinalizer(() =>
2719+
Deferred.succeed(releaseIsolatedFetch, undefined).pipe(Effect.asVoid),
2720+
);
2721+
const blockingSpawner = ChildProcessSpawner.make((command) =>
2722+
Effect.gen(function* () {
2723+
const handle = yield* delegate.spawn(command);
2724+
if (
2725+
!ChildProcess.isStandardCommand(command) ||
2726+
!command.args.includes("--no-write-fetch-head") ||
2727+
command.options.env?.GIT_OBJECT_DIRECTORY === undefined
2728+
) {
2729+
return handle;
2730+
}
2731+
isolatedFetchCount += 1;
2732+
return ChildProcessSpawner.makeHandle({
2733+
pid: handle.pid,
2734+
exitCode: handle.exitCode.pipe(
2735+
Effect.tap(() => Deferred.succeed(isolatedFetchExited, undefined)),
2736+
Effect.tap(() => Deferred.await(releaseIsolatedFetch)),
2737+
),
2738+
isRunning: handle.isRunning,
2739+
kill: handle.kill,
2740+
unref: handle.unref,
2741+
stdin: handle.stdin,
2742+
stdout: handle.stdout,
2743+
stderr: handle.stderr,
2744+
all: handle.all,
2745+
getInputFd: handle.getInputFd,
2746+
getOutputFd: handle.getOutputFd,
2747+
});
2748+
}),
2749+
);
2750+
const driver = yield* makeGitVcsDriverCore().pipe(
2751+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, blockingSpawner),
2752+
Effect.provide(layerServerConfig),
2753+
);
2754+
const refresh = yield* driver
2755+
.statusDetailsRemote(cwd)
2756+
.pipe(Effect.forkChild({ startImmediately: true }));
2757+
2758+
yield* Deferred.await(isolatedFetchExited);
2759+
const sharedTemporaryRefs = yield* git(cwd, [
2760+
"for-each-ref",
2761+
"--format=%(refname)",
2762+
"refs/t3-status-fetch",
2763+
]);
2764+
const fileSystem = yield* FileSystem.FileSystem;
2765+
const pathService = yield* Path.Path;
2766+
const gitCommonDirRaw = yield* git(cwd, ["rev-parse", "--git-common-dir"]);
2767+
const gitCommonDir = pathService.isAbsolute(gitCommonDirRaw)
2768+
? gitCommonDirRaw
2769+
: pathService.resolve(cwd, gitCommonDirRaw);
2770+
const isolatedFetchRoot = pathService.join(gitCommonDir, "objects", "t3-status-fetch");
2771+
const [isolatedGitDir] = yield* fileSystem.readDirectory(isolatedFetchRoot, {
2772+
recursive: false,
2773+
});
2774+
if (isolatedGitDir === undefined) {
2775+
return yield* Effect.die("expected an isolated Git status fetch directory");
2776+
}
2777+
const privateTemporaryRefs = yield* git(cwd, [
2778+
"--git-dir",
2779+
pathService.join(isolatedFetchRoot, isolatedGitDir),
2780+
"for-each-ref",
2781+
"--format=%(refname)",
2782+
"refs/t3-status-fetch",
2783+
]);
2784+
2785+
assert.equal(sharedTemporaryRefs, "");
2786+
assert.match(privateTemporaryRefs, /\/upstream$/u);
2787+
2788+
yield* git(cwd, [
2789+
"update-ref",
2790+
`refs/remotes/origin/${initialBranch}`,
2791+
concurrentCommit,
2792+
]);
2793+
yield* Deferred.succeed(releaseIsolatedFetch, undefined);
2794+
const refreshedStatus = yield* Fiber.join(refresh);
2795+
assert.equal(refreshedStatus.behindCount, 1);
2796+
2797+
yield* TestClock.adjust("16 seconds");
2798+
yield* driver.statusDetailsRemote(cwd);
2799+
assert.equal(isolatedFetchCount, 2);
2800+
}),
2801+
),
27952802
);
27962803

27972804
it.effect("cleans partial packs after repeated status fetch timeouts", () =>
@@ -2869,6 +2876,10 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
28692876
const otherPack = path.join(commonDir, "objects", "pack", "tmp_pack_other_operation");
28702877
yield* fs.makeDirectory(path.dirname(otherPack), { recursive: true });
28712878
yield* fs.writeFileString(otherPack, "another operation's data");
2879+
const oldTime = DateTime.toDateUtc(
2880+
DateTime.subtractDuration(yield* DateTime.now, Duration.hours(2)),
2881+
);
2882+
yield* fs.utimes(otherPack, oldTime, oldTime);
28722883
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
28732884

28742885
for (let attempt = 0; attempt < 3; attempt++) {
@@ -2908,7 +2919,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
29082919
);
29092920
const driver = yield* makeGitVcsDriverCore().pipe(
29102921
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
2911-
Effect.provide(ServerConfigLayer),
2922+
Effect.provide(layerServerConfig),
29122923
);
29132924
const refresh = yield* driver
29142925
.statusDetailsRemote(cwd)
@@ -3086,7 +3097,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
30863097
yield* git(remote, ["init", "--bare"]);
30873098
yield* git(cwd, ["remote", "add", "origin", remote]);
30883099
yield* git(cwd, ["push", "-u", "origin", "main"]);
3089-
yield* git(remote, ["symbolic-ref", "HEAD", "refs/heads/main"]);
3100+
yield* git(remote, ["--git-dir", remote, "symbolic-ref", "HEAD", "refs/heads/main"]);
30903101
yield* git(cwd, ["symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/main"]);
30913102
yield* git(cwd, ["checkout", "-b", "feature/default-refresh"]);
30923103
yield* writeTextFile(cwd, "feature.txt", "feature\n");
@@ -3122,7 +3133,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
31223133
yield* git(remote, ["init", "--bare"]);
31233134
yield* git(cwd, ["remote", "add", "origin", remote]);
31243135
yield* git(cwd, ["push", "-u", "origin", "main"]);
3125-
yield* git(remote, ["symbolic-ref", "HEAD", "refs/heads/main"]);
3136+
yield* git(remote, ["--git-dir", remote, "symbolic-ref", "HEAD", "refs/heads/main"]);
31263137
yield* git(cwd, [
31273138
"symbolic-ref",
31283139
"refs/remotes/origin/HEAD",

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ const STATUS_ORIGIN_EXISTS_CACHE_TTL = Duration.minutes(5);
9595
const GIT_STATUS_FETCH_OBJECT_DIRECTORY = "t3-status-fetch";
9696
const GIT_STATUS_FETCH_STALE_AGE = Duration.hours(1);
9797
const GIT_STATUS_FETCH_REF_PREFIX = "refs/t3-status-fetch";
98-
const GIT_ZERO_OID = "0".repeat(40);
9998
const STATUS_UPSTREAM_REFRESH_ENV = Object.freeze({
10099
GCM_INTERACTIVE: "never",
101100
GIT_ASKPASS: "",
@@ -1276,13 +1275,20 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
12761275
});
12771276

12781277
const resolveCurrentUpstream = Effect.fn("resolveCurrentUpstream")(function* (cwd: string) {
1278+
const currentRef = yield* runGitStdout(
1279+
"GitVcsDriver.resolveCurrentUpstream.currentRef",
1280+
cwd,
1281+
["symbolic-ref", "--quiet", "HEAD"],
1282+
true,
1283+
);
1284+
if (currentRef.trim().length === 0) return null;
12791285
const upstream = yield* runGitStdout(
12801286
"GitVcsDriver.resolveCurrentUpstream",
12811287
cwd,
12821288
[
12831289
"for-each-ref",
12841290
"--format=%(HEAD)%09%(upstream:short)%09%(upstream)%09%(upstream:remotename)%09%(upstream:remoteref)%09%(upstream:trackshort)",
1285-
"refs/heads",
1291+
currentRef.trim(),
12861292
],
12871293
true,
12881294
).pipe(
@@ -1339,7 +1345,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13391345
);
13401346

13411347
yield* cleanupStaleIsolatedFetches;
1342-
const operationId = yield* crypto.randomUUIDv4.pipe(
1348+
const operationId = yield* crypto.randomUUIDv4().pipe(
13431349
Effect.mapError(
13441350
(cause) =>
13451351
new GitCommandError({
@@ -1384,10 +1390,18 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
13841390
);
13851391

13861392
yield* Effect.gen(function* () {
1393+
const objectFormat = yield* executeGit(
1394+
"GitVcsDriver.fetchRemoteForStatus.objectFormat",
1395+
fetchCwd,
1396+
["--git-dir", gitCommonDir, "config", "--get", "extensions.objectFormat"],
1397+
{ allowNonZeroExit: true },
1398+
).pipe(Effect.map((result) => result.stdout.trim()));
1399+
const zeroOid = "0".repeat(objectFormat === "sha256" ? 64 : 40);
13871400
yield* runGit("GitVcsDriver.fetchRemoteForStatus.init", fetchCwd, [
13881401
"init",
13891402
"--bare",
13901403
"--quiet",
1404+
...(objectFormat === "sha256" ? ["--object-format=sha256"] : []),
13911405
temporaryRoot,
13921406
]);
13931407
yield* fileSystem
@@ -1418,7 +1432,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
14181432
{ allowNonZeroExit: true },
14191433
);
14201434
const expectedTarget =
1421-
previousTarget.exitCode === 0 ? previousTarget.stdout.trim() : GIT_ZERO_OID;
1435+
previousTarget.exitCode === 0 ? previousTarget.stdout.trim() : zeroOid;
14221436
// A zero unpack limit bypasses Git's pack-size threshold and can select loose objects.
14231437
// Use one so every nonempty transfer is indexed as a pack in the isolated directory.
14241438
const fetchArgs = [
@@ -1523,7 +1537,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
15231537
{ allowNonZeroExit: true },
15241538
);
15251539
const currentTargetOid =
1526-
currentTarget.exitCode === 0 ? currentTarget.stdout.trim() : GIT_ZERO_OID;
1540+
currentTarget.exitCode === 0 ? currentTarget.stdout.trim() : zeroOid;
15271541
if (currentTargetOid !== expectedTarget) return;
15281542

15291543
return yield* new GitCommandError({

0 commit comments

Comments
 (0)