]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blobdiff - vendor/github.com/hashicorp/go-getter/get_git.go
deps: github.com/hashicorp/terraform@sdk-v0.11-with-go-modules
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / go-getter / get_git.go
index 07281398322bd0e3e61e81a6ae2ee25332463a52..cb1d02947ab5bef3187171db74567e634fe71d8b 100644 (file)
@@ -11,6 +11,7 @@ import (
        "strings"
 
        urlhelper "github.com/hashicorp/go-getter/helper/url"
+       "github.com/hashicorp/go-safetemp"
        "github.com/hashicorp/go-version"
 )
 
@@ -105,13 +106,11 @@ func (g *GitGetter) Get(dst string, u *url.URL) error {
 // GetFile for Git doesn't support updating at this time. It will download
 // the file every time.
 func (g *GitGetter) GetFile(dst string, u *url.URL) error {
-       td, err := ioutil.TempDir("", "getter-git")
+       td, tdcloser, err := safetemp.Dir("", "getter")
        if err != nil {
                return err
        }
-       if err := os.RemoveAll(td); err != nil {
-               return err
-       }
+       defer tdcloser.Close()
 
        // Get the filename, and strip the filename from the URL so we can
        // just get the repository directly.
@@ -180,17 +179,34 @@ func (g *GitGetter) fetchSubmodules(dst, sshKeyFile string) error {
 // 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) {
-       var sshOpts []string
+       const gitSSHCommand = "GIT_SSH_COMMAND="
+       var sshCmd []string
+
+       // If we have an existing GIT_SSH_COMMAND, we need to append our options.
+       // We will also remove our old entry to make sure the behavior is the same
+       // with versions of Go < 1.9.
+       env := os.Environ()
+       for i, v := range env {
+               if strings.HasPrefix(v, gitSSHCommand) {
+                       sshCmd = []string{v}
+
+                       env[i], env[len(env)-1] = env[len(env)-1], env[i]
+                       env = env[:len(env)-1]
+                       break
+               }
+       }
+
+       if len(sshCmd) == 0 {
+               sshCmd = []string{gitSSHCommand + "ssh"}
+       }
 
        if sshKeyFile != "" {
                // We have an SSH key temp file configured, tell ssh about this.
-               sshOpts = append(sshOpts, "-i", sshKeyFile)
+               sshCmd = append(sshCmd, "-i", sshKeyFile)
        }
 
-       cmd.Env = append(os.Environ(),
-               // Set the ssh command to use for clones.
-               "GIT_SSH_COMMAND=ssh "+strings.Join(sshOpts, " "),
-       )
+       env = append(env, strings.Join(sshCmd, " "))
+       cmd.Env = env
 }
 
 // checkGitVersion is used to check the version of git installed on the system