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
|
|
|
|
|
|
|
|
|
namespace SourceGit.Commands {
|
|
|
|
|
public class Fetch : Command {
|
|
|
|
|
public Fetch(string repo, string remote, bool prune, Action<string> outputHandler) {
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_outputHandler = outputHandler;
|
|
|
|
|
WorkingDirectory = repo;
|
|
|
|
|
Context = repo;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
TraitErrorAsOutput = true;
|
2021-10-12 02:14:48 -07:00
|
|
|
|
|
|
|
|
|
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
|
|
|
|
|
if (!string.IsNullOrEmpty(sshKey)) {
|
2023-08-23 05:45:12 -07:00
|
|
|
|
Args = $"-c core.sshCommand=\"ssh -i '{sshKey}'\" ";
|
2021-10-12 02:14:48 -07:00
|
|
|
|
} else {
|
2023-08-22 23:05:19 -07:00
|
|
|
|
Args = "-c credential.helper=manager ";
|
2021-10-12 02:14:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Args += "fetch --progress --verbose ";
|
2021-04-29 05:05:55 -07:00
|
|
|
|
if (prune) Args += "--prune ";
|
|
|
|
|
Args += remote;
|
2024-02-18 00:26:07 -08:00
|
|
|
|
|
|
|
|
|
AutoFetch.MarkFetched(repo);
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 22:29:44 -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;
|
|
|
|
|
|
|
|
|
|
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
|
|
|
|
|
if (!string.IsNullOrEmpty(sshKey)) {
|
2023-08-23 05:45:12 -07:00
|
|
|
|
Args = $"-c core.sshCommand=\"ssh -i '{sshKey}'\" ";
|
2023-08-17 22:29:44 -07:00
|
|
|
|
} else {
|
2023-08-22 23:05:19 -07:00
|
|
|
|
Args = "-c credential.helper=manager ";
|
2023-08-17 22:29:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Args += $"fetch --progress --verbose {remote} {remoteBranch}:{localBranch}";
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
protected override void OnReadline(string line) {
|
|
|
|
|
_outputHandler?.Invoke(line);
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
private Action<string> _outputHandler;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
2024-02-18 00:26:07 -08:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2024-02-19 01:40:36 -08:00
|
|
|
|
|
|
|
|
|
Thread.Sleep(2000);
|
2024-02-18 00:26:07 -08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|