diff --git a/lib/ide/git/git_repository.dart b/lib/ide/git/git_repository.dart index 3247568cd..4001be8c9 100644 --- a/lib/ide/git/git_repository.dart +++ b/lib/ide/git/git_repository.dart @@ -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'; @@ -44,6 +62,8 @@ class IdeGitRepository extends ChangeNotifier { this.refreshDelay = const Duration(milliseconds: 500), }) { unawaited(refresh()); + _armAutoFetch(); + GitAutoFetchSetting.changes?.addListener(_onAutoFetchSettingChanged); } final IdeGitService service; @@ -56,6 +76,7 @@ class IdeGitRepository extends ChangeNotifier { bool _loaded = false; bool _disposed = false; Timer? _refreshTimer; + Timer? _autoFetchTimer; StreamSubscription? _watcher; Future _queue = Future.value(); Future? _pendingRefresh; @@ -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; @@ -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(); } diff --git a/lib/main.dart b/lib/main.dart index 582e87992..b0b10af10 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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 diff --git a/test/ide/git/git_repository_test.dart b/test/ide/git/git_repository_test.dart index da222b883..7faf49945 100644 --- a/test/ide/git/git_repository_test.dart +++ b/test/ide/git/git_repository_test.dart @@ -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.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.delayed(const Duration(milliseconds: 140)); + expect(git.callsTo('fetch'), isEmpty); + }); + }); + group('past the status limit', () { String huge() => [ '## main\x00',