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

@ -104,6 +104,7 @@ namespace SourceGit.ViewModels
}
CallUIThread(() =>
{
_repo.MarkFetched();
_repo.MarkBranchesDirtyManually();
_repo.SetWatcherEnabled(true);
});

View file

@ -62,7 +62,12 @@ namespace SourceGit.ViewModels
new Commands.Fetch(_repo.FullPath, SelectedRemote.Name, Prune, NoTags, SetProgressDescription).Exec();
}
CallUIThread(() => _repo.SetWatcherEnabled(true));
CallUIThread(() =>
{
_repo.MarkFetched();
_repo.SetWatcherEnabled(true);
});
return true;
});
}

View file

@ -179,7 +179,6 @@ namespace SourceGit.ViewModels
ActiveWorkspace.Repositories.Clear();
ActiveWorkspace.ActiveIdx = 0;
Models.AutoFetchManager.Instance.RemoveRepository(repo.FullPath);
repo.Close();
Welcome.Instance.ClearSearchFilter();
@ -293,7 +292,6 @@ namespace SourceGit.ViewModels
};
repo.Open();
Models.AutoFetchManager.Instance.AddRepository(repo.FullPath, repo.GitDir);
if (page == null)
{
@ -522,7 +520,6 @@ namespace SourceGit.ViewModels
if (removeFromWorkspace)
ActiveWorkspace.Repositories.Remove(repo.FullPath);
Models.AutoFetchManager.Instance.RemoveRepository(repo.FullPath);
repo.Close();
}

View file

@ -44,6 +44,14 @@ namespace SourceGit.ViewModels
return _node.Id;
}
public override bool IsInProgress()
{
if (_data is Repository { IsAutoFetching: true })
return true;
return base.IsInProgress();
}
public void CopyPath()
{
if (_node.IsRepository)

View file

@ -18,7 +18,7 @@ namespace SourceGit.ViewModels
public static bool CanCreatePopup()
{
return Active != null && (Active._popup == null || !Active._popup.InProgress);
return Active?.IsInProgress() != true;
}
public static void ShowPopup(Popup popup)
@ -40,6 +40,11 @@ namespace SourceGit.ViewModels
return string.Empty;
}
public virtual bool IsInProgress()
{
return _popup is { InProgress: true };
}
public async void ProcessPopup()
{
if (_popup != null)

View file

@ -216,35 +216,6 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _gitDefaultCloneDir, value);
}
public bool GitAutoFetch
{
get => Models.AutoFetchManager.Instance.IsEnabled;
set
{
if (Models.AutoFetchManager.Instance.IsEnabled != value)
{
Models.AutoFetchManager.Instance.IsEnabled = value;
OnPropertyChanged();
}
}
}
public int? GitAutoFetchInterval
{
get => Models.AutoFetchManager.Instance.Interval;
set
{
if (value is null || value < 1)
return;
if (Models.AutoFetchManager.Instance.Interval != value)
{
Models.AutoFetchManager.Instance.Interval = (int)value;
OnPropertyChanged();
}
}
}
public int ShellOrTerminal
{
get => _shellOrTerminal;

View file

@ -140,6 +140,8 @@ namespace SourceGit.ViewModels
if (!rs)
return false;
_repo.MarkFetched();
// Use merge/rebase instead of pull as fetch is done manually.
if (UseRebase)
{

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;
}
}

View file

@ -42,6 +42,26 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _httpProxy, value);
}
public bool EnableAutoFetch
{
get => _repo.Settings.EnableAutoFetch;
set => _repo.Settings.EnableAutoFetch = value;
}
public int? AutoFetchInterval
{
get => _repo.Settings.AutoFetchInterval;
set
{
if (value is null || value < 1)
return;
var interval = (int)value;
if (_repo.Settings.AutoFetchInterval != interval)
_repo.Settings.AutoFetchInterval = interval;
}
}
public AvaloniaList<Models.CommitTemplate> CommitTemplates
{
get => _repo.Settings.CommitTemplates;