TypeScript
fix(server): preserve merge ancestry on selected-file commits (#24744)
Alex Southwell66eb776c9b45
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.tsindex 5f80c8c2fcc..8363e16c324 100644--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts@@ -4824,6 +4824,137 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {}),);+ it.effect.each([+ { location: "root", ref: "refs/heads/MERGE_HEAD" },+ { location: "nested", ref: "refs/heads/MERGE_HEAD" },+ { location: "worktree", ref: "refs/heads/MERGE_HEAD" },+ { location: "root", ref: "refs/tags/MERGE_HEAD" },+ { location: "nested", ref: "refs/tags/MERGE_HEAD" },+ { location: "worktree", ref: "refs/tags/MERGE_HEAD" },+ ])(+ "commits selected files with a MERGE_HEAD ref and no merge ($location, $ref)",+ ({ location, ref }) =>+ Effect.gen(function* () {+ let cwd = yield* makeTmpDir();+ yield* initRepoWithCommit(cwd);+ const driver = yield* GitVcsDriver.GitVcsDriver;+ const fs = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const originalHead = yield* git(cwd, ["rev-parse", "HEAD"]);+ yield* git(cwd, ["update-ref", ref, originalHead]);+ if (location === "worktree") {+ const linked = path.join(yield* makeTmpDir(), "linked");+ yield* git(cwd, ["worktree", "add", "-b", "linked", linked]);+ cwd = linked;+ }+ const commitCwd = location === "nested" ? path.join(cwd, "nested") : cwd;+ yield* fs.makeDirectory(commitCwd, { recursive: true });+ yield* writeTextFile(cwd, "selected.txt", "selected\n");+ yield* writeTextFile(cwd, "unrelated.txt", "unrelated\n");+ const mergePath = yield* git(commitCwd, ["rev-parse", "--git-path", "MERGE_HEAD"]);+ assert.isFalse(yield* fs.exists(path.resolve(commitCwd, mergePath)));++ yield* driver.commit(commitCwd, "Selected without merge", "", {+ stage: { filePaths: ["selected.txt"] },+ });++ assert.equal(yield* git(cwd, ["show", "-s", "--format=%P", "HEAD"]), originalHead);+ assert.equal(+ yield* git(cwd, ["show", "--pretty=", "--name-only", "HEAD"]),+ "selected.txt",+ );+ assert.equal(yield* git(cwd, ["rev-parse", ref]), originalHead);+ assert.include(yield* git(cwd, ["status", "--porcelain"]), "?? unrelated.txt");+ }),+ );++ it.effect.each([+ { location: "root", conflict: false },+ { location: "nested", conflict: false },+ { location: "worktree", conflict: false },+ { location: "root", conflict: true },+ { location: "nested", conflict: true },+ { location: "worktree", conflict: true },+ ])(+ "rejects selected-file merge commits without mutation ($location, conflict: $conflict)",+ ({ location, conflict }) =>+ Effect.gen(function* () {+ let cwd = yield* makeTmpDir();+ const { initialBranch } = yield* initRepoWithCommit(cwd);+ const driver = yield* GitVcsDriver.GitVcsDriver;+ const fs = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ yield* git(cwd, ["checkout", "-b", "feature"]);+ yield* writeTextFile(cwd, "feature.txt", "feature\n");+ if (conflict) yield* writeTextFile(cwd, "README.md", "feature version\n");+ yield* git(cwd, ["add", "."]);+ yield* git(cwd, ["commit", "-m", "Feature"]);+ const featureHead = yield* git(cwd, ["rev-parse", "HEAD"]);+ yield* git(cwd, ["checkout", initialBranch]);+ yield* writeTextFile(cwd, "main.txt", "main\n");+ if (conflict) yield* writeTextFile(cwd, "README.md", "main version\n");+ yield* git(cwd, ["add", "."]);+ yield* git(cwd, ["commit", "-m", "Main"]);+ const mainHead = yield* git(cwd, ["rev-parse", "HEAD"]);+ if (location === "worktree") {+ const worktree = path.join(yield* makeTmpDir(), "linked");+ yield* git(cwd, ["worktree", "add", "-b", "linked", worktree]);+ cwd = worktree;+ }+ const merge = yield* driver.execute({+ operation: "test.merge",+ cwd,+ args: ["merge", "--no-commit", "--no-ff", "feature"],+ allowNonZeroExit: true,+ });+ assert.equal(merge.exitCode, conflict ? 1 : 0);+ yield* writeTextFile(cwd, "unrelated.txt", "unrelated\n");+ const commitCwd = location === "nested" ? path.join(cwd, "nested") : cwd;+ yield* fs.makeDirectory(commitCwd, { recursive: true });+ const gitDir = yield* git(cwd, ["rev-parse", "--absolute-git-dir"]);+ const names = ["index", "MERGE_HEAD", "MERGE_MSG", "MERGE_MODE", "ORIG_HEAD"];+ const before = names.map((name) => NodeFS.readFileSync(path.join(gitDir, name)));+ const files = ["README.md", "feature.txt", "main.txt", "unrelated.txt"];+ const contents = files.map((name) => NodeFS.readFileSync(path.join(cwd, name)));+ const unmerged = yield* git(cwd, ["ls-files", "--unmerged"]);++ const result = yield* driver+ .commit(commitCwd, "Partial merge", "", { stage: { filePaths: ["feature.txt"] } })+ .pipe(Effect.result);+ if (Result.isSuccess(result)) {+ const parents = yield* git(cwd, ["show", "-s", "--format=%P", "HEAD"]);+ assert.fail(+ `Selected merge commit succeeded with parents ${parents}; MERGE_HEAD exists: ${NodeFS.existsSync(path.join(gitDir, "MERGE_HEAD"))}`,+ );+ } else {+ assert.include(result.failure.detail, "Cannot commit selected files during a merge");+ assert.include(result.failure.detail, "Commit all files");+ }+ assert.deepEqual(+ names.map((name) => NodeFS.readFileSync(path.join(gitDir, name))),+ before,+ );+ assert.deepEqual(+ files.map((name) => NodeFS.readFileSync(path.join(cwd, name))),+ contents,+ );+ assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), mainHead);+ assert.equal(yield* git(cwd, ["ls-files", "--unmerged"]), unmerged);++ if (conflict) yield* writeTextFile(cwd, "README.md", "resolved version\n");+ yield* driver.commit(commitCwd, "Finish merge", "", { stage: {} });+ assert.equal(+ yield* git(cwd, ["show", "-s", "--format=%P", "HEAD"]),+ `${mainHead} ${featureHead}`,+ );+ assert.isFalse(NodeFS.existsSync(path.join(gitDir, "MERGE_HEAD")));+ assert.equal(yield* git(cwd, ["show", "HEAD:feature.txt"]), "feature");+ assert.equal(yield* git(cwd, ["show", "HEAD:unrelated.txt"]), "unrelated");+ if (conflict)+ assert.equal(yield* git(cwd, ["show", "HEAD:README.md"]), "resolved version");+ }),+ );+it.effect("treats selected file paths literally", () =>Effect.gen(function* () {const cwd = yield* makeTmpDir();diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.tsindex b66f93fa6ac..71945b0b6f7 100644--- a/apps/server/src/vcs/GitVcsDriverCore.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.ts@@ -2394,6 +2394,33 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*{ env },);} else {+ const mergeHeadPath = yield* runGitStdout("GitVcsDriver.commit.mergeHead", cwd, [+ "rev-parse",+ "--git-path",+ "MERGE_HEAD",+ ]);+ const isMerging = yield* fileSystem.exists(path.resolve(cwd, mergeHeadPath.trim())).pipe(+ Effect.mapError(+ (cause) =>+ new GitCommandError({+ operation: "GitVcsDriver.commit.mergeHead",+ command: "git commit",+ cwd,+ detail: "Could not check whether a merge is in progress.",+ cause,+ }),+ ),+ );+ // Like git commit --only, reject partial merges before reset can discard their ancestry.+ if (isMerging) {+ return yield* new GitCommandError({+ operation: "GitVcsDriver.commit",+ command: "git commit",+ cwd,+ detail:+ "Cannot commit selected files during a merge. Commit all files to finish the merge.",+ });+ }yield* runGit("GitVcsDriver.commit.reset", cwd, ["reset"]).pipe(Effect.catchTags({ GitCommandError: () => Effect.void }),);