Skip to content

Commit 86ef9fd

Browse files
committed
send full diff to PR title/body generator
1 parent 8afc4a6 commit 86ef9fd

2 files changed

Lines changed: 39 additions & 24 deletions

File tree

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import path from "node:path";
33
import { execGh } from "@posthog/git/gh";
44
import {
55
getAllBranches,
6-
getChangedFilesBetweenBranches,
76
getChangedFilesDetailed,
87
getCommitConventions,
98
getCommitsBetweenBranches,
109
getCurrentBranch,
1110
getDefaultBranch,
11+
getDiffAgainstRemote,
1212
getDiffStats,
1313
getFileAtHead,
1414
getLatestCommit,
@@ -636,8 +636,12 @@ export class GitService extends TypedEventEmitter<GitServiceEvents> {
636636
draft?: boolean,
637637
): Promise<CreatePrOutput> {
638638
const args = ["pr", "create"];
639-
if (title) args.push("--title", title);
640-
if (body) args.push("--body", body);
639+
if (title) {
640+
args.push("--title", title);
641+
args.push("--body", body || "");
642+
} else {
643+
args.push("--fill");
644+
}
641645
if (draft) args.push("--draft");
642646

643647
const result = await execGh(args, { cwd: directoryPath });
@@ -909,30 +913,27 @@ ${truncatedDiff}`;
909913
]);
910914

911915
const head = currentBranch ?? undefined;
912-
const [commits, branchFiles, uncommittedFiles] = await Promise.all([
916+
const [branchDiff, stagedDiff, unstagedDiff, commits] = await Promise.all([
917+
getDiffAgainstRemote(directoryPath, defaultBranch),
918+
getStagedDiff(directoryPath),
919+
getUnstagedDiff(directoryPath),
913920
getCommitsBetweenBranches(directoryPath, defaultBranch, head, 30),
914-
getChangedFilesBetweenBranches(directoryPath, defaultBranch, head),
915-
this.getChangedFilesHead(directoryPath),
916921
]);
917922

918-
const seenPaths = new Set(branchFiles.map((f) => f.path));
919-
const changedFiles = [...branchFiles];
920-
for (const file of uncommittedFiles) {
921-
if (!seenPaths.has(file.path)) {
922-
changedFiles.push(file);
923-
seenPaths.add(file.path);
924-
}
925-
}
926-
927-
if (commits.length === 0 && changedFiles.length === 0) {
923+
const uncommittedDiff = [stagedDiff, unstagedDiff]
924+
.filter(Boolean)
925+
.join("\n");
926+
const parts = [branchDiff, uncommittedDiff].filter(Boolean);
927+
const fullDiff = parts.join("\n");
928+
if (commits.length === 0 && !fullDiff) {
928929
return { title: "", body: "" };
929930
}
930-
931931
const commitsSummary = commits.map((c) => `- ${c.message}`).join("\n");
932-
933-
const filesSummary = changedFiles
934-
.map((f) => `${f.status}: ${f.path}`)
935-
.join("\n");
932+
const truncatedDiff = fullDiff
933+
? fullDiff.length > MAX_DIFF_LENGTH
934+
? `${fullDiff.slice(0, MAX_DIFF_LENGTH)}\n... (diff truncated)`
935+
: fullDiff
936+
: "";
936937

937938
const templateHint = prTemplate.template
938939
? `The repository has a PR template. Use it as a guide for structure but adapt the content to match the actual changes:\n${prTemplate.template.slice(
@@ -959,6 +960,7 @@ Rules for the body:
959960
- Include a "What changed?" section with bullet points describing the key changes
960961
- Be thorough but concise
961962
- Use markdown formatting
963+
- Only describe changes that are actually in the diff — do not invent or assume changes
962964
${templateHint}
963965
964966
Do not include any explanation outside the TITLE and BODY sections.`;
@@ -970,12 +972,12 @@ Branch: ${currentBranch ?? "unknown"} -> ${defaultBranch}
970972
Commits in this PR:
971973
${commitsSummary || "(no commits yet - changes are uncommitted)"}
972974
973-
Changed files:
974-
${filesSummary || "(no file changes detected)"}`;
975+
Diff:
976+
${truncatedDiff || "(no diff available)"}`;
975977

976978
log.debug("Generating PR title and body", {
977979
commitCount: commits.length,
978-
fileCount: changedFiles.length,
980+
diffLength: fullDiff.length,
979981
hasTemplate: !!prTemplate.template,
980982
});
981983

packages/git/src/queries.ts

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

938+
export async function getDiffAgainstRemote(
939+
baseDir: string,
940+
baseBranch: string,
941+
options?: CreateGitClientOptions,
942+
): Promise<string> {
943+
const manager = getGitOperationManager();
944+
return manager.executeRead(
945+
baseDir,
946+
(git) => git.diff([`origin/${baseBranch}...HEAD`]),
947+
{ signal: options?.abortSignal },
948+
);
949+
}
950+
938951
export async function isCommitOnRemote(
939952
baseDir: string,
940953
commit: string,

0 commit comments

Comments
 (0)