1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-11-23 22:24:51 +02:00

Suppress output from background fetch (unless there were errors) (#5044)

The output from background fetching is noisy and pollutes the command
log. Don't show it by default, unless there were errors, in which case
it is important to see e.g. which fork was deleted.
This commit is contained in:
Stefan Haller
2025-11-15 18:32:43 +01:00
committed by GitHub
5 changed files with 39 additions and 3 deletions

View File

@@ -55,7 +55,7 @@ func (self *DiffCommands) GetDiff(staged bool, additionalArgs ...string) (string
Dir(self.repoPaths.worktreePath). Dir(self.repoPaths.worktreePath).
Arg(additionalArgs...). Arg(additionalArgs...).
ToArgv(), ToArgv(),
).RunWithOutput() ).DontLog().RunWithOutput()
} }
type DiffToolCmdOptions struct { type DiffToolCmdOptions struct {

View File

@@ -79,6 +79,7 @@ func (self *SyncCommands) FetchBackgroundCmdObj() *oscommands.CmdObj {
cmdObj := self.cmd.New(cmdArgs) cmdObj := self.cmd.New(cmdArgs)
cmdObj.DontLog().FailOnCredentialRequest() cmdObj.DontLog().FailOnCredentialRequest()
cmdObj.SuppressOutputUnlessError()
return cmdObj return cmdObj
} }

View File

@@ -100,7 +100,13 @@ func TestSyncPush(t *testing.T) {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
instance := buildSyncCommands(commonDeps{}) instance := buildSyncCommands(commonDeps{})
task := gocui.NewFakeTask() task := gocui.NewFakeTask()
s.test(instance.PushCmdObj(task, s.opts)) cmdObj, err := instance.PushCmdObj(task, s.opts)
if err == nil {
assert.True(t, cmdObj.ShouldLog())
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT)
assert.False(t, cmdObj.ShouldSuppressOutputUnlessError())
}
s.test(cmdObj, err)
}) })
} }
} }
@@ -119,6 +125,7 @@ func TestSyncFetch(t *testing.T) {
test: func(cmdObj *oscommands.CmdObj) { test: func(cmdObj *oscommands.CmdObj) {
assert.True(t, cmdObj.ShouldLog()) assert.True(t, cmdObj.ShouldLog())
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT) assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT)
assert.False(t, cmdObj.ShouldSuppressOutputUnlessError())
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--no-write-fetch-head"}) assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--no-write-fetch-head"})
}, },
}, },
@@ -128,6 +135,7 @@ func TestSyncFetch(t *testing.T) {
test: func(cmdObj *oscommands.CmdObj) { test: func(cmdObj *oscommands.CmdObj) {
assert.True(t, cmdObj.ShouldLog()) assert.True(t, cmdObj.ShouldLog())
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT) assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT)
assert.False(t, cmdObj.ShouldSuppressOutputUnlessError())
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--all", "--no-write-fetch-head"}) assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--all", "--no-write-fetch-head"})
}, },
}, },
@@ -157,6 +165,7 @@ func TestSyncFetchBackground(t *testing.T) {
test: func(cmdObj *oscommands.CmdObj) { test: func(cmdObj *oscommands.CmdObj) {
assert.False(t, cmdObj.ShouldLog()) assert.False(t, cmdObj.ShouldLog())
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.FAIL) assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.FAIL)
assert.True(t, cmdObj.ShouldSuppressOutputUnlessError())
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--no-write-fetch-head"}) assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--no-write-fetch-head"})
}, },
}, },
@@ -166,6 +175,7 @@ func TestSyncFetchBackground(t *testing.T) {
test: func(cmdObj *oscommands.CmdObj) { test: func(cmdObj *oscommands.CmdObj) {
assert.False(t, cmdObj.ShouldLog()) assert.False(t, cmdObj.ShouldLog())
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.FAIL) assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.FAIL)
assert.True(t, cmdObj.ShouldSuppressOutputUnlessError())
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--all", "--no-write-fetch-head"}) assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--all", "--no-write-fetch-head"})
}, },
}, },

View File

@@ -22,6 +22,9 @@ type CmdObj struct {
// see StreamOutput() // see StreamOutput()
streamOutput bool streamOutput bool
// see SuppressOutputUnlessError()
suppressOutputUnlessError bool
// see UsePty() // see UsePty()
usePty bool usePty bool
@@ -123,6 +126,18 @@ func (self *CmdObj) StreamOutput() *CmdObj {
return self return self
} }
// when you call this, the streamed output will be suppressed unless there is an error
func (self *CmdObj) SuppressOutputUnlessError() *CmdObj {
self.suppressOutputUnlessError = true
return self
}
// returns true if SuppressOutputUnlessError() was called
func (self *CmdObj) ShouldSuppressOutputUnlessError() bool {
return self.suppressOutputUnlessError
}
// returns true if StreamOutput() was called // returns true if StreamOutput() was called
func (self *CmdObj) ShouldStreamOutput() bool { func (self *CmdObj) ShouldStreamOutput() bool {
return self.streamOutput return self.streamOutput

View File

@@ -227,7 +227,13 @@ func (self *cmdObjRunner) runAndStreamAux(
cmdObj *CmdObj, cmdObj *CmdObj,
onRun func(*cmdHandler, io.Writer), onRun func(*cmdHandler, io.Writer),
) error { ) error {
cmdWriter := self.guiIO.newCmdWriterFn() var cmdWriter io.Writer
var combinedOutput bytes.Buffer
if cmdObj.ShouldSuppressOutputUnlessError() {
cmdWriter = &combinedOutput
} else {
cmdWriter = self.guiIO.newCmdWriterFn()
}
if cmdObj.ShouldLog() { if cmdObj.ShouldLog() {
self.logCmdObj(cmdObj) self.logCmdObj(cmdObj)
@@ -267,6 +273,10 @@ func (self *cmdObjRunner) runAndStreamAux(
self.log.Infof("%s (%s)", cmdObj.ToString(), time.Since(t)) self.log.Infof("%s (%s)", cmdObj.ToString(), time.Since(t))
if err != nil { if err != nil {
if cmdObj.suppressOutputUnlessError {
_, _ = self.guiIO.newCmdWriterFn().Write(combinedOutput.Bytes())
}
errStr := stderr.String() errStr := stderr.String()
if errStr != "" { if errStr != "" {
return errors.New(errStr) return errors.New(errStr)