Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't change GIT_SSH_COMMAND when there is no sshKeyFile #459

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions get_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ func findRemoteDefaultBranch(ctx context.Context, u *url.URL) string {
// setupGitEnv sets up the environment for the given command. This is used to
// pass configuration data to git and ssh and enables advanced cloning methods.
func setupGitEnv(cmd *exec.Cmd, sshKeyFile string) {
// If there's no sshKeyFile argument to deal with, we can skip this
// entirely.
if sshKeyFile == "" {
return
}
const gitSSHCommand = "GIT_SSH_COMMAND="
var sshCmd []string

Expand All @@ -323,14 +328,12 @@ func setupGitEnv(cmd *exec.Cmd, sshKeyFile string) {
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)
}
sshCmd = append(sshCmd, "-i", sshKeyFile)
env = append(env, strings.Join(sshCmd, " "))
// We have an SSH key temp file configured, tell ssh about this.
if runtime.GOOS == "windows" {
sshKeyFile = strings.Replace(sshKeyFile, `\`, `/`, -1)
}
sshCmd = append(sshCmd, "-i", sshKeyFile)
env = append(env, strings.Join(sshCmd, " "))

cmd.Env = env
}
Expand Down
23 changes: 23 additions & 0 deletions get_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,29 @@ func TestGitGetter_setupGitEnvWithExisting_sshKey(t *testing.T) {
}
}

func TestGitGetter_setupGitEnvWithNoKeyFile(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")
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" {
t.Fatalf("unexpected GIT_SSH_COMMAND: %q", actual)
}
}

func TestGitGetter_subdirectory_symlink(t *testing.T) {
if !testHasGit {
t.Skip("git not found, skipping")
Expand Down
Loading