Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions apps/server/src/git/GitManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3116,6 +3116,53 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
}),
);

it.effect.each([undefined, ["README.md"]])(
"a failed generation preserves staging changed while generating (paths: %s)",
(filePaths) =>
Effect.gen(function* () {
const repoDir = yield* makeTempDir("t3code-git-manager-");
yield* initRepo(repoDir);
NodeFS.writeFileSync(NodePath.join(repoDir, "README.md"), "hello\nstaged\n");
yield* runGit(repoDir, ["add", "README.md"]);
NodeFS.appendFileSync(NodePath.join(repoDir, "README.md"), "unstaged\n");
NodeFS.writeFileSync(NodePath.join(repoDir, "untracked.txt"), "untracked\n");
const indexBefore = NodeFS.readFileSync(NodePath.join(repoDir, ".git/index"));
const headBefore = (yield* runGit(repoDir, ["rev-parse", "HEAD"])).stdout;
const gitDriver = yield* GitVcsDriver.GitVcsDriver;
let indexDuring: Buffer | undefined;
let indexAfterUserStage: Buffer | undefined;
const { manager } = yield* makeManager({
textGeneration: {
generateCommitMessage: () =>
Effect.gen(function* () {
indexDuring = NodeFS.readFileSync(NodePath.join(repoDir, ".git/index"));
yield* runGit(repoDir, ["add", "untracked.txt"]).pipe(
Effect.provideService(GitVcsDriver.GitVcsDriver, gitDriver),
Effect.orDie,
);
indexAfterUserStage = NodeFS.readFileSync(NodePath.join(repoDir, ".git/index"));
return yield* new TextGenerationError({
operation: "generateCommitMessage",
detail: "Provider rejected generation",
});
}),
},
});
const result = yield* runStackedAction(manager, {
cwd: repoDir,
action: "commit",
...(filePaths ? { filePaths } : {}),
}).pipe(Effect.result);
expect(Result.isFailure(result)).toBe(true);
expect(indexDuring).toEqual(indexBefore);
expect(NodeFS.readFileSync(NodePath.join(repoDir, ".git/index"))).toEqual(
indexAfterUserStage,
);
expect((yield* runGit(repoDir, ["rev-parse", "HEAD"])).stdout).toBe(headBefore);
expect((yield* runGit(repoDir, ["diff"])).stdout).toContain("+unstaged");
}),
);

it.effect("uses custom commit message when provided", () =>
Effect.gen(function* () {
const repoDir = yield* makeTempDir("t3code-git-manager-");
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/git/GitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,7 @@ export const make = Effect.gen(function* () {
: null;
const { commitSha } = yield* gitCore.commit(cwd, suggestion.subject, suggestion.body, {
timeoutMs: COMMIT_TIMEOUT_MS,
stage: filePaths ? { filePaths } : {},
Comment thread
maria-rcks marked this conversation as resolved.
...(commitProgress ? { progress: commitProgress } : {}),
});
if (currentHookName !== null) {
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/vcs/GitVcsDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ export interface GitCommitProgress {
export interface GitCommitOptions {
readonly timeoutMs?: number;
readonly progress?: GitCommitProgress;
/** Stage the current working tree immediately before committing. */
readonly stage?: { readonly filePaths?: readonly string[] };
}

export interface GitDeleteLocalBranchInput {
Expand Down
101 changes: 92 additions & 9 deletions apps/server/src/vcs/GitVcsDriverCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3052,7 +3052,7 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
});

describe("commit context", () => {
it.effect("stages selected files and commits only those files", () =>
it.effect("prepares selected files without staging and commits only those files", () =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
yield* initRepoWithCommit(cwd);
Expand All @@ -3064,8 +3064,9 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
const context = yield* driver.prepareCommitContext(cwd, ["a.txt"]);
assert.include(context?.stagedSummary ?? "", "a.txt");
assert.notInclude(context?.stagedSummary ?? "", "b.txt");
assert.equal(yield* git(cwd, ["diff", "--cached", "--name-only"]), "");

const commit = yield* driver.commit(cwd, "Add a", "");
const commit = yield* driver.commit(cwd, "Add a", "", { stage: { filePaths: ["a.txt"] } });
assert.match(commit.commitSha, /^[a-f0-9]{40}$/);
assert.equal(yield* git(cwd, ["log", "-1", "--pretty=%s"]), "Add a");

Expand All @@ -3084,14 +3085,96 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
yield* writeTextFile(cwd, "selected[1].txt", "literal\n");
yield* writeTextFile(cwd, "selected1.txt", "pattern match\n");

yield* driver.prepareCommitContext(cwd, ["selected[1].txt"]);
const context = yield* driver.prepareCommitContext(cwd, ["selected[1].txt"]);

assert.equal(yield* git(cwd, ["diff", "--cached", "--name-only"]), "selected[1].txt");
assert.include(context?.stagedSummary ?? "", "selected[1].txt");
assert.notInclude(context?.stagedSummary ?? "", "selected1.txt");
assert.equal(yield* git(cwd, ["diff", "--cached", "--name-only"]), "");
yield* driver.commit(cwd, "Add literal path", "", {
stage: { filePaths: ["selected[1].txt"] },
});
assert.equal(
yield* git(cwd, ["show", "--pretty=", "--name-only", "HEAD"]),
"selected[1].txt",
);

const status = yield* git(cwd, ["status", "--porcelain"]);
assert.include(status, "?? selected1.txt");
}),
);

it.effect.each([false, true])(
"preserves partial staging and split-index files (split: %s)",
(split) =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
yield* initRepoWithCommit(cwd);
if (split) yield* git(cwd, ["config", "core.splitIndex", "true"]);
yield* writeTextFile(cwd, "README.md", "# test\nstaged\n");
yield* git(cwd, ["add", "README.md"]);
yield* writeTextFile(cwd, "README.md", "# test\nstaged\nunstaged\n");
yield* writeTextFile(cwd, "untracked.txt", "untracked\n");
yield* writeTextFile(cwd, ".gitignore", "ignored.txt\n");
yield* writeTextFile(cwd, "ignored.txt", "ignored\n");
const before = NodeFS.readFileSync(`${cwd}/.git/index`);
const shared = NodeFS.readdirSync(`${cwd}/.git`).filter((name) =>
name.startsWith("sharedindex."),
);
const driver = yield* GitVcsDriver.GitVcsDriver;
for (const paths of [undefined, ["README.md"]]) {
const context = yield* driver.prepareCommitContext(cwd, paths);
assert.include(context?.stagedPatch ?? "", "unstaged");
assert.notInclude(context?.stagedSummary ?? "", "ignored.txt");
assert.deepEqual(NodeFS.readFileSync(`${cwd}/.git/index`), before);
assert.deepEqual(
NodeFS.readdirSync(`${cwd}/.git`).filter((name) => name.startsWith("sharedindex.")),
shared,
);
}
}),
);

it.effect("prepares an unborn repository without creating a real index", () =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
const driver = yield* GitVcsDriver.GitVcsDriver;
yield* driver.initRepo({ cwd });
yield* git(cwd, ["config", "user.email", "test@test.com"]);
yield* git(cwd, ["config", "user.name", "Test"]);
yield* writeTextFile(cwd, "initial.txt", "initial\n");
for (const paths of [undefined, ["initial.txt"]]) {
const context = yield* driver.prepareCommitContext(cwd, paths);
assert.include(context?.stagedSummary ?? "", "initial.txt");
assert.isFalse(NodeFS.existsSync(`${cwd}/.git/index`));
}
yield* driver.commit(cwd, "Initial", "", { stage: { filePaths: ["initial.txt"] } });
assert.equal(yield* git(cwd, ["show", "HEAD:initial.txt"]), "initial");
}),
);

it.effect("preserves merge metadata while preparing selected and all files", () =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
const { initialBranch } = yield* initRepoWithCommit(cwd);
yield* git(cwd, ["checkout", "-b", "feature"]);
yield* writeTextFile(cwd, "feature.txt", "feature\n");
yield* git(cwd, ["add", "."]);
yield* git(cwd, ["commit", "-m", "Feature"]);
yield* git(cwd, ["checkout", initialBranch]);
yield* git(cwd, ["merge", "--no-commit", "--no-ff", "feature"]);
yield* writeTextFile(cwd, "README.md", "# test\nmerge edit\n");
const names = ["index", "MERGE_HEAD", "MERGE_MSG", "ORIG_HEAD"];
const before = names.map((name) => NodeFS.readFileSync(`${cwd}/.git/${name}`));
const driver = yield* GitVcsDriver.GitVcsDriver;
for (const paths of [undefined, ["README.md"]]) {
yield* driver.prepareCommitContext(cwd, paths);
assert.deepEqual(
names.map((name) => NodeFS.readFileSync(`${cwd}/.git/${name}`)),
before,
);
}
}),
);
});

