From c4874df619276c807772bae6e62d9cb9718a7077 Mon Sep 17 00:00:00 2001 From: risk danger olson Date: Wed, 3 Feb 2016 12:06:23 -0700 Subject: [PATCH] don't pass GIT_TRACE to exec.Command calls in the git package --- git/git.go | 27 +++++++++++++++++++++++++-- git/git_nix.go | 4 +++- git/git_test.go | 10 +++++++++- git/git_win.go | 1 + script/cibuild | 4 ++++ 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/git/git.go b/git/git.go index abe5c1ffc6..b8b5b1493f 100644 --- a/git/git.go +++ b/git/git.go @@ -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) +} diff --git a/git/git_nix.go b/git/git_nix.go index 7f1538d569..f660e31bc9 100644 --- a/git/git_nix.go +++ b/git/git_nix.go @@ -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 } diff --git a/git/git_test.go b/git/git_test.go index 0233fbeaa1..1e45094160 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -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") +} diff --git a/git/git_win.go b/git/git_win.go index 4c8f668fa6..5e87fc1570 100644 --- a/git/git_win.go +++ b/git/git_win.go @@ -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 } diff --git a/script/cibuild b/script/cibuild index 1f56f22cf2..1c0b3c8a37 100755 --- a/script/cibuild +++ b/script/cibuild @@ -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