refactor: move auto-fetch from global preference to repository settings

This commit is contained in:
leo 2024-09-26 10:50:21 +08:00
parent 8e31ea9140
commit 1ba294a07b
No known key found for this signature in database
25 changed files with 166 additions and 227 deletions

View file

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Collections;
@ -323,6 +324,12 @@ namespace SourceGit.ViewModels
}
}
public bool IsAutoFetching
{
get;
private set;
}
public void Open()
{
var settingsFile = Path.Combine(_gitDir, "sourcegit.settings");
@ -359,6 +366,7 @@ namespace SourceGit.ViewModels
_inProgressContext = null;
_hasUnsolvedConflicts = false;
_autoFetchTimer = new Timer(AutoFetchImpl, null, 5000, 5000);
RefreshAll();
}
@ -377,6 +385,9 @@ namespace SourceGit.ViewModels
}
_settings = null;
_autoFetchTimer.Dispose();
_autoFetchTimer = null;
_watcher?.Dispose();
_histories.Cleanup();
_workingCopy.Cleanup();
@ -628,6 +639,11 @@ namespace SourceGit.ViewModels
_watcher.MarkWorkingCopyDirtyManually();
}
public void MarkFetched()
{
_lastFetchTime = DateTime.Now;
}
public void NavigateToCommit(string sha)
{
if (_histories != null)
@ -1991,6 +2007,28 @@ namespace SourceGit.ViewModels
}
}
private void AutoFetchImpl(object sender)
{
if (!_settings.EnableAutoFetch || IsAutoFetching)
return;
var lockFile = Path.Combine(_gitDir, "index.lock");
if (File.Exists(lockFile))
return;
var now = DateTime.Now;
var desire = _lastFetchTime.AddMinutes(_settings.AutoFetchInterval);
if (desire > now)
return;
IsAutoFetching = true;
Dispatcher.UIThread.Invoke(() => OnPropertyChanged(nameof(IsAutoFetching)));
new Commands.Fetch(_fullpath, "--all", true, false, null) { RaiseError = false }.Exec();
_lastFetchTime = DateTime.Now;
IsAutoFetching = false;
Dispatcher.UIThread.Invoke(() => OnPropertyChanged(nameof(IsAutoFetching)));
}
private string _fullpath = string.Empty;
private string _gitDir = string.Empty;
private Models.RepositorySettings _settings = null;
@ -2036,5 +2074,8 @@ namespace SourceGit.ViewModels
private InProgressContext _inProgressContext = null;
private bool _hasUnsolvedConflicts = false;
private Models.Commit _searchResultSelectedCommit = null;
private Timer _autoFetchTimer = null;
private DateTime _lastFetchTime = DateTime.MinValue;
}
}