Skip to content

Commit 65478f4

Browse files
committed
git: autofetch
1 parent c7fca1c commit 65478f4

3 files changed

Lines changed: 73 additions & 1 deletion

File tree

extensions/git/src/autofetch.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
'use strict';
7+
8+
import { workspace, Disposable } from 'vscode';
9+
import { GitErrorCodes } from './git';
10+
import { Model } from './model';
11+
import { throttle } from './util';
12+
import { decorate } from 'core-decorators';
13+
14+
export class AutoFetcher {
15+
16+
private static Period = 3 * 60 * 1000 /* three minutes */;
17+
private disposables: Disposable[] = [];
18+
private timer: NodeJS.Timer;
19+
20+
constructor(private model: Model) {
21+
workspace.onDidChangeConfiguration(this.onConfiguration, this, this.disposables);
22+
this.onConfiguration();
23+
}
24+
25+
private onConfiguration(): void {
26+
const gitConfig = workspace.getConfiguration('git');
27+
28+
if (gitConfig.get<boolean>('autofetch') === false) {
29+
this.disable();
30+
} else {
31+
this.enable();
32+
}
33+
}
34+
35+
enable(): void {
36+
if (this.timer) {
37+
return;
38+
}
39+
40+
this.fetch();
41+
this.timer = setInterval(() => this.fetch(), AutoFetcher.Period);
42+
}
43+
44+
disable(): void {
45+
clearInterval(this.timer);
46+
}
47+
48+
@decorate(throttle)
49+
private async fetch(): Promise<void> {
50+
try {
51+
await this.model.fetch();
52+
} catch (err) {
53+
if (err.gitErrorCode === GitErrorCodes.AuthenticationFailed) {
54+
this.disable();
55+
}
56+
}
57+
}
58+
59+
dispose(): void {
60+
this.disable();
61+
this.disposables.forEach(d => d.dispose());
62+
}
63+
}

extensions/git/src/main.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { CommandCenter } from './commands';
1313
import { CheckoutStatusBar, SyncStatusBar } from './statusbar';
1414
import { filterEvent, anyEvent, throttle } from './util';
1515
import { GitContentProvider } from './contentProvider';
16+
import { AutoFetcher } from './autofetch';
1617
import * as nls from 'vscode-nls';
1718
import { decorate, debounce } from 'core-decorators';
1819

@@ -73,6 +74,8 @@ async function init(disposables: Disposable[]): Promise<void> {
7374
const checkoutStatusBar = new CheckoutStatusBar(model);
7475
const syncStatusBar = new SyncStatusBar(model);
7576

77+
const autoFetcher = new AutoFetcher(model);
78+
7679
disposables.push(
7780
commandCenter,
7881
provider,
@@ -81,7 +84,8 @@ async function init(disposables: Disposable[]): Promise<void> {
8184
fsWatcher,
8285
watcher,
8386
checkoutStatusBar,
84-
syncStatusBar
87+
syncStatusBar,
88+
autoFetcher
8589
);
8690
}
8791

extensions/git/src/model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ export class Model {
332332
await this.update();
333333
}
334334

335+
async fetch(): Promise<void> {
336+
await this.repository.fetch();
337+
await this.update();
338+
}
339+
335340
async sync(): Promise<void> {
336341
await this.repository.sync();
337342
await this.update();

0 commit comments

Comments
 (0)