Skip to content

Commit 41d47e2

Browse files
authored
feat(code): add git diff head query (#1412)
## Problem in #1410 i am adding a new diff viewer it needs to be very fast we don't have a sufficiently-fast query yet <!-- Who is this for and what problem does it solve? --> <!-- Closes #ISSUE_ID --> ## Changes adds a new query `getDiffHead` to slurp up the whole diff in one query <!-- What did you change and why? --> <!-- If there are frontend changes, include screenshots. --> ## How did you test this? <!-- Describe what you tested -- manual steps, automated tests, or both. --> <!-- If you're an agent, only list tests you actually ran. -->
1 parent 2cf02ce commit 41d47e2

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

apps/code/src/main/services/git/schemas.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ export const getFileAtHeadInput = z.object({
120120
});
121121
export const getFileAtHeadOutput = z.string().nullable();
122122

123+
// getDiffHead schemas
124+
export const getDiffHeadInput = z.object({
125+
directoryPath: z.string(),
126+
ignoreWhitespace: z.boolean().optional(),
127+
});
128+
export const getDiffHeadOutput = z.string();
129+
123130
// getDiffStats schemas
124131
export const getDiffStatsInput = directoryPathInput;
125132
export const getDiffStatsOutput = diffStatsSchema;

apps/code/src/main/services/git/service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getCurrentBranch,
1010
getDefaultBranch,
1111
getDiffAgainstRemote,
12+
getDiffHead,
1213
getDiffStats,
1314
getFileAtHead,
1415
getLatestCommit,
@@ -275,6 +276,13 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
275276
return getFileAtHead(directoryPath, filePath);
276277
}
277278

279+
public async getDiffHead(
280+
directoryPath: string,
281+
ignoreWhitespace?: boolean,
282+
): Promise<string> {
283+
return getDiffHead(directoryPath, { ignoreWhitespace });
284+
}
285+
278286
public async getDiffStats(directoryPath: string): Promise<DiffStats> {
279287
const stats = await getDiffStats(directoryPath, {
280288
excludePatterns: [".claude", "CLAUDE.local.md"],

apps/code/src/main/trpc/routers/git.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import {
2929
getCommitConventionsOutput,
3030
getCurrentBranchInput,
3131
getCurrentBranchOutput,
32+
getDiffHeadInput,
33+
getDiffHeadOutput,
3234
getDiffStatsInput,
3335
getDiffStatsOutput,
3436
getFileAtHeadInput,
@@ -136,6 +138,13 @@ export const gitRouter = router({
136138
getService().getFileAtHead(input.directoryPath, input.filePath),
137139
),
138140

141+
getDiffHead: publicProcedure
142+
.input(getDiffHeadInput)
143+
.output(getDiffHeadOutput)
144+
.query(({ input }) =>
145+
getService().getDiffHead(input.directoryPath, input.ignoreWhitespace),
146+
),
147+
139148
getDiffStats: publicProcedure
140149
.input(getDiffStatsInput)
141150
.output(getDiffStatsOutput)

packages/git/src/queries.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,18 @@ export async function getUnstagedDiff(
935935
});
936936
}
937937

938+
export async function getDiffHead(
939+
baseDir: string,
940+
options?: CreateGitClientOptions & { ignoreWhitespace?: boolean },
941+
): Promise<string> {
942+
const manager = getGitOperationManager();
943+
const args = ["HEAD"];
944+
if (options?.ignoreWhitespace) args.push("--ignore-all-space");
945+
return manager.executeRead(baseDir, (git) => git.diff(args), {
946+
signal: options?.abortSignal,
947+
});
948+
}
949+
938950
export async function getDiffAgainstRemote(
939951
baseDir: string,
940952
baseBranch: string,

0 commit comments

Comments
 (0)