don't pass GIT_TRACE to exec.Command calls in the git package

This commit is contained in:
risk danger olson 2016-02-03 12:06:23 -07:00
parent 064043040e
commit c4874df619
5 changed files with 42 additions and 4 deletions

@ -3,6 +3,7 @@ package git
import (
"bufio"
"bytes"
"errors"
"fmt"
"io/ioutil"
@ -455,10 +456,13 @@ func GetCommitSummary(commit string) (*CommitSummary, error) {
func GitAndRootDirs() (string, string, error) {
cmd := execCommand("git", "rev-parse", "--git-dir", "--show-toplevel")
out, err := cmd.CombinedOutput()
buf := &bytes.Buffer{}
cmd.Stderr = buf
out, err := cmd.Output()
output := string(out)
if err != nil {
return "", "", fmt.Errorf("Failed to call git rev-parse --git-dir --show-toplevel: %q", output)
return "", "", fmt.Errorf("Failed to call git rev-parse --git-dir --show-toplevel: %q", buf.String())
}
paths := strings.Split(output, "\n")
@ -617,3 +621,22 @@ func IsVersionAtLeast(actualVersion, desiredVersion string) bool {
return actual >= atleast
}
// An env for an exec.Command without GIT_TRACE
var env []string
var traceEnv = "GIT_TRACE="
func init() {
realEnv := os.Environ()
env = make([]string, 0, len(realEnv))
for _, kv := range realEnv {
if strings.HasPrefix(kv, traceEnv) {
continue
}
env = append(env, kv)
}
fmt.Println(env)
fmt.Println(realEnv)
}

@ -9,5 +9,7 @@ import (
// execCommand is a small platform specific wrapper around os/exec.Command
func execCommand(name string, arg ...string) *exec.Cmd {
return exec.Command(name, arg...)
cmd := exec.Command(name, arg...)
cmd.Env = env
return cmd
}

@ -260,7 +260,6 @@ func TestWorkTrees(t *testing.T) {
}
func TestVersionCompare(t *testing.T) {
assert.Equal(t, true, IsVersionAtLeast("2.6.0", "2.6.0"))
assert.Equal(t, true, IsVersionAtLeast("2.6.0", "2.6"))
assert.Equal(t, true, IsVersionAtLeast("2.6.0", "2"))
@ -272,3 +271,12 @@ func TestVersionCompare(t *testing.T) {
assert.Equal(t, false, IsVersionAtLeast("2.5.0", "2.5.1"))
assert.Equal(t, false, IsVersionAtLeast("2.5.2", "2.5.10"))
}
func TestGitAndRootDirs(t *testing.T) {
git, root, err := GitAndRootDirs()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, git, root+"/.git")
}

@ -12,5 +12,6 @@ import (
func execCommand(name string, arg ...string) *exec.Cmd {
cmd := exec.Command(name, arg...)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
cmd.Env = env
return cmd
}

@ -1,4 +1,8 @@
#!/usr/bin/env bash
set -e
script/test
# re-run test to ensure GIT_TRACE output doesn't leak into the git package
GIT_TRACE=1 script/test git
VERBOSE_LOGS=1 script/integration