11import type { TextEditor , Uri } from 'vscode' ;
22import { ProgressLocation } from 'vscode' ;
33import type { Container } from '../container' ;
4- import type { MarkdownContentMetadata } from '../documents/markdown' ;
5- import { GitUri } from '../git/gitUri' ;
64import type { GitBranchReference } from '../git/models/reference' ;
75import { getBranchMergeTargetName } from '../git/utils/-webview/branch.utils' ;
86import { showGenericErrorMessage } from '../messages' ;
9- import type { AIExplainSource } from '../plus/ai/aiProviderService' ;
107import { prepareCompareDataForAIRequest } from '../plus/ai/aiProviderService' ;
118import { ReferencesQuickPickIncludes , showReferencePicker } from '../quickpicks/referencePicker' ;
12- import { getBestRepositoryOrShowPicker } from '../quickpicks/repositoryPicker' ;
139import { command } from '../system/-webview/command' ;
14- import { showMarkdownPreview } from '../system/-webview/markdown' ;
1510import { Logger } from '../system/logger' ;
1611import { getNodeRepoPath } from '../views/nodes/abstract/viewNode' ;
17- import { GlCommandBase } from './commandBase' ;
18- import { getCommandUri } from './commandBase.utils' ;
1912import type { CommandContext } from './commandContext' ;
2013import { isCommandContextViewNodeHasBranch } from './commandContext.utils' ;
14+ import type { ExplainBaseArgs } from './explainBase' ;
15+ import { ExplainCommandBase } from './explainBase' ;
2116
22- export interface ExplainBranchCommandArgs {
23- repoPath ?: string | Uri ;
17+ export interface ExplainBranchCommandArgs extends ExplainBaseArgs {
2418 ref ?: string ;
25- source ?: AIExplainSource ;
2619}
2720
2821@command ( )
29- export class ExplainBranchCommand extends GlCommandBase {
30- constructor ( private readonly container : Container ) {
31- super ( [ 'gitlens.ai.explainBranch' , 'gitlens.ai.explainBranch:views' ] ) ;
22+ export class ExplainBranchCommand extends ExplainCommandBase {
23+ pickerTitle = 'Explain Branch Changes' ;
24+ repoPickerPlaceholder = 'Choose which repository to explain a branch from' ;
25+
26+ constructor ( container : Container ) {
27+ super ( container , [ 'gitlens.ai.explainBranch' , 'gitlens.ai.explainBranch:views' ] ) ;
3228 }
3329
3430 protected override preExecute ( context : CommandContext , args ?: ExplainBranchCommandArgs ) : Promise < void > {
@@ -45,41 +41,26 @@ export class ExplainBranchCommand extends GlCommandBase {
4541 async execute ( editor ?: TextEditor , uri ?: Uri , args ?: ExplainBranchCommandArgs ) : Promise < void > {
4642 args = { ...args } ;
4743
48- let repository ;
49- if ( args ?. repoPath != null ) {
50- repository = this . container . git . getRepository ( args . repoPath ) ;
51- } else {
52- uri = getCommandUri ( uri , editor ) ;
53- const gitUri = uri != null ? await GitUri . fromUri ( uri ) : undefined ;
54- repository = await getBestRepositoryOrShowPicker (
55- gitUri ,
56- editor ,
57- 'Explain Branch Changes' ,
58- 'Choose which repository to explain a branch from' ,
59- ) ;
44+ const svc = await this . getRepositoryService ( editor , uri , args ) ;
45+ if ( svc == null ) {
46+ void showGenericErrorMessage ( 'Unable to find a repository' ) ;
47+ return ;
6048 }
6149
62- if ( repository == null ) return ;
63-
6450 try {
6551 // Clarifying the head branch
6652 if ( args . ref == null ) {
6753 // If no ref is provided, show a picker to select a branch
68- const pick = ( await showReferencePicker (
69- repository . path ,
70- 'Explain Branch Changes' ,
71- 'Choose a branch to explain' ,
72- {
73- include : ReferencesQuickPickIncludes . Branches ,
74- sort : { branches : { current : true } } ,
75- } ,
76- ) ) as GitBranchReference | undefined ;
54+ const pick = ( await showReferencePicker ( svc . path , this . pickerTitle , 'Choose a branch to explain' , {
55+ include : ReferencesQuickPickIncludes . Branches ,
56+ sort : { branches : { current : true } } ,
57+ } ) ) as GitBranchReference | undefined ;
7758 if ( pick ?. ref == null ) return ;
7859 args . ref = pick . ref ;
7960 }
8061
8162 // Get the branch
82- const branch = await repository . git . branches . getBranch ( args . ref ) ;
63+ const branch = await svc . branches . getBranch ( args . ref ) ;
8364 if ( branch == null ) {
8465 void showGenericErrorMessage ( 'Unable to find the specified branch' ) ;
8566 return ;
@@ -89,7 +70,7 @@ export class ExplainBranchCommand extends GlCommandBase {
8970 const baseBranchNameResult = await getBranchMergeTargetName ( this . container , branch ) ;
9071 let baseBranch ;
9172 if ( ! baseBranchNameResult . paused ) {
92- baseBranch = await repository . git . branches . getBranch ( baseBranchNameResult . value ) ;
73+ baseBranch = await svc . branches . getBranch ( baseBranchNameResult . value ) ;
9374 }
9475
9576 if ( ! baseBranch ) {
@@ -98,7 +79,7 @@ export class ExplainBranchCommand extends GlCommandBase {
9879 }
9980
10081 // Get the diff between the branch and its upstream or base
101- const compareData = await prepareCompareDataForAIRequest ( repository , branch . ref , baseBranch . ref , {
82+ const compareData = await prepareCompareDataForAIRequest ( svc , branch . ref , baseBranch . ref , {
10283 reportNoDiffService : ( ) => void showGenericErrorMessage ( 'Unable to get diff service' ) ,
10384 reportNoCommitsService : ( ) => void showGenericErrorMessage ( 'Unable to get commits service' ) ,
10485 reportNoChanges : ( ) => void showGenericErrorMessage ( 'No changes found to explain' ) ,
@@ -138,9 +119,7 @@ export class ExplainBranchCommand extends GlCommandBase {
138119 return ;
139120 }
140121
141- 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 } ` ;
142-
143- const contentMetadata = {
122+ this . openDocument ( result , `/explain/branch/${ branch . ref } /${ result . model . id } ` , {
144123 header : {
145124 title : 'Branch Summary' ,
146125 subtitle : branch . name ,
@@ -149,18 +128,9 @@ export class ExplainBranchCommand extends GlCommandBase {
149128 command : {
150129 label : 'Explain Branch Changes' ,
151130 name : 'gitlens.ai.explainBranch' ,
152- args : { repoPath : repository . path , ref : branch . ref , source : args . source } ,
131+ args : { repoPath : svc . path , ref : branch . ref , source : args . source } ,
153132 } ,
154- } ;
155-
156- const documentUri = this . container . markdown . openDocument (
157- content ,
158- `/explain/branch/${ branch . ref } /${ result . model . id } ` ,
159- branch . name ,
160- contentMetadata as MarkdownContentMetadata ,
161- ) ;
162-
163- showMarkdownPreview ( documentUri ) ;
133+ } ) ;
164134 } catch ( ex ) {
165135 Logger . error ( ex , 'ExplainBranchCommand' , 'execute' ) ;
166136 void showGenericErrorMessage ( 'Unable to explain branch' ) ;
0 commit comments