History

Add Git action modal with custom commit message support

- Add a Git action confirmation modal with live commit/push/PR progress states - Accept optional `commitMessage` input and skip AI message generation when provided - Expand server and contracts tests for custom commit message handling

Julius Marminge committed 9 months ago

Commit 36bc85f1 parent 3c939e85 files · +538 −44Browse files

Showing the changes to apps/server/src (2 of 5 files). Show all

Unified · Split

2 files
  1. apps/server/src/gitManager.test.ts +34 −0
  2. apps/server/src/gitManager.ts +41 −13
apps/server/src/gitManager.test.ts+34−0
@@ -248,6 +248,40 @@ describe("GitManager", () => { · 247 unmodified lines
248248 );
249249 });
250250
251+ it("uses custom commit message when provided", async () => {
252+ const repoDir = makeTempDir("t3code-git-manager-");
253+ await initRepo(repoDir);
254+ fs.writeFileSync(path.join(repoDir, "README.md"), "hello\ncustom\n");
255+ let generatedCount = 0;
256+
257+ const manager = new GitManager({
258+ textGenerator: createTextGenerator({
259+ generateCommitMessage: async () => {
260+ generatedCount += 1;
261+ return {
262+ subject: "this should not be used",
263+ body: "",
264+ };
265+ },
266+ }),
267+ });
268+ const result = await manager.runStackedAction({
269+ cwd: repoDir,
270+ action: "commit",
271+ commitMessage: "feat: custom summary line\n\n- details from user",
272+ });
273+
274+ expect(result.commit.status).toBe("created");
275+ expect(result.commit.subject).toBe("feat: custom summary line");
276+ expect(generatedCount).toBe(0);
277+ expect(await runGitStdout(repoDir, ["log", "-1", "--pretty=%s"])).toBe(
278+ "feat: custom summary line",
279+ );
280+ expect(await runGitStdout(repoDir, ["log", "-1", "--pretty=%b"])).toContain(
281+ "- details from user",
282+ );
283+ });
284+
251285 it("skips commit when there are no uncommitted changes", async () => {
252286 const repoDir = makeTempDir("t3code-git-manager-");
253287 await initRepo(repoDir);
apps/server/src/gitManager.ts+41−13
@@ -106,6 +106,24 @@ function sanitizeCommitMessage( · 105 unmodified lines
106106 };
107107 }
108108
109+function parseCustomCommitMessage(raw: string): CommitMessageGenerationResult | null {
110+ const normalized = raw.replace(/\r\n/g, "\n").trim();
111+ if (normalized.length === 0) {
112+ return null;
113+ }
114+
115+ const [firstLine, ...rest] = normalized.split("\n");
116+ const subject = firstLine?.trim() ?? "";
117+ if (subject.length === 0) {
118+ return null;
119+ }
120+
121+ return {
122+ subject,
123+ body: rest.join("\n").trim(),
124+ };
125+}
126+
109127 function extractBranchFromRef(ref: string): string {
110128 const normalized = ref.trim();
111129
@@ -222,7 +240,11 @@ export class GitManager { · 110 unmodified lines
222240 throw new Error("Cannot create a pull request from detached HEAD.");
223241 }
224242
225− const commit = await this.runCommitStep(input.cwd, initialStatus.branch);
243+ const commit = await this.runCommitStep(
244+ input.cwd,
245+ initialStatus.branch,
246+ input.commitMessage,
247+ );
226248
227249 const push = wantsPush
228250 ? await this.gitCore.pushCurrentBranch(input.cwd, initialStatus.branch)
@@ -243,6 +265,7 @@ export class GitManager { · 14 unmodified lines
243265 private async runCommitStep(
244266 cwd: string,
245267 branch: string | null,
268+ commitMessage?: string,
246269 ): Promise<{
247270 status: "created" | "skipped_no_changes";
248271 commitSha?: string | undefined;
@@ -253,18 +276,23 @@ export class GitManager { · 4 unmodified lines
253276 return { status: "skipped_no_changes" };
254277 }
255278
256− let generated: CommitMessageGenerationResult;
257− try {
258− generated = sanitizeCommitMessage(
259− await this.textGenerator.generateCommitMessage({
260− cwd,
261− branch,
262− stagedSummary: limitContext(context.stagedSummary, 8_000),
263− stagedPatch: limitContext(context.stagedPatch, 50_000),
264− }),
265− );
266− } catch (error) {
267− throw asCommandNotFound("codex", error) ?? error;
279+ let generated: CommitMessageGenerationResult | null = parseCustomCommitMessage(
280+ commitMessage ?? "",
281+ );
282+
283+ if (!generated) {
284+ try {
285+ generated = sanitizeCommitMessage(
286+ await this.textGenerator.generateCommitMessage({
287+ cwd,
288+ branch,
289+ stagedSummary: limitContext(context.stagedSummary, 8_000),
290+ stagedPatch: limitContext(context.stagedPatch, 50_000),
291+ }),
292+ );
293+ } catch (error) {
294+ throw asCommandNotFound("codex", error) ?? error;
295+ }
268296 }
269297
270298 const { commitSha } = await this.gitCore.commit(cwd, generated.subject, generated.body);