Skip to content

Commit a80c332

Browse files
committed
fix(server): preserve index during commit generation
1 parent 2d156a8 commit a80c332

5 files changed

Lines changed: 281 additions & 60 deletions

File tree

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,51 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
22592259
}),
22602260
);
22612261

2262+
it.effect("preserves the exact index when commit message generation fails", () =>
2263+
Effect.gen(function* () {
2264+
const repoDir = yield* makeTempDir("t3code-git-manager-");
2265+
yield* initRepo(repoDir);
2266+
NodeFS.writeFileSync(NodePath.join(repoDir, "README.md"), "staged before commit flow\n");
2267+
yield* runGit(repoDir, ["add", "README.md"]);
2268+
NodeFS.writeFileSync(NodePath.join(repoDir, "new-file.txt"), "new\n");
2269+
2270+
const indexPath = NodePath.join(repoDir, ".git", "index");
2271+
const originalIndex = NodeFS.readFileSync(indexPath);
2272+
const headBefore = yield* runGit(repoDir, ["rev-parse", "HEAD"]).pipe(
2273+
Effect.map((result) => result.stdout.trim()),
2274+
);
2275+
const { manager } = yield* makeManager({
2276+
textGeneration: {
2277+
generateCommitMessage: () =>
2278+
Effect.fail(
2279+
new TextGenerationError({
2280+
operation: "generateCommitMessage",
2281+
detail: "API key is not configured",
2282+
}),
2283+
),
2284+
},
2285+
});
2286+
2287+
const error = yield* runStackedAction(manager, {
2288+
cwd: repoDir,
2289+
action: "commit",
2290+
}).pipe(Effect.flip);
2291+
2292+
expect(error._tag).toBe("TextGenerationError");
2293+
expect(NodeFS.readFileSync(indexPath).equals(originalIndex)).toBe(true);
2294+
expect(
2295+
yield* runGit(repoDir, ["rev-parse", "HEAD"]).pipe(
2296+
Effect.map((result) => result.stdout.trim()),
2297+
),
2298+
).toBe(headBefore);
2299+
const status = yield* runGit(repoDir, ["status", "--porcelain"]).pipe(
2300+
Effect.map((result) => result.stdout),
2301+
);
2302+
expect(status).toContain("M README.md");
2303+
expect(status).toContain("?? new-file.txt");
2304+
}),
2305+
);
2306+
22622307
it.effect("creates a commit when working tree is dirty", () =>
22632308
Effect.gen(function* () {
22642309
const repoDir = yield* makeTempDir("t3code-git-manager-");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,7 @@ export const make = Effect.gen(function* () {
17611761
const { commitSha } = yield* gitCore.commit(cwd, suggestion.subject, suggestion.body, {
17621762
timeoutMs: COMMIT_TIMEOUT_MS,
17631763
...(commitProgress ? { progress: commitProgress } : {}),
1764+
stage: filePaths ? { filePaths } : {},
17641765
});
17651766
if (currentHookName !== null) {
17661767
yield* emit({

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ export interface GitCommitProgress {
114114
export interface GitCommitOptions {
115115
readonly timeoutMs?: number;
116116
readonly progress?: GitCommitProgress;
117+
readonly stage?: {
118+
readonly filePaths?: readonly string[];
119+
};
117120
}
118121

119122
export interface GitPushResult {

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

Lines changed: 111 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,46 +1597,134 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
15971597
});
15981598

15991599
describe("commit context", () => {
1600-
it.effect("stages selected files and commits only those files", () =>
1601-
Effect.gen(function* () {
1602-
const cwd = yield* makeTmpDir();
1603-
yield* initRepoWithCommit(cwd);
1604-
const driver = yield* GitVcsDriver.GitVcsDriver;
1600+
it.effect(
1601+
"prepares selected files without changing the index and commits only those files",
1602+
() =>
1603+
Effect.gen(function* () {
1604+
const cwd = yield* makeTmpDir();
1605+
yield* initRepoWithCommit(cwd);
1606+
const driver = yield* GitVcsDriver.GitVcsDriver;
1607+
1608+
yield* writeTextFile(cwd, "a.txt", "a\n");
1609+
yield* writeTextFile(cwd, "b.txt", "b\n");
16051610

1606-
yield* writeTextFile(cwd, "a.txt", "a\n");
1607-
yield* writeTextFile(cwd, "b.txt", "b\n");
1611+
const context = yield* driver.prepareCommitContext(cwd, ["a.txt"]);
1612+
assert.include(context?.stagedSummary ?? "", "a.txt");
1613+
assert.notInclude(context?.stagedSummary ?? "", "b.txt");
1614+
assert.equal(yield* git(cwd, ["diff", "--cached", "--name-only"]), "");
16081615

1609-
const context = yield* driver.prepareCommitContext(cwd, ["a.txt"]);
1610-
assert.include(context?.stagedSummary ?? "", "a.txt");
1611-
assert.notInclude(context?.stagedSummary ?? "", "b.txt");
1616+
const commit = yield* driver.commit(cwd, "Add a", "", {
1617+
stage: { filePaths: ["a.txt"] },
1618+
});
1619+
assert.match(commit.commitSha, /^[a-f0-9]{40}$/);
1620+
assert.equal(yield* git(cwd, ["log", "-1", "--pretty=%s"]), "Add a");
16121621

1613-
const commit = yield* driver.commit(cwd, "Add a", "");
1614-
assert.match(commit.commitSha, /^[a-f0-9]{40}$/);
1615-
assert.equal(yield* git(cwd, ["log", "-1", "--pretty=%s"]), "Add a");
1622+
const status = yield* git(cwd, ["status", "--porcelain"]);
1623+
assert.include(status, "?? b.txt");
1624+
assert.notInclude(status, "a.txt");
1625+
}),
1626+
);
16161627

1617-
const status = yield* git(cwd, ["status", "--porcelain"]);
1618-
assert.include(status, "?? b.txt");
1619-
assert.notInclude(status, "a.txt");
1628+
it.effect("prepares selected files without clearing merge state", () =>
1629+
Effect.gen(function* () {
1630+
const cwd = yield* makeTmpDir();
1631+
const { initialBranch } = yield* initRepoWithCommit(cwd);
1632+
const driver = yield* GitVcsDriver.GitVcsDriver;
1633+
const fileSystem = yield* FileSystem.FileSystem;
1634+
const pathService = yield* Path.Path;
1635+
1636+
yield* git(cwd, ["checkout", "-b", "merge-source"]);
1637+
yield* writeTextFile(cwd, "merge-source.txt", "source\n");
1638+
yield* git(cwd, ["add", "merge-source.txt"]);
1639+
yield* git(cwd, ["commit", "-m", "Add merge source"]);
1640+
yield* git(cwd, ["checkout", initialBranch]);
1641+
yield* writeTextFile(cwd, "main.txt", "main\n");
1642+
yield* git(cwd, ["add", "main.txt"]);
1643+
yield* git(cwd, ["commit", "-m", "Advance main"]);
1644+
yield* git(cwd, ["merge", "--no-commit", "--no-ff", "merge-source"]);
1645+
yield* writeTextFile(cwd, "selected.txt", "selected\n");
1646+
1647+
const indexPath = pathService.join(cwd, ".git", "index");
1648+
const mergeHeadPath = pathService.join(cwd, ".git", "MERGE_HEAD");
1649+
const originalIndex = yield* fileSystem.readFile(indexPath);
1650+
const originalMergeHead = yield* fileSystem.readFile(mergeHeadPath);
1651+
1652+
const context = yield* driver.prepareCommitContext(cwd, ["selected.txt"]);
1653+
1654+
assert.include(context?.stagedSummary ?? "", "selected.txt");
1655+
assert.deepEqual(
1656+
Array.from(yield* fileSystem.readFile(indexPath)),
1657+
Array.from(originalIndex),
1658+
);
1659+
assert.deepEqual(
1660+
Array.from(yield* fileSystem.readFile(mergeHeadPath)),
1661+
Array.from(originalMergeHead),
1662+
);
16201663
}),
16211664
);
16221665

16231666
it.effect("treats selected file paths literally", () =>
16241667
Effect.gen(function* () {
16251668
const cwd = yield* makeTmpDir();
1626-
yield* initRepoWithCommit(cwd);
1669+
yield* git(cwd, ["init", "--initial-branch=main"]);
1670+
yield* git(cwd, ["config", "user.email", "[email protected]"]);
1671+
yield* git(cwd, ["config", "user.name", "Test"]);
16271672
const driver = yield* GitVcsDriver.GitVcsDriver;
16281673

16291674
yield* writeTextFile(cwd, "selected[1].txt", "literal\n");
16301675
yield* writeTextFile(cwd, "selected1.txt", "pattern match\n");
1676+
yield* git(cwd, ["add", "selected1.txt"]);
16311677

16321678
yield* driver.prepareCommitContext(cwd, ["selected[1].txt"]);
16331679

1634-
assert.equal(yield* git(cwd, ["diff", "--cached", "--name-only"]), "selected[1].txt");
1680+
assert.equal(yield* git(cwd, ["diff", "--cached", "--name-only"]), "selected1.txt");
1681+
yield* driver.commit(cwd, "Add literal path", "", {
1682+
stage: { filePaths: ["selected[1].txt"] },
1683+
});
1684+
assert.equal(
1685+
yield* git(cwd, ["diff-tree", "--root", "--no-commit-id", "--name-only", "-r", "HEAD"]),
1686+
"selected[1].txt",
1687+
);
16351688

16361689
const status = yield* git(cwd, ["status", "--porcelain"]);
16371690
assert.include(status, "?? selected1.txt");
16381691
}),
16391692
);
1693+
1694+
it.effect("includes force-added ignored files without changing the index", () =>
1695+
Effect.gen(function* () {
1696+
const cwd = yield* makeTmpDir();
1697+
yield* initRepoWithCommit(cwd);
1698+
const driver = yield* GitVcsDriver.GitVcsDriver;
1699+
const fileSystem = yield* FileSystem.FileSystem;
1700+
const pathService = yield* Path.Path;
1701+
1702+
yield* writeTextFile(cwd, ".gitignore", "*.generated\n");
1703+
yield* git(cwd, ["add", ".gitignore"]);
1704+
yield* git(cwd, ["commit", "-m", "Ignore generated files"]);
1705+
yield* writeTextFile(cwd, "included.generated", "included\n");
1706+
yield* git(cwd, ["add", "-f", "included.generated"]);
1707+
const indexPath = pathService.join(cwd, ".git", "index");
1708+
const originalIndex = yield* fileSystem.readFile(indexPath);
1709+
1710+
const allFilesContext = yield* driver.prepareCommitContext(cwd);
1711+
const selectedFileContext = yield* driver.prepareCommitContext(cwd, ["included.generated"]);
1712+
1713+
assert.include(allFilesContext?.stagedSummary ?? "", "included.generated");
1714+
assert.include(selectedFileContext?.stagedSummary ?? "", "included.generated");
1715+
assert.deepEqual(
1716+
Array.from(yield* fileSystem.readFile(indexPath)),
1717+
Array.from(originalIndex),
1718+
);
1719+
yield* driver.commit(cwd, "Add generated file", "", {
1720+
stage: { filePaths: ["included.generated"] },
1721+
});
1722+
assert.equal(
1723+
yield* git(cwd, ["diff-tree", "--no-commit-id", "--name-only", "-r", "HEAD"]),
1724+
"included.generated",
1725+
);
1726+
}),
1727+
);
16401728
});
16411729

16421730
describe("remote operations", () => {
@@ -1727,7 +1815,7 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
17271815
refName: "feature/push",
17281816
});
17291817
yield* writeTextFile(cwd, "feature.txt", "feature\n");
1730-
yield* (yield* GitVcsDriver.GitVcsDriver).prepareCommitContext(cwd);
1818+
yield* git(cwd, ["add", "-A"]);
17311819
yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add feature", "");
17321820

17331821
const pushed = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null);
@@ -1799,7 +1887,7 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
17991887
yield* git(cwd, ["remote", "add", "origin", remote]);
18001888
yield* git(cwd, ["push", "-u", "origin", "main"]);
18011889
yield* writeTextFile(cwd, "upstream.txt", "upstream\n");
1802-
yield* driver.prepareCommitContext(cwd);
1890+
yield* git(cwd, ["add", "-A"]);
18031891
yield* driver.commit(cwd, "Add upstream update", "");
18041892

18051893
const pushed = yield* driver.pushCurrentBranch(cwd, null);
@@ -1840,7 +1928,7 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
18401928
const devSha = yield* git(cwd, ["rev-parse", "HEAD"]);
18411929
yield* git(cwd, ["checkout", "-b", "feature/x", "origin/dev"]);
18421930
yield* writeTextFile(cwd, "feature.txt", "feature\n");
1843-
yield* driver.prepareCommitContext(cwd);
1931+
yield* git(cwd, ["add", "-A"]);
18441932
yield* driver.commit(cwd, "Add feature", "");
18451933

18461934
const pushed = yield* driver.pushCurrentBranch(cwd, null);
@@ -1874,7 +1962,7 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
18741962
yield* git(cwd, ["checkout", "-b", "feature/y", "origin/main"]);
18751963
yield* git(cwd, ["config", "branch.feature/y.gh-merge-base", "release/v2"]);
18761964
yield* writeTextFile(cwd, "feature.txt", "feature\n");
1877-
yield* driver.prepareCommitContext(cwd);
1965+
yield* git(cwd, ["add", "-A"]);
18781966
yield* driver.commit(cwd, "Add feature", "");
18791967

18801968
const pushed = yield* driver.pushCurrentBranch(cwd, null);
@@ -1912,7 +2000,7 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
19122000
"upstream/effect-atom",
19132001
);
19142002
yield* writeTextFile(cwd, "alias.txt", "alias\n");
1915-
yield* driver.prepareCommitContext(cwd);
2003+
yield* git(cwd, ["add", "-A"]);
19162004
yield* driver.commit(cwd, "Add alias update", "");
19172005

19182006
const pushed = yield* driver.pushCurrentBranch(cwd, null);

0 commit comments

Comments
 (0)