Files
lazygit/pkg/gui/controllers/status_controller.go
Stefan HallerandClaude Opus 5.5 89f269af58 Rewrite the update check that u in the status panel starts
Checking for an update with `u` has several problems:

- It offers to download and install the new version for any build
  that has a version, including builds from Homebrew or a Linux
  distribution. Overwriting those makes the package manager report the
  wrong version.
- It only logs its errors, so the user never learns why nothing
  happens. This includes "You already have the latest version". For a
  build from a git checkout, the version is a commit hash; comparing it
  with the release version fails, and the user sees nothing at all.
- Installing always fails on Windows.

Rewrite it on top of the functions from the previous commits. Show the
result of the check in all cases. If there's an update, show a menu
that depends on how lazygit was installed. We only offer "Install now"
for our release archives, and only if we can write to the folder of
the executable. For package managers that we know, we show the command
that updates lazygit. Otherwise we tell the user where to get the new
version. Disable `u` for dev builds and say why.

Installing replaces the executable with a rename, so quitting during
an update can no longer leave a broken binary behind. Instead of
asking for confirmation, wait for the rename to finish before exiting.

Remove the check at startup for now. It never ran, because it checked
for the build source "buildBinary" while .goreleaser.yml sets
"binaryRelease". A later commit adds a new one.

Use os.Executable instead of the osext package; os.Executable didn't
exist yet when this code was written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 16:11:57 +01:00

217 lines
6.9 KiB
Go

