Skip to content

Commit f8a48b6

Browse files
committed
Suppress output from background fetch
However, show it when there was an error. This is important for the case that a fork that you have as a remote was deleted, in which case the command log is the only way to get notified about that.
1 parent eb91a43 commit f8a48b6

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func TestSyncPush(t *testing.T) {
104104
if err == nil {
105105
assert.True(t, cmdObj.ShouldLog())
106106
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT)
107+
assert.False(t, cmdObj.ShouldSuppressOutputUnlessError())
107108
}
108109
s.test(cmdObj, err)
109110
})
@@ -124,6 +125,7 @@ func TestSyncFetch(t *testing.T) {
124125
test: func(cmdObj *oscommands.CmdObj) {
125126
assert.True(t, cmdObj.ShouldLog())
126127
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT)
128+
assert.False(t, cmdObj.ShouldSuppressOutputUnlessError())
127129
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--no-write-fetch-head"})
128130
},
129131
},
@@ -133,6 +135,7 @@ func TestSyncFetch(t *testing.T) {
133135
test: func(cmdObj *oscommands.CmdObj) {
134136
assert.True(t, cmdObj.ShouldLog())
135137
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT)
138+
assert.False(t, cmdObj.ShouldSuppressOutputUnlessError())
136139
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--all", "--no-write-fetch-head"})
137140
},
138141
},
@@ -162,6 +165,7 @@ func TestSyncFetchBackground(t *testing.T) {
162165
test: func(cmdObj *oscommands.CmdObj) {
163166
assert.False(t, cmdObj.ShouldLog())
164167
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.FAIL)
168+
assert.True(t, cmdObj.ShouldSuppressOutputUnlessError())
165169
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--no-write-fetch-head"})
166170
},
167171
},
@@ -171,6 +175,7 @@ func TestSyncFetchBackground(t *testing.T) {
171175
test: func(cmdObj *oscommands.CmdObj) {
172176
assert.False(t, cmdObj.ShouldLog())
173177
assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.FAIL)
178+
assert.True(t, cmdObj.ShouldSuppressOutputUnlessError())
174179
assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--all", "--no-write-fetch-head"})
175180
},
176181
},

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)