@devries-cameron20 / t3code-1 / commits / 66eb776c9b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): preserve merge ancestry on selected-file commits (#24744)

Co-authored-by: SAPHID <saphid@users.noreply.github.com>

Alex Southwell committed Oct 31, 2026 at 08:47 UTC 66eb776c9b45e819ff6e5774fa753f10f1c0f7b7
2 files changed +158
apps/server/src/vcs/GitVcsDriverCore.test.ts
+131
@@ -4824,6 +4824,137 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
4824 }),
4825 );
4826
4827 + it.effect.each([
4828 + { location: "root", ref: "refs/heads/MERGE_HEAD" },
4829 + { location: "nested", ref: "refs/heads/MERGE_HEAD" },
4830 + { location: "worktree", ref: "refs/heads/MERGE_HEAD" },
4831 + { location: "root", ref: "refs/tags/MERGE_HEAD" },
4832 + { location: "nested", ref: "refs/tags/MERGE_HEAD" },
4833 + { location: "worktree", ref: "refs/tags/MERGE_HEAD" },
4834 + ])(
4835 + "commits selected files with a MERGE_HEAD ref and no merge ($location, $ref)",
4836 + ({ location, ref }) =>
4837 + Effect.gen(function* () {
4838 + let cwd = yield* makeTmpDir();
4839 + yield* initRepoWithCommit(cwd);
4840 + const driver = yield* GitVcsDriver.GitVcsDriver;
4841 + const fs = yield* FileSystem.FileSystem;
4842 + const path = yield* Path.Path;
4843 + const originalHead = yield* git(cwd, ["rev-parse", "HEAD"]);
4844 + yield* git(cwd, ["update-ref", ref, originalHead]);
4845 + if (location === "worktree") {
4846 + const linked = path.join(yield* makeTmpDir(), "linked");
4847 + yield* git(cwd, ["worktree", "add", "-b", "linked", linked]);
4848 + cwd = linked;
4849 + }
4850 + const commitCwd = location === "nested" ? path.join(cwd, "nested") : cwd;
4851 + yield* fs.makeDirectory(commitCwd, { recursive: true });
4852 + yield* writeTextFile(cwd, "selected.txt", "selected\n");
4853 + yield* writeTextFile(cwd, "unrelated.txt", "unrelated\n");
4854 + const mergePath = yield* git(commitCwd, ["rev-parse", "--git-path", "MERGE_HEAD"]);
4855 + assert.isFalse(yield* fs.exists(path.resolve(commitCwd, mergePath)));
4856 +
4857 + yield* driver.commit(commitCwd, "Selected without merge", "", {
4858 + stage: { filePaths: ["selected.txt"] },
4859 + });
4860 +
4861 + assert.equal(yield* git(cwd, ["show", "-s", "--format=%P", "HEAD"]), originalHead);
4862 + assert.equal(
4863 + yield* git(cwd, ["show", "--pretty=", "--name-only", "HEAD"]),
4864 + "selected.txt",
4865 + );
4866 + assert.equal(yield* git(cwd, ["rev-parse", ref]), originalHead);
4867 + assert.include(yield* git(cwd, ["status", "--porcelain"]), "?? unrelated.txt");
4868 + }),
4869 + );
4870 +
4871 + it.effect.each([
4872 + { location: "root", conflict: false },
4873 + { location: "nested", conflict: false },
4874 + { location: "worktree", conflict: false },
4875 + { location: "root", conflict: true },
4876 + { location: "nested", conflict: true },
4877 + { location: "worktree", conflict: true },
4878 + ])(
4879 + "rejects selected-file merge commits without mutation ($location, conflict: $conflict)",
4880 + ({ location, conflict }) =>
4881 + Effect.gen(function* () {
4882 + let cwd = yield* makeTmpDir();
4883 + const { initialBranch } = yield* initRepoWithCommit(cwd);
4884 + const driver = yield* GitVcsDriver.GitVcsDriver;
4885 + const fs = yield* FileSystem.FileSystem;
4886 + const path = yield* Path.Path;
4887 + yield* git(cwd, ["checkout", "-b", "feature"]);
4888 + yield* writeTextFile(cwd, "feature.txt", "feature\n");
4889 + if (conflict) yield* writeTextFile(cwd, "README.md", "feature version\n");
4890 + yield* git(cwd, ["add", "."]);
4891 + yield* git(cwd, ["commit", "-m", "Feature"]);
4892 + const featureHead = yield* git(cwd, ["rev-parse", "HEAD"]);
4893 + yield* git(cwd, ["checkout", initialBranch]);
4894 + yield* writeTextFile(cwd, "main.txt", "main\n");
4895 + if (conflict) yield* writeTextFile(cwd, "README.md", "main version\n");
4896 + yield* git(cwd, ["add", "."]);
4897 + yield* git(cwd, ["commit", "-m", "Main"]);
4898 + const mainHead = yield* git(cwd, ["rev-parse", "HEAD"]);
4899 + if (location === "worktree") {
4900 + const worktree = path.join(yield* makeTmpDir(), "linked");
4901 + yield* git(cwd, ["worktree", "add", "-b", "linked", worktree]);
4902 + cwd = worktree;
4903 + }
4904 + const merge = yield* driver.execute({
4905 + operation: "test.merge",
4906 + cwd,
4907 + args: ["merge", "--no-commit", "--no-ff", "feature"],
4908 + allowNonZeroExit: true,
4909 + });
4910 + assert.equal(merge.exitCode, conflict ? 1 : 0);
4911 + yield* writeTextFile(cwd, "unrelated.txt", "unrelated\n");
4912 + const commitCwd = location === "nested" ? path.join(cwd, "nested") : cwd;
4913 + yield* fs.makeDirectory(commitCwd, { recursive: true });
4914 + const gitDir = yield* git(cwd, ["rev-parse", "--absolute-git-dir"]);
4915 + const names = ["index", "MERGE_HEAD", "MERGE_MSG", "MERGE_MODE", "ORIG_HEAD"];
4916 + const before = names.map((name) => NodeFS.readFileSync(path.join(gitDir, name)));
4917 + const files = ["README.md", "feature.txt", "main.txt", "unrelated.txt"];
4918 + const contents = files.map((name) => NodeFS.readFileSync(path.join(cwd, name)));
4919 + const unmerged = yield* git(cwd, ["ls-files", "--unmerged"]);
4920 +
4921 + const result = yield* driver
4922 + .commit(commitCwd, "Partial merge", "", { stage: { filePaths: ["feature.txt"] } })
4923 + .pipe(Effect.result);
4924 + if (Result.isSuccess(result)) {
4925 + const parents = yield* git(cwd, ["show", "-s", "--format=%P", "HEAD"]);
4926 + assert.fail(
4927 + `Selected merge commit succeeded with parents ${parents}; MERGE_HEAD exists: ${NodeFS.existsSync(path.join(gitDir, "MERGE_HEAD"))}`,
4928 + );
4929 + } else {
4930 + assert.include(result.failure.detail, "Cannot commit selected files during a merge");
4931 + assert.include(result.failure.detail, "Commit all files");
4932 + }
4933 + assert.deepEqual(
4934 + names.map((name) => NodeFS.readFileSync(path.join(gitDir, name))),
4935 + before,
4936 + );
4937 + assert.deepEqual(
4938 + files.map((name) => NodeFS.readFileSync(path.join(cwd, name))),
4939 + contents,
4940 + );
4941 + assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), mainHead);
4942 + assert.equal(yield* git(cwd, ["ls-files", "--unmerged"]), unmerged);
4943 +
4944 + if (conflict) yield* writeTextFile(cwd, "README.md", "resolved version\n");
4945 + yield* driver.commit(commitCwd, "Finish merge", "", { stage: {} });
4946 + assert.equal(
4947 + yield* git(cwd, ["show", "-s", "--format=%P", "HEAD"]),
4948 + `${mainHead} ${featureHead}`,
4949 + );
4950 + assert.isFalse(NodeFS.existsSync(path.join(gitDir, "MERGE_HEAD")));
4951 + assert.equal(yield* git(cwd, ["show", "HEAD:feature.txt"]), "feature");
4952 + assert.equal(yield* git(cwd, ["show", "HEAD:unrelated.txt"]), "unrelated");
4953 + if (conflict)
4954 + assert.equal(yield* git(cwd, ["show", "HEAD:README.md"]), "resolved version");
4955 + }),
4956 + );
4957 +
4958 it.effect("treats selected file paths literally", () =>
4959 Effect.gen(function* () {
4960 const cwd = yield* makeTmpDir();
apps/server/src/vcs/GitVcsDriverCore.ts
+27
@@ -2394,6 +2394,33 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2394 { env },
2395 );
2396 } else {
2397 + const mergeHeadPath = yield* runGitStdout("GitVcsDriver.commit.mergeHead", cwd, [
2398 + "rev-parse",
2399 + "--git-path",
2400 + "MERGE_HEAD",
2401 + ]);
2402 + const isMerging = yield* fileSystem.exists(path.resolve(cwd, mergeHeadPath.trim())).pipe(
2403 + Effect.mapError(
2404 + (cause) =>
2405 + new GitCommandError({
2406 + operation: "GitVcsDriver.commit.mergeHead",
2407 + command: "git commit",
2408 + cwd,
2409 + detail: "Could not check whether a merge is in progress.",
2410 + cause,
2411 + }),
2412 + ),
2413 + );
2414 + // Like git commit --only, reject partial merges before reset can discard their ancestry.
2415 + if (isMerging) {
2416 + return yield* new GitCommandError({
2417 + operation: "GitVcsDriver.commit",
2418 + command: "git commit",
2419 + cwd,
2420 + detail:
2421 + "Cannot commit selected files during a merge. Commit all files to finish the merge.",
2422 + });
2423 + }
2424 yield* runGit("GitVcsDriver.commit.reset", cwd, ["reset"]).pipe(
2425 Effect.catchTags({ GitCommandError: () => Effect.void }),
2426 );