Skip to content

Commit b6e99f6

Browse files
committed
Updates explain branch
- improves markdown format - moves prepareCompareDataForAIRequest out of AIProviderService
1 parent 5ac7bb6 commit b6e99f6

File tree

2 files changed

+74
-82
lines changed

2 files changed

+74
-82
lines changed

src/commands/explainBranch.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { GitUri } from '../git/gitUri';
55
import type { GitBranch } from '../git/models/branch';
66
import { showGenericErrorMessage } from '../messages';
77
import type { AIExplainSource } from '../plus/ai/aiProviderService';
8+
import { prepareCompareDataForAIRequest } from '../plus/ai/aiProviderService';
89
import { ReferencesQuickPickIncludes, showReferencePicker } from '../quickpicks/referencePicker';
910
import { getBestRepositoryOrShowPicker } from '../quickpicks/repositoryPicker';
1011
import { command } from '../system/-webview/command';
@@ -59,7 +60,7 @@ export class ExplainBranchCommand extends GlCommandBase {
5960
if (repository == null) return;
6061

6162
try {
62-
//// Clarifying the head branch
63+
// Clarifying the head branch
6364
if (args.ref == null) {
6465
// If no ref is provided, show a picker to select a branch
6566
const pick = await showReferencePicker(
@@ -82,27 +83,20 @@ export class ExplainBranchCommand extends GlCommandBase {
8283
return;
8384
}
8485

85-
//// Clarifying the base branch
86+
// Clarifying the base branch
8687
const baseBranchName = await getMergeTarget(this.container, branch);
8788
const baseBranch = await repository.git.branches().getBranch(baseBranchName);
8889
if (!baseBranch) {
89-
void showGenericErrorMessage(
90-
'Unable to find the base branch for the specified branch. Probably it is undefined. Set it up and try again.',
91-
);
90+
void showGenericErrorMessage(`Unable to find the base branch for ${branch.name}.`);
9291
return;
9392
}
9493

9594
// Get the diff between the branch and its upstream or base
96-
const compareData = await this.container.ai.prepareCompareDataForAIRequest(
97-
repository,
98-
branch.ref,
99-
baseBranch.ref,
100-
{
101-
reportNoDiffService: () => void showGenericErrorMessage('Unable to get diff service'),
102-
reportNoCommitsService: () => void showGenericErrorMessage('Unable to get commits service'),
103-
reportNoChanges: () => void showGenericErrorMessage('No changes found to explain'),
104-
},
105-
);
95+
const compareData = await prepareCompareDataForAIRequest(repository, branch.ref, baseBranch.ref, {
96+
reportNoDiffService: () => void showGenericErrorMessage('Unable to get diff service'),
97+
reportNoCommitsService: () => void showGenericErrorMessage('Unable to get commits service'),
98+
reportNoChanges: () => void showGenericErrorMessage('No changes found to explain'),
99+
});
106100

107101
if (compareData == null) {
108102
return;
@@ -129,16 +123,14 @@ export class ExplainBranchCommand extends GlCommandBase {
129123
},
130124
);
131125

132-
// Display the result
133-
let content = `# Branch: ${branch.name}\n`;
134-
if (result != null) {
135-
content += `> Generated by ${result.model.name}\n\n----\n\n${result?.parsed.summary}\n\n${result?.parsed.body}`;
136-
} else {
137-
content += `> No changes found to explain.`;
126+
if (result == null) {
127+
void showGenericErrorMessage(`Unable to explain branch ${branch.name}`);
128+
return;
138129
}
139-
// Add changes temporarily for debug purposes, so it's easier to review what content has been explained
140-
const changesMd = `${changes.message}\n\n${changes.diff}`;
141-
void showMarkdownPreview(`${content}\n\n\`\`\`\n${changesMd.replaceAll('`', '')}\n\`\`\`\n`);
130+
131+
const content = `# Branch Summary\n\n> Generated by ${result.model.name}\n\n## ${branch.name}\n\n${result?.parsed.summary}\n\n${result?.parsed.body}`;
132+
133+
void showMarkdownPreview(content);
142134
} catch (ex) {
143135
Logger.error(ex, 'ExplainBranchCommand', 'execute');
144136
void showGenericErrorMessage('Unable to explain branch');

src/plus/ai/aiProviderService.ts

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -626,63 +626,6 @@ 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-
686629
async generateCreatePullRequest(
687630
repo: Repository,
688631
baseRef: string,
@@ -698,7 +641,7 @@ export class AIProviderService implements Disposable {
698641
const result = await this.sendRequest(
699642
'generate-create-pullRequest',
700643
async (model, reporting, cancellation, maxInputTokens, retries) => {
701-
const compareData = await this.prepareCompareDataForAIRequest(repo, headRef, baseRef, {
644+
const compareData = await prepareCompareDataForAIRequest(repo, headRef, baseRef, {
702645
cancellation: cancellation,
703646
});
704647

@@ -1452,3 +1395,60 @@ function isPrimaryAIProvider(provider: AIProviders): provider is AIPrimaryProvid
14521395
function isPrimaryAIProviderModel(model: AIModel): model is AIModel<AIPrimaryProviders, AIProviderAndModel> {
14531396
return isPrimaryAIProvider(model.provider.id);
14541397
}
1398+
1399+
export async function prepareCompareDataForAIRequest(
1400+
repo: Repository,
1401+
headRef: string,
1402+
baseRef: string,
1403+
options?: {
1404+
cancellation?: CancellationToken;
1405+
reportNoDiffService?: () => void;
1406+
reportNoCommitsService?: () => void;
1407+
reportNoChanges?: () => void;
1408+
},
1409+
): Promise<{ diff: string; logMessages: string } | undefined> {
1410+
const { cancellation, reportNoDiffService, reportNoCommitsService, reportNoChanges } = options ?? {};
1411+
const diffService = repo.git.diff();
1412+
if (diffService?.getDiff === undefined) {
1413+
if (reportNoDiffService) {
1414+
reportNoDiffService();
1415+
return;
1416+
}
1417+
}
1418+
1419+
const commitsService = repo.git.commits();
1420+
if (commitsService?.getLog === undefined) {
1421+
if (reportNoCommitsService) {
1422+
reportNoCommitsService();
1423+
return;
1424+
}
1425+
}
1426+
1427+
const [diffResult, logResult] = await Promise.allSettled([
1428+
diffService.getDiff?.(headRef, baseRef, { notation: '...' }),
1429+
commitsService.getLog(`${baseRef}..${headRef}`),
1430+
]);
1431+
const diff = getSettledValue(diffResult);
1432+
const log = getSettledValue(logResult);
1433+
1434+
if (!diff?.contents || !log?.commits?.size) {
1435+
reportNoChanges?.();
1436+
return undefined;
1437+
}
1438+
1439+
if (cancellation?.isCancellationRequested) throw new CancellationError();
1440+
1441+
const commitMessages: string[] = [];
1442+
for (const commit of [...log.commits.values()].sort((a, b) => a.date.getTime() - b.date.getTime())) {
1443+
const message = commit.message ?? commit.summary;
1444+
if (message) {
1445+
commitMessages.push(
1446+
`<commit-message ${commit.date.toISOString()}>\n${
1447+
commit.message ?? commit.summary
1448+
}\n<end-of-commit-message>`,
1449+
);
1450+
}
1451+
}
1452+
1453+
return { diff: diff.contents, logMessages: commitMessages.join('\n\n') };
1454+
}

0 commit comments

Comments
 (0)