mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-28 12:33:35 +01:00
Git - branch protection (#149360)
* Initial implementation of branch protection * Pull request feedback * Update extensions/git/src/commands.ts Co-authored-by: João Moreno <joao.moreno@microsoft.com>
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import * as picomatch from 'picomatch';
|
||||
import { Command, commands, Disposable, LineChange, MessageOptions, Position, ProgressLocation, QuickPickItem, Range, SourceControlResourceState, TextDocumentShowOptions, TextEditor, Uri, ViewColumn, window, workspace, WorkspaceEdit, WorkspaceFolder, TimelineItem, env, Selection, TextDocumentContentProvider, InputBoxValidationSeverity } from 'vscode';
|
||||
import TelemetryReporter from '@vscode/extension-telemetry';
|
||||
import * as nls from 'vscode-nls';
|
||||
@@ -1496,6 +1497,36 @@ export class CommandCenter {
|
||||
opts.all = 'tracked';
|
||||
}
|
||||
|
||||
// Branch protection
|
||||
const branchProtection = config.get<string[]>('branchProtection')!.map(bp => bp.trim()).filter(bp => bp !== '');
|
||||
const branchProtectionPrompt = config.get<'alwaysCommit' | 'alwaysCommitToNewBranch' | 'alwaysPrompt'>('branchProtectionPrompt')!;
|
||||
const branchIsProtected = branchProtection.some(bp => picomatch.isMatch(repository.HEAD?.name ?? '', bp));
|
||||
|
||||
if (branchIsProtected && (branchProtectionPrompt === 'alwaysPrompt' || branchProtectionPrompt === 'alwaysCommitToNewBranch')) {
|
||||
const commitToNewBranch = localize('commit to branch', "Commit to a New Branch");
|
||||
|
||||
let pick: string | undefined = commitToNewBranch;
|
||||
|
||||
if (branchProtectionPrompt === 'alwaysPrompt') {
|
||||
const message = localize('confirm branch protection commit', "You are trying to commit to a protected branch and you might not have permission to push your commits to the remote.\n\nHow would you like to proceed?");
|
||||
const commit = localize('commit changes', "Commit Anyway");
|
||||
|
||||
pick = await window.showWarningMessage(message, { modal: true }, commitToNewBranch, commit);
|
||||
}
|
||||
|
||||
if (!pick) {
|
||||
return false;
|
||||
} else if (pick === commitToNewBranch) {
|
||||
const branchName = await this.promptForBranchName(repository);
|
||||
|
||||
if (!branchName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await repository.branch(branchName, true);
|
||||
}
|
||||
}
|
||||
|
||||
await repository.commit(message, opts);
|
||||
|
||||
const postCommitCommand = config.get<'none' | 'push' | 'sync'>('postCommitCommand');
|
||||
|
||||
Reference in New Issue
Block a user