forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi1.ts
More file actions
146 lines (125 loc) · 4.61 KB
/
Copy pathapi1.ts
File metadata and controls
146 lines (125 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { APIState, PublishEvent } from '../@types/git';
import { TernarySearchTree } from '../common/utils';
import { API, IGit, Repository } from './api';
export const enum RefType {
Head,
RemoteHead,
Tag,
}
export const enum GitErrorCodes {
BadConfigFile = 'BadConfigFile',
AuthenticationFailed = 'AuthenticationFailed',
NoUserNameConfigured = 'NoUserNameConfigured',
NoUserEmailConfigured = 'NoUserEmailConfigured',
NoRemoteRepositorySpecified = 'NoRemoteRepositorySpecified',
NotAGitRepository = 'NotAGitRepository',
NotAtRepositoryRoot = 'NotAtRepositoryRoot',
Conflict = 'Conflict',
StashConflict = 'StashConflict',
UnmergedChanges = 'UnmergedChanges',
PushRejected = 'PushRejected',
RemoteConnectionError = 'RemoteConnectionError',
DirtyWorkTree = 'DirtyWorkTree',
CantOpenResource = 'CantOpenResource',
GitNotFound = 'GitNotFound',
CantCreatePipe = 'CantCreatePipe',
CantAccessRemote = 'CantAccessRemote',
RepositoryNotFound = 'RepositoryNotFound',
RepositoryIsLocked = 'RepositoryIsLocked',
BranchNotFullyMerged = 'BranchNotFullyMerged',
NoRemoteReference = 'NoRemoteReference',
InvalidBranchName = 'InvalidBranchName',
BranchAlreadyExists = 'BranchAlreadyExists',
NoLocalChanges = 'NoLocalChanges',
NoStashFound = 'NoStashFound',
LocalChangesOverwritten = 'LocalChangesOverwritten',
NoUpstreamBranch = 'NoUpstreamBranch',
IsInSubmodule = 'IsInSubmodule',
WrongCase = 'WrongCase',
CantLockRef = 'CantLockRef',
CantRebaseMultipleBranches = 'CantRebaseMultipleBranches',
PatchDoesNotApply = 'PatchDoesNotApply',
}
export class GitApiImpl implements API, IGit, vscode.Disposable {
private static _handlePool: number = 0;
private _providers = new Map<number, IGit>();
public get repositories(): Repository[] {
const ret: Repository[] = [];
this._providers.forEach(({ repositories }) => {
if (repositories) {
ret.push(...repositories);
}
});
return ret;
}
public get state(): APIState | undefined {
if (this._providers.size === 0) {
return undefined;
}
for (const [, { state }] of this._providers) {
if (state !== 'initialized') {
return 'uninitialized';
}
}
return 'initialized';
}
private _onDidOpenRepository = new vscode.EventEmitter<Repository>();
readonly onDidOpenRepository: vscode.Event<Repository> = this._onDidOpenRepository.event;
private _onDidCloseRepository = new vscode.EventEmitter<Repository>();
readonly onDidCloseRepository: vscode.Event<Repository> = this._onDidCloseRepository.event;
private _onDidChangeState = new vscode.EventEmitter<APIState>();
readonly onDidChangeState: vscode.Event<APIState> = this._onDidChangeState.event;
private _onDidPublish = new vscode.EventEmitter<PublishEvent>();
readonly onDidPublish: vscode.Event<PublishEvent> = this._onDidPublish.event;
private _disposables: vscode.Disposable[];
constructor() {
this._disposables = [];
}
registerGitProvider(provider: IGit): vscode.Disposable {
const handle = this._nextHandle();
this._providers.set(handle, provider);
this._disposables.push(provider.onDidCloseRepository(e => this._onDidCloseRepository.fire(e)));
this._disposables.push(provider.onDidOpenRepository(e => this._onDidOpenRepository.fire(e)));
if (provider.onDidChangeState) {
this._disposables.push(provider.onDidChangeState(e => this._onDidChangeState.fire(e)));
}
if (provider.onDidPublish) {
this._disposables.push(provider.onDidPublish(e => this._onDidPublish.fire(e)));
}
provider.repositories.forEach(repository => {
this._onDidOpenRepository.fire(repository);
});
return {
dispose: () => {
const repos = provider?.repositories;
if (repos && repos.length > 0) {
repos.forEach(r => this._onDidCloseRepository.fire(r));
}
this._providers.delete(handle);
},
};
}
getGitProvider(uri: vscode.Uri): IGit | undefined {
const foldersMap = TernarySearchTree.forUris<IGit>();
this._providers.forEach(provider => {
const repos = provider.repositories;
if (repos && repos.length > 0) {
for (const repository of repos) {
foldersMap.set(repository.rootUri, provider);
}
}
});
return foldersMap.findSubstr(uri);
}
private _nextHandle(): number {
return GitApiImpl._handlePool++;
}
dispose() {
this._disposables.forEach(disposable => disposable.dispose());
}
}