Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/git/src/branchProtection.ts
3316 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Disposable, Event, EventEmitter, Uri, workspace } from 'vscode';
7
import { BranchProtection, BranchProtectionProvider } from './api/git';
8
import { dispose, filterEvent } from './util';
9
10
export interface IBranchProtectionProviderRegistry {
11
readonly onDidChangeBranchProtectionProviders: Event<Uri>;
12
13
getBranchProtectionProviders(root: Uri): BranchProtectionProvider[];
14
registerBranchProtectionProvider(root: Uri, provider: BranchProtectionProvider): Disposable;
15
}
16
17
export class GitBranchProtectionProvider implements BranchProtectionProvider {
18
19
private readonly _onDidChangeBranchProtection = new EventEmitter<Uri>();
20
onDidChangeBranchProtection = this._onDidChangeBranchProtection.event;
21
22
private branchProtection!: BranchProtection;
23
24
private disposables: Disposable[] = [];
25
26
constructor(private readonly repositoryRoot: Uri) {
27
const onDidChangeBranchProtectionEvent = filterEvent(workspace.onDidChangeConfiguration, e => e.affectsConfiguration('git.branchProtection', repositoryRoot));
28
onDidChangeBranchProtectionEvent(this.updateBranchProtection, this, this.disposables);
29
this.updateBranchProtection();
30
}
31
32
provideBranchProtection(): BranchProtection[] {
33
return [this.branchProtection];
34
}
35
36
private updateBranchProtection(): void {
37
const scopedConfig = workspace.getConfiguration('git', this.repositoryRoot);
38
const branchProtectionConfig = scopedConfig.get<unknown>('branchProtection') ?? [];
39
const branchProtectionValues = Array.isArray(branchProtectionConfig) ? branchProtectionConfig : [branchProtectionConfig];
40
41
const branches = branchProtectionValues
42
.map(bp => typeof bp === 'string' ? bp.trim() : '')
43
.filter(bp => bp !== '');
44
45
this.branchProtection = { remote: '', rules: [{ include: branches }] };
46
this._onDidChangeBranchProtection.fire(this.repositoryRoot);
47
}
48
49
dispose(): void {
50
this.disposables = dispose(this.disposables);
51
}
52
}
53
54