|
9 | 9 | "time" |
10 | 10 |
|
11 | 11 | "github.com/jesseduffield/gocui" |
| 12 | + "github.com/jesseduffield/lazygit/pkg/commands/oscommands" |
12 | 13 | "github.com/jesseduffield/lazygit/pkg/utils" |
13 | 14 | "github.com/sasha-s/go-deadlock" |
14 | 15 | "github.com/sirupsen/logrus" |
@@ -165,6 +166,17 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p |
165 | 166 | // and the user is flicking through a bunch of items. |
166 | 167 | self.throttle = time.Since(startTime) < THROTTLE_TIME && timeToStart > COMMAND_START_THRESHOLD |
167 | 168 |
|
| 169 | + // Kill the still-running command. The only reason to do this is to save CPU usage |
| 170 | + // when flicking through several very long diffs when diff.algorithm = histogram is |
| 171 | + // being used, in which case multiple git processes continue to calculate expensive |
| 172 | + // diffs in the background even though they have been stopped already. |
| 173 | + // |
| 174 | + // Unfortunately this will do nothing on Windows, so Windows users will have to live |
| 175 | + // with the higher CPU usage. |
| 176 | + if err := oscommands.TerminateProcessGracefully(cmd); err != nil { |
| 177 | + self.Log.Errorf("error when trying to terminate cmd task: %v; Command: %v %v", err, cmd.Path, cmd.Args) |
| 178 | + } |
| 179 | + |
168 | 180 | // close the task's stdout pipe (or the pty if we're using one) to make the command terminate |
169 | 181 | onDone() |
170 | 182 | } |
@@ -291,11 +303,15 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p |
291 | 303 |
|
292 | 304 | refreshViewIfStale() |
293 | 305 |
|
294 | | - if err := cmd.Wait(); err != nil { |
295 | | - select { |
296 | | - case <-opts.Stop: |
297 | | - // it's fine if we've killed this program ourselves |
298 | | - default: |
| 306 | + select { |
| 307 | + case <-opts.Stop: |
| 308 | + // If we stopped the task, don't block waiting for it; this could cause a delay if |
| 309 | + // the process takes a while until it actually terminates. We still want to call |
| 310 | + // Wait to reclaim any resources, but do it on a background goroutine, and ignore |
| 311 | + // any errors. |
| 312 | + go func() { _ = cmd.Wait() }() |
| 313 | + default: |
| 314 | + if err := cmd.Wait(); err != nil { |
299 | 315 | self.Log.Errorf("Unexpected error when running cmd task: %v; Failed command: %v %v", err, cmd.Path, cmd.Args) |
300 | 316 | } |
301 | 317 | } |
|
0 commit comments