2024-02-05 23:08:37 -08:00
|
|
|
|
using System;
|
2024-02-18 00:26:07 -08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.Commands
|
|
|
|
|
{
|
|
|
|
|
public class Fetch : Command
|
|
|
|
|
{
|
2024-07-01 01:19:08 -07:00
|
|
|
|
public Fetch(string repo, string remote, bool prune, bool noTags, Action<string> outputHandler)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_outputHandler = outputHandler;
|
|
|
|
|
WorkingDirectory = repo;
|
|
|
|
|
Context = repo;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
TraitErrorAsOutput = true;
|
2024-07-09 03:13:15 -07:00
|
|
|
|
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
|
|
|
|
|
Args = "fetch --progress --verbose ";
|
2021-10-12 02:14:48 -07:00
|
|
|
|
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (prune)
|
|
|
|
|
Args += "--prune ";
|
2024-07-01 01:19:08 -07:00
|
|
|
|
|
|
|
|
|
if (noTags)
|
|
|
|
|
Args += "--no-tags ";
|
|
|
|
|
else
|
|
|
|
|
Args += "--force ";
|
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
|
Args += remote;
|
2024-02-18 00:26:07 -08:00
|
|
|
|
|
|
|
|
|
AutoFetch.MarkFetched(repo);
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public Fetch(string repo, string remote, string localBranch, string remoteBranch, Action<string> outputHandler)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_outputHandler = outputHandler;
|
|
|
|
|
WorkingDirectory = repo;
|
|
|
|
|
Context = repo;
|
2023-08-17 22:29:44 -07:00
|
|
|
|
TraitErrorAsOutput = true;
|
2024-07-09 03:13:15 -07:00
|
|
|
|
SSHKey = new Config(repo).Get($"remote.{remote}.sshkey");
|
|
|
|
|
Args = $"fetch --progress --verbose {remote} {remoteBranch}:{localBranch}";
|
2023-08-17 22:29:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
protected override void OnReadline(string line)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_outputHandler?.Invoke(line);
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly Action<string> _outputHandler;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
2024-02-18 00:26:07 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public class AutoFetch
|
|
|
|
|
{
|
|
|
|
|
public static bool IsEnabled
|
|
|
|
|
{
|
2024-02-18 00:26:07 -08:00
|
|
|
|
get;
|
|
|
|
|
set;
|
|
|
|
|
} = false;
|
|
|
|
|
|
2024-05-11 02:37:54 -07:00
|
|
|
|
public static int Interval
|
|
|
|
|
{
|
|
|
|
|
get => _interval;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value < 1)
|
|
|
|
|
return;
|
|
|
|
|
_interval = value;
|
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
|
|
|
|
foreach (var job in _jobs)
|
|
|
|
|
{
|
|
|
|
|
job.Value.NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(_interval));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
class Job
|
|
|
|
|
{
|
2024-02-18 00:26:07 -08:00
|
|
|
|
public Fetch Cmd = null;
|
|
|
|
|
public DateTime NextRunTimepoint = DateTime.MinValue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
static AutoFetch()
|
|
|
|
|
{
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if (!IsEnabled)
|
|
|
|
|
{
|
2024-02-18 00:26:07 -08:00
|
|
|
|
Thread.Sleep(10000);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var now = DateTime.Now;
|
|
|
|
|
var uptodate = new List<Job>();
|
2024-03-17 18:37:06 -07:00
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
|
|
|
|
foreach (var job in _jobs)
|
|
|
|
|
{
|
|
|
|
|
if (job.Value.NextRunTimepoint.Subtract(now).TotalSeconds <= 0)
|
|
|
|
|
{
|
2024-02-18 00:26:07 -08:00
|
|
|
|
uptodate.Add(job.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var job in uptodate)
|
|
|
|
|
{
|
2024-02-18 00:26:07 -08:00
|
|
|
|
job.Cmd.Exec();
|
2024-05-11 02:37:54 -07:00
|
|
|
|
job.NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(Interval));
|
2024-02-18 00:26:07 -08:00
|
|
|
|
}
|
2024-02-19 01:40:36 -08:00
|
|
|
|
|
|
|
|
|
Thread.Sleep(2000);
|
2024-02-18 00:26:07 -08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public static void AddRepository(string repo)
|
|
|
|
|
{
|
|
|
|
|
var job = new Job
|
|
|
|
|
{
|
2024-07-01 01:19:08 -07:00
|
|
|
|
Cmd = new Fetch(repo, "--all", true, false, null) { RaiseError = false },
|
2024-05-11 02:37:54 -07:00
|
|
|
|
NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(Interval)),
|
2024-02-18 00:26:07 -08:00
|
|
|
|
};
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
|
|
|
|
if (_jobs.ContainsKey(repo))
|
|
|
|
|
{
|
2024-02-18 00:26:07 -08:00
|
|
|
|
_jobs[repo] = job;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-18 00:26:07 -08:00
|
|
|
|
_jobs.Add(repo, job);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public static void RemoveRepository(string repo)
|
|
|
|
|
{
|
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
2024-02-18 00:26:07 -08:00
|
|
|
|
_jobs.Remove(repo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public static void MarkFetched(string repo)
|
|
|
|
|
{
|
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (_jobs.TryGetValue(repo, out var value))
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-05-11 02:37:54 -07:00
|
|
|
|
value.NextRunTimepoint = DateTime.Now.AddMinutes(Convert.ToDouble(Interval));
|
2024-02-18 00:26:07 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private static readonly Dictionary<string, Job> _jobs = new Dictionary<string, Job>();
|
|
|
|
|
private static readonly object _lock = new object();
|
2024-05-11 02:37:54 -07:00
|
|
|
|
private static int _interval = 10;
|
2024-02-18 00:26:07 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|