Seeing some odd behaviour from os/exec.Command

125 views
Skip to first unread message

Ajith Ramanathan

unread,
Nov 21, 2025, 3:04:14 PM (14 days ago) Nov 21
to golang-nuts
Hi.

I'm trying to execute 

git -c gc.reflogexpire=0 -c gc.reflogexpireunreachable=0 -c gc.rerereresolved=0 -c gc.rerereunresolved=0 -c gc.prunreexpire=now gc

from inside git.  When I run this in a terminal I get output like:

Enumerating objects: 539, done.
Counting objects: 100% (539/539), done.
Delta compression using up to 4 threads
Compressing objects: 100% (235/235), done.
Writing objects: 100% (539/539), done.
Total 539 (delta 298), reused 539 (delta 298), pack-reused 0 (from 0)
Enumerating cruft objects: 491, done.
Traversing cruft objects: 597, done.
Counting objects: 100% (501/501), done.
Delta compression using up to 4 threads
Compressing objects: 100% (216/216), done.
Writing objects: 100% (501/501), done.
Total 501 (delta 284), reused 501 (delta 284), pack-reused 0 (from 0)

However, when I run it using os/exec.Command I get nothing on stderr or stdout.  Other git commands do output to stderr and stdout, so I'm a little mystified.  Here is a test program demonstrating this:

package main

import (
"fmt"
"io"
"os/exec"
)

func run(cmd string, args ...string) {
runner := exec.Command(cmd, args...)

outp, _ := runner.StdoutPipe()
errp, _ := runner.StderrPipe()

if err := runner.Start(); err != nil {
panic(err)
}

outs, _ := io.ReadAll(outp)
errs, _ := io.ReadAll(errp)

err := runner.Wait()

fmt.Printf("cmd: %q\n", runner.String())
fmt.Printf("stdout: %q\n", outs)
fmt.Printf("stderr: %q\n", errs)
fmt.Printf("err: %v\n", err)
}

func main() {
run("git", "--version")
fmt.Println("-----------------------------------------------------------------------------")
run(
"git",
"-c", "gc.reflogexpire=0",
"-c", "gc.reflogexpireunreachable=0",
"-c", "gc.rerereresolved=0",
"-c", "gc.rerereunresolved=0",
"-c", "gc.prunreexpire=now",
"gc",
)
fmt.Println("-----------------------------------------------------------------------------")
run("git", "skidoosh")
}


and here is its output:

cmd: "/usr/bin/git --version"
stdout: "git version 2.51.2\n"
stderr: ""
err: <nil>
-----------------------------------------------------------------------------
cmd: "/usr/bin/git -c gc.reflogexpire=0 -c gc.reflogexpireunreachable=0 -c gc.rerereresolved=0 -c gc.rerereunresolved=0 -c gc.prunreexpire=now gc"
stdout: ""
stderr: ""
err: <nil>
-----------------------------------------------------------------------------
cmd: "/usr/bin/git skidoosh"
stdout: ""
stderr: "git: 'skidoosh' is not a git command. See 'git --help'.\n"
err: exit status 1

I feel like I'm missing something obvious, but it isn't clear what that might me.

Ian Lance Taylor

unread,
Nov 21, 2025, 3:21:11 PM (14 days ago) Nov 21
to Ajith Ramanathan, golang-nuts
"git gc" changes its output based on whether it is connected to a tty.

You'll see the same effect if you run "git gc >/tmp/stdout 2>/tmp/stderr".

I don't know why "git gc" behaves that way.

Ian

Dan Kortschak

unread,
Nov 21, 2025, 3:26:11 PM (14 days ago) Nov 21
to golan...@googlegroups.com
On Fri, 2025-11-21 at 09:08 -0800, Ajith Ramanathan wrote:
> However, when I run it using os/exec.Command I get nothing on stderr
> or stdout.

Do you know that the command is not checking whether it's running on a
tty?

Kurtis Rader

unread,
Nov 21, 2025, 3:29:50 PM (14 days ago) Nov 21
to Ajith Ramanathan, golang-nuts
That behavior typically means the program is predicating the output on stdout/stderr being attached to a tty (terminal). You can confirm that hypothesis by running

git ... </dev/null >stdout 2>stderr

Then look at the content of stdout and stderr to see if the expected messages are present. If they aren't then that confirms the program only outputs that information if connected to a tty.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/5e861a7a-141a-4399-85c1-6cda8a88a344n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Ajith Ramanathan

unread,
Nov 21, 2025, 6:49:50 PM (14 days ago) Nov 21
to Dan Kortschak, golan...@googlegroups.com
Thanks for the quick replies.  


I should have realized that: I can only plead a fart-of-brain.   

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages