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

export interface CreatePrSagaOutput {
Expand All @@ -32,7 +33,11 @@ export interface CreatePrDeps {
): Promise<{ previousBranch: string; currentBranch: string }>;
getChangedFilesHead(dir: string): Promise<ChangedFile[]>;
generateCommitMessage(dir: string): Promise<{ message: string }>;
commit(dir: string, message: string): Promise<CommitOutput>;
commit(
dir: string,
message: string,
stagedOnly?: boolean,
): Promise<CommitOutput>;
getSyncStatus(dir: string): Promise<GitSyncStatus>;
push(dir: string): Promise<PushOutput>;
publish(dir: string): Promise<PublishOutput>;
Expand Down Expand Up @@ -120,7 +125,11 @@ export class CreatePrSaga extends Saga<CreatePrSagaInput, CreatePrSagaOutput> {
await this.step({
name: "committing",
execute: async () => {
const result = await this.deps.commit(directoryPath, commitMessage!);
const result = await this.deps.commit(
directoryPath,
commitMessage!,
input.stagedOnly,
);
if (!result.success) throw new Error(result.message);
return result;
},
Expand Down
15 changes: 12 additions & 3 deletions apps/code/src/main/services/git/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const changedFileSchema = z.object({
originalPath: z.string().optional(),
linesAdded: z.number().optional(),
linesRemoved: z.number().optional(),
staged: z.boolean().optional(),
});

export type ChangedFile = z.infer<typeof changedFileSchema>;
Expand Down Expand Up @@ -120,17 +121,23 @@ export const getFileAtHeadInput = z.object({
});
export const getFileAtHeadOutput = z.string().nullable();

// getDiffHead schemas
export const getDiffHeadInput = z.object({
// Shared diff schemas (getDiffHead, getDiffCached, getDiffUnstaged)
export const diffInput = z.object({
directoryPath: z.string(),
ignoreWhitespace: z.boolean().optional(),
});
export const getDiffHeadOutput = z.string();
export const diffOutput = z.string();

// getDiffStats schemas
export const getDiffStatsInput = directoryPathInput;
export const getDiffStatsOutput = diffStatsSchema;

// stageFiles / unstageFiles shared schema
export const stageFilesInput = z.object({
directoryPath: z.string(),
paths: z.array(z.string()),
});

// getCurrentBranch schemas
export const getCurrentBranchInput = directoryPathInput;
export const getCurrentBranchOutput = z.string().nullable();
Expand Down Expand Up @@ -198,6 +205,7 @@ export const commitInput = z.object({
message: z.string(),
paths: z.array(z.string()).optional(),
allowEmpty: z.boolean().optional(),
stagedOnly: z.boolean().optional(),
});

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

export type CreatePrInput = z.infer<typeof createPrInput>;
Expand Down
43 changes: 38 additions & 5 deletions apps/code/src/main/services/git/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
getUnstagedDiff,
fetch as gitFetch,
isGitRepository,
stageFiles,
unstageFiles,
} from "@posthog/git/queries";
import { CreateBranchSaga, SwitchBranchSaga } from "@posthog/git/sagas/branch";
import { CloneSaga } from "@posthog/git/sagas/clone";
Expand Down Expand Up @@ -266,6 +268,7 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
originalPath: f.originalPath,
linesAdded: f.linesAdded,
linesRemoved: f.linesRemoved,
staged: f.staged,
}));
}

Expand All @@ -283,6 +286,36 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
return getDiffHead(directoryPath, { ignoreWhitespace });
}

public async getDiffCached(
directoryPath: string,
ignoreWhitespace?: boolean,
): Promise<string> {
return getStagedDiff(directoryPath, { ignoreWhitespace });
}

public async getDiffUnstaged(
directoryPath: string,
ignoreWhitespace?: boolean,
): Promise<string> {
return getUnstagedDiff(directoryPath, { ignoreWhitespace });
}

public async stageFiles(
directoryPath: string,
paths: string[],
): Promise<GitStateSnapshot> {
await stageFiles(directoryPath, paths);
return this.getStateSnapshot(directoryPath);
}

public async unstageFiles(
directoryPath: string,
paths: string[],
): Promise<GitStateSnapshot> {
await unstageFiles(directoryPath, paths);
return this.getStateSnapshot(directoryPath);
}

