Skip to content

Commit c70d9c9

Browse files
committed
don't change GIT_SSH_COMMAND if there's no keyfile
The reason for setupGitEnv is to add a `-i keyfile` argument to GIT_SSH_COMMAND. If there is no keyfile, we do not need to modify the environment at all.
1 parent 3d5770f commit c70d9c9

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

get_git.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ func findRemoteDefaultBranch(ctx context.Context, u *url.URL) string {
302302
// setupGitEnv sets up the environment for the given command. This is used to
303303
// pass configuration data to git and ssh and enables advanced cloning methods.
304304
func setupGitEnv(cmd *exec.Cmd, sshKeyFile string) {
305+
// If there's no sshKeyFile argument to deal with, we can skip this
306+
// entirely.
307+
if sshKeyFile == "" {
308+
return
309+
}
305310
const gitSSHCommand = "GIT_SSH_COMMAND="
306311
var sshCmd []string
307312

@@ -323,14 +328,12 @@ func setupGitEnv(cmd *exec.Cmd, sshKeyFile string) {
323328
sshCmd = []string{gitSSHCommand + "ssh"}
324329
}
325330

326-
if sshKeyFile != "" {
327-
// We have an SSH key temp file configured, tell ssh about this.
328-
if runtime.GOOS == "windows" {
329-
sshKeyFile = strings.Replace(sshKeyFile, `\`, `/`, -1)
330-
}
331-
sshCmd = append(sshCmd, "-i", sshKeyFile)
332-
env = append(env, strings.Join(sshCmd, " "))
331+
// We have an SSH key temp file configured, tell ssh about this.
332+
if runtime.GOOS == "windows" {
333+
sshKeyFile = strings.Replace(sshKeyFile, `\`, `/`, -1)
333334
}
335+
sshCmd = append(sshCmd, "-i", sshKeyFile)
336+
env = append(env, strings.Join(sshCmd, " "))
334337

335338
cmd.Env = env
336339
}

get_git_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,29 @@ func TestGitGetter_setupGitEnvWithExisting_sshKey(t *testing.T) {
699699
}
700700
}
701701

702+
func TestGitGetter_setupGitEnvWithNoKeyFile(t *testing.T) {
703+
if runtime.GOOS == "windows" {
704+
t.Skipf("skipping on windows since the test requires sh")
705+
return
706+
}
707+
708+
// start with an existing ssh command configuration
709+
os.Setenv("GIT_SSH_COMMAND", "ssh -o StrictHostKeyChecking=no")
710+
defer os.Setenv("GIT_SSH_COMMAND", "")
711+
712+
cmd := exec.Command("/bin/sh", "-c", "echo $GIT_SSH_COMMAND")
713+
setupGitEnv(cmd, "")
714+
out, err := cmd.Output()
715+
if err != nil {
716+
t.Fatal(err)
717+
}
718+
719+
actual := strings.TrimSpace(string(out))
720+
if actual != "ssh -o StrictHostKeyChecking=no" {
721+
t.Fatalf("unexpected GIT_SSH_COMMAND: %q", actual)
722+
}
723+
}
724+
702725
func TestGitGetter_subdirectory_symlink(t *testing.T) {
703726
if !testHasGit {
704727
t.Skip("git not found, skipping")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy