-
Notifications
You must be signed in to change notification settings - Fork 30.1k
/
Copy pathscmService.ts
328 lines (256 loc) · 10.3 KB
/
scmService.ts
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { Event, Emitter } from 'vs/base/common/event';
import { ISCMService, ISCMProvider, ISCMInput, ISCMRepository, IInputValidator, ISCMInputChangeEvent, SCMInputChangeReason, InputValidationType, IInputValidation } from './scm';
import { ILogService } from 'vs/platform/log/common/log';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { HistoryNavigator2 } from 'vs/base/common/history';
import { IMarkdownString } from 'vs/base/common/htmlContent';
import { Iterable } from 'vs/base/common/iterator';
class SCMInput implements ISCMInput {
private _value = '';
get value(): string {
return this._value;
}
private readonly _onDidChange = new Emitter<ISCMInputChangeEvent>();
readonly onDidChange: Event<ISCMInputChangeEvent> = this._onDidChange.event;
private _placeholder = '';
get placeholder(): string {
return this._placeholder;
}
set placeholder(placeholder: string) {
this._placeholder = placeholder;
this._onDidChangePlaceholder.fire(placeholder);
}
private readonly _onDidChangePlaceholder = new Emitter<string>();
readonly onDidChangePlaceholder: Event<string> = this._onDidChangePlaceholder.event;
private _enabled = true;
get enabled(): boolean {
return this._enabled;
}
set enabled(enabled: boolean) {
this._enabled = enabled;
this._onDidChangeEnablement.fire(enabled);
}
private readonly _onDidChangeEnablement = new Emitter<boolean>();
readonly onDidChangeEnablement: Event<boolean> = this._onDidChangeEnablement.event;
private _visible = true;
get visible(): boolean {
return this._visible;
}
set visible(visible: boolean) {
this._visible = visible;
this._onDidChangeVisibility.fire(visible);
}
private readonly _onDidChangeVisibility = new Emitter<boolean>();
readonly onDidChangeVisibility: Event<boolean> = this._onDidChangeVisibility.event;
setFocus(): void {
this._onDidChangeFocus.fire();
}
private readonly _onDidChangeFocus = new Emitter<void>();
readonly onDidChangeFocus: Event<void> = this._onDidChangeFocus.event;
showValidationMessage(message: string | IMarkdownString, type: InputValidationType): void {
this._onDidChangeValidationMessage.fire({ message: message, type: type });
}
private readonly _onDidChangeValidationMessage = new Emitter<IInputValidation>();
readonly onDidChangeValidationMessage: Event<IInputValidation> = this._onDidChangeValidationMessage.event;
private _validateInput: IInputValidator = () => Promise.resolve(undefined);
get validateInput(): IInputValidator {
return this._validateInput;
}
set validateInput(validateInput: IInputValidator) {
this._validateInput = validateInput;
this._onDidChangeValidateInput.fire();
}
private readonly _onDidChangeValidateInput = new Emitter<void>();
readonly onDidChangeValidateInput: Event<void> = this._onDidChangeValidateInput.event;
private historyNavigator: HistoryNavigator2<string>;
private didChangeHistory: boolean;
private static didGarbageCollect = false;
private static migrateAndGarbageCollectStorage(storageService: IStorageService): void {
if (SCMInput.didGarbageCollect) {
return;
}
// Migrate from old format // TODO@joao: remove this migration code a few releases
const userKeys = Iterable.filter(storageService.keys(StorageScope.APPLICATION, StorageTarget.USER), key => key.startsWith('scm/input:'));
for (const key of userKeys) {
try {
const rawHistory = storageService.get(key, StorageScope.APPLICATION, '');
const history = JSON.parse(rawHistory);
if (Array.isArray(history)) {
if (history.length === 0 || (history.length === 1 && history[0] === '')) {
// remove empty histories
storageService.remove(key, StorageScope.APPLICATION);
} else {
// migrate existing histories to have a timestamp
storageService.store(key, JSON.stringify({ timestamp: new Date().getTime(), history }), StorageScope.APPLICATION, StorageTarget.MACHINE);
}
} else {
// move to MACHINE target
storageService.store(key, rawHistory, StorageScope.APPLICATION, StorageTarget.MACHINE);
}
} catch {
// remove unparseable entries
storageService.remove(key, StorageScope.APPLICATION);
}
}
// Garbage collect
const machineKeys = Iterable.filter(storageService.keys(StorageScope.APPLICATION, StorageTarget.MACHINE), key => key.startsWith('scm/input:'));
for (const key of machineKeys) {
try {
const history = JSON.parse(storageService.get(key, StorageScope.APPLICATION, ''));
if (Array.isArray(history?.history) && Number.isInteger(history?.timestamp) && new Date().getTime() - history?.timestamp > 2592000000) {
// garbage collect after 30 days
storageService.remove(key, StorageScope.APPLICATION);
}
} catch {
// remove unparseable entries
storageService.remove(key, StorageScope.APPLICATION);
}
}
SCMInput.didGarbageCollect = true;
}
constructor(
readonly repository: ISCMRepository,
@IStorageService private storageService: IStorageService
) {
SCMInput.migrateAndGarbageCollectStorage(storageService);
const key = this.repository.provider.rootUri ? `scm/input:${this.repository.provider.label}:${this.repository.provider.rootUri?.path}` : undefined;
let history: string[] | undefined;
if (key) {
try {
history = JSON.parse(this.storageService.get(key, StorageScope.APPLICATION, '')).history;
history = history?.map(s => s ?? '');
} catch {
// noop
}
}
if (!Array.isArray(history) || history.length === 0) {
history = [this._value];
} else {
this._value = history[history.length - 1];
}
this.historyNavigator = new HistoryNavigator2(history, 50);
this.didChangeHistory = false;
if (key) {
this.storageService.onWillSaveState(_ => {
if (this.historyNavigator.isAtEnd()) {
this.saveValue();
}
if (!this.didChangeHistory) {
return;
}
const history = [...this.historyNavigator].map(s => s ?? '');
if (history.length === 0 || (history.length === 1 && history[0] === '')) {
storageService.remove(key, StorageScope.APPLICATION);
} else {
storageService.store(key, JSON.stringify({ timestamp: new Date().getTime(), history }), StorageScope.APPLICATION, StorageTarget.MACHINE);
}
this.didChangeHistory = false;
});
}
}
setValue(value: string, transient: boolean, reason?: SCMInputChangeReason) {
if (value === this._value) {
return;
}
if (!transient) {
this.saveValue();
this.historyNavigator.add(value);
this.didChangeHistory = true;
}
this._value = value;
this._onDidChange.fire({ value, reason });
}
showNextHistoryValue(): void {
if (this.historyNavigator.isAtEnd()) {
return;
} else if (!this.historyNavigator.has(this.value)) {
this.saveValue();
this.historyNavigator.resetCursor();
}
const value = this.historyNavigator.next();
this.setValue(value, true, SCMInputChangeReason.HistoryNext);
}
showPreviousHistoryValue(): void {
if (this.historyNavigator.isAtEnd()) {
this.saveValue();
} else if (!this.historyNavigator.has(this._value)) {
this.saveValue();
this.historyNavigator.resetCursor();
}
const value = this.historyNavigator.previous();
this.setValue(value, true, SCMInputChangeReason.HistoryPrevious);
}
private saveValue(): void {
const oldValue = this.historyNavigator.replaceLast(this._value);
this.didChangeHistory = this.didChangeHistory || (oldValue !== this._value);
}
}
class SCMRepository implements ISCMRepository {
private _selected = false;
get selected(): boolean {
return this._selected;
}
private readonly _onDidChangeSelection = new Emitter<boolean>();
readonly onDidChangeSelection: Event<boolean> = this._onDidChangeSelection.event;
readonly input: ISCMInput = new SCMInput(this, this.storageService);
constructor(
public readonly id: string,
public readonly provider: ISCMProvider,
private disposable: IDisposable,
@IStorageService private storageService: IStorageService
) { }
setSelected(selected: boolean): void {
if (this._selected === selected) {
return;
}
this._selected = selected;
this._onDidChangeSelection.fire(selected);
}
dispose(): void {
this.disposable.dispose();
this.provider.dispose();
}
}
export class SCMService implements ISCMService {
declare readonly _serviceBrand: undefined;
_repositories = new Map<string, ISCMRepository>(); // used in tests
get repositories(): Iterable<ISCMRepository> { return this._repositories.values(); }
get repositoryCount(): number { return this._repositories.size; }
private providerCount: IContextKey<number>;
private readonly _onDidAddProvider = new Emitter<ISCMRepository>();
readonly onDidAddRepository: Event<ISCMRepository> = this._onDidAddProvider.event;
private readonly _onDidRemoveProvider = new Emitter<ISCMRepository>();
readonly onDidRemoveRepository: Event<ISCMRepository> = this._onDidRemoveProvider.event;
constructor(
@ILogService private readonly logService: ILogService,
@IContextKeyService contextKeyService: IContextKeyService,
@IStorageService private storageService: IStorageService
) {
this.providerCount = contextKeyService.createKey('scm.providerCount', 0);
}
registerSCMProvider(provider: ISCMProvider): ISCMRepository {
this.logService.trace('SCMService#registerSCMProvider');
if (this._repositories.has(provider.id)) {
throw new Error(`SCM Provider ${provider.id} already exists.`);
}
const disposable = toDisposable(() => {
this._repositories.delete(provider.id);
this._onDidRemoveProvider.fire(repository);
this.providerCount.set(this._repositories.size);
});
const repository = new SCMRepository(provider.id, provider, disposable, this.storageService);
this._repositories.set(provider.id, repository);
this._onDidAddProvider.fire(repository);
this.providerCount.set(this._repositories.size);
return repository;
}
getRepository(id: string): ISCMRepository | undefined {
return this._repositories.get(id);
}
}