Git - polish view stash command (#202499)

* Implement drop stash command

* Add apply and pop commands

* Rename "preview" command to "view"

* Fixed action order

* Add icons, update commands
This commit is contained in:
Ladislau Szomoru 2024-01-15 14:49:01 +01:00 committed by GitHub
parent 2b06224582
commit 3ae8c8ea62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 139 additions and 20 deletions

View file

@ -668,6 +668,13 @@
"category": "Git",
"enablement": "!operationInProgress"
},
{
"command": "git.stashPopEditor",
"title": "%command.stashPopEditor%",
"icon": "$(git-stash-pop)",
"category": "Git",
"enablement": "!operationInProgress"
},
{
"command": "git.stashApply",
"title": "%command.stashApply%",
@ -680,6 +687,13 @@
"category": "Git",
"enablement": "!operationInProgress"
},
{
"command": "git.stashApplyEditor",
"title": "%command.stashApplyEditor%",
"icon": "$(git-stash-apply)",
"category": "Git",
"enablement": "!operationInProgress"
},
{
"command": "git.stashDrop",
"title": "%command.stashDrop%",
@ -693,8 +707,15 @@
"enablement": "!operationInProgress"
},
{
"command": "git.stashPreview",
"title": "%command.stashPreview%",
"command": "git.stashDropEditor",
"title": "%command.stashDropEditor%",
"icon": "$(trash)",
"category": "Git",
"enablement": "!operationInProgress"
},
{
"command": "git.stashView",
"title": "%command.stashView%",
"category": "Git",
"enablement": "!operationInProgress"
},
@ -1215,6 +1236,10 @@
"command": "git.stashPopLatest",
"when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0"
},
{
"command": "git.stashPopEditor",
"when": "false"
},
{
"command": "git.stashApply",
"when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0"
@ -1223,6 +1248,10 @@
"command": "git.stashApplyLatest",
"when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0"
},
{
"command": "git.stashApplyEditor",
"when": "false"
},
{
"command": "git.stashDrop",
"when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0"
@ -1231,6 +1260,10 @@
"command": "git.stashDropAll",
"when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0"
},
{
"command": "git.stashDropEditor",
"when": "false"
},
{
"command": "git.timeline.openDiff",
"when": "false"
@ -1284,7 +1317,7 @@
"when": "config.git.enabled && !git.missing && git.parentRepositoryCount != 0"
},
{
"command": "git.stashPreview",
"command": "git.stashView",
"when": "config.git.enabled && !git.missing && config.multiDiffEditor.experimental.enabled"
},
{
@ -1837,6 +1870,17 @@
"command": "git.revertSelectedRanges",
"group": "2_git@3",
"when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0 && isInDiffEditor && resourceScheme =~ /^git$|^file$/"
},
{
"command": "git.stashApplyEditor",
"alt": "git.stashPopEditor",
"group": "navigation@1",
"when": "config.git.enabled && !git.missing && resourceScheme == git-stash"
},
{
"command": "git.stashDropEditor",
"group": "navigation@2",
"when": "config.git.enabled && !git.missing && resourceScheme == git-stash"
}
],
"editor/context": [
@ -2168,7 +2212,7 @@
"group": "4_drop@2"
},
{
"command": "git.stashPreview",
"command": "git.stashView",
"when": "config.multiDiffEditor.experimental.enabled",
"group": "5_preview@1"
}

View file

@ -100,11 +100,14 @@
"command.stashStaged": "Stash Staged",
"command.stashPop": "Pop Stash...",
"command.stashPopLatest": "Pop Latest Stash",
"command.stashPopEditor": "Pop Stash",
"command.stashApply": "Apply Stash...",
"command.stashApplyLatest": "Apply Latest Stash",
"command.stashApplyEditor": "Apply Stash",
"command.stashDrop": "Drop Stash...",
"command.stashDropAll": "Drop All Stashes...",
"command.stashPreview": "Preview Stash...",
"command.stashDropEditor": "Drop Stash",
"command.stashView": "View Stash...",
"command.timelineOpenDiff": "Open Changes",
"command.timelineCopyCommitId": "Copy Commit ID",
"command.timelineCopyCommitMessage": "Copy Commit Message",

View file