public async getDiffStats(directoryPath: string): Promise<DiffStats> {
const stats = await getDiffStats(directoryPath, {
excludePatterns: [".claude", "CLAUDE.local.md"],
Expand Down Expand Up @@ -479,6 +512,7 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
prTitle?: string;
prBody?: string;
draft?: boolean;
stagedOnly?: boolean;
}): Promise<CreatePrOutput> {
const { directoryPath, flowId } = input;

Expand All @@ -502,7 +536,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) => this.commit(dir, msg),
commit: (dir, msg, stagedOnly) => this.commit(dir, msg, { stagedOnly }),
getSyncStatus: (dir) => this.getGitSyncStatus(dir),
push: (dir) => this.push(dir),
publish: (dir) => this.publish(dir),
Expand All @@ -521,6 +555,7 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
prTitle: input.prTitle,
prBody: input.prBody,
draft: input.draft,
stagedOnly: input.stagedOnly,
});

if (!result.success) {
Expand Down Expand Up @@ -584,8 +619,7 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
public async commit(
directoryPath: string,
message: string,
paths?: string[],
allowEmpty?: boolean,
options?: { paths?: string[]; allowEmpty?: boolean; stagedOnly?: boolean },
): Promise<CommitOutput> {
const fail = (msg: string): CommitOutput => ({
success: false,
Expand All @@ -600,8 +634,7 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
const result = await saga.run({
baseDir: directoryPath,
message: message.trim(),
paths,
allowEmpty,
...options,
});

if (!result.success) return fail(result.error);
Expand Down
49 changes: 39 additions & 10 deletions apps/code/src/main/trpc/routers/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
createPrOutput,
detectRepoInput,
detectRepoOutput,
diffInput,
diffOutput,
discardFileChangesInput,
discardFileChangesOutput,
generateCommitMessageInput,
Expand All @@ -29,8 +31,6 @@ import {
getCommitConventionsOutput,
getCurrentBranchInput,
getCurrentBranchOutput,
getDiffHeadInput,
getDiffHeadOutput,
getDiffStatsInput,
getDiffStatsOutput,
getFileAtHeadInput,
Expand All @@ -45,6 +45,7 @@ import {
getPrTemplateInput,
getPrTemplateOutput,
ghStatusOutput,
gitStateSnapshotSchema,
openPrInput,
openPrOutput,
prStatusInput,
Expand All @@ -57,6 +58,7 @@ import {
pushOutput,
searchGithubIssuesInput,
searchGithubIssuesOutput,
stageFilesInput,
syncInput,
syncOutput,
validateRepoInput,
Expand Down Expand Up @@ -139,17 +141,45 @@ export const gitRouter = router({
),

getDiffHead: publicProcedure
.input(getDiffHeadInput)
.output(getDiffHeadOutput)
.input(diffInput)
.output(diffOutput)
.query(({ input }) =>
getService().getDiffHead(input.directoryPath, input.ignoreWhitespace),
),

getDiffCached: publicProcedure
.input(diffInput)
.output(diffOutput)
.query(({ input }) =>
getService().getDiffCached(input.directoryPath, input.ignoreWhitespace),
),

getDiffUnstaged: publicProcedure
.input(diffInput)
.output(diffOutput)
.query(({ input }) =>
getService().getDiffUnstaged(input.directoryPath, input.ignoreWhitespace),
),

getDiffStats: publicProcedure
.input(getDiffStatsInput)
.output(getDiffStatsOutput)
.query(({ input }) => getService().getDiffStats(input.directoryPath)),

stageFiles: publicProcedure
.input(stageFilesInput)
.output(gitStateSnapshotSchema)
.mutation(({ input }) =>
getService().stageFiles(input.directoryPath, input.paths),
),

unstageFiles: publicProcedure
.input(stageFilesInput)
.output(gitStateSnapshotSchema)
.mutation(({ input }) =>
getService().unstageFiles(input.directoryPath, input.paths),
),

discardFileChanges: publicProcedure
.input(discardFileChangesInput)
.output(discardFileChangesOutput)
Expand Down Expand Up @@ -189,12 +219,11 @@ export const gitRouter = router({
.input(commitInput)
.output(commitOutput)
.mutation(({ input }) =>
getService().commit(
input.directoryPath,
input.message,
input.paths,
input.allowEmpty,
),
getService().commit(input.directoryPath, input.message, {
paths: input.paths,
allowEmpty: input.allowEmpty,
stagedOnly: input.stagedOnly,
}),
),

push: publicProcedure
Expand Down
Loading
Loading