diff --git a/src/Commands/Branch.cs b/src/Commands/Branch.cs index cd4ec599..890b54ee 100644 --- a/src/Commands/Branch.cs +++ b/src/Commands/Branch.cs @@ -25,14 +25,12 @@ var cmd = new Command(); cmd.WorkingDirectory = repo; cmd.Context = repo; + if (string.IsNullOrEmpty(upstream)) - { cmd.Args = $"branch {name} --unset-upstream"; - } else - { cmd.Args = $"branch {name} -u {upstream}"; - } + return cmd.Exec(); } @@ -50,14 +48,8 @@ var cmd = new Command(); cmd.WorkingDirectory = repo; cmd.Context = repo; - - var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); - if (string.IsNullOrEmpty(sshKey)) - cmd.Args = "-c credential.helper=manager "; - else - cmd.UseSSHKey(sshKey); - - cmd.Args += $"push {remote} --delete {name}"; + cmd.SSHKey = new Config(repo).Get($"remote.{remote}.sshkey"); + cmd.Args = $"push {remote} --delete {name}"; return cmd.Exec(); } } diff --git a/src/Commands/Clone.cs b/src/Commands/Clone.cs index cdefbb23..683b8846 100644 --- a/src/Commands/Clone.cs +++ b/src/Commands/Clone.cs @@ -11,17 +11,14 @@ namespace SourceGit.Commands Context = ctx; WorkingDirectory = path; TraitErrorAsOutput = true; - - if (string.IsNullOrEmpty(sshKey)) - Args = "-c credential.helper=manager "; - else - UseSSHKey(sshKey); - - Args += "clone --progress --verbose --recurse-submodules "; + SSHKey = sshKey; + Args = "clone --progress --verbose --recurse-submodules "; if (!string.IsNullOrEmpty(extraArgs)) Args += $"{extraArgs} "; + Args += $"{url} "; + if (!string.IsNullOrEmpty(localName)) Args += localName; diff --git a/src/Commands/Command.cs b/src/Commands/Command.cs index 1adb9422..1cd499d0 100644 --- a/src/Commands/Command.cs +++ b/src/Commands/Command.cs @@ -33,23 +33,10 @@ namespace SourceGit.Commands public CancelToken Cancel { get; set; } = null; public string WorkingDirectory { get; set; } = null; public EditorType Editor { get; set; } = EditorType.CoreEditor; // Only used in Exec() mode + public string SSHKey { get; set; } = string.Empty; public string Args { get; set; } = string.Empty; public bool RaiseError { get; set; } = true; public bool TraitErrorAsOutput { get; set; } = false; - public Dictionary Envs { get; set; } = new Dictionary(); - - public void UseSSHKey(string key) - { - UseSSHAskpass(); - Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{key}'"); - } - - public void UseSSHAskpass() - { - Envs.Add("DISPLAY", "required"); - Envs.Add("SSH_ASKPASS", $"\"{Process.GetCurrentProcess().MainModule.FileName}\" --askpass"); - Envs.Add("SSH_ASKPASS_REQUIRE", "prefer"); - } public bool Exec() { @@ -63,15 +50,30 @@ namespace SourceGit.Commands start.StandardOutputEncoding = Encoding.UTF8; start.StandardErrorEncoding = Encoding.UTF8; - // Editors - var editorProgram = $"\\\"{Process.GetCurrentProcess().MainModule.FileName}\\\""; + // Force using this app as SSH askpass program + var selfExecFile = Process.GetCurrentProcess().MainModule.FileName; + start.Environment.Add("DISPLAY", "required"); + start.Environment.Add("SSH_ASKPASS", $"\"{selfExecFile}\" --askpass"); + start.Environment.Add("SSH_ASKPASS_REQUIRE", "prefer"); + + // If an SSH private key was provided, sets the environment. + if (!string.IsNullOrEmpty(SSHKey)) + start.Environment.Add("GIT_SSH_COMMAND", $"ssh -i '{SSHKey}'"); + else + start.Arguments += "-c credential.helper=manager "; + + // Force using en_US.UTF-8 locale to avoid GCM crash + if (OperatingSystem.IsLinux()) + start.Environment.Add("LANG", "en_US.UTF-8"); + + // Force using this app as git editor. switch (Editor) { case EditorType.CoreEditor: - start.Arguments += $"-c core.editor=\"{editorProgram} --core-editor\" "; + start.Arguments += $"-c core.editor=\"\\\"{selfExecFile}\\\" --core-editor\" "; break; case EditorType.RebaseEditor: - start.Arguments += $"-c core.editor=\"{editorProgram} --rebase-message-editor\" -c sequence.editor=\"{editorProgram} --rebase-todo-editor\" -c rebase.abbreviateCommands=true "; + start.Arguments += $"-c core.editor=\"\\\"{selfExecFile}\\\" --rebase-message-editor\" -c sequence.editor=\"\\\"{selfExecFile}\\\" --rebase-todo-editor\" -c rebase.abbreviateCommands=true "; break; default: start.Arguments += "-c core.editor=true "; @@ -81,14 +83,7 @@ namespace SourceGit.Commands // Append command args start.Arguments += Args; - // User environment overrides. - foreach (var kv in Envs) - start.Environment.Add(kv.Key, kv.Value); - - // Force using en_US.UTF-8 locale to avoid GCM crash - if (OperatingSystem.IsLinux()) - start.Environment.Add("LANG", "en_US.UTF-8"); - + // Working directory if (!string.IsNullOrEmpty(WorkingDirectory)) start.WorkingDirectory = WorkingDirectory; diff --git a/src/Commands/Commit.cs b/src/Commands/Commit.cs index 8c8f575d..6232fc17 100644 --- a/src/Commands/Commit.cs +++ b/src/Commands/Commit.cs @@ -19,8 +19,6 @@ namespace SourceGit.Commands Args += " --amend --no-edit"; if (allowEmpty) Args += " --allow-empty"; - - UseSSHAskpass(); } } } diff --git a/src/Commands/Fetch.cs b/src/Commands/Fetch.cs index c169645d..03bba91c 100644 --- a/src/Commands/Fetch.cs +++ b/src/Commands/Fetch.cs @@ -13,14 +13,9 @@ namespace SourceGit.Commands WorkingDirectory = repo; Context = repo; TraitErrorAsOutput = true; + SSHKey = new Config(repo).Get($"remote.{remote}.sshkey"); + Args = "fetch --progress --verbose "; - var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); - if (string.IsNullOrEmpty(sshKey)) - Args = "-c credential.helper=manager "; - else - UseSSHKey(sshKey); - - Args += "fetch --progress --verbose "; if (prune) Args += "--prune "; @@ -40,14 +35,8 @@ namespace SourceGit.Commands WorkingDirectory = repo; Context = repo; TraitErrorAsOutput = true; - - var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); - if (string.IsNullOrEmpty(sshKey)) - Args = "-c credential.helper=manager "; - else - UseSSHKey(sshKey); - - Args += $"fetch --progress --verbose {remote} {remoteBranch}:{localBranch}"; + SSHKey = new Config(repo).Get($"remote.{remote}.sshkey"); + Args = $"fetch --progress --verbose {remote} {remoteBranch}:{localBranch}"; } protected override void OnReadline(string line) diff --git a/src/Commands/Pull.cs b/src/Commands/Pull.cs index 5ed81603..a4efa4b6 100644 --- a/src/Commands/Pull.cs +++ b/src/Commands/Pull.cs @@ -10,14 +10,9 @@ namespace SourceGit.Commands WorkingDirectory = repo; Context = repo; TraitErrorAsOutput = true; + SSHKey = new Config(repo).Get($"remote.{remote}.sshkey"); + Args = "pull --verbose --progress --tags "; - var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); - if (string.IsNullOrEmpty(sshKey)) - Args = "-c credential.helper=manager "; - else - UseSSHKey(sshKey); - - Args += "pull --verbose --progress --tags "; if (useRebase) Args += "--rebase "; if (noTags) diff --git a/src/Commands/Push.cs b/src/Commands/Push.cs index 80ad10e6..31a69eb9 100644 --- a/src/Commands/Push.cs +++ b/src/Commands/Push.cs @@ -6,18 +6,13 @@ namespace SourceGit.Commands { public Push(string repo, string local, string remote, string remoteBranch, bool withTags, bool force, bool track, Action onProgress) { + _outputHandler = onProgress; + WorkingDirectory = repo; Context = repo; TraitErrorAsOutput = true; - _outputHandler = onProgress; - - var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); - if (string.IsNullOrEmpty(sshKey)) - Args = "-c credential.helper=manager "; - else - UseSSHKey(sshKey); - - Args += "push --progress --verbose "; + SSHKey = new Config(repo).Get($"remote.{remote}.sshkey"); + Args = "push --progress --verbose "; if (withTags) Args += "--tags "; @@ -33,16 +28,12 @@ namespace SourceGit.Commands { WorkingDirectory = repo; Context = repo; + SSHKey = new Config(repo).Get($"remote.{remote}.sshkey"); + Args = "push "; - var sshKey = new Config(repo).Get($"remote.{remote}.sshkey"); - if (string.IsNullOrEmpty(sshKey)) - Args = "-c credential.helper=manager "; - else - UseSSHKey(sshKey); - - Args += "push "; if (isDelete) Args += "--delete "; + Args += $"{remote} refs/tags/{tag}"; }