From 8045df1b9423e233a79306a90257c28fc7e39a4e Mon Sep 17 00:00:00 2001 From: Ladislau Szomoru <3372902+lszomoru@users.noreply.github.com> Date: Wed, 29 Jun 2022 16:00:09 +0200 Subject: [PATCH] Git - Add commands to the editor title to accept/discard commit message (#153692) Add commands to the editor title to accept/discard commit message --- extensions/git/package.json | 30 ++++++++++++++++++++++ extensions/git/package.nls.json | 2 ++ extensions/git/src/commands.ts | 45 +++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/extensions/git/package.json b/extensions/git/package.json index 32c70fed3b7..3e706e0ec6a 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -307,6 +307,18 @@ "category": "Git", "enablement": "!commitInProgress" }, + { + "command": "git.commitMessageAccept", + "title": "%command.commitMessageAccept%", + "icon": "$(check)", + "category": "Git" + }, + { + "command": "git.commitMessageDiscard", + "title": "%command.commitMessageDiscard%", + "icon": "$(discard)", + "category": "Git" + }, { "command": "git.restoreCommitTemplate", "title": "%command.restoreCommitTemplate%", @@ -796,6 +808,14 @@ "command": "git.restoreCommitTemplate", "when": "false" }, + { + "command": "git.commitMessageAccept", + "when": "false" + }, + { + "command": "git.commitMessageDiscard", + "when": "false" + }, { "command": "git.revealInExplorer", "when": "false" @@ -1481,6 +1501,16 @@ "group": "navigation", "when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0 && !isInDiffEditor && !isMergeEditor && resourceScheme == file && scmActiveResourceHasChanges" }, + { + "command": "git.commitMessageAccept", + "group": "navigation", + "when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0 && editorLangId == git-commit && commitInProgress" + }, + { + "command": "git.commitMessageDiscard", + "group": "navigation", + "when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0 && editorLangId == git-commit && commitInProgress" + }, { "command": "git.stageSelectedRanges", "group": "2_git@1", diff --git a/extensions/git/package.nls.json b/extensions/git/package.nls.json index 2d6c88e59e1..c5b11a7b85a 100644 --- a/extensions/git/package.nls.json +++ b/extensions/git/package.nls.json @@ -46,6 +46,8 @@ "command.commitAllNoVerify": "Commit All (No Verify)", "command.commitAllSignedNoVerify": "Commit All (Signed Off, No Verify)", "command.commitAllAmendNoVerify": "Commit All (Amend, No Verify)", + "command.commitMessageAccept": "Accept Commit Message", + "command.commitMessageDiscard": "Discard Commit Message", "command.restoreCommitTemplate": "Restore Commit Template", "command.undoCommit": "Undo Last Commit", "command.checkout": "Checkout to...", diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 6a7670baacc..052f266eedd 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -1711,6 +1711,51 @@ export class CommandCenter { await this.commitWithAnyInput(repository, { all: true, amend: true }); } + @command('git.commitMessageAccept') + async commitMessageAccept(arg?: Uri): Promise { + if (!arg) { return; } + + // Close the tab + this._closeEditorTab(arg); + } + + @command('git.commitMessageDiscard') + async commitMessageDiscard(arg?: Uri): Promise { + if (!arg) { return; } + + // Clear the contents of the editor + const editors = window.visibleTextEditors + .filter(e => e.document.languageId === 'git-commit' && e.document.uri.toString() === arg.toString()); + + if (editors.length !== 1) { return; } + + const commitMsgEditor = editors[0]; + const commitMsgDocument = commitMsgEditor.document; + + const editResult = await commitMsgEditor.edit(builder => { + const firstLine = commitMsgDocument.lineAt(0); + const lastLine = commitMsgDocument.lineAt(commitMsgDocument.lineCount - 1); + + builder.delete(new Range(firstLine.range.start, lastLine.range.end)); + }); + + if (!editResult) { return; } + + // Save the document + const saveResult = await commitMsgDocument.save(); + if (!saveResult) { return; } + + // Close the tab + this._closeEditorTab(arg); + } + + private _closeEditorTab(uri: Uri): void { + const tabToClose = window.tabGroups.all.map(g => g.tabs).flat() + .filter(t => t.input instanceof TabInputText && t.input.uri.toString() === uri.toString()); + + window.tabGroups.close(tabToClose); + } + private async _commitEmpty(repository: Repository, noVerify?: boolean): Promise { const root = Uri.file(repository.root); const config = workspace.getConfiguration('git', root);