2021-04-29 05:05:55 -07:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace SourceGit.Commands {
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 拉回
|
|
|
|
/// </summary>
|
|
|
|
public class Pull : Command {
|
|
|
|
private Action<string> handler = null;
|
|
|
|
private bool needStash = false;
|
|
|
|
|
|
|
|
public Pull(string repo, string remote, string branch, bool useRebase, bool autoStash, Action<string> onProgress) {
|
|
|
|
Cwd = repo;
|
|
|
|
TraitErrorAsOutput = true;
|
|
|
|
handler = onProgress;
|
|
|
|
|
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 {
|
|
|
|
Args = "-c credential.helper=manager ";
|
|
|
|
}
|
|
|
|
|
|
|
|
Args += "pull --verbose --progress --tags ";
|
|
|
|
|
2021-04-29 05:05:55 -07:00
|
|
|
if (useRebase) Args += "--rebase ";
|
|
|
|
if (autoStash) {
|
|
|
|
if (useRebase) Args += "--autostash ";
|
|
|
|
else needStash = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Args += $"{remote} {branch}";
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Run() {
|
|
|
|
if (needStash) {
|
|
|
|
var changes = new LocalChanges(Cwd).Result();
|
|
|
|
if (changes.Count > 0) {
|
2021-04-29 23:21:52 -07:00
|
|
|
if (!new Stash(Cwd).Push(changes, "PULL_AUTO_STASH")) {
|
2021-04-29 05:05:55 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
needStash = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var succ = Exec();
|
|
|
|
if (needStash) new Stash(Cwd).Pop("stash@{0}");
|
|
|
|
return succ;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void OnReadline(string line) {
|
|
|
|
handler?.Invoke(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|