Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions git/cmd/gitcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (r Runner) Command(ctx context.Context, dir string, args ...string) *exec.C
if r.basicAuth != nil {
panic("gitcmd: Command cannot be used with WithBasicAuth; use Run or Output so credentials can be cleaned up")
}
cmd := exec.CommandContext(ctx, "git", args...)
cmd := gitCommand(ctx, args...)
cmd.Dir = dir
cmd.Env, _ = r.commandEnv(ctx, dir)
return cmd
Expand All @@ -107,7 +107,7 @@ func (r Runner) Output(ctx context.Context, dir string, args ...string) ([]byte,

// Run runs git and returns stdout, stderr, and a *GitError on failure.
func (r Runner) Run(ctx context.Context, dir string, stdin io.Reader, args ...string) ([]byte, []byte, error) {
cmd := exec.CommandContext(ctx, "git", args...)
cmd := gitCommand(ctx, args...)
cmd.Dir = dir
var cleanup func()
cmd.Env, cleanup = r.commandEnv(ctx, dir)
Expand All @@ -131,6 +131,12 @@ func (r Runner) Run(ctx context.Context, dir string, stdin io.Reader, args ...st
return stdout.Bytes(), stderr.Bytes(), nil
}

func gitCommand(ctx context.Context, args ...string) *exec.Cmd {
cmd := exec.CommandContext(ctx, "git", args...)
prepareGitCommand(cmd)
return cmd
}

type basicAuth struct {
username string
password string
Expand Down Expand Up @@ -231,7 +237,7 @@ func readSafeDirectories(ctx context.Context, env []string, dir string) []string
for _, scope := range scopes {
// --includes is required for explicit-scope reads to honor include.path
// and includeIf directives the way git's default config sequence does.
cmd := exec.CommandContext(ctx, "git", "config", scope, "--includes", "-z", "--get-all", "safe.directory")
cmd := gitCommand(ctx, "config", scope, "--includes", "-z", "--get-all", "safe.directory")
cmd.Dir = dir
cmd.Env = env
out, err := cmd.Output()
Expand Down
7 changes: 7 additions & 0 deletions git/cmd/gitcmd_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !windows

package gitcmd

import "os/exec"

func prepareGitCommand(cmd *exec.Cmd) {}
18 changes: 18 additions & 0 deletions git/cmd/gitcmd_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build windows

package gitcmd

import (
"os/exec"
"syscall"

"golang.org/x/sys/windows"
)

func prepareGitCommand(cmd *exec.Cmd) {
if cmd.SysProcAttr == nil {
cmd.SysProcAttr = &syscall.SysProcAttr{}
}
// Console-less callers otherwise cause git.exe to allocate a visible window.
cmd.SysProcAttr.CreationFlags |= windows.CREATE_NO_WINDOW
}
21 changes: 21 additions & 0 deletions git/cmd/gitcmd_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build windows

package gitcmd

import (
"context"
"testing"

"golang.org/x/sys/windows"
)

func TestRunnerCommandHidesGitConsoleWindowOnWindows(t *testing.T) {
cmd := New().Command(context.Background(), "", "status")

if cmd.SysProcAttr == nil {
t.Fatal("git command SysProcAttr is nil")
}
if cmd.SysProcAttr.CreationFlags&windows.CREATE_NO_WINDOW == 0 {
t.Fatalf("git command creation flags = %#x, want CREATE_NO_WINDOW", cmd.SysProcAttr.CreationFlags)
}
}
Loading