describe("remote operations", () => {
Expand Down Expand Up @@ -3271,7 +3354,7 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
refName: "feature/push",
});
yield* writeTextFile(cwd, "feature.txt", "feature\n");
yield* (yield* GitVcsDriver.GitVcsDriver).prepareCommitContext(cwd);
yield* git(cwd, ["add", "-A"]);
yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add feature", "");

const pushed = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null);
Expand Down Expand Up @@ -3343,7 +3426,7 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
yield* git(cwd, ["remote", "add", "origin", remote]);
yield* git(cwd, ["push", "-u", "origin", "main"]);
yield* writeTextFile(cwd, "upstream.txt", "upstream\n");
yield* driver.prepareCommitContext(cwd);
yield* git(cwd, ["add", "-A"]);
yield* driver.commit(cwd, "Add upstream update", "");

const pushed = yield* driver.pushCurrentBranch(cwd, null);
Expand Down Expand Up @@ -3384,7 +3467,7 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
const devSha = yield* git(cwd, ["rev-parse", "HEAD"]);
yield* git(cwd, ["checkout", "-b", "feature/x", "origin/dev"]);
yield* writeTextFile(cwd, "feature.txt", "feature\n");
yield* driver.prepareCommitContext(cwd);
yield* git(cwd, ["add", "-A"]);
yield* driver.commit(cwd, "Add feature", "");

const pushed = yield* driver.pushCurrentBranch(cwd, null);
Expand Down Expand Up @@ -3418,7 +3501,7 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
yield* git(cwd, ["checkout", "-b", "feature/y", "origin/main"]);
yield* git(cwd, ["config", "branch.feature/y.gh-merge-base", "release/v2"]);
yield* writeTextFile(cwd, "feature.txt", "feature\n");
yield* driver.prepareCommitContext(cwd);
yield* git(cwd, ["add", "-A"]);
yield* driver.commit(cwd, "Add feature", "");

const pushed = yield* driver.pushCurrentBranch(cwd, null);
Expand Down Expand Up @@ -3456,7 +3539,7 @@ it.layer(TestLayer)("GitVcsDriver core integration", (it) => {
"upstream/effect-atom",
);
yield* writeTextFile(cwd, "alias.txt", "alias\n");
yield* driver.prepareCommitContext(cwd);
yield* git(cwd, ["add", "-A"]);
yield* driver.commit(cwd, "Add alias update", "");

const pushed = yield* driver.pushCurrentBranch(cwd, null);
Expand Down
Loading
Loading