@ -3535,6 +3535,17 @@ export class CommandCenter {
await repository.popStash();
}
@command('git.stashPopEditor')
async stashPopEditor(uri: Uri): Promise<void> {
const result = await this.getStashFromUri(uri);
if (!result) {
return;
}
await result.repository.popStash(result.stash.index);
await commands.executeCommand('workbench.action.closeActiveEditor');
}
@command('git.stashApply', { repository: true })
async stashApply(repository: Repository): Promise<void> {
const placeHolder = l10n.t('Pick a stash to apply');
@ -3559,6 +3570,17 @@ export class CommandCenter {
await repository.applyStash();
}
@command('git.stashApplyEditor')
async stashApplyEditor(uri: Uri): Promise<void> {
const result = await this.getStashFromUri(uri);
if (!result) {
return;
}
await result.repository.applyStash(result.stash.index);
await commands.executeCommand('workbench.action.closeActiveEditor');
}
@command('git.stashDrop', { repository: true })
async stashDrop(repository: Repository): Promise<void> {
const placeHolder = l10n.t('Pick a stash to drop');
@ -3568,18 +3590,7 @@ export class CommandCenter {
return;
}
// request confirmation for the operation
const yes = l10n.t('Yes');
const result = await window.showWarningMessage(
l10n.t('Are you sure you want to drop the stash: {0}?', stash.description),
{ modal: true },
yes
);
if (result !== yes) {
return;
}
await repository.dropStash(stash.index);
await this._stashDrop(repository, stash);
}
@command('git.stashDropAll', { repository: true })
@ -3605,9 +3616,36 @@ export class CommandCenter {
await repository.dropStash();
}
@command('git.stashPreview', { repository: true })
async stashPreview(repository: Repository): Promise<void> {
const placeHolder = l10n.t('Pick a stash to preview');
@command('git.stashDropEditor')
async stashDropEditor(uri: Uri): Promise<void> {
const result = await this.getStashFromUri(uri);
if (!result) {
return;
}
if (await this._stashDrop(result.repository, result.stash)) {
await commands.executeCommand('workbench.action.closeActiveEditor');
}
}
async _stashDrop(repository: Repository, stash: Stash): Promise<boolean> {
const yes = l10n.t('Yes');
const result = await window.showWarningMessage(
l10n.t('Are you sure you want to drop the stash: {0}?', stash.description),
{ modal: true },
yes
);
if (result !== yes) {
return false;
}
await repository.dropStash(stash.index);
return true;
}
@command('git.stashView', { repository: true })
async stashView(repository: Repository): Promise<void> {
const placeHolder = l10n.t('Pick a stash to view');
const stash = await this.pickStash(repository, placeHolder);
if (!stash) {
@ -3646,6 +3684,36 @@ export class CommandCenter {
return result?.stash;
}
private async getStashFromUri(uri: Uri | undefined): Promise<{ repository: Repository; stash: Stash } | undefined> {
if (!uri || uri.scheme !== 'git-stash') {
return undefined;
}
const stashUri = fromGitUri(uri);
// Repository
const repository = this.model.getRepository(stashUri.path);
if (!repository) {
return undefined;
}
// Stash
const regex = /^stash@{(\d+)}$/;
const match = regex.exec(stashUri.ref);
if (!match) {
return undefined;
}
const [, index] = match;
const stashes = await repository.getStashes();
const stash = stashes.find(stash => stash.index === parseInt(index));
if (!stash) {
return undefined;
}
return { repository, stash };
}
@command('git.timeline.openDiff', { repository: false })
async timelineOpenDiff(item: TimelineItem, uri: Uri | undefined, _source: string) {
const cmd = this.resolveTimelineOpenDiffCommand(

View file

@ -588,6 +588,9 @@ export const Codicon = {
diffSingle: register('diff-single', 0xec22),
diffMultiple: register('diff-multiple', 0xec23),
surroundWith: register('surround-with', 0xec24),
gitStash: register('git-stash', 0xec26),
gitStashApply: register('git-stash-apply', 0xec27),
gitStashPop: register('git-stash-pop', 0xec28),
// derived icons, that could become separate icons

View file

@ -48,6 +48,7 @@ export class CollapseAllAction extends Action2 {
when: ContextKeyExpr.and(ContextKeyExpr.equals('activeEditor', MultiDiffEditor.ID), ContextKeyExpr.not('multiDiffEditorAllCollapsed')),
id: MenuId.EditorTitle,
group: 'navigation',
order: 100
},
f1: true,
});