package controllers
import (
"fmt"
"strings"
"time"
"github.com/jesseduffield/lazygit/pkg/constants"
"github.com/jesseduffield/lazygit/pkg/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
)
type StatusController struct {
baseController
c *ControllerCommon
}
var _ types.IController = &StatusController{}
func NewStatusController(
c *ControllerCommon,
) *StatusController {
return &StatusController{
baseController: baseController{},
c: c,
}
}
func (self *StatusController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Keys: opts.GetKeys(opts.Config.Universal.Edit),
Handler: self.editConfig,
Description: self.c.Tr.EditConfig,
Tooltip: self.c.Tr.EditFileTooltip,
DisplayOnScreen: true,
},
{
Keys: opts.GetKeys(opts.Config.Status.CheckForUpdate),
Handler: self.c.Helpers().Update.CheckForUpdateInForeground,
GetDisabledReason: self.c.Helpers().Update.CheckForUpdateDisabledReason,
Description: self.c.Tr.CheckForUpdate,
DisplayOnScreen: true,
},
{
Keys: opts.GetKeys(opts.Config.Status.RecentRepos),
Handler: self.c.Helpers().Repos.CreateRecentReposMenu,
Description: self.c.Tr.SwitchRepo,
DisplayOnScreen: true,
},
{
Keys: opts.GetKeys(opts.Config.Status.AllBranchesLogGraph),
Handler: func() error { self.switchToOrRotateAllBranchesLogs(); return nil },
Description: self.c.Tr.AllBranchesLogGraph,
},
{
Keys: opts.GetKeys(opts.Config.Status.AllBranchesLogGraphReverse),
Handler: func() error { self.switchToOrRotateAllBranchesLogsBackward(); return nil },
Description: self.c.Tr.AllBranchesLogGraphReverse,
},
}
return bindings
}
func (self *StatusController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
return []*gocui.ViewMouseBinding{
{
ViewName: self.Context().GetViewName(),
Key: gocui.MouseLeft,
Handler: self.onClick,
},
}
}
func (self *StatusController) GetOnRenderToMain() func() {
return func() {
switch self.c.UserConfig().Gui.StatusPanelView {
case "dashboard":
self.showDashboard()
case "allBranchesLog":
self.showAllBranchLogs()
default:
self.showDashboard()
}
}
}
func (self *StatusController) Context() types.Context {
return self.c.Contexts().Status
}
func (self *StatusController) onClick(opts gocui.ViewMouseBindingOpts) error {
// TODO: move into some abstraction (status is currently not a listViewContext where a lot of this code lives)
currentBranch := self.c.Helpers().Refs.GetCheckedOutRef()
if currentBranch == nil {
// need to wait for branches to refresh
return nil
}
self.c.Context().Push(self.Context(), types.OnFocusOpts{})
upstreamStatus := utils.Decolorise(presentation.BranchStatus(currentBranch, types.ItemOperationNone, self.c.Tr, time.Now(), self.c.UserConfig()))
repoName := self.c.Git().RepoPaths.RepoName()
workingTreeState := self.c.Git().Status.WorkingTreeState()
if workingTreeState.Any() {
workingTreeStatus := fmt.Sprintf("(%s)", workingTreeState.LowerCaseTitle(self.c.Tr))
if cursorInSubstring(opts.X, upstreamStatus+" ", workingTreeStatus) {
return self.c.Helpers().MergeAndRebase.CreateRebaseOptionsMenu()
}
if cursorInSubstring(opts.X, upstreamStatus+" "+workingTreeStatus+" ", repoName) {
return self.c.Helpers().Repos.CreateRecentReposMenu()
}
} else if cursorInSubstring(opts.X, upstreamStatus+" ", repoName) {
return self.c.Helpers().Repos.CreateRecentReposMenu()
}
return nil
}
func runeCount(str string) int {
return len([]rune(str))
}
func cursorInSubstring(cx int, prefix string, substring string) bool {
return cx >= runeCount(prefix) && cx < runeCount(prefix+substring)
}
func lazygitTitle() string {
return `
_ _ _
| | (_) |
| | __ _ _____ _ __ _ _| |_
| |/ _` + "`" + ` |_ / | | |/ _` + "`" + ` | | __|
| | (_| |/ /| |_| | (_| | | |_
|_|\__,_/___|\__, |\__, |_|\__|
__/ | __/ |
|___/ |___/ `
}
func (self *StatusController) editConfig() error {
return (&EditConfigAction{c: self.c}).Call()
}
func (self *StatusController) showAllBranchLogs() {
cmdObj := self.c.Git().Branch.AllBranchesLogCmdObj()
task := types.NewRunDiffRendererTask(cmdObj.GetCmd())
title := self.c.Tr.LogTitle
if i, n := self.c.Git().Branch.GetAllBranchesLogIdxAndCount(); n > 1 {
title = fmt.Sprintf(self.c.Tr.LogXOfYTitle, i+1, n)
}
self.c.RenderToMainViews(types.RefreshMainOpts{
Pair: self.c.MainViewPairs().Normal,
Main: &types.ViewUpdateOpts{
Title: title,
Task: task,
},
})
}
// Switches to the all branches view, or, if already on that view,
// rotates to the next command in the list, and then renders it.
func (self *StatusController) switchToOrRotateAllBranchesLogs() {
// A bit of a hack to ensure we only rotate to the next branch log command
// if we currently are looking at a branch log. Otherwise, we should just show
// the current index (if we are coming from the dashboard).
if self.c.Views().Main.Title != self.c.Tr.StatusTitle {
self.c.Git().Branch.RotateAllBranchesLogIdx()
}
self.showAllBranchLogs()
}
// Switches to the all branches view, or, if already on that view,
// rotates to the previous command in the list, and then renders it.
func (self *StatusController) switchToOrRotateAllBranchesLogsBackward() {
// A bit of a hack to ensure we only rotate to the previous branch log command
// if we currently are looking at a branch log. Otherwise, we should just show
// the current index (if we are coming from the dashboard).
if self.c.Views().Main.Title != self.c.Tr.StatusTitle {
self.c.Git().Branch.RotateAllBranchesLogIdxBackward()
}
self.showAllBranchLogs()
}
func (self *StatusController) showDashboard() {
versionStr := "master"
version, err := types.ParseVersionNumber(self.c.GetConfig().GetVersion())
if err == nil {
// Don't just take the version string as is, but format it again. This
// way it will be correct even if a distribution omits the "v", or the
// ".0" at the end.
versionStr = version.String()
}
dashboardString := strings.Join(
[]string{
lazygitTitle(),
fmt.Sprintf("Copyright %d Jesse Duffield", time.Now().Year()),
fmt.Sprintf("Keybindings: %s", fmt.Sprintf(constants.Links.Docs.Keybindings, versionStr)),
fmt.Sprintf("Config Options: %s", fmt.Sprintf(constants.Links.Docs.Config, versionStr)),
fmt.Sprintf("Tutorial: %s", constants.Links.Docs.Tutorial),
fmt.Sprintf("Raise an Issue: %s", constants.Links.Issues),
fmt.Sprintf("Release Notes: %s", constants.Links.Releases),
}, "\n\n") + "\n"
self.c.RenderToMainViews(types.RefreshMainOpts{
Pair: self.c.MainViewPairs().Normal,
Main: &types.ViewUpdateOpts{
Title: self.c.Tr.StatusTitle,
Task: types.NewRenderStringTask(dashboardString),
},
})
}