Skip to content

Commit a63d57d

Browse files
sergeibbbd13
authored andcommitted
Isolates shared logic of collecting compare data for AI to a method
1 parent 0a47225 commit a63d57d

File tree

2 files changed

+78
-50
lines changed

2 files changed

+78
-50
lines changed

src/commands/explainBranch.ts

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -81,48 +81,29 @@ export class ExplainBranchCommand extends GlCommandBase {
8181
}
8282

8383
// Get the diff between the branch and its upstream or base
84-
const diffService = repository.git.diff();
85-
if (diffService?.getDiff === undefined) {
86-
void showGenericErrorMessage('Unable to get diff service');
87-
return;
88-
}
89-
90-
const commitsService = repository.git.commits();
91-
if (commitsService?.getLog === undefined) {
92-
void showGenericErrorMessage('Unable to get commits service');
93-
return;
94-
}
95-
96-
const [diffResult, logResult] = await Promise.allSettled([
97-
diffService.getDiff?.(branch.ref, baseBranch.ref, { notation: '...' }),
98-
commitsService.getLog(`${baseBranch.ref}..${branch.ref}`),
99-
]);
84+
const compareData = await this.container.ai.prepareCompareDataForAIRequest(
85+
repository,
86+
branch.ref,
87+
baseBranch.ref,
88+
{
89+
reportNoDiffService: () => void showGenericErrorMessage('Unable to get diff service'),
90+
reportNoCommitsService: () => void showGenericErrorMessage('Unable to get commits service'),
91+
reportNoChanges: () => void showGenericErrorMessage('No changes found to explain'),
92+
},
93+
);
10094

101-
const diff = getSettledValue(diffResult);
102-
const log = getSettledValue(logResult);
103-
if (!diff?.contents || !log?.commits?.size) {
104-
void showGenericErrorMessage('No changes found to explain');
95+
if (compareData == null) {
10596
return;
10697
}
10798

108-
const commitMessages: string[] = [];
109-
for (const commit of [...log.commits.values()].sort((a, b) => a.date.getTime() - b.date.getTime())) {
110-
const message = commit.message ?? commit.summary;
111-
if (message) {
112-
commitMessages.push(
113-
`<commit-message ${commit.date.toISOString()}>\n${
114-
commit.message ?? commit.summary
115-
}\n<end-of-commit-message>`,
116-
);
117-
}
118-
}
99+
const { diff, logMessages } = compareData;
119100

120101
const changes = {
121-
diff: diff.contents,
102+
diff: diff,
122103
message: `Changes in branch ${branch.name}
123104
that is ahead of its target by number of commits with the following messages:\n\n
124105
<commits>
125-
${commitMessages.join('\n\n')}
106+
${logMessages}
126107
<end-of-commits>
127108
`,
128109
};

src/plus/ai/aiProviderService.ts

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,63 @@ export class AIProviderService implements Disposable {
626626
return result != null ? { ...result, parsed: parseSummarizeResult(result.content) } : undefined;
627627
}
628628

629+
async prepareCompareDataForAIRequest(
630+
repo: Repository,
631+
headRef: string,
632+
baseRef: string,
633+
options?: {
634+
cancellation?: CancellationToken;
635+
reportNoDiffService?: () => void;
636+
reportNoCommitsService?: () => void;
637+
reportNoChanges?: () => void;
638+
},
639+
): Promise<{ diff: string; logMessages: string } | undefined> {
640+
const { cancellation, reportNoDiffService, reportNoCommitsService, reportNoChanges } = options ?? {};
641+
const diffService = repo.git.diff();
642+
if (diffService?.getDiff === undefined) {
643+
if (reportNoDiffService) {
644+
reportNoDiffService();
645+
return;
646+
}
647+
}
648+
649+
const commitsService = repo.git.commits();
650+
if (commitsService?.getLog === undefined) {
651+
if (reportNoCommitsService) {
652+
reportNoCommitsService();
653+
return;
654+
}
655+
}
656+
657+
const [diffResult, logResult] = await Promise.allSettled([
658+
diffService.getDiff?.(headRef, baseRef, { notation: '...' }),
659+
commitsService.getLog(`${baseRef}..${headRef}`),
660+
]);
661+
const diff = getSettledValue(diffResult);
662+
const log = getSettledValue(logResult);
663+
664+
if (!diff?.contents || !log?.commits?.size) {
665+
reportNoChanges?.();
666+
return undefined;
667+
}
668+
669+
if (cancellation?.isCancellationRequested) throw new CancellationError();
670+
671+
const commitMessages: string[] = [];
672+
for (const commit of [...log.commits.values()].sort((a, b) => a.date.getTime() - b.date.getTime())) {
673+
const message = commit.message ?? commit.summary;
674+
if (message) {
675+
commitMessages.push(
676+
`<commit-message ${commit.date.toISOString()}>\n${
677+
commit.message ?? commit.summary
678+
}\n<end-of-commit-message>`,
679+
);
680+
}
681+
}
682+
683+
return { diff: diff.contents, logMessages: commitMessages.join('\n\n') };
684+
}
685+
629686
async generateCreatePullRequest(
630687
repo: Repository,
631688
baseRef: string,
@@ -641,31 +698,21 @@ export class AIProviderService implements Disposable {
641698
const result = await this.sendRequest(
642699
'generate-create-pullRequest',
643700
async (model, reporting, cancellation, maxInputTokens, retries) => {
644-
const [diffResult, logResult] = await Promise.allSettled([
645-
repo.git.diff().getDiff?.(headRef, baseRef, { notation: '...' }),
646-
repo.git.commits().getLog(`${baseRef}..${headRef}`),
647-
]);
648-
649-
const diff = getSettledValue(diffResult);
650-
const log = getSettledValue(logResult);
701+
const compareData = await this.prepareCompareDataForAIRequest(repo, headRef, baseRef, {
702+
cancellation: cancellation,
703+
});
651704

652-
if (!diff?.contents || !log?.commits?.size) {
705+
if (!compareData?.diff || !compareData?.logMessages) {
653706
throw new AINoRequestDataError('No changes to generate a pull request from.');
654707
}
655708

656-
if (cancellation.isCancellationRequested) throw new CancellationError();
657-
658-
const commitMessages: string[] = [];
659-
for (const commit of [...log.commits.values()].sort((a, b) => a.date.getTime() - b.date.getTime())) {
660-
commitMessages.push(commit.message ?? commit.summary);
661-
}
662-
709+
const { diff, logMessages } = compareData;
663710
const { prompt } = await this.getPrompt(
664711
'generate-create-pullRequest',
665712
model,
666713
{
667-
diff: diff.contents,
668-
data: commitMessages.join('\n'),
714+
diff: diff,
715+
data: logMessages,
669716
context: options?.context,
670717
instructions: configuration.get('ai.generateCreatePullRequest.customInstructions'),
671718
},

0 commit comments

Comments
 (0)