Skip to content

Commit 58d2ffa

Browse files
authored
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.
2 parents 933573f + f8a48b6 commit 58d2ffa

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

pkg/commands/git_commands/diff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (self *DiffCommands) GetDiff(staged bool, additionalArgs ...string) (string
5555
Dir(self.repoPaths.worktreePath).
5656
Arg(additionalArgs...).
5757
ToArgv(),
58-
).RunWithOutput()
58+
).DontLog().RunWithOutput()
5959
}
6060

6161
type DiffToolCmdOptions struct {

pkg/commands/git_commands/sync.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func (self *SyncCommands) FetchBackgroundCmdObj() *oscommands.CmdObj {
7979

8080
cmdObj := self.cmd.New(cmdArgs)
8181
cmdObj.DontLog().FailOnCredentialRequest()
82+
cmdObj.SuppressOutputUnlessError()
8283
return cmdObj
8384
}
8485

pkg/commands/git_commands/sync_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,13 @@ func TestSyncPush(t *testing.T) {
100100
t.Run(s.testName, func(t *testing.T) {
101101
instance := buildSyncCommands(commonDeps{})
102102
task := gocui.NewFakeTask()
103-
s.test(instance.PushCmdObj(task, s.opts))
103+
cmdObj, err := instance.PushCmdObj(task, s.opts)
104+
if err == nil {
105+
assert.True(t, cmdObj.ShouldLog())
106+
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT)
107+
assert.False(t, cmdObj.ShouldSuppressOutputUnlessError())
108+
}
109+
s.test(cmdObj, err)
104110
})
105111
}
106112
}
@@ -119,6 +125,7 @@ func TestSyncFetch(t *testing.T) {
119125
test: func(cmdObj *oscommands.CmdObj) {
120126
assert.True(t, cmdObj.ShouldLog())
121127
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT)
128+
assert.False(t, cmdObj.ShouldSuppressOutputUnlessError())
122129
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--no-write-fetch-head"})
123130
},
124131
},
@@ -128,6 +135,7 @@ func TestSyncFetch(t *testing.T) {
128135
test: func(cmdObj *oscommands.CmdObj) {
129136
assert.True(t, cmdObj.ShouldLog())
130137
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT)
138+
assert.False(t, cmdObj.ShouldSuppressOutputUnlessError())
131139
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--all", "--no-write-fetch-head"})
132140
},
133141
},
@@ -157,6 +165,7 @@ func TestSyncFetchBackground(t *testing.T) {
157165
test: func(cmdObj *oscommands.CmdObj) {
158166
assert.False(t, cmdObj.ShouldLog())
159167
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.FAIL)
168+
assert.True(t, cmdObj.ShouldSuppressOutputUnlessError())
160169
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--no-write-fetch-head"})
161170
},
162171
},
@@ -166,6 +175,7 @@ func TestSyncFetchBackground(t *testing.T) {
166175
test: func(cmdObj *oscommands.CmdObj) {
167176
assert.False(t, cmdObj.ShouldLog())
168177
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.FAIL)
178+
assert.True(t, cmdObj.ShouldSuppressOutputUnlessError())
169179
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--all", "--no-write-fetch-head"})
170180
},
171181
},

pkg/commands/oscommands/cmd_obj.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ type CmdObj struct {
2222
// see StreamOutput()
2323
streamOutput bool
2424

25+
// see SuppressOutputUnlessError()
26+
suppressOutputUnlessError bool
27+
2528
// see UsePty()
2629
usePty bool
2730

@@ -123,6 +126,18 @@ func (self *CmdObj) StreamOutput() *CmdObj {
123126
return self
124127
}
125128

129+
// when you call this, the streamed output will be suppressed unless there is an error
130+
func (self *CmdObj) SuppressOutputUnlessError() *CmdObj {
131+
self.suppressOutputUnlessError = true
132+
133+
return self
134+
}
135+
136+
// returns true if SuppressOutputUnlessError() was called
137+
func (self *CmdObj) ShouldSuppressOutputUnlessError() bool {
138+
return self.suppressOutputUnlessError
139+
}
140+
126141
// returns true if StreamOutput() was called
127142
func (self *CmdObj) ShouldStreamOutput() bool {
128143
return self.streamOutput

pkg/commands/oscommands/cmd_obj_runner.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,13 @@ func (self *cmdObjRunner) runAndStreamAux(
227227
cmdObj *CmdObj,
228228
onRun func(*cmdHandler, io.Writer),
229229
) error {
230-
cmdWriter := self.guiIO.newCmdWriterFn()
230+
var cmdWriter io.Writer
231+
var combinedOutput bytes.Buffer
232+
if cmdObj.ShouldSuppressOutputUnlessError() {
233+
cmdWriter = &combinedOutput
234+
} else {
235+
cmdWriter = self.guiIO.newCmdWriterFn()
236+
}
231237

232238
if cmdObj.ShouldLog() {
233239
self.logCmdObj(cmdObj)
@@ -267,6 +273,10 @@ func (self *cmdObjRunner) runAndStreamAux(
267273
self.log.Infof("%s (%s)", cmdObj.ToString(), time.Since(t))
268274

269275
if err != nil {
276+
if cmdObj.suppressOutputUnlessError {
277+
_, _ = self.guiIO.newCmdWriterFn().Write(combinedOutput.Bytes())
278+
}
279+
270280
errStr := stderr.String()
271281
if errStr != "" {
272282
return errors.New(errStr)

0 commit comments

Comments
 (0)