Skip to content

Fix delay with flicking through files or commits when git diff is very slow #4803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions pkg/commands/oscommands/os.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package oscommands

import (
"fmt"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -329,15 +328,3 @@ func GetLazygitPath() string {
}
return `"` + filepath.ToSlash(ex) + `"`
}

func (c *OSCommand) UpdateWindowTitle() error {
if c.Platform.OS != "windows" {
return nil
}
path, getWdErr := os.Getwd()
if getWdErr != nil {
return getWdErr
}
argString := fmt.Sprint("title ", filepath.Base(path), " - Lazygit")
return c.Cmd.NewShell(argString, c.UserConfig().OS.ShellFunctionsFile).Run()
}
14 changes: 14 additions & 0 deletions pkg/commands/oscommands/os_default_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package oscommands

import (
"os"
"os/exec"
"runtime"
"strings"
"syscall"
)

func GetPlatform() *Platform {
Expand Down Expand Up @@ -34,3 +36,15 @@ func getUserShell() string {

return "bash"
}

func (c *OSCommand) UpdateWindowTitle() error {
return nil
}

func TerminateProcessGracefully(cmd *exec.Cmd) error {
if cmd.Process == nil {
return nil
}

return cmd.Process.Signal(syscall.SIGTERM)
}
21 changes: 21 additions & 0 deletions pkg/commands/oscommands/os_windows.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
package oscommands

import (
"fmt"
"os"
"os/exec"
"path/filepath"
)

func GetPlatform() *Platform {
return &Platform{
OS: "windows",
Shell: "cmd",
ShellArg: "/c",
}
}

func (c *OSCommand) UpdateWindowTitle() error {
path, getWdErr := os.Getwd()
if getWdErr != nil {
return getWdErr
}
argString := fmt.Sprint("title ", filepath.Base(path), " - Lazygit")
return c.Cmd.NewShell(argString, c.UserConfig().OS.ShellFunctionsFile).Run()
}

func TerminateProcessGracefully(cmd *exec.Cmd) error {
// Signals other than SIGKILL are not supported on Windows
return nil
}
26 changes: 21 additions & 5 deletions pkg/tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sasha-s/go-deadlock"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -165,6 +166,17 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p
// and the user is flicking through a bunch of items.
self.throttle = time.Since(startTime) < THROTTLE_TIME && timeToStart > COMMAND_START_THRESHOLD

// Kill the still-running command. The only reason to do this is to save CPU usage
// when flicking through several very long diffs when diff.algorithm = histogram is
// being used, in which case multiple git processes continue to calculate expensive
// diffs in the background even though they have been stopped already.
//
// Unfortunately this will do nothing on Windows, so Windows users will have to live
// with the higher CPU usage.
if err := oscommands.TerminateProcessGracefully(cmd); err != nil {
self.Log.Errorf("error when trying to terminate cmd task: %v; Command: %v %v", err, cmd.Path, cmd.Args)
}

// close the task's stdout pipe (or the pty if we're using one) to make the command terminate
onDone()
}
Expand Down Expand Up @@ -291,11 +303,15 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p

refreshViewIfStale()

if err := cmd.Wait(); err != nil {
select {
case <-opts.Stop:
// it's fine if we've killed this program ourselves
default:
select {
case <-opts.Stop:
// If we stopped the task, don't block waiting for it; this could cause a delay if
// the process takes a while until it actually terminates. We still want to call
// Wait to reclaim any resources, but do it on a background goroutine, and ignore
// any errors.
go func() { _ = cmd.Wait() }()
default:
if err := cmd.Wait(); err != nil {
self.Log.Errorf("Unexpected error when running cmd task: %v; Failed command: %v %v", err, cmd.Path, cmd.Args)
}
}
Expand Down
Loading