Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions lib/ide/git/git_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ import 'package:flutter/foundation.dart';
import 'git_model.dart';
import 'git_service.dart';

/// `git.autofetchInterval`: seconds between background fetches; 0 (the
/// default) leaves the counters as they are until a manual Fetch. VS Code's
/// `git.autofetch` behavior, as a plain interval the user turns on.
abstract final class GitAutoFetchSetting {
static const settingKey = 'git.autofetchInterval';

/// The interval the user set, or zero when off or not a positive number.
static Duration interval(Object? setting) => switch (setting) {
final int seconds when seconds > 0 => Duration(seconds: seconds),
_ => Duration.zero,
};

/// Read each time a fetch timer is armed (main.dart points these at
/// settings.json).
static Duration Function() current = () => Duration.zero;
static Listenable? changes;
}

/// [IdeGitRepository.graphBranch] for the current branch and its upstream.
const ideGraphCurrentBranch = 'HEAD';

Expand All @@ -44,6 +62,8 @@ class IdeGitRepository extends ChangeNotifier {
this.refreshDelay = const Duration(milliseconds: 500),
}) {
unawaited(refresh());
_armAutoFetch();
GitAutoFetchSetting.changes?.addListener(_onAutoFetchSettingChanged);
}

final IdeGitService service;
Expand All @@ -56,6 +76,7 @@ class IdeGitRepository extends ChangeNotifier {
bool _loaded = false;
bool _disposed = false;
Timer? _refreshTimer;
Timer? _autoFetchTimer;
StreamSubscription<void>? _watcher;
Future<void> _queue = Future.value();
Future<void>? _pendingRefresh;
Expand Down Expand Up @@ -138,6 +159,24 @@ class IdeGitRepository extends ChangeNotifier {
_refreshTimer = Timer(refreshDelay, () => unawaited(refresh()));
}

void _onAutoFetchSettingChanged() {
_armAutoFetch();
}

/// Arms a periodic background fetch when the user set a positive
/// interval. Only a branch with an upstream is fetched: without one
/// there is nothing to be behind, and `git fetch --all` would still
/// touch every remote on every tick.
void _armAutoFetch() {
_autoFetchTimer?.cancel();
_autoFetchTimer = null;
final interval = GitAutoFetchSetting.current();
if (_disposed || interval <= Duration.zero) return;
_autoFetchTimer = Timer.periodic(interval, (_) {
if (_state?.head.upstream != null) unawaited(fetch());
});
}

void _watch(String root) {
if (_watcher != null) return;
var working = false;
Expand Down Expand Up @@ -908,6 +947,8 @@ class IdeGitRepository extends ChangeNotifier {
void dispose() {
_disposed = true;
_refreshTimer?.cancel();
_autoFetchTimer?.cancel();
GitAutoFetchSetting.changes?.removeListener(_onAutoFetchSettingChanged);
unawaited(_watcher?.cancel());
super.dispose();
}
Expand Down
6 changes: 6 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,12 @@ void followSettingsFiles(SettingsFiles files) {
EditorWordWrap.follow(files.settings);
// Whether the code editors show their minimap.
EditorMinimap.follow(files.settings);
// settings.json's git.autofetchInterval: seconds between background
// fetches, 0 (the default) for none. Each repository arms its own timer.
GitAutoFetchSetting.current = () => GitAutoFetchSetting.interval(
files.settings[GitAutoFetchSetting.settingKey],
);
GitAutoFetchSetting.changes = files.settings;
}

/// The app's updates (lib/update/): looked for as settings.json's
Expand Down
45 changes: 45 additions & 0 deletions test/ide/git/git_repository_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,51 @@ void main() {
},
);

group('automatic fetch', () {
tearDown(() {
GitAutoFetchSetting.current = () => Duration.zero;
GitAutoFetchSetting.changes = null;
});

test('fetches a tracking branch on the interval and refreshes behind',
() async {
GitAutoFetchSetting.current = () => const Duration(milliseconds: 40);
git.status = '## main...origin/main\x00';
final auto = IdeGitRepository(
IdeGitService(
git.root,
runner: git.run,
watcher: (_) => changes.stream,
),
refreshDelay: const Duration(milliseconds: 10),
);
addTearDown(auto.dispose);
await auto.refresh();
git.calls.clear();

git.status = '## main...origin/main [behind 1]\x00';
await Future<void>.delayed(const Duration(milliseconds: 140));

expect(git.callsTo('fetch'), isNotEmpty);
expect(auto.state!.head.behind, 1);
});

test('a branch with no upstream is not fetched', () async {
GitAutoFetchSetting.current = () => const Duration(milliseconds: 40);
git.status = '## main\x00';
final auto = IdeGitRepository(
IdeGitService(git.root, runner: git.run),
refreshDelay: const Duration(milliseconds: 10),
);
addTearDown(auto.dispose);
await auto.refresh();
git.calls.clear();

await Future<void>.delayed(const Duration(milliseconds: 140));
expect(git.callsTo('fetch'), isEmpty);
});
});

group('past the status limit', () {
String huge() => [
'## main\x00',
Expand Down