Path: blob/main/extensions/git/src/branchProtection.ts
3316 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Disposable, Event, EventEmitter, Uri, workspace } from 'vscode';6import { BranchProtection, BranchProtectionProvider } from './api/git';7import { dispose, filterEvent } from './util';89export interface IBranchProtectionProviderRegistry {10readonly onDidChangeBranchProtectionProviders: Event<Uri>;1112getBranchProtectionProviders(root: Uri): BranchProtectionProvider[];13registerBranchProtectionProvider(root: Uri, provider: BranchProtectionProvider): Disposable;14}1516export class GitBranchProtectionProvider implements BranchProtectionProvider {1718private readonly _onDidChangeBranchProtection = new EventEmitter<Uri>();19onDidChangeBranchProtection = this._onDidChangeBranchProtection.event;2021private branchProtection!: BranchProtection;2223private disposables: Disposable[] = [];2425constructor(private readonly repositoryRoot: Uri) {26const onDidChangeBranchProtectionEvent = filterEvent(workspace.onDidChangeConfiguration, e => e.affectsConfiguration('git.branchProtection', repositoryRoot));27onDidChangeBranchProtectionEvent(this.updateBranchProtection, this, this.disposables);28this.updateBranchProtection();29}3031provideBranchProtection(): BranchProtection[] {32return [this.branchProtection];33}3435private updateBranchProtection(): void {36const scopedConfig = workspace.getConfiguration('git', this.repositoryRoot);37const branchProtectionConfig = scopedConfig.get<unknown>('branchProtection') ?? [];38const branchProtectionValues = Array.isArray(branchProtectionConfig) ? branchProtectionConfig : [branchProtectionConfig];3940const branches = branchProtectionValues41.map(bp => typeof bp === 'string' ? bp.trim() : '')42.filter(bp => bp !== '');4344this.branchProtection = { remote: '', rules: [{ include: branches }] };45this._onDidChangeBranchProtection.fire(this.repositoryRoot);46}4748dispose(): void {49this.disposables = dispose(this.disposables);50}51}525354