forked from JohnstonCode/svn-scm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.ts
1009 lines (840 loc) · 26.6 KB
/
repository.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as path from "path";
import { clearInterval, setInterval } from "timers";
import {
commands,
Disposable,
Event,
EventEmitter,
ProgressLocation,
scm,
SourceControl,
SourceControlInputBox,
TextDocument,
Uri,
window,
workspace
} from "vscode";
import {
IAuth,
IFileStatus,
IOperations,
ISvnInfo,
ISvnResourceGroup,
Operation,
RepositoryState,
Status,
SvnDepth,
SvnUriAction
} from "./common/types";
import { debounce, globalSequentialize, memoize, throttle } from "./decorators";
import { exists } from "./fs";
import { configuration } from "./helpers/configuration";
import OperationsImpl from "./operationsImpl";
import { PathNormalizer } from "./pathNormalizer";
import { IRemoteRepository } from "./remoteRepository";
import { Resource } from "./resource";
import { StatusBarCommands } from "./statusbar/statusBarCommands";
import { svnErrorCodes } from "./svn";
import { Repository as BaseRepository } from "./svnRepository";
import { toSvnUri } from "./uri";
import {
anyEvent,
dispose,
eventToPromise,
filterEvent,
isDescendant,
isReadOnly,
timeout,
toDisposable
} from "./util";
import { match, matchAll } from "./util/globMatch";
import { RepositoryFilesWatcher } from "./watchers/repositoryFilesWatcher";
function shouldShowProgress(operation: Operation): boolean {
switch (operation) {
case Operation.CurrentBranch:
case Operation.Show:
case Operation.Info:
return false;
default:
return true;
}
}
export class Repository implements IRemoteRepository {
public sourceControl: SourceControl;
public statusBar: StatusBarCommands;
public changes: ISvnResourceGroup;
public unversioned: ISvnResourceGroup;
public remoteChanges?: ISvnResourceGroup;
public changelists: Map<string, ISvnResourceGroup> = new Map();
public conflicts: ISvnResourceGroup;
public statusIgnored: IFileStatus[] = [];
public statusExternal: IFileStatus[] = [];
private disposables: Disposable[] = [];
public currentBranch = "";
public remoteChangedFiles: number = 0;
public isIncomplete: boolean = false;
public needCleanUp: boolean = false;
private remoteChangedUpdateInterval?: NodeJS.Timer;
private deletedUris: Uri[] = [];
private lastPromptAuth?: Thenable<IAuth | undefined>;
private _fsWatcher: RepositoryFilesWatcher;
public get fsWatcher() {
return this._fsWatcher;
}
private _onDidChangeRepository = new EventEmitter<Uri>();
public readonly onDidChangeRepository: Event<Uri> = this
._onDidChangeRepository.event;
private _onDidChangeState = new EventEmitter<RepositoryState>();
public readonly onDidChangeState: Event<RepositoryState> = this
._onDidChangeState.event;
private _onDidChangeStatus = new EventEmitter<void>();
public readonly onDidChangeStatus: Event<void> = this._onDidChangeStatus
.event;
private _onDidChangeRemoteChangedFiles = new EventEmitter<void>();
public readonly onDidChangeRemoteChangedFile: Event<void> = this
._onDidChangeRemoteChangedFiles.event;
private _onRunOperation = new EventEmitter<Operation>();
public readonly onRunOperation: Event<Operation> = this._onRunOperation.event;
private _onDidRunOperation = new EventEmitter<Operation>();
public readonly onDidRunOperation: Event<Operation> = this._onDidRunOperation
.event;
@memoize
get onDidChangeOperations(): Event<void> {
return anyEvent(
this.onRunOperation as Event<any>,
this.onDidRunOperation as Event<any>
);
}
private _operations = new OperationsImpl();
get operations(): IOperations {
return this._operations;
}
private _state = RepositoryState.Idle;
get state(): RepositoryState {
return this._state;
}
set state(state: RepositoryState) {
this._state = state;
this._onDidChangeState.fire(state);
this.changes.resourceStates = [];
this.unversioned.resourceStates = [];
this.conflicts.resourceStates = [];
this.changelists.forEach((group, _changelist) => {
group.resourceStates = [];
});
if (this.remoteChanges) {
this.remoteChanges.dispose();
}
this.isIncomplete = false;
this.needCleanUp = false;
}
get root(): string {
return this.repository.root;
}
get workspaceRoot(): string {
return this.repository.workspaceRoot;
}
/** 'svn://repo.x/branches/b1' e.g. */
get branchRoot(): Uri {
return Uri.parse(this.repository.info.url);
}
get inputBox(): SourceControlInputBox {
return this.sourceControl.inputBox;
}
get username(): string | undefined {
return this.repository.username;
}
set username(username: string | undefined) {
this.repository.username = username;
}
get password(): string | undefined {
return this.repository.password;
}
set password(password: string | undefined) {
this.repository.password = password;
}
constructor(public repository: BaseRepository) {
this._fsWatcher = new RepositoryFilesWatcher(repository.root);
this.disposables.push(this._fsWatcher);
this._fsWatcher.onDidAny(this.onFSChange, this, this.disposables);
// TODO on svn switch event fired two times since two files were changed
this._fsWatcher.onDidSvnAny(
async (e: Uri) => {
await this.repository.updateInfo();
this._onDidChangeRepository.fire(e);
},
this,
this.disposables
);
this.sourceControl = scm.createSourceControl(
"svn",
"SVN",
Uri.file(repository.workspaceRoot)
);
this.sourceControl.count = 0;
this.sourceControl.inputBox.placeholder =
"Message (press Ctrl+Enter to commit)";
this.sourceControl.acceptInputCommand = {
command: "svn.commitWithMessage",
title: "commit",
arguments: [this.sourceControl]
};
this.sourceControl.quickDiffProvider = this;
this.disposables.push(this.sourceControl);
this.statusBar = new StatusBarCommands(this);
this.disposables.push(this.statusBar);
this.statusBar.onDidChange(
() => (this.sourceControl.statusBarCommands = this.statusBar.commands),
null,
this.disposables
);
this.changes = this.sourceControl.createResourceGroup(
"changes",
"Changes"
) as ISvnResourceGroup;
this.conflicts = this.sourceControl.createResourceGroup(
"conflicts",
"conflicts"
) as ISvnResourceGroup;
this.unversioned = this.sourceControl.createResourceGroup(
"unversioned",
"Unversioned"
) as ISvnResourceGroup;
this.changes.hideWhenEmpty = true;
this.unversioned.hideWhenEmpty = true;
this.conflicts.hideWhenEmpty = true;
this.disposables.push(this.changes);
this.disposables.push(this.conflicts);
// The this.unversioned can recreated by update state model
this.disposables.push(toDisposable(() => this.unversioned.dispose()));
// Dispose the setInterval of Remote Changes
this.disposables.push(
toDisposable(() => {
if (this.remoteChangedUpdateInterval) {
clearInterval(this.remoteChangedUpdateInterval);
}
})
);
// For each deleted file, append to list
this._fsWatcher.onDidWorkspaceDelete(
uri => this.deletedUris.push(uri),
this,
this.disposables
);
// Only check deleted files after the status list is fully updated
this.onDidChangeStatus(this.actionForDeletedFiles, this, this.disposables);
this.createRemoteChangedInterval();
this.updateRemoteChangedFiles();
// On change config, dispose current interval and create a new.
configuration.onDidChange(e => {
if (e.affectsConfiguration("svn.remoteChanges.checkFrequency")) {
if (this.remoteChangedUpdateInterval) {
clearInterval(this.remoteChangedUpdateInterval);
}
this.createRemoteChangedInterval();
this.updateRemoteChangedFiles();
}
});
this.status();
this.disposables.push(
workspace.onDidSaveTextDocument(document => {
this.onDidSaveTextDocument(document);
})
);
}
private createRemoteChangedInterval() {
const updateFreq = configuration.get<number>(
"remoteChanges.checkFrequency",
300
);
if (!updateFreq) {
return;
}
this.remoteChangedUpdateInterval = setInterval(() => {
this.updateRemoteChangedFiles();
}, 1000 * updateFreq);
}
/**
* Check all recently deleted files and compare with svn status "missing"
*/
@debounce(1000)
private async actionForDeletedFiles() {
if (!this.deletedUris.length) {
return;
}
const allUris = this.deletedUris;
this.deletedUris = [];
const actionForDeletedFiles = configuration.get<string>(
"delete.actionForDeletedFiles",
"prompt"
);
if (actionForDeletedFiles === "none") {
return;
}
const resources = allUris
.map(uri => this.getResourceFromFile(uri))
.filter(
resource => resource && resource.type === Status.MISSING
) as Resource[];
let uris = resources.map(resource => resource.resourceUri);
if (!uris.length) {
return;
}
const ignoredRulesForDeletedFiles = configuration.get<string[]>(
"delete.ignoredRulesForDeletedFiles",
[]
);
const rules = ignoredRulesForDeletedFiles.map(ignored => match(ignored));
if (rules.length) {
uris = uris.filter(uri => {
// Check first for relative URL (Better for workspace configuration)
const relativePath = this.repository.removeAbsolutePath(uri.fsPath);
// If some match, remove from list
return !rules.some(
rule => rule.match(relativePath) || rule.match(uri.fsPath)
);
});
}
if (!uris.length) {
return;
}
if (actionForDeletedFiles === "remove") {
return await this.removeFiles(uris.map(uri => uri.fsPath), false);
} else if (actionForDeletedFiles === "prompt") {
return await commands.executeCommand("svn.promptRemove", ...uris);
}
return;
}
@debounce(1000)
public async updateRemoteChangedFiles() {
const updateFreq = configuration.get<number>(
"remoteChanges.checkFrequency",
300
);
if (updateFreq) {
this.run(Operation.StatusRemote);
} else {
// Remove list of remote changes
if (this.remoteChanges) {
this.remoteChanges.dispose();
this.remoteChanges = undefined;
}
}
}
private onFSChange(_uri: Uri): void {
const autorefresh = configuration.get<boolean>("autorefresh");
if (!autorefresh) {
return;
}
if (!this.operations.isIdle()) {
return;
}
this.eventuallyUpdateWhenIdleAndWait();
}
@debounce(1000)
private eventuallyUpdateWhenIdleAndWait(): void {
this.updateWhenIdleAndWait();
}
@throttle
private async updateWhenIdleAndWait(): Promise<void> {
await this.whenIdleAndFocused();
await this.status();
await timeout(5000);
}
public async whenIdleAndFocused(): Promise<void> {
while (true) {
if (!this.operations.isIdle()) {
await eventToPromise(this.onDidRunOperation);
continue;
}
if (!window.state.focused) {
const onDidFocusWindow = filterEvent(
window.onDidChangeWindowState,
e => e.focused
);
await eventToPromise(onDidFocusWindow);
continue;
}
return;
}
}
@throttle
@globalSequentialize("updateModelState")
public async updateModelState(checkRemoteChanges: boolean = false) {
const changes: any[] = [];
const unversioned: any[] = [];
const conflicts: any[] = [];
const changelists: Map<string, Resource[]> = new Map();
const remoteChanges: any[] = [];
this.statusExternal = [];
this.statusIgnored = [];
this.isIncomplete = false;
this.needCleanUp = false;
const combineExternal = configuration.get<boolean>(
"sourceControl.combineExternalIfSameServer",
false
);
const statuses =
(await this.retryRun(async () => {
return await this.repository.getStatus({
includeIgnored: true,
includeExternals: combineExternal,
checkRemoteChanges
});
})) || [];
const fileConfig = workspace.getConfiguration("files", Uri.file(this.root));
const filesToExclude = fileConfig.get<any>("exclude");
const excludeList: string[] = [];
for (const pattern in filesToExclude) {
if (filesToExclude.hasOwnProperty(pattern)) {
const negate = !filesToExclude[pattern];
excludeList.push((negate ? "!" : "") + pattern);
}
}
this.statusExternal = statuses.filter(
status => status.status === Status.EXTERNAL
);
if (combineExternal && this.statusExternal.length) {
const repositoryUuid = await this.repository.getRepositoryUuid();
this.statusExternal = this.statusExternal.filter(
status => repositoryUuid !== status.repositoryUuid
);
}
const statusesRepository = statuses.filter(status => {
if (status.status === Status.EXTERNAL) {
return false;
}
return !this.statusExternal.some(external =>
isDescendant(external.path, status.path)
);
});
const hideUnversioned = configuration.get<boolean>(
"sourceControl.hideUnversioned"
);
for (const status of statusesRepository) {
if (status.path === ".") {
this.isIncomplete = status.status === Status.INCOMPLETE;
this.needCleanUp = status.wcStatus.locked;
}
// If exists a switched item, the repository is incomplete
// To simulate, run "svn switch" and kill "svn" proccess
// After, run "svn update"
if (status.wcStatus.switched) {
this.isIncomplete = true;
}
if (
status.wcStatus.locked ||
status.wcStatus.switched ||
status.status === Status.INCOMPLETE
) {
// On commit, `svn status` return all locked files with status="normal" and props="none"
continue;
}
if (matchAll(status.path, excludeList, { dot: true })) {
continue;
}
const uri = Uri.file(path.join(this.workspaceRoot, status.path));
const renameUri = status.rename
? Uri.file(path.join(this.workspaceRoot, status.rename))
: undefined;
if (status.reposStatus) {
remoteChanges.push(
new Resource(
uri,
status.reposStatus.item,
undefined,
status.reposStatus.props,
true
)
);
}
const resource = new Resource(
uri,
status.status,
renameUri,
status.props
);
if (
(status.status === Status.NORMAL || status.status === Status.NONE) &&
(status.props === Status.NORMAL || status.props === Status.NONE) &&
!status.changelist
) {
// Ignore non changed itens
continue;
} else if (status.status === Status.IGNORED) {
this.statusIgnored.push(status);
} else if (status.status === Status.CONFLICTED) {
conflicts.push(resource);
} else if (status.status === Status.UNVERSIONED) {
if (hideUnversioned) {
continue;
}
const matches = status.path.match(
/(.+?)\.(mine|working|merge-\w+\.r\d+|r\d+)$/
);
// If file end with (mine, working, merge, etc..) and has file without extension
if (
matches &&
matches[1] &&
statuses.some(s => s.path === matches[1])
) {
continue;
} else {
unversioned.push(resource);
}
} else if (status.changelist) {
let changelist = changelists.get(status.changelist);
if (!changelist) {
changelist = [];
}
changelist.push(resource);
changelists.set(status.changelist, changelist);
} else {
changes.push(resource);
}
}
this.changes.resourceStates = changes;
this.conflicts.resourceStates = conflicts;
const prevChangelistsSize = this.changelists.size;
this.changelists.forEach((group, _changelist) => {
group.resourceStates = [];
});
const counts = [this.changes, this.conflicts];
const ignoreOnStatusCountList = configuration.get<string[]>(
"sourceControl.ignoreOnStatusCount"
);
changelists.forEach((resources, changelist) => {
let group = this.changelists.get(changelist);
if (!group) {
// Prefix 'changelist-' to prevent double id with 'change' or 'external'
group = this.sourceControl.createResourceGroup(
`changelist-${changelist}`,
`Changelist "${changelist}"`
) as ISvnResourceGroup;
group.hideWhenEmpty = true;
this.disposables.push(group);
this.changelists.set(changelist, group);
}
group.resourceStates = resources;
if (!ignoreOnStatusCountList.includes(changelist)) {
counts.push(group);
}
});
// Recreate unversioned group to move after changelists
if (prevChangelistsSize !== this.changelists.size) {
this.unversioned.dispose();
this.unversioned = this.sourceControl.createResourceGroup(
"unversioned",
"Unversioned"
) as ISvnResourceGroup;
this.unversioned.hideWhenEmpty = true;
}
this.unversioned.resourceStates = unversioned;
if (configuration.get<boolean>("sourceControl.countUnversioned", false)) {
counts.push(this.unversioned);
}
this.sourceControl.count = counts.reduce(
(a, b) => a + b.resourceStates.length,
0
);
// Recreate remoteChanges group to move after unversioned
if (!this.remoteChanges || prevChangelistsSize !== this.changelists.size) {
/**
* Destroy and create for keep at last position
*/
let tempResourceStates: Resource[] = [];
if (this.remoteChanges) {
tempResourceStates = this.remoteChanges.resourceStates;
this.remoteChanges.dispose();
}
this.remoteChanges = this.sourceControl.createResourceGroup(
"remotechanges",
"Remote Changes"
) as ISvnResourceGroup;
this.remoteChanges.repository = this;
this.remoteChanges.hideWhenEmpty = true;
this.remoteChanges.resourceStates = tempResourceStates;
}
// Update remote changes group
if (checkRemoteChanges) {
this.remoteChanges.resourceStates = remoteChanges;
if (remoteChanges.length !== this.remoteChangedFiles) {
this.remoteChangedFiles = remoteChanges.length;
this._onDidChangeRemoteChangedFiles.fire();
}
}
this._onDidChangeStatus.fire();
this.currentBranch = await this.getCurrentBranch();
return Promise.resolve();
}
public getResourceFromFile(uri: string | Uri): Resource | undefined {
if (typeof uri === "string") {
uri = Uri.file(uri);
}
const groups = [
this.changes,
this.conflicts,
this.unversioned,
...this.changelists.values()
];
const uriString = uri.toString();
for (const group of groups) {
for (const resource of group.resourceStates) {
if (
uriString === resource.resourceUri.toString() &&
resource instanceof Resource
) {
return resource;
}
}
}
return undefined;
}
public provideOriginalResource(uri: Uri): Uri | undefined {
if (uri.scheme !== "file") {
return;
}
// Not has original resource for content of ".svn" folder
if (isDescendant(path.join(this.root, ".svn"), uri.fsPath)) {
return;
}
return toSvnUri(uri, SvnUriAction.SHOW, {}, true);
}
public async getBranches() {
try {
return await this.repository.getBranches();
} catch (error) {
return [];
}
}
@throttle
public async status() {
return this.run(Operation.Status);
}
public async show(
filePath: string | Uri,
revision?: string
): Promise<string> {
return this.run<string>(Operation.Show, () => {
return this.repository.show(filePath, revision);
});
}
public async addFiles(files: string[]) {
return this.run(Operation.Add, () => this.repository.addFiles(files));
}
public async addChangelist(files: string[], changelist: string) {
return this.run(Operation.AddChangelist, () =>
this.repository.addChangelist(files, changelist)
);
}
public async removeChangelist(files: string[]) {
return this.run(Operation.RemoveChangelist, () =>
this.repository.removeChangelist(files)
);
}
public async getCurrentBranch() {
return this.run(Operation.CurrentBranch, async () => {
return this.repository.getCurrentBranch();
});
}
public async newBranch(
name: string,
commitMessage: string = "Created new branch"
) {
return this.run(Operation.NewBranch, async () => {
await this.repository.newBranch(name, commitMessage);
this.updateRemoteChangedFiles();
});
}
public async switchBranch(name: string, force: boolean = false) {
await this.run(Operation.SwitchBranch, async () => {
await this.repository.switchBranch(name, force);
this.updateRemoteChangedFiles();
});
}
public async updateRevision(
ignoreExternals: boolean = false
): Promise<string> {
return this.run<string>(Operation.Update, async () => {
const response = await this.repository.update(ignoreExternals);
this.updateRemoteChangedFiles();
return response;
});
}
public async pullIncomingChange(path: string) {
return this.run<string>(Operation.Update, async () => {
const response = await this.repository.pullIncomingChange(path);
this.updateRemoteChangedFiles();
return response;
});
}
public async resolve(files: string[], action: string) {
return this.run(Operation.Resolve, () =>
this.repository.resolve(files, action)
);
}
public async commitFiles(message: string, files: any[]) {
return this.run(Operation.Commit, () =>
this.repository.commitFiles(message, files)
);
}
public async revert(files: string[], depth: keyof typeof SvnDepth) {
return this.run(Operation.Revert, () =>
this.repository.revert(files, depth)
);
}
public async info(path: string) {
return this.run(Operation.Info, () => this.repository.getInfo(path));
}
public async patch(files: string[]) {
return this.run(Operation.Patch, () => this.repository.patch(files));
}
public async patchChangelist(changelistName: string) {
return this.run(Operation.Patch, () =>
this.repository.patchChangelist(changelistName)
);
}
public async removeFiles(files: string[], keepLocal: boolean) {
return this.run(Operation.Remove, () =>
this.repository.removeFiles(files, keepLocal)
);
}
public async plainLog() {
return this.run(Operation.Log, () => this.repository.plainLog());
}
public async log(
rfrom: string,
rto: string,
limit: number,
target?: string | Uri
) {
return this.run(Operation.Log, () =>
this.repository.log(rfrom, rto, limit, target)
);
}
public async cleanup() {
return this.run(Operation.CleanUp, () => this.repository.cleanup());
}
public async getInfo(path: string, revision?: string): Promise<ISvnInfo> {
return this.run(Operation.Info, () =>
this.repository.getInfo(path, revision, true)
);
}
public async finishCheckout() {
return this.run(Operation.SwitchBranch, () =>
this.repository.finishCheckout()
);
}
public async addToIgnore(
expressions: string[],
directory: string,
recursive: boolean = false
) {
return this.run(Operation.Ignore, () =>
this.repository.addToIgnore(expressions, directory, recursive)
);
}
public async rename(oldFile: string, newFile: string) {
return this.run(Operation.Rename, () =>
this.repository.rename(oldFile, newFile)
);
}
public getPathNormalizer(): PathNormalizer {
return new PathNormalizer(this.repository.info);
}
public async promptAuth(): Promise<IAuth | undefined> {
// Prevent multiple prompts for auth
if (this.lastPromptAuth) {
return this.lastPromptAuth;
}
this.lastPromptAuth = commands.executeCommand("svn.promptAuth");
const result = await this.lastPromptAuth;
if (result) {
this.username = result.username;
this.password = result.password;
}
this.lastPromptAuth = undefined;
return result;
}
public onDidSaveTextDocument(document: TextDocument) {
const uriString = document.uri.toString();
const conflict = this.conflicts.resourceStates.find(
resource => resource.resourceUri.toString() === uriString
);
if (!conflict) {
return;
}
const text = document.getText();
// Check for lines begin with "<<<<<<", "=======", ">>>>>>>"
if (!/^<{7}[^]+^={7}[^]+^>{7}/m.test(text)) {
commands.executeCommand("svn.resolved", conflict.resourceUri);
}
}
private async run<T>(
operation: Operation,
runOperation: () => Promise<T> = () => Promise.resolve<any>(null)
): Promise<T> {
if (this.state !== RepositoryState.Idle) {
throw new Error("Repository not initialized");
}
const run = async () => {
this._operations.start(operation);
this._onRunOperation.fire(operation);
try {
const result = await this.retryRun(runOperation);
const checkRemote = operation === Operation.StatusRemote;
if (!isReadOnly(operation)) {
await this.updateModelState(checkRemote);
}
return result;
} catch (err) {
if (err.svnErrorCode === svnErrorCodes.NotASvnRepository) {
this.state = RepositoryState.Disposed;
}
const rootExists = await exists(this.workspaceRoot);
if (!rootExists) {
await commands.executeCommand("svn.close", this);
}
throw err;
} finally {
this._operations.end(operation);
this._onDidRunOperation.fire(operation);
}
};
return shouldShowProgress(operation)
? window.withProgress({ location: ProgressLocation.SourceControl }, run)
: run();
}
private async retryRun<T>(
runOperation: () => Promise<T> = () => Promise.resolve<any>(null)
): Promise<T> {
let attempt = 0;
while (true) {
try {
attempt++;
return await runOperation();
} catch (err) {
if (
err.svnErrorCode === svnErrorCodes.RepositoryIsLocked &&
attempt <= 10
) {
// quatratic backoff
await timeout(Math.pow(attempt, 2) * 50);
} else if (
err.svnErrorCode === svnErrorCodes.AuthorizationFailed &&
attempt <= 3
) {
const result = await this.promptAuth();
if (!result) {
throw err;
}
} else {
throw err;