From 663810e275674080075a29ced578935ee71ae975 Mon Sep 17 00:00:00 2001 From: Vyacheslav Pukhanov Date: Thu, 10 Sep 2020 12:14:28 +0300 Subject: [PATCH] Adds commands for --no-verify commit variants (#106335) * add `{allow,confirm}NoVerifyCommit` options * adds commit comands with no verify * handles no verify command variants * handle no verify commit option * only display no verify variants when option is set --- extensions/git/package.json | 120 +++++++++++++++++++++++++++++++- extensions/git/package.nls.json | 10 +++ extensions/git/src/api/git.d.ts | 1 + extensions/git/src/commands.ts | 70 ++++++++++++++++++- extensions/git/src/git.ts | 5 ++ 5 files changed, 202 insertions(+), 4 deletions(-) diff --git a/extensions/git/package.json b/extensions/git/package.json index 1e577006c1f..5ea3c70e51f 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -216,6 +216,47 @@ "title": "%command.commitAllAmend%", "category": "Git" }, + { + "command": "git.commitNoVerify", + "title": "%command.commitNoVerify%", + "category": "Git", + "icon": "$(check)" + }, + { + "command": "git.commitStagedNoVerify", + "title": "%command.commitStagedNoVerify%", + "category": "Git" + }, + { + "command": "git.commitEmptyNoVerify", + "title": "%command.commitEmptyNoVerify%", + "category": "Git" + }, + { + "command": "git.commitStagedSignedNoVerify", + "title": "%command.commitStagedSignedNoVerify%", + "category": "Git" + }, + { + "command": "git.commitStagedAmendNoVerify", + "title": "%command.commitStagedAmendNoVerify%", + "category": "Git" + }, + { + "command": "git.commitAllNoVerify", + "title": "%command.commitAllNoVerify%", + "category": "Git" + }, + { + "command": "git.commitAllSignedNoVerify", + "title": "%command.commitAllSignedNoVerify%", + "category": "Git" + }, + { + "command": "git.commitAllAmendNoVerify", + "title": "%command.commitAllAmendNoVerify%", + "category": "Git" + }, { "command": "git.restoreCommitTemplate", "title": "%command.restoreCommitTemplate%", @@ -581,6 +622,38 @@ "command": "git.commitAllAmend", "when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0" }, + { + "command": "git.commitNoVerify", + "when": "config.git.enabled && !git.missing && config.git.allowNoVerifyCommit && gitOpenRepositoryCount != 0" + }, + { + "command": "git.commitStagedNoVerify", + "when": "config.git.enabled && !git.missing && config.git.allowNoVerifyCommit && gitOpenRepositoryCount != 0" + }, + { + "command": "git.commitEmptyNoVerify", + "when": "config.git.enabled && !git.missing && config.git.allowNoVerifyCommit && gitOpenRepositoryCount != 0" + }, + { + "command": "git.commitStagedSignedNoVerify", + "when": "config.git.enabled && !git.missing && config.git.allowNoVerifyCommit && gitOpenRepositoryCount != 0" + }, + { + "command": "git.commitStagedAmendNoVerify", + "when": "config.git.enabled && !git.missing && config.git.allowNoVerifyCommit && gitOpenRepositoryCount != 0" + }, + { + "command": "git.commitAllNoVerify", + "when": "config.git.enabled && !git.missing && config.git.allowNoVerifyCommit && gitOpenRepositoryCount != 0" + }, + { + "command": "git.commitAllSignedNoVerify", + "when": "config.git.enabled && !git.missing && config.git.allowNoVerifyCommit && gitOpenRepositoryCount != 0" + }, + { + "command": "git.commitAllAmendNoVerify", + "when": "config.git.enabled && !git.missing && config.git.allowNoVerifyCommit && gitOpenRepositoryCount != 0" + }, { "command": "git.restoreCommitTemplate", "when": "false" @@ -1239,13 +1312,38 @@ "command": "git.rebaseAbort", "group": "1_commit@5" }, + { + "command": "git.commitNoVerify", + "group": "1_commit@6", + "when": "config.git.allowNoVerifyCommit" + }, + { + "command": "git.commitStagedNoVerify", + "group": "1_commit@7", + "when": "config.git.allowNoVerifyCommit" + }, + { + "command": "git.commitAllNoVerify", + "group": "1_commit@8", + "when": "config.git.allowNoVerifyCommit" + }, { "command": "git.commitStagedAmend", "group": "2_amend@1" }, { "command": "git.commitAllAmend", - "group": "2_amend@1" + "group": "2_amend@2" + }, + { + "command": "git.commitStagedAmendNoVerify", + "group": "2_amend@3", + "when": "config.git.allowNoVerifyCommit" + }, + { + "command": "git.commitAllAmendNoVerify", + "group": "2_amend@4", + "when": "config.git.allowNoVerifyCommit" }, { "command": "git.commitStagedSigned", @@ -1254,6 +1352,16 @@ { "command": "git.commitAllSigned", "group": "3_signoff@2" + }, + { + "command": "git.commitStagedSignedNoVerify", + "group": "3_signoff@3", + "when": "config.git.allowNoVerifyCommit" + }, + { + "command": "git.commitAllSignedNoVerify", + "group": "3_signoff@4", + "when": "config.git.allowNoVerifyCommit" } ], "git.changes": [ @@ -1730,6 +1838,16 @@ "default": true, "description": "%config.confirmForcePush%" }, + "git.allowNoVerifyCommit": { + "type": "boolean", + "default": false, + "description": "%config.allowNoVerifyCommit%" + }, + "git.confirmNoVerifyCommit": { + "type": "boolean", + "default": true, + "description": "%config.confirmNoVerifyCommit%" + }, "git.openDiffOnClick": { "type": "boolean", "scope": "resource", diff --git a/extensions/git/package.nls.json b/extensions/git/package.nls.json index b2aee5a34ba..561f8c02f4b 100644 --- a/extensions/git/package.nls.json +++ b/extensions/git/package.nls.json @@ -34,6 +34,14 @@ "command.commitAll": "Commit All", "command.commitAllSigned": "Commit All (Signed Off)", "command.commitAllAmend": "Commit All (Amend)", + "command.commitNoVerify": "Commit (No Nerify)", + "command.commitStagedNoVerify": "Commit Staged (No Verify)", + "command.commitEmptyNoVerify": "Commit Empty (No Verify)", + "command.commitStagedSignedNoVerify": "Commit Staged (Signed Off, No Verify)", + "command.commitStagedAmendNoVerify": "Commit Staged (Amend, No Verify)", + "command.commitAllNoVerify": "Commit All (No Verify)", + "command.commitAllSignedNoVerify": "Commit All (Signed Off, No Verify)", + "command.commitAllAmendNoVerify": "Commit All (Amend, No Verify)", "command.restoreCommitTemplate": "Restore Commit Template", "command.undoCommit": "Undo Last Commit", "command.checkout": "Checkout to...", @@ -139,6 +147,8 @@ "config.allowForcePush": "Controls whether force push (with or without lease) is enabled.", "config.useForcePushWithLease": "Controls whether force pushing uses the safer force-with-lease variant.", "config.confirmForcePush": "Controls whether to ask for confirmation before force-pushing.", + "config.allowNoVerifyCommit": "Controls whether commits without running pre-commit and commit-msg hooks are allowed.", + "config.confirmNoVerifyCommit": "Controls whether to ask for confirmation before commiting without verification.", "config.openDiffOnClick": "Controls whether the diff editor should be opened when clicking a change. Otherwise the regular editor will be opened.", "config.supportCancellation": "Controls whether a notification comes up when running the Sync action, which allows the user to cancel the operation.", "config.branchSortOrder": "Controls the sort order for branches.", diff --git a/extensions/git/src/api/git.d.ts b/extensions/git/src/api/git.d.ts index 54f21b96dbb..f63b0b12c3c 100644 --- a/extensions/git/src/api/git.d.ts +++ b/extensions/git/src/api/git.d.ts @@ -130,6 +130,7 @@ export interface CommitOptions { signoff?: boolean; signCommit?: boolean; empty?: boolean; + noVerify?: boolean; } export interface BranchQuery { diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 447cf2ed2b2..287483e40d5 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -1441,6 +1441,26 @@ export class CommandCenter { return false; } + if (opts.noVerify) { + if (!config.get('allowNoVerifyCommit')) { + await window.showErrorMessage(localize('no verify commit not allowed', "Commits without verification are not allowed, please enable them with the 'git.allowNoVerifyCommit' setting.")); + return false; + } + + if (config.get('confirmNoVerifyCommit')) { + const message = localize('confirm no verify commit', "You are about to commit your changes without verification, this skips pre-commit hooks and can be undesirable.\n\nAre you sure to continue?"); + const yes = localize('ok', "OK"); + const neverAgain = localize('never ask again', "OK, Don't Ask Again"); + const pick = await window.showWarningMessage(message, { modal: true }, yes, neverAgain); + + if (pick === neverAgain) { + config.update('confirmNoVerifyCommit', false, true); + } else if (pick !== yes) { + return false; + } + } + } + const message = await getCommitMessage(); if (!message) { @@ -1545,8 +1565,7 @@ export class CommandCenter { await this.commitWithAnyInput(repository, { all: true, amend: true }); } - @command('git.commitEmpty', { repository: true }) - async commitEmpty(repository: Repository): Promise { + private async _commitEmpty(repository: Repository, noVerify?: boolean): Promise { const root = Uri.file(repository.root); const config = workspace.getConfiguration('git', root); const shouldPrompt = config.get('confirmEmptyCommits') === true; @@ -1564,7 +1583,52 @@ export class CommandCenter { } } - await this.commitWithAnyInput(repository, { empty: true }); + await this.commitWithAnyInput(repository, { empty: true, noVerify }); + } + + @command('git.commitEmpty', { repository: true }) + async commitEmpty(repository: Repository): Promise { + await this._commitEmpty(repository); + } + + @command('git.commitNoVerify', { repository: true }) + async commitNoVerify(repository: Repository): Promise { + await this.commitWithAnyInput(repository, { noVerify: true }); + } + + @command('git.commitStagedNoVerify', { repository: true }) + async commitStagedNoVerify(repository: Repository): Promise { + await this.commitWithAnyInput(repository, { all: false, noVerify: true }); + } + + @command('git.commitStagedSignedNoVerify', { repository: true }) + async commitStagedSignedNoVerify(repository: Repository): Promise { + await this.commitWithAnyInput(repository, { all: false, signoff: true, noVerify: true }); + } + + @command('git.commitStagedAmendNoVerify', { repository: true }) + async commitStagedAmendNoVerify(repository: Repository): Promise { + await this.commitWithAnyInput(repository, { all: false, amend: true, noVerify: true }); + } + + @command('git.commitAllNoVerify', { repository: true }) + async commitAllNoVerify(repository: Repository): Promise { + await this.commitWithAnyInput(repository, { all: true, noVerify: true }); + } + + @command('git.commitAllSignedNoVerify', { repository: true }) + async commitAllSignedNoVerify(repository: Repository): Promise { + await this.commitWithAnyInput(repository, { all: true, signoff: true, noVerify: true }); + } + + @command('git.commitAllAmendNoVerify', { repository: true }) + async commitAllAmendNoVerify(repository: Repository): Promise { + await this.commitWithAnyInput(repository, { all: true, amend: true, noVerify: true }); + } + + @command('git.commitEmptyNoVerify', { repository: true }) + async commitEmptyNoVerify(repository: Repository): Promise { + await this._commitEmpty(repository, true); } @command('git.restoreCommitTemplate', { repository: true }) diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index d974c7bf1ca..45e49009932 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -1322,10 +1322,15 @@ export class Repository { if (opts.signCommit) { args.push('-S'); } + if (opts.empty) { args.push('--allow-empty'); } + if (opts.noVerify) { + args.push('--no-verify'); + } + try { await this.run(args, { input: message || '' }); } catch (commitErr) {