Skip to content

Commit 622a7cb

Browse files
only21milclaude
andcommitted
fix(server): selected-file commits work with Git older than 2.25
git add learned --pathspec-from-file in Git 2.25, so on older Git the stdin form fails and nothing is committed. Read the Git version once, the first time a selected-file commit needs it, and keep the stdin call on 2.25 or newer; older Git, or a version that cannot be read, gets the argument form main uses. Either way the selection is still one git add. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0a06300 commit 622a7cb

3 files changed

Lines changed: 129 additions & 9 deletions

File tree

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
5656
import * as VcsProjectConfig from "../vcs/VcsProjectConfig.ts";
5757
import * as GitWorkflowService from "./GitWorkflowService.ts";
5858
import type { SourceControlProvider } from "@t3tools/source-control-core/server/SourceControlProvider";
59+
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
5960
import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
6061
import * as ServerConfig from "../config.ts";
6162
import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
@@ -3235,6 +3236,36 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
32353236
}),
32363237
);
32373238

3239+
it.effect.skipIf(!symlinksSupported)(
3240+
"keeps staged changes when git add refuses a selected file",
3241+
() =>
3242+
Effect.gen(function* () {
3243+
const repoDir = yield* makeTempDir("t3code-git-manager-");
3244+
yield* initRepo(repoDir);
3245+
NodeFS.writeFileSync(NodePath.join(repoDir, "README.md"), "hello\nstaged\n");
3246+
yield* runGit(repoDir, ["add", "README.md"]);
3247+
NodeFS.writeFileSync(NodePath.join(repoDir, "a.txt"), "file a\n");
3248+
NodeFS.mkdirSync(NodePath.join(repoDir, "real"));
3249+
NodeFS.writeFileSync(NodePath.join(repoDir, "real", "b.txt"), "file b\n");
3250+
// git add refuses a path beyond a symbolic link.
3251+
NodeFS.symlinkSync("real", NodePath.join(repoDir, "link"));
3252+
const stagedBefore = (yield* runGit(repoDir, ["diff", "--cached"])).stdout;
3253+
const headBefore = (yield* runGit(repoDir, ["rev-parse", "HEAD"])).stdout;
3254+
3255+
const { manager } = yield* makeManager();
3256+
const result = yield* runStackedAction(manager, {
3257+
cwd: repoDir,
3258+
action: "commit",
3259+
commitMessage: "Selected files",
3260+
filePaths: ["a.txt", "link/b.txt"],
3261+
}).pipe(Effect.result);
3262+
3263+
expect(Result.isFailure(result)).toBe(true);
3264+
expect((yield* runGit(repoDir, ["diff", "--cached"])).stdout).toBe(stagedBefore);
3265+
expect((yield* runGit(repoDir, ["rev-parse", "HEAD"])).stdout).toBe(headBefore);
3266+
}),
3267+
);
3268+
32383269
it.effect("commits only selected files when filePaths is provided", () =>
32393270
Effect.gen(function* () {
32403271
const repoDir = yield* makeTempDir("t3code-git-manager-");

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import * as ServerConfig from "../config.ts";
4141
import {
4242
makeGitVcsDriverCore,
4343
parseGitCheckoutProgressLine,
44+
gitSupportsPathspecFromFile,
4445
splitNullSeparatedGitStdoutPaths,
4546
windowsLongPathConfigEnv,
4647
} from "./GitVcsDriverCore.ts";
@@ -1389,6 +1390,66 @@ it.effect("reads worktrees from git that rejects worktree list -z", () =>
13891390
),
13901391
);
13911392

1393+
it("reads from git version whether git add takes --pathspec-from-file", () => {
1394+
for (const [output, supported] of [
1395+
["git version 2.24.4\n", false],
1396+
["git version 2.24.3 (Apple Git-128)\n", false],
1397+
["git version 2.25.0\n", true],
1398+
["git version 2.45.1.windows.1\n", true],
1399+
["git version 2.53.0\n", true],
1400+
["unexpected output\n", false],
1401+
] as const) {
1402+
assert.equal(gitSupportsPathspecFromFile(output), supported, output);
1403+
}
1404+
});
1405+
1406+
it.effect("commits selected files with git that rejects --pathspec-from-file", () =>
1407+
Effect.scoped(
1408+
Effect.gen(function* () {
1409+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
1410+
// Git before 2.25 has no `add --pathspec-from-file` and exits with a usage error.
1411+
const oldGitSpawner = ChildProcessSpawner.make((command) => {
1412+
if (!ChildProcess.isStandardCommand(command)) {
1413+
return Effect.die("expected a standard Git command");
1414+
}
1415+
if (command.args.at(-1) === "version") {
1416+
return Effect.succeed(makeSuccessfulHandle("git version 2.24.4\n"));
1417+
}
1418+
if (command.args.some((arg) => String(arg).startsWith("--pathspec-from-file"))) {
1419+
return Effect.succeed(makeFailedHandle("error: unknown option `pathspec-from-file=-'"));
1420+
}
1421+
return delegate.spawn(command);
1422+
});
1423+
const driver = yield* makeGitVcsDriverCore().pipe(
1424+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, oldGitSpawner),
1425+
);
1426+
yield* Effect.gen(function* () {
1427+
const cwd = yield* makeTmpDir();
1428+
yield* initRepoWithCommit(cwd);
1429+
const selected = ["selected[1].txt", "-dash.txt", "with space.txt"];
1430+
for (const file of selected) yield* writeTextFile(cwd, file, "selected\n");
1431+
yield* writeTextFile(cwd, "left.txt", "left\n");
1432+
1433+
yield* driver.commit(cwd, "Selected", "", { stage: { filePaths: selected } });
1434+
1435+
assert.sameMembers(
1436+
(yield* git(cwd, ["show", "--pretty=", "--name-only", "-z", "HEAD"]))
1437+
.split("\0")
1438+
.filter(Boolean),
1439+
selected,
1440+
);
1441+
assert.equal(yield* git(cwd, ["status", "--porcelain"]), "?? left.txt");
1442+
}).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, driver));
1443+
}),
1444+
).pipe(
1445+
Effect.provide(
1446+
layerServerConfig.pipe(
1447+
Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
1448+
),
1449+
),
1450+
),
1451+
);
1452+
13921453
it.effect("does not report a separate git dir as the worktree of its branch", () =>
13931454
Effect.scoped(
13941455
Effect.gen(function* () {

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

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,14 @@ function splitNullSeparatedPaths(input: string, truncated: boolean): string[] {
319319
return parts.filter((value) => value.length > 0);
320320
}
321321

322+
// Parses "git version 2.24.3 (Apple Git-128)" or "git version 2.45.1.windows.1".
323+
export const gitSupportsPathspecFromFile = (versionOutput: string): boolean => {
324+
const match = /^git version (\d+)\.(\d+)/.exec(versionOutput);
325+
if (match === null) return false;
326+
const major = Number(match[1]);
327+
return major > 2 || (major === 2 && Number(match[2]) >= 25);
328+
};
329+
322330
export function splitNullSeparatedGitStdoutPaths(
323331
result: Pick<GitVcsDriver.ExecuteGitResult, "stdout" | "stdoutTruncated">,
324332
): string[] {
@@ -2437,6 +2445,16 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
24372445
})),
24382446
);
24392447

2448+
// git add takes --pathspec-from-file from Git 2.25; older Git keeps the argument form.
2449+
let pathspecFromFileSupported: boolean | undefined;
2450+
const supportsPathspecFromFile = Effect.fnUntraced(function* (cwd: string) {
2451+
if (pathspecFromFileSupported !== undefined) return pathspecFromFileSupported;
2452+
const version = yield* executeGit("GitVcsDriver.version", cwd, ["version"]).pipe(Effect.option);
2453+
if (Option.isNone(version)) return false;
2454+
pathspecFromFileSupported = gitSupportsPathspecFromFile(version.value.stdout);
2455+
return pathspecFromFileSupported;
2456+
});
2457+
24402458
const stageCommitChanges = Effect.fnUntraced(function* (
24412459
cwd: string,
24422460
filePaths?: readonly string[],
@@ -2498,16 +2516,26 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
24982516
}
24992517
yield* runGit("GitVcsDriver.commit.reset", cwd, ["reset", "--quiet"]);
25002518
}
2501-
yield* runGit(
2502-
"GitVcsDriver.commit.addSelected",
2503-
cwd,
2504-
[...config, "add", "-A", "--pathspec-from-file=-", "--pathspec-file-nul"],
2505-
{
2506-
env,
2519+
if (yield* supportsPathspecFromFile(cwd)) {
2520+
yield* runGit(
2521+
"GitVcsDriver.commit.addSelected",
2522+
cwd,
2523+
[...config, "add", "-A", "--pathspec-from-file=-", "--pathspec-file-nul"],
2524+
{
2525+
env,
2526+
// Status paths are repository-relative, while cwd can be a subdirectory.
2527+
stdin: filePaths.map((file) => `:(top,literal)${file}\0`).join(""),
2528+
},
2529+
);
2530+
} else {
2531+
yield* runGit(
2532+
"GitVcsDriver.commit.addSelected",
2533+
cwd,
25072534
// Status paths are repository-relative, while cwd can be a subdirectory.
2508-
stdin: filePaths.map((file) => `:(top,literal)${file}\0`).join(""),
2509-
},
2510-
);
2535+
[...config, "add", "-A", "--", ...filePaths.map((file) => `:(top,literal)${file}`)],
2536+
env ? { env } : {},
2537+
);
2538+
}
25112539
} else {
25122540
yield* runGit(
25132541
"GitVcsDriver.commit.addAll",

0 commit comments

Comments
 (0)