Git - Add commands to the editor title to accept/discard commit message (#153692)

Add commands to the editor title to accept/discard commit message
This commit is contained in:
Ladislau Szomoru 2022-06-29 16:00:09 +02:00 committed by GitHub
parent 6d4dae4873
commit 8045df1b94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 0 deletions

View file

@ -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",

View file

@ -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...",

View file

@ -1711,6 +1711,51 @@ export class CommandCenter {
await this.commitWithAnyInput(repository, { all: true, amend: true });
}
@command('git.commitMessageAccept')
async commitMessageAccept(arg?: Uri): Promise<void> {
if (!arg) { return; }
// Close the tab
this._closeEditorTab(arg);
}
@command('git.commitMessageDiscard')
async commitMessageDiscard(arg?: Uri): Promise<void> {
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<void> {
const root = Uri.file(repository.root);
const config = workspace.getConfiguration('git', root);