Skip to content
Open
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
12 changes: 6 additions & 6 deletions apps/code/src/main/services/git/create-pr-saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface CreatePrSagaInput {
prBody?: string;
draft?: boolean;
stagedOnly?: boolean;
taskId?: string;
}

export interface CreatePrSagaOutput {
Expand All @@ -36,7 +37,7 @@ export interface CreatePrDeps {
commit(
dir: string,
message: string,
stagedOnly?: boolean,
options?: { stagedOnly?: boolean; taskId?: string },
): Promise<CommitOutput>;
getSyncStatus(dir: string): Promise<GitSyncStatus>;
push(dir: string): Promise<PushOutput>;
Expand Down Expand Up @@ -125,11 +126,10 @@ export class CreatePrSaga extends Saga<CreatePrSagaInput, CreatePrSagaOutput> {
await this.step({
name: "committing",
execute: async () => {
const result = await this.deps.commit(
directoryPath,
commitMessage!,
input.stagedOnly,
);
const result = await this.deps.commit(directoryPath, commitMessage!, {
stagedOnly: input.stagedOnly,
taskId: input.taskId,
});
if (!result.success) throw new Error(result.message);
return result;
},
Expand Down
2 changes: 2 additions & 0 deletions apps/code/src/main/services/git/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export const commitInput = z.object({
paths: z.array(z.string()).optional(),
allowEmpty: z.boolean().optional(),
stagedOnly: z.boolean().optional(),
taskId: z.string().optional(),
});

export type CommitInput = z.infer<typeof commitInput>;
Expand Down Expand Up @@ -250,6 +251,7 @@ export const createPrInput = z.object({
prBody: z.string().optional(),
draft: z.boolean().optional(),
stagedOnly: z.boolean().optional(),
taskId: z.string().optional(),
});

export type CreatePrInput = z.infer<typeof createPrInput>;
Expand Down
11 changes: 9 additions & 2 deletions apps/code/src/main/services/git/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
prBody?: string;
draft?: boolean;
stagedOnly?: boolean;
taskId?: string;
}): Promise<CreatePrOutput> {
const { directoryPath, flowId } = input;

Expand All @@ -536,7 +537,7 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
checkoutBranch: (dir, name) => this.checkoutBranch(dir, name),
getChangedFilesHead: (dir) => this.getChangedFilesHead(dir),
generateCommitMessage: (dir) => this.generateCommitMessage(dir),
commit: (dir, msg, stagedOnly) => this.commit(dir, msg, { stagedOnly }),
commit: (dir, msg, opts) => this.commit(dir, msg, opts),
getSyncStatus: (dir) => this.getGitSyncStatus(dir),
push: (dir) => this.push(dir),
publish: (dir) => this.publish(dir),
Expand All @@ -556,6 +557,7 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
prBody: input.prBody,
draft: input.draft,
stagedOnly: input.stagedOnly,
taskId: input.taskId,
});

if (!result.success) {
Expand Down Expand Up @@ -619,7 +621,12 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
public async commit(
directoryPath: string,
message: string,
options?: { paths?: string[]; allowEmpty?: boolean; stagedOnly?: boolean },
options?: {
paths?: string[];
allowEmpty?: boolean;
stagedOnly?: boolean;
taskId?: string;
},
): Promise<CommitOutput> {
const fail = (msg: string): CommitOutput => ({
success: false,
Expand Down
1 change: 1 addition & 0 deletions apps/code/src/main/trpc/routers/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ export const gitRouter = router({
paths: input.paths,
allowEmpty: input.allowEmpty,
stagedOnly: input.stagedOnly,
taskId: input.taskId,
}),
),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export function useGitInteraction(
prBody: store.prBody.trim() || undefined,
draft: store.createPrDraft || undefined,
stagedOnly: stagedOnly || undefined,
taskId,
});

if (!result.success) {
Expand Down Expand Up @@ -349,6 +350,7 @@ export function useGitInteraction(
directoryPath: repoPath,
message,
stagedOnly: stagedOnly || undefined,
taskId,
});

if (!result.success) {
Expand Down
21 changes: 18 additions & 3 deletions packages/git/src/sagas/commit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { GitSaga, type GitSagaInput } from "../git-saga";

function buildPostHogTrailers(taskId?: string): string[] {
const trailers = ["Generated-By: PostHog Code"];
if (taskId) trailers.push(`Task-Id: ${taskId}`);
return trailers;
}

export interface CommitInput extends GitSagaInput {
message: string;
paths?: string[];
allowEmpty?: boolean;
stagedOnly?: boolean;
taskId?: string;
}

export interface CommitOutput {
Expand All @@ -19,7 +26,7 @@ export class CommitSaga extends GitSaga<CommitInput, CommitOutput> {
protected async executeGitOperations(
input: CommitInput,
): Promise<CommitOutput> {
const { message, paths, allowEmpty, stagedOnly } = input;
const { message, paths, allowEmpty, stagedOnly, taskId } = input;

const originalHead = await this.readOnlyStep("get-original-head", () =>
this.git.revparse(["HEAD"]),
Expand Down Expand Up @@ -62,11 +69,19 @@ export class CommitSaga extends GitSaga<CommitInput, CommitOutput> {
});
}

const trailers = buildPostHogTrailers(taskId);

const commitOptions: Record<string, null | string[]> = {};
if (allowEmpty) commitOptions["--allow-empty"] = null;
if (trailers.length > 0) commitOptions["--trailer"] = trailers;

const hasOptions = Object.keys(commitOptions).length > 0;

const commitResult = await this.step({
name: "commit",
execute: () =>
allowEmpty
? this.git.commit(message, undefined, { "--allow-empty": null })
hasOptions
? this.git.commit(message, undefined, commitOptions)
: this.git.commit(message),
rollback: async () => {
await this.git.reset(["--soft", originalHead]);
Expand Down
Loading