feature<AutoFetch>: finish auto fetch remotes

This commit is contained in:
leo 2024-02-18 16:26:07 +08:00
parent b40ca42d73
commit 05c9d9be5b
6 changed files with 88 additions and 6 deletions

View file

@ -1,4 +1,7 @@
using System; using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace SourceGit.Commands { namespace SourceGit.Commands {
public class Fetch : Command { public class Fetch : Command {
@ -18,6 +21,8 @@ namespace SourceGit.Commands {
Args += "fetch --progress --verbose "; Args += "fetch --progress --verbose ";
if (prune) Args += "--prune "; if (prune) Args += "--prune ";
Args += remote; Args += remote;
AutoFetch.MarkFetched(repo);
} }
public Fetch(string repo, string remote, string localBranch, string remoteBranch, Action<string> outputHandler) { public Fetch(string repo, string remote, string localBranch, string remoteBranch, Action<string> outputHandler) {
@ -42,4 +47,75 @@ namespace SourceGit.Commands {
private Action<string> _outputHandler; private Action<string> _outputHandler;
} }
public class AutoFetch {
public static bool IsEnabled {
get;
set;
} = false;
class Job {
public Fetch Cmd = null;
public DateTime NextRunTimepoint = DateTime.MinValue;
}
static AutoFetch() {
Task.Run(() => {
while (true) {
if (!IsEnabled) {
Thread.Sleep(10000);
continue;
}
var now = DateTime.Now;
var uptodate = new List<Job>();
lock (_lock) {
foreach (var job in _jobs) {
if (job.Value.NextRunTimepoint.Subtract(now).TotalSeconds <= 0) {
uptodate.Add(job.Value);
}
}
}
foreach (var job in uptodate) {
job.Cmd.Exec();
job.NextRunTimepoint = DateTime.Now.AddSeconds(_fetchInterval);
}
}
});
}
public static void AddRepository(string repo) {
var job = new Job {
Cmd = new Fetch(repo, "--all", true, null) { RaiseError = false },
NextRunTimepoint = DateTime.Now.AddSeconds(_fetchInterval),
};
lock (_lock) {
if (_jobs.ContainsKey(repo)) {
_jobs[repo] = job;
} else {
_jobs.Add(repo, job);
}
}
}
public static void RemoveRepository(string repo) {
lock (_lock) {
_jobs.Remove(repo);
}
}
public static void MarkFetched(string repo) {
lock (_lock) {
if (_jobs.ContainsKey(repo)) {
_jobs[repo].NextRunTimepoint = DateTime.Now.AddSeconds(_fetchInterval);
}
}
}
private static Dictionary<string, Job> _jobs = new Dictionary<string, Job>();
private static object _lock = new object();
private static double _fetchInterval = 10 * 60;
}
} }

View file

@ -393,7 +393,7 @@
<sys:String x:Key="Text.Preference.Git.Email">User Email</sys:String> <sys:String x:Key="Text.Preference.Git.Email">User Email</sys:String>
<sys:String x:Key="Text.Preference.Git.Email.Placeholder">Global git user email</sys:String> <sys:String x:Key="Text.Preference.Git.Email.Placeholder">Global git user email</sys:String>
<sys:String x:Key="Text.Preference.Git.CRLF">Enable Auto CRLF</sys:String> <sys:String x:Key="Text.Preference.Git.CRLF">Enable Auto CRLF</sys:String>
<sys:String x:Key="Text.Preference.Git.AutoFetch">Fetch remotes automatically (need restart)</sys:String> <sys:String x:Key="Text.Preference.Git.AutoFetch">Fetch remotes automatically</sys:String>
<sys:String x:Key="Text.Preference.GPG">GPG SIGNING</sys:String> <sys:String x:Key="Text.Preference.GPG">GPG SIGNING</sys:String>
<sys:String x:Key="Text.Preference.GPG.Enabled">Commit GPG signing</sys:String> <sys:String x:Key="Text.Preference.GPG.Enabled">Commit GPG signing</sys:String>
<sys:String x:Key="Text.Preference.GPG.Path">Install Path</sys:String> <sys:String x:Key="Text.Preference.GPG.Path">Install Path</sys:String>

View file

@ -392,7 +392,7 @@
<sys:String x:Key="Text.Preference.Git.Email">邮箱</sys:String> <sys:String x:Key="Text.Preference.Git.Email">邮箱</sys:String>
<sys:String x:Key="Text.Preference.Git.Email.Placeholder">默认GIT用户邮箱</sys:String> <sys:String x:Key="Text.Preference.Git.Email.Placeholder">默认GIT用户邮箱</sys:String>
<sys:String x:Key="Text.Preference.Git.CRLF">自动换行转换</sys:String> <sys:String x:Key="Text.Preference.Git.CRLF">自动换行转换</sys:String>
<sys:String x:Key="Text.Preference.Git.AutoFetch">启用定时自动拉取远程更新(重启生效)</sys:String> <sys:String x:Key="Text.Preference.Git.AutoFetch">启用定时自动拉取远程更新</sys:String>
<sys:String x:Key="Text.Preference.GPG">GPG签名</sys:String> <sys:String x:Key="Text.Preference.GPG">GPG签名</sys:String>
<sys:String x:Key="Text.Preference.GPG.Enabled">启用提交签名</sys:String> <sys:String x:Key="Text.Preference.GPG.Enabled">启用提交签名</sys:String>
<sys:String x:Key="Text.Preference.GPG.Path">可执行文件位置</sys:String> <sys:String x:Key="Text.Preference.GPG.Path">可执行文件位置</sys:String>

View file

@ -123,6 +123,7 @@ namespace SourceGit.ViewModels {
} }
repo.Open(); repo.Open();
Commands.AutoFetch.AddRepository(repo.FullPath);
if (page == null) { if (page == null) {
if (ActivePage == null || ActivePage.Node.IsRepository) { if (ActivePage == null || ActivePage.Node.IsRepository) {
@ -146,6 +147,7 @@ namespace SourceGit.ViewModels {
var repo = Preference.FindRepository(page.Node.Id); var repo = Preference.FindRepository(page.Node.Id);
if (repo == null) return; if (repo == null) return;
Commands.AutoFetch.RemoveRepository(repo.FullPath);
repo.Close(); repo.Close();
} }

View file

@ -112,8 +112,13 @@ namespace SourceGit.ViewModels {
} }
public bool GitAutoFetch { public bool GitAutoFetch {
get => _gitAutoFetch; get => Commands.AutoFetch.IsEnabled;
set => SetProperty(ref _gitAutoFetch, value); set {
if (Commands.AutoFetch.IsEnabled != value) {
Commands.AutoFetch.IsEnabled = value;
OnPropertyChanged(nameof(GitAutoFetch));
}
}
} }
public int ExternalMergeToolType { public int ExternalMergeToolType {
@ -242,7 +247,6 @@ namespace SourceGit.ViewModels {
private Models.ChangeViewMode _commitChangeViewMode = Models.ChangeViewMode.List; private Models.ChangeViewMode _commitChangeViewMode = Models.ChangeViewMode.List;
private string _gitDefaultCloneDir = string.Empty; private string _gitDefaultCloneDir = string.Empty;
private bool _gitAutoFetch = false;
private int _externalMergeToolType = 0; private int _externalMergeToolType = 0;
private string _externalMergeToolPath = string.Empty; private string _externalMergeToolPath = string.Empty;

View file

@ -103,7 +103,7 @@
<!-- Messages --> <!-- Messages -->
<TextBlock Grid.Row="3" Grid.Column="0" Classes="info_label" Text="{DynamicResource Text.CommitDetail.Info.Message}" VerticalAlignment="Top" Margin="0,4,0,0" /> <TextBlock Grid.Row="3" Grid.Column="0" Classes="info_label" Text="{DynamicResource Text.CommitDetail.Info.Message}" VerticalAlignment="Top" Margin="0,4,0,0" />
<ScrollViewer Grid.Row="3" Grid.Column="1" Margin="12,4,0,0" MaxHeight="100" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"> <ScrollViewer Grid.Row="3" Grid.Column="1" Margin="12,5,0,0" MaxHeight="100" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<SelectableTextBlock Text="{Binding FullMessage}" FontSize="12" FontFamily="{StaticResource JetBrainsMono}" TextWrapping="Wrap"/> <SelectableTextBlock Text="{Binding FullMessage}" FontSize="12" FontFamily="{StaticResource JetBrainsMono}" TextWrapping="Wrap"/>
</ScrollViewer> </ScrollViewer>
</Grid> </Grid>