2021-04-29 05:05:55 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.Commands {
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 拉取
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Fetch : Command {
|
|
|
|
|
private Action<string> handler = null;
|
|
|
|
|
|
|
|
|
|
public Fetch(string repo, string remote, bool prune, Action<string> outputHandler) {
|
|
|
|
|
Cwd = repo;
|
|
|
|
|
TraitErrorAsOutput = true;
|
2021-10-12 02:14:48 -07:00
|
|
|
|
|
|
|
|
|
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
|
|
|
|
|
if (!string.IsNullOrEmpty(sshKey)) {
|
2021-10-12 02:26:39 -07:00
|
|
|
|
Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{sshKey}'");
|
2021-10-12 02:14:48 -07:00
|
|
|
|
Args = "";
|
|
|
|
|
} else {
|
2021-11-14 20:21:34 -08:00
|
|
|
|
Args = "-c credential.helper=manager-core ";
|
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;
|
|
|
|
|
handler = outputHandler;
|
|
|
|
|
AutoFetch.MarkFetched(repo);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 22:29:44 -07:00
|
|
|
|
public Fetch(string repo, string remote, string localBranch, string remoteBranch, Action<string> outputHandler) {
|
|
|
|
|
Cwd = repo;
|
|
|
|
|
TraitErrorAsOutput = true;
|
|
|
|
|
|
|
|
|
|
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
|
|
|
|
|
if (!string.IsNullOrEmpty(sshKey)) {
|
|
|
|
|
Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{sshKey}'");
|
|
|
|
|
Args = "";
|
|
|
|
|
} else {
|
|
|
|
|
Args = "-c credential.helper=manager-core ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Args += $"fetch --progress --verbose {remote} {remoteBranch}:{localBranch}";
|
|
|
|
|
handler = outputHandler;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
|
public override void OnReadline(string line) {
|
|
|
|
|
handler?.Invoke(line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 自动拉取(每隔10分钟)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class AutoFetch {
|
|
|
|
|
private static Dictionary<string, AutoFetch> jobs = new Dictionary<string, AutoFetch>();
|
|
|
|
|
|
|
|
|
|
private Fetch cmd = null;
|
|
|
|
|
private long nextFetchPoint = 0;
|
|
|
|
|
private Timer timer = null;
|
|
|
|
|
|
|
|
|
|
public static void Start(string repo) {
|
2021-10-12 20:19:45 -07:00
|
|
|
|
if (!Models.Preference.Instance.Git.AutoFetchRemotes) return;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
|
|
|
|
|
// 只自动更新加入管理列表中的仓库(子模块等不自动更新)
|
|
|
|
|
var exists = Models.Preference.Instance.FindRepository(repo);
|
|
|
|
|
if (exists == null) return;
|
|
|
|
|
|
|
|
|
|
var job = new AutoFetch(repo);
|
|
|
|
|
jobs.Add(repo, job);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void MarkFetched(string repo) {
|
|
|
|
|
if (!jobs.ContainsKey(repo)) return;
|
2021-04-29 18:05:29 -07:00
|
|
|
|
jobs[repo].nextFetchPoint = DateTime.Now.AddMinutes(10).ToFileTime();
|
2021-04-29 05:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Stop(string repo) {
|
|
|
|
|
if (!jobs.ContainsKey(repo)) return;
|
|
|
|
|
|
|
|
|
|
jobs[repo].timer.Dispose();
|
|
|
|
|
jobs.Remove(repo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AutoFetch(string repo) {
|
|
|
|
|
cmd = new Fetch(repo, "--all", true, null);
|
2021-05-30 18:03:58 -07:00
|
|
|
|
cmd.DontRaiseError = true;
|
|
|
|
|
|
2021-04-29 18:05:29 -07:00
|
|
|
|
nextFetchPoint = DateTime.Now.AddMinutes(10).ToFileTime();
|
2021-04-29 05:05:55 -07:00
|
|
|
|
timer = new Timer(OnTick, null, 60000, 10000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnTick(object o) {
|
|
|
|
|
var now = DateTime.Now.ToFileTime();
|
|
|
|
|
if (nextFetchPoint > now) return;
|
2021-08-05 00:54:00 -07:00
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
|
Models.Watcher.SetEnabled(cmd.Cwd, false);
|
|
|
|
|
cmd.Exec();
|
2021-04-29 18:05:29 -07:00
|
|
|
|
nextFetchPoint = DateTime.Now.AddMinutes(10).ToFileTime();
|
2021-04-29 05:05:55 -07:00
|
|
|
|
Models.Watcher.SetEnabled(cmd.Cwd, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|