optimize<SSH>: using core.sshCommand instead of environment parameter GIT_SSH_COMMAND

This commit is contained in:
leo 2023-08-23 20:45:12 +08:00
parent fc43edb6d2
commit 1c10d9a286
6 changed files with 16 additions and 30 deletions

View file

@ -15,9 +15,8 @@ namespace SourceGit.Commands {
handler = outputHandler; handler = outputHandler;
onError = errHandler; onError = errHandler;
if (!string.IsNullOrEmpty(sshKey)) { if (string.IsNullOrEmpty(sshKey)) {
Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{sshKey}'"); Args = $"-c core.sshCommand=\"ssh -i '{sshKey}'\" ";
Args = "";
} else { } else {
Args = "-c credential.helper=manager "; Args = "-c credential.helper=manager ";
} }

View file

@ -52,11 +52,6 @@ namespace SourceGit.Commands {
/// </summary> /// </summary>
public bool TraitErrorAsOutput { get; set; } = false; public bool TraitErrorAsOutput { get; set; } = false;
/// <summary>
/// 用于设置该进程独有的环境变量
/// </summary>
public Dictionary<string, string> Envs { get; set; } = new Dictionary<string, string>();
/// <summary> /// <summary>
/// 运行 /// 运行
/// </summary> /// </summary>
@ -73,8 +68,6 @@ namespace SourceGit.Commands {
if (!string.IsNullOrEmpty(Cwd)) start.WorkingDirectory = Cwd; if (!string.IsNullOrEmpty(Cwd)) start.WorkingDirectory = Cwd;
foreach (var kv in Envs) start.EnvironmentVariables[kv.Key] = kv.Value;
var progressFilter = new Regex(@"\s\d+%\s"); var progressFilter = new Regex(@"\s\d+%\s");
var errs = new List<string>(); var errs = new List<string>();
var proc = new Process() { StartInfo = start }; var proc = new Process() { StartInfo = start };

View file

@ -16,8 +16,7 @@ namespace SourceGit.Commands {
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
if (!string.IsNullOrEmpty(sshKey)) { if (!string.IsNullOrEmpty(sshKey)) {
Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{sshKey}'"); Args = $"-c core.sshCommand=\"ssh -i '{sshKey}'\" ";
Args = "";
} else { } else {
Args = "-c credential.helper=manager "; Args = "-c credential.helper=manager ";
} }
@ -35,8 +34,7 @@ namespace SourceGit.Commands {
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
if (!string.IsNullOrEmpty(sshKey)) { if (!string.IsNullOrEmpty(sshKey)) {
Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{sshKey}'"); Args = $"-c core.sshCommand=\"ssh -i '{sshKey}'\" ";
Args = "";
} else { } else {
Args = "-c credential.helper=manager "; Args = "-c credential.helper=manager ";
} }

View file

@ -17,8 +17,7 @@ namespace SourceGit.Commands {
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
if (!string.IsNullOrEmpty(sshKey)) { if (!string.IsNullOrEmpty(sshKey)) {
Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{sshKey}'"); Args = $"-c core.sshCommand=\"ssh -i '{sshKey}'\" ";
Args = "";
} else { } else {
Args = "-c credential.helper=manager "; Args = "-c credential.helper=manager ";
} }

View file

@ -14,8 +14,7 @@ namespace SourceGit.Commands {
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
if (!string.IsNullOrEmpty(sshKey)) { if (!string.IsNullOrEmpty(sshKey)) {
Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{sshKey}'"); Args = $"-c core.sshCommand=\"ssh -i '{sshKey}'\" ";
Args = "";
} else { } else {
Args = "-c credential.helper=manager "; Args = "-c credential.helper=manager ";
} }
@ -34,8 +33,7 @@ namespace SourceGit.Commands {
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
if (!string.IsNullOrEmpty(sshKey)) { if (!string.IsNullOrEmpty(sshKey)) {
Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{sshKey}'"); Args = $"-c core.sshCommand=\"ssh -i '{sshKey}'\" ";
Args = "";
} else { } else {
Args = "-c credential.helper=manager "; Args = "-c credential.helper=manager ";
} }
@ -48,8 +46,7 @@ namespace SourceGit.Commands {
var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); var sshKey = new Config(repo).Get($"remote.{remote}.sshkey");
if (!string.IsNullOrEmpty(sshKey)) { if (!string.IsNullOrEmpty(sshKey)) {
Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{sshKey}'"); Args = $"-c core.sshCommand=\"ssh -i '{sshKey}'\" ";
Args = "";
} else { } else {
Args = "-c credential.helper=manager "; Args = "-c credential.helper=manager ";
} }

View file

@ -52,13 +52,16 @@ namespace SourceGit.Views.Popups {
return Task.Run(() => { return Task.Run(() => {
Models.Watcher.SetEnabled(repo.Path, false); Models.Watcher.SetEnabled(repo.Path, false);
if (string.IsNullOrEmpty(sshKey)) {
new Commands.Config(repo.Path).Set($"remote.{RemoteName}.sshkey", null);
} else {
new Commands.Config(repo.Path).Set($"remote.{RemoteName}.sshkey", sshKey);
}
if (remote == null) { if (remote == null) {
var succ = new Commands.Remote(repo.Path).Add(RemoteName, RemoteURL); var succ = new Commands.Remote(repo.Path).Add(RemoteName, RemoteURL);
if (succ) new Commands.Fetch(repo.Path, RemoteName, true, UpdateProgress).Exec(); if (succ) new Commands.Fetch(repo.Path, RemoteName, true, UpdateProgress).Exec();
if (!string.IsNullOrEmpty(sshKey)) {
new Commands.Config(repo.Path).Set($"remote.{RemoteName}.sshkey", sshKey);
}
} else { } else {
if (remote.URL != RemoteURL) { if (remote.URL != RemoteURL) {
var succ = new Commands.Remote(repo.Path).SetURL(remote.Name, RemoteURL); var succ = new Commands.Remote(repo.Path).SetURL(remote.Name, RemoteURL);
@ -69,11 +72,8 @@ namespace SourceGit.Views.Popups {
var succ = new Commands.Remote(repo.Path).Rename(remote.Name, RemoteName); var succ = new Commands.Remote(repo.Path).Rename(remote.Name, RemoteName);
if (succ) remote.Name = RemoteName; if (succ) remote.Name = RemoteName;
} }
}
if (!string.IsNullOrEmpty(sshKey)) {
new Commands.Config(repo.Path).Set($"remote.{RemoteName}.sshkey", sshKey);
}
}
Models.Watcher.SetEnabled(repo.Path, true); Models.Watcher.SetEnabled(repo.Path, true);
return true; return true;
}); });