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;
|
2023-08-04 04:00:21 -07:00
|
|
|
needStash = autoStash;
|
2021-04-29 05:05:55 -07:00
|
|
|
|
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 += "pull --verbose --progress --tags ";
|
2021-04-29 05:05:55 -07:00
|
|
|
if (useRebase) Args += "--rebase ";
|
|
|
|
Args += $"{remote} {branch}";
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Run() {
|
|
|
|
if (needStash) {
|
|
|
|
var changes = new LocalChanges(Cwd).Result();
|
|
|
|
if (changes.Count > 0) {
|
2023-10-10 01:59:47 -07:00
|
|
|
if (!new Stash(Cwd).Push(changes, "PULL_AUTO_STASH", true)) {
|
2021-04-29 05:05:55 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
needStash = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var succ = Exec();
|
2023-08-04 04:00:21 -07:00
|
|
|
if (succ && needStash) new Stash(Cwd).Pop("stash@{0}");
|
2021-04-29 05:05:55 -07:00
|
|
|
return succ;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void OnReadline(string line) {
|
|
|
|
handler?.Invoke(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|