CleanGitEnv returns a clean environment for running `git` commands so that they won't be affected by the local environment.
()
| 115 | // CleanGitEnv returns a clean environment for running `git` commands |
| 116 | // so that they won't be affected by the local environment. |
| 117 | func CleanGitEnv() []string { |
| 118 | osEnv := os.Environ() |
| 119 | env := make([]string, 0, len(osEnv)+3) |
| 120 | for _, e := range osEnv { |
| 121 | i := strings.IndexByte(e, '=') |
| 122 | if i == -1 { |
| 123 | // This shouldn't happen, but if it does, |
| 124 | // ignore it. |
| 125 | continue |
| 126 | } |
| 127 | k := e[:i] |
| 128 | if localEnvVars[k] { |
| 129 | continue |
| 130 | } |
| 131 | env = append(env, e) |
| 132 | } |
| 133 | return append( |
| 134 | env, |
| 135 | fmt.Sprintf("HOME=%s", os.DevNull), |
| 136 | fmt.Sprintf("XDG_CONFIG_HOME=%s", os.DevNull), |
| 137 | "GIT_CONFIG_NOSYSTEM=1", |
| 138 | ) |
| 139 | } |
| 140 | |
| 141 | // GitCommand creates an `*exec.Cmd` for running `git` in `repo` with |
| 142 | // the specified arguments. |
no outgoing calls
no test coverage detected