Skip to content

Commit 521f668

Browse files
committed
Adds command to Explain WIP
1 parent b391e08 commit 521f668

File tree

5 files changed

+94
-13
lines changed

5 files changed

+94
-13
lines changed

contributions.json

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@
5151
},
5252
"gitlens.ai.explainWip": {
5353
"label": "Explain Working Changes (Preview)...",
54-
"commandPalette": "gitlens:enabled && !gitlens:readonly && !gitlens:untrusted && gitlens:gk:organization:ai:enabled"
54+
"commandPalette": "gitlens:enabled && !gitlens:readonly && !gitlens:untrusted && gitlens:gk:organization:ai:enabled",
55+
"menus": {
56+
"view/item/context": [
57+
{
58+
"when": "viewItem =~ /gitlens:worktree\\b/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && gitlens:gk:organization:ai:enabled",
59+
"group": "3_gitlens_ai",
60+
"order": 1
61+
}
62+
]
63+
}
5564
},
5665
"gitlens.ai.generateChangelog": {
5766
"label": "Generate Changelog (Preview)...",
@@ -2016,6 +2025,18 @@
20162025
]
20172026
}
20182027
},
2028+
"gitlens.graph.explainWip": {
2029+
"label": "Explain Working Changes (Preview)",
2030+
"menus": {
2031+
"webview/context": [
2032+
{
2033+
"when": "webviewItem =~ /gitlens:wip\\b/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && gitlens:gk:organization:ai:enabled",
2034+
"group": "1_gitlens_actions_3",
2035+
"order": 1
2036+
}
2037+
]
2038+
}
2039+
},
20192040
"gitlens.graph.fetch": {
20202041
"label": "Fetch",
20212042
"icon": "$(repo-fetch)",

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6840,6 +6840,10 @@
68406840
"command": "gitlens.graph.explainStash",
68416841
"title": "Explain Stash (Preview)"
68426842
},
6843+
{
6844+
"command": "gitlens.graph.explainWip",
6845+
"title": "Explain Working Changes (Preview)"
6846+
},
68436847
{
68446848
"command": "gitlens.graph.fetch",
68456849
"title": "Fetch",
@@ -10997,6 +11001,10 @@
1099711001
"command": "gitlens.graph.explainStash",
1099811002
"when": "false"
1099911003
},
11004+
{
11005+
"command": "gitlens.graph.explainWip",
11006+
"when": "false"
11007+
},
1100011008
{
1100111009
"command": "gitlens.graph.fetch",
1100211010
"when": "false"
@@ -17947,6 +17955,11 @@
1794717955
"when": "viewItem =~ /gitlens:worktree\\b/ && !listMultiSelection",
1794817956
"group": "3_gitlens@1"
1794917957
},
17958+
{
17959+
"command": "gitlens.ai.explainWip",
17960+
"when": "viewItem =~ /gitlens:worktree\\b/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && gitlens:gk:organization:ai:enabled",
17961+
"group": "3_gitlens_ai@1"
17962+
},
1795017963
{
1795117964
"command": "gitlens.views.deleteWorktree",
1795217965
"when": "viewItem =~ /gitlens:worktree\\b(?!.*?\\b\\+(active|default)\\b)/ && !listMultiSelection && !gitlens:readonly",
@@ -20382,6 +20395,11 @@
2038220395
"when": "webviewItem == gitlens:wip && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders",
2038320396
"group": "1_gitlens_actions@4"
2038420397
},
20398+
{
20399+
"command": "gitlens.graph.explainWip",
20400+
"when": "webviewItem =~ /gitlens:wip\\b/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && gitlens:gk:organization:ai:enabled",
20401+
"group": "1_gitlens_actions_3@1"
20402+
},
2038520403
{
2038620404
"command": "gitlens.graph.hideRefGroup",
2038720405
"when": "webviewItemGroup =~ /gitlens:refGroup\\b(?!.*?\\b\\+current\\b)/",

src/commands/explainWip.ts

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Logger } from '../system/logger';
1212
import { GlCommandBase } from './commandBase';
1313
import { getCommandUri } from './commandBase.utils';
1414
import type { CommandContext } from './commandContext';
15+
import { isCommandContextViewNodeHasRepoPath, isCommandContextViewNodeHasRepository } from './commandContext.utils';
1516

1617
export interface ExplainWipCommandArgs {
1718
repoPath?: string | Uri;
@@ -26,21 +27,38 @@ export class ExplainWipCommand extends GlCommandBase {
2627
}
2728

2829
protected override preExecute(context: CommandContext, args?: ExplainWipCommandArgs): Promise<void> {
30+
if (isCommandContextViewNodeHasRepository(context)) {
31+
args = { ...args };
32+
args.repoPath = context.node.repo.path;
33+
args.source = args.source ?? { source: 'view', type: 'wip' };
34+
} else if (isCommandContextViewNodeHasRepoPath(context)) {
35+
args = { ...args };
36+
args.repoPath = context.node.repoPath;
37+
args.source = args.source ?? { source: 'view', type: 'wip' };
38+
}
39+
2940
return this.execute(context.editor, context.uri, args);
3041
}
3142

3243
async execute(editor?: TextEditor, uri?: Uri, args?: ExplainWipCommandArgs): Promise<void> {
3344
args = { ...args };
34-
uri = getCommandUri(uri, editor);
3545

36-
const gitUri = uri != null ? await GitUri.fromUri(uri) : undefined;
46+
let repository;
47+
if (args?.repoPath != null) {
48+
repository = this.container.git.getRepository(args.repoPath);
49+
}
50+
51+
if (repository == null) {
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 Working Changes',
58+
'Choose which repository to explain working changes from',
59+
);
60+
}
3761

38-
const repository = await getBestRepositoryOrShowPicker(
39-
gitUri,
40-
editor,
41-
'Explain Working Changes',
42-
'Choose which repository to explain working changes from',
43-
);
4462
if (repository == null) return;
4563

4664
try {
@@ -51,7 +69,18 @@ export class ExplainWipCommand extends GlCommandBase {
5169
return;
5270
}
5371

54-
const diff = await diffService.getDiff(args?.staged ? uncommittedStaged : uncommitted);
72+
// If args?.staged is undefined, should we get all changes (staged and unstaged)?
73+
let stagedLabel;
74+
let to;
75+
if (args?.staged === true) {
76+
stagedLabel = 'Staged';
77+
to = uncommittedStaged;
78+
} else {
79+
stagedLabel = 'Unstaged';
80+
to = uncommitted;
81+
}
82+
83+
const diff = await diffService.getDiff(to);
5584
if (!diff?.contents) {
5685
void showGenericErrorMessage('No working changes found to explain');
5786
return;
@@ -61,7 +90,7 @@ export class ExplainWipCommand extends GlCommandBase {
6190
const result = await this.container.ai.explainChanges(
6291
{
6392
diff: diff.contents,
64-
message: args?.staged ? 'Staged working changes' : 'Unstaged working changes',
93+
message: `${stagedLabel} working changes`,
6594
},
6695
args.source ?? { source: 'commandPalette', type: 'wip' },
6796
{
@@ -70,9 +99,9 @@ export class ExplainWipCommand extends GlCommandBase {
7099
);
71100

72101
// Display the result
73-
let content = `# ${args?.staged ? 'Staged' : 'Unstaged'} Working Changes\n`;
102+
let content = `# Working Changes Summary\n\n`;
74103
if (result != null) {
75-
content += `> Generated by ${result.model.name}\n\n----\n\n${result?.parsed.summary}\n\n${result?.parsed.body}`;
104+
content += `> Generated by ${result.model.name}\n\n## ${stagedLabel} Changes\n\n${result?.parsed.summary}\n\n${result?.parsed.body}`;
76105
} else {
77106
content += `> No changes found to explain.`;
78107
}

src/constants.commands.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export type ContributedCommands =
7373
| 'gitlens.graph.deleteTag'
7474
| 'gitlens.graph.explainCommit'
7575
| 'gitlens.graph.explainStash'
76+
| 'gitlens.graph.explainWip'
7677
| 'gitlens.graph.fetch'
7778
| 'gitlens.graph.hideLocalBranch'
7879
| 'gitlens.graph.hideRefGroup'

src/webviews/plus/graph/graphWebview.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ export class GraphWebviewProvider implements WebviewProvider<State, State, Graph
688688
this.host.registerWebviewCommand('gitlens.graph.ai.generateCommitMessage', this.generateCommitMessage),
689689
this.host.registerWebviewCommand('gitlens.graph.explainCommit', this.explainCommit),
690690
this.host.registerWebviewCommand('gitlens.graph.explainStash', this.explainStash),
691+
this.host.registerWebviewCommand('gitlens.graph.explainWip', this.explainWip),
691692

692693
this.host.registerWebviewCommand('gitlens.graph.compareSelectedCommits.multi', this.compareSelectedCommits),
693694
this.host.registerWebviewCommand('gitlens.graph.abortPausedOperation', this.abortPausedOperation),
@@ -3828,6 +3829,17 @@ export class GraphWebviewProvider implements WebviewProvider<State, State, Graph
38283829
});
38293830
}
38303831

3832+
@log()
3833+
private explainWip(item?: GraphItemContext) {
3834+
const ref = this.getGraphItemRef(item, 'revision');
3835+
if (ref == null) return Promise.resolve();
3836+
3837+
return executeCommand('gitlens.ai.explainWip', {
3838+
repoPath: ref.repoPath,
3839+
source: { source: 'graph', type: 'wip' },
3840+
});
3841+
}
3842+
38313843
@log()
38323844
private async openFiles(item?: GraphItemContext) {
38333845
const commit = await this.getCommitFromGraphItemRef(item);

0 commit comments

Comments
 (0)