From 39304496502c762b3264b117e5f6bd1864108ce9 Mon Sep 17 00:00:00 2001 From: Alistair Scott Date: Wed, 6 Sep 2023 13:16:17 +0100 Subject: [PATCH] Don't break existing `GIT_SSH_COMMAND` env var --- get_git.go | 11 +++++++---- get_git_test.go | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/get_git.go b/get_git.go index 2313a3fba..12af61fb6 100644 --- a/get_git.go +++ b/get_git.go @@ -319,17 +319,20 @@ func setupGitEnv(cmd *exec.Cmd, sshKeyFile string) { } } - if len(sshCmd) == 0 { - sshCmd = []string{gitSSHCommand + "ssh"} - } - if sshKeyFile != "" { // We have an SSH key temp file configured, tell ssh about this. if runtime.GOOS == "windows" { sshKeyFile = strings.Replace(sshKeyFile, `\`, `/`, -1) } + + if len(sshCmd) == 0 { + sshCmd = []string{gitSSHCommand + "ssh"} + } + sshCmd = append(sshCmd, "-i", sshKeyFile) env = append(env, strings.Join(sshCmd, " ")) + } else if len(sshCmd) > 0 { + env = append(env, strings.Join(sshCmd, " ")) } cmd.Env = env diff --git a/get_git_test.go b/get_git_test.go index d123bb5c5..6a2790902 100644 --- a/get_git_test.go +++ b/get_git_test.go @@ -699,6 +699,29 @@ func TestGitGetter_setupGitEnvWithExisting_sshKey(t *testing.T) { } } +func TestGitGetter_setupGitEnvWithExisting_noSSHKey(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skipf("skipping on windows since the test requires sh") + return + } + + // start with an existing ssh command configuration + os.Setenv("GIT_SSH_COMMAND", "ssh -o StrictHostKeyChecking=no -o IdentitiesOnly=yes") + defer os.Setenv("GIT_SSH_COMMAND", "") + + cmd := exec.Command("/bin/sh", "-c", "echo $GIT_SSH_COMMAND") + setupGitEnv(cmd, "") + out, err := cmd.Output() + if err != nil { + t.Fatal(err) + } + + actual := strings.TrimSpace(string(out)) + if actual != "ssh -o StrictHostKeyChecking=no -o IdentitiesOnly=yes" { + t.Fatalf("unexpected GIT_SSH_COMMAND: %q", actual) + } +} + func TestGitGetter_subdirectory_symlink(t *testing.T) { if !testHasGit { t.Skip("git not found, skipping")