Skip to content

Commit

Permalink
don't pass GIT_TRACE to exec.Command calls in the git package
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Feb 3, 2016
1 parent 0640430 commit c4874df
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
27 changes: 25 additions & 2 deletions git/git.go
Expand Up @@ -3,6 +3,7 @@ package git

import (
"bufio"
"bytes"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
4 changes: 3 additions & 1 deletion git/git_nix.go
Expand Up @@ -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
}
10 changes: 9 additions & 1 deletion git/git_test.go
Expand Up @@ -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"))
Expand All @@ -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")
}
1 change: 1 addition & 0 deletions git/git_win.go
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions 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

0 comments on commit c4874df

Please sign in to comment.