From e5acbed8488f7e633b2fe1b95f44b9edbd4e94fc Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 8 Aug 2025 20:12:05 +0200 Subject: [PATCH 1/3] Cleanup: move UpdateWindowTitle to platform-specific source files No need to do a runtime check if we already have the platform-specific files. --- pkg/commands/oscommands/os.go | 13 ------------- pkg/commands/oscommands/os_default_platform.go | 4 ++++ pkg/commands/oscommands/os_windows.go | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index b14738e38c0..f070e485627 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -1,7 +1,6 @@ package oscommands import ( - "fmt" "io" "os" "os/exec" @@ -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() -} diff --git a/pkg/commands/oscommands/os_default_platform.go b/pkg/commands/oscommands/os_default_platform.go index 11e1dc9aac4..5ea872c8c35 100644 --- a/pkg/commands/oscommands/os_default_platform.go +++ b/pkg/commands/oscommands/os_default_platform.go @@ -34,3 +34,7 @@ func getUserShell() string { return "bash" } + +func (c *OSCommand) UpdateWindowTitle() error { + return nil +} diff --git a/pkg/commands/oscommands/os_windows.go b/pkg/commands/oscommands/os_windows.go index 783f50518a1..0c69ca68506 100644 --- a/pkg/commands/oscommands/os_windows.go +++ b/pkg/commands/oscommands/os_windows.go @@ -1,5 +1,11 @@ package oscommands +import ( + "fmt" + "os" + "path/filepath" +) + func GetPlatform() *Platform { return &Platform{ OS: "windows", @@ -7,3 +13,12 @@ func GetPlatform() *Platform { 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() +} From 898f5651163d8ff9a28889003450872a1b27f29f Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 9 Aug 2025 11:45:52 +0200 Subject: [PATCH 2/3] Fix delay with flicking through files or commits when git diff is very slow One reason why git diff can be very slow is when "diff.algorithm = histogram" is being used. In this case, showing a very long single-file diff can take seconds to load, and you'll see the "loading..." message in the main view until we got the first lines of the diff to show. There's nothing really we can do about this delay; however, when switching to another, shorter file (or commit) while the "loading..." message is still showing, this switch should be instantaneous. And it was before 0.54.0, but we broke this in 0.54.0 with 8d7740a5acab (#4782); now users have to wait for the slow git diff command to output more text before the switch occurs. To fix this, don't block waiting for the process to terminate if we just stopped it. --- pkg/tasks/tasks.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/tasks/tasks.go b/pkg/tasks/tasks.go index 5401b171430..27fcf817bb9 100644 --- a/pkg/tasks/tasks.go +++ b/pkg/tasks/tasks.go @@ -291,11 +291,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) } } From e056e33da5c2a4db1f77cfbf01eac1775dfa13f0 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 8 Aug 2025 19:55:16 +0200 Subject: [PATCH 3/3] Improve CPU usage when flicking through large diffs The previous commit already fixed the user-visible lag, but there's still a problem with multiple background git processes consuming resources calculating diffs that we are never going to show. Improve this by terminating those processes (by sending them a TERM signal). Unfortunately this is only possible on Linux and Mac, so Windows users will have to live with the higher CPU usage. The recommended workaround is to not use "diff.algorithm = histogram". --- pkg/commands/oscommands/os_default_platform.go | 10 ++++++++++ pkg/commands/oscommands/os_windows.go | 6 ++++++ pkg/tasks/tasks.go | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/pkg/commands/oscommands/os_default_platform.go b/pkg/commands/oscommands/os_default_platform.go index 5ea872c8c35..3d7eae48177 100644 --- a/pkg/commands/oscommands/os_default_platform.go +++ b/pkg/commands/oscommands/os_default_platform.go @@ -5,8 +5,10 @@ package oscommands import ( "os" + "os/exec" "runtime" "strings" + "syscall" ) func GetPlatform() *Platform { @@ -38,3 +40,11 @@ func getUserShell() string { 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) +} diff --git a/pkg/commands/oscommands/os_windows.go b/pkg/commands/oscommands/os_windows.go index 0c69ca68506..605ed768275 100644 --- a/pkg/commands/oscommands/os_windows.go +++ b/pkg/commands/oscommands/os_windows.go @@ -3,6 +3,7 @@ package oscommands import ( "fmt" "os" + "os/exec" "path/filepath" ) @@ -22,3 +23,8 @@ func (c *OSCommand) UpdateWindowTitle() error { 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 +} diff --git a/pkg/tasks/tasks.go b/pkg/tasks/tasks.go index 27fcf817bb9..5c5875fa879 100644 --- a/pkg/tasks/tasks.go +++ b/pkg/tasks/tasks.go @@ -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" @@ -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() }