Skip to content

Commit

Permalink
more centralised handling of refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Mar 28, 2020
1 parent 39315ca commit 198d237
Show file tree
Hide file tree
Showing 32 changed files with 189 additions and 126 deletions.
1 change: 1 addition & 0 deletions AgeMQemvPk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YrWrygkDZr
1 change: 1 addition & 0 deletions CqSiBeejXz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnzuDhnmXj
1 change: 1 addition & 0 deletions EzGTvaqMIv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FwtesbRmIy
1 change: 1 addition & 0 deletions FcKgiBNeUX
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yZXPONHgYA
1 change: 1 addition & 0 deletions KtDIVguUOP
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zTceFimWln
1 change: 1 addition & 0 deletions TGpKLocWft
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wYOKNLUbIn
1 change: 1 addition & 0 deletions UkUKNwQtgP
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KJkGhkBJvv
1 change: 1 addition & 0 deletions kTrVvqUAxG
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LdOMEqAOcq
9 changes: 1 addition & 8 deletions pkg/commands/branch_list_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"regexp"
"strconv"
"strings"

"github.com/jesseduffield/lazygit/pkg/utils"
Expand Down Expand Up @@ -145,13 +144,7 @@ func (b *BranchListBuilder) obtainReflogBranches() []*Branch {
reflogBranches := make([]*Branch, 0, len(b.ReflogCommits))
for _, commit := range b.ReflogCommits {
if match := re.FindStringSubmatch(commit.Name); len(match) == 3 {
timestamp, err := strconv.Atoi(commit.Date)
if err != nil {
b.Log.Errorf("couldn't parse reflog date: %s", commit.Date)
continue
}

recency := utils.UnixToTimeAgo(timestamp)
recency := utils.UnixToTimeAgo(commit.UnixTimestamp)
for _, branchName := range match[1:] {
if !foundBranchesMap[branchName] {
foundBranchesMap[branchName] = true
Expand Down
16 changes: 8 additions & 8 deletions pkg/commands/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package commands

// Commit : A git commit
type Commit struct {
Sha string
Name string
Status string // one of "unpushed", "pushed", "merged", "rebasing" or "selected"
Action string // one of "", "pick", "edit", "squash", "reword", "drop", "fixup"
Tags []string
ExtraInfo string // something like 'HEAD -> master, tag: v0.15.2'
Author string
Date string
Sha string
Name string
Status string // one of "unpushed", "pushed", "merged", "rebasing" or "selected"
Action string // one of "", "pick", "edit", "squash", "reword", "drop", "fixup"
Tags []string
ExtraInfo string // something like 'HEAD -> master, tag: v0.15.2'
Author string
UnixTimestamp int64
}

func (c *Commit) ShortSha() string {
Expand Down
19 changes: 11 additions & 8 deletions pkg/commands/commit_list_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/fatih/color"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (c *CommitListBuilder) extractCommitFromLine(line string) *Commit {
split := strings.Split(line, SEPARATION_CHAR)

sha := split[0]
date := split[1]
unixTimestamp := split[1]
author := split[2]
extraInfo := strings.TrimSpace(split[3])
message := strings.Join(split[4:], SEPARATION_CHAR)
Expand All @@ -70,13 +71,15 @@ func (c *CommitListBuilder) extractCommitFromLine(line string) *Commit {
}
}

unitTimestampInt, _ := strconv.Atoi(unixTimestamp)

return &Commit{
Sha: sha,
Name: message,
Tags: tags,
ExtraInfo: extraInfo,
Date: date,
Author: author,
Sha: sha,
Name: message,
Tags: tags,
ExtraInfo: extraInfo,
UnixTimestamp: int64(unitTimestampInt),
Author: author,
}
}

Expand Down Expand Up @@ -305,5 +308,5 @@ func (c *CommitListBuilder) getLogCmd(limit bool) *exec.Cmd {
limitFlag = "-300"
}

return c.OSCommand.ExecutableFromString(fmt.Sprintf("git log --oneline --pretty=format:\"%%H%s%%ar%s%%aN%s%%d%s%%s\" %s --abbrev=%d", SEPARATION_CHAR, SEPARATION_CHAR, SEPARATION_CHAR, SEPARATION_CHAR, limitFlag, 20))
return c.OSCommand.ExecutableFromString(fmt.Sprintf("git log --oneline --pretty=format:\"%%H%s%%at%s%%aN%s%%d%s%%s\" %s --abbrev=%d --date=unix ", SEPARATION_CHAR, SEPARATION_CHAR, SEPARATION_CHAR, SEPARATION_CHAR, limitFlag, 20))
}
18 changes: 8 additions & 10 deletions pkg/commands/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,11 +484,7 @@ func (c *GitCommand) GitStatus() (string, error) {

// IsInMergeState states whether we are still mid-merge
func (c *GitCommand) IsInMergeState() (bool, error) {
output, err := c.OSCommand.RunCommandWithOutput("git status --untracked-files=all")
if err != nil {
return false, err
}
return strings.Contains(output, "conclude merge") || strings.Contains(output, "unmerged paths"), nil
return c.OSCommand.FileExists(fmt.Sprintf("%s/MERGE_HEAD", c.DotGitDir))
}

// RebaseMode returns "" for non-rebase mode, "normal" for normal rebase
Expand Down Expand Up @@ -1133,14 +1129,16 @@ func (c *GitCommand) GetNewReflogCommits(lastReflogCommit *Commit) ([]*Commit, e
return false, nil
}

unixTimestamp, _ := strconv.Atoi(match[2])

commit := &Commit{
Sha: match[1],
Name: match[3],
Date: match[2],
Status: "reflog",
Sha: match[1],
Name: match[3],
UnixTimestamp: int64(unixTimestamp),
Status: "reflog",
}

if lastReflogCommit != nil && commit.Sha == lastReflogCommit.Sha && commit.Date == lastReflogCommit.Date {
if lastReflogCommit != nil && commit.Sha == lastReflogCommit.Sha && commit.UnixTimestamp == lastReflogCommit.UnixTimestamp {
// after this point we already have these reflogs loaded so we'll simply return the new ones
return true, nil
}
Expand Down
20 changes: 8 additions & 12 deletions pkg/gui/branches_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (gui *Gui) handleForceCheckout(g *gocui.Gui, v *gocui.View) error {
if err := gui.GitCommand.Checkout(branch.Name, commands.CheckoutOptions{Force: true}); err != nil {
_ = gui.createErrorPanel(g, err.Error())
}
return gui.refreshSidePanels(g)
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
}, nil)
}

Expand Down Expand Up @@ -167,25 +167,23 @@ func (gui *Gui) handleCheckoutRef(ref string, options handleCheckoutRefOptions)
}

onSuccess()

if err := gui.GitCommand.StashDo(0, "pop"); err != nil {
if err := gui.refreshSidePanels(g); err != nil {
if err := gui.refreshSidePanels(refreshOptions{mode: BLOCK_UI}); err != nil {
return err
}
return gui.createErrorPanel(g, err.Error())
}
return gui.refreshSidePanels(g)
return gui.refreshSidePanels(refreshOptions{mode: BLOCK_UI})
}, nil)
}

if err := gui.createErrorPanel(gui.g, err.Error()); err != nil {
return err
}
}

onSuccess()

return gui.refreshSidePanels(gui.g)
return gui.refreshSidePanels(refreshOptions{mode: BLOCK_UI})
})
}

Expand Down Expand Up @@ -218,10 +216,8 @@ func (gui *Gui) handleNewBranch(g *gocui.Gui, v *gocui.View) error {
if err := gui.GitCommand.NewBranch(gui.trimmedContent(v), branch.Name); err != nil {
return gui.createErrorPanel(g, err.Error())
}
if err := gui.refreshSidePanels(g); err != nil {
return gui.createErrorPanel(g, err.Error())
}
return gui.handleBranchSelect(g, v)
gui.State.Panels.Branches.SelectedLine = 0
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
})
}

Expand Down Expand Up @@ -263,7 +259,7 @@ func (gui *Gui) deleteNamedBranch(g *gocui.Gui, v *gocui.View, selectedBranch *c
}
return gui.createErrorPanel(g, errMessage)
}
return gui.refreshSidePanels(g)
return gui.refreshSidePanels(refreshOptions{mode: ASYNC, scope: []int{BRANCHES}})
}, nil)
}

Expand Down Expand Up @@ -357,7 +353,7 @@ func (gui *Gui) handleFastForward(g *gocui.Gui, v *gocui.View) error {
if err := gui.GitCommand.PullWithoutPasswordCheck("--ff-only"); err != nil {
_ = gui.createErrorPanel(gui.g, err.Error())
}
_ = gui.refreshSidePanels(gui.g)
_ = gui.refreshSidePanels(refreshOptions{mode: ASYNC})
} else {
if err := gui.GitCommand.FastForward(branch.Name, remoteName, remoteBranchName); err != nil {
_ = gui.createErrorPanel(gui.g, err.Error())
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/commit_files_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (gui *Gui) handleDiscardOldFileChange(g *gocui.Gui, v *gocui.View) error {
}
}

return gui.refreshSidePanels(gui.g)
return gui.refreshSidePanels(refreshOptions{mode: BLOCK_UI})
})
}, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/commit_message_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (gui *Gui) handleCommitConfirm(g *gocui.Gui, v *gocui.View) error {
_ = v.SetOrigin(0, 0)
_, _ = g.SetViewOnBottom("commitMessage")
_ = gui.switchFocus(g, v, gui.getFilesView())
return gui.refreshSidePanels(g)
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
}

func (gui *Gui) handleCommitClose(g *gocui.Gui, v *gocui.View) error {
Expand Down
13 changes: 4 additions & 9 deletions pkg/gui/commits_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ func (gui *Gui) handleCommitSelect(g *gocui.Gui, v *gocui.View) error {
}

func (gui *Gui) refreshCommits(g *gocui.Gui) error {
if err := gui.updateWorkTreeState(); err != nil {
return gui.createErrorPanel(gui.g, err.Error())
}

wg := sync.WaitGroup{}
wg.Add(2)

Expand All @@ -87,17 +83,16 @@ func (gui *Gui) refreshCommits(g *gocui.Gui) error {

go func() {
gui.refreshCommitsWithLimit()
if g.CurrentView() == gui.getCommitFilesView() || (g.CurrentView() == gui.getMainView() || gui.State.MainContext == "patch-building") {
gui.refreshCommitFilesView()
}
wg.Done()
}()

wg.Wait()

gui.refreshStatus()

if g.CurrentView() == gui.getCommitFilesView() || (g.CurrentView() == gui.getMainView() || gui.State.MainContext == "patch-building") {
return gui.refreshCommitFilesView()
}

return nil
}

Expand Down Expand Up @@ -497,7 +492,7 @@ func (gui *Gui) handleCreateFixupCommit(g *gocui.Gui, v *gocui.View) error {
return gui.createErrorPanel(g, err.Error())
}

return gui.refreshSidePanels(gui.g)
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
}, nil)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/credentials_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ func (gui *Gui) HandleCredentialsPopup(g *gocui.Gui, popupOpened bool, cmdErr er
_ = gui.createSpecificErrorPanel(errMessage, gui.getFilesView(), false)
} else {
_ = gui.closeConfirmationPrompt(g, true)
_ = gui.refreshSidePanels(g)
_ = gui.refreshSidePanels(refreshOptions{mode: ASYNC})
}
}
10 changes: 5 additions & 5 deletions pkg/gui/files_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (gui *Gui) handleWIPCommitPress(g *gocui.Gui, filesView *gocui.View) error
}

func (gui *Gui) handleCommitPress(g *gocui.Gui, filesView *gocui.View) error {
if len(gui.stagedFiles()) == 0 && gui.State.WorkingTreeState == "normal" {
if len(gui.stagedFiles()) == 0 && gui.workingTreeState() == "normal" {
return gui.createErrorPanel(g, gui.Tr.SLocalize("NoStagedFilesToCommit"))
}
commitMessageView := gui.getCommitMessageView()
Expand All @@ -298,7 +298,7 @@ func (gui *Gui) handleCommitPress(g *gocui.Gui, filesView *gocui.View) error {
}

func (gui *Gui) handleAmendCommitPress(g *gocui.Gui, filesView *gocui.View) error {
if len(gui.stagedFiles()) == 0 && gui.State.WorkingTreeState == "normal" {
if len(gui.stagedFiles()) == 0 && gui.workingTreeState() == "normal" {
return gui.createErrorPanel(g, gui.Tr.SLocalize("NoStagedFilesToCommit"))
}
if len(gui.State.Commits) == 0 {
Expand All @@ -317,14 +317,14 @@ func (gui *Gui) handleAmendCommitPress(g *gocui.Gui, filesView *gocui.View) erro
return nil
}

return gui.refreshSidePanels(g)
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
}, nil)
}

// handleCommitEditorPress - handle when the user wants to commit changes via
// their editor rather than via the popup panel
func (gui *Gui) handleCommitEditorPress(g *gocui.Gui, filesView *gocui.View) error {
if len(gui.stagedFiles()) == 0 && gui.State.WorkingTreeState == "normal" {
if len(gui.stagedFiles()) == 0 && gui.workingTreeState() == "normal" {
return gui.createErrorPanel(g, gui.Tr.SLocalize("NoStagedFilesToCommit"))
}
gui.PrepareSubProcess(g, "git", "commit")
Expand Down Expand Up @@ -375,7 +375,7 @@ func (gui *Gui) refreshStateFiles() error {
}

gui.refreshSelectedLine(&gui.State.Panels.Files.SelectedLine, len(gui.State.Files))
return gui.updateWorkTreeState()
return nil
}

func (gui *Gui) catSelectedFile(g *gocui.Gui) (string, error) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ type guiState struct {
Platform commands.Platform
Updating bool
Panels *panelStates
WorkingTreeState string // one of "merging", "rebasing", "normal"
MainContext string // used to keep the main and secondary views' contexts in sync
CherryPickedCommits []*commands.Commit
SplitMainPanel bool
Expand Down Expand Up @@ -351,7 +350,7 @@ func (gui *Gui) scrollDownConfirmationPanel(g *gocui.Gui, v *gocui.View) error {
}

func (gui *Gui) handleRefresh(g *gocui.Gui, v *gocui.View) error {
return gui.refreshSidePanels(g)
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
}

func max(a, b int) int {
Expand Down Expand Up @@ -863,7 +862,7 @@ func (gui *Gui) loadNewRepo() error {
}
gui.waitForIntro.Done()

if err := gui.refreshSidePanels(gui.g); err != nil {
if err := gui.refreshSidePanels(refreshOptions{mode: ASYNC}); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/merge_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (gui *Gui) handleCompleteMerge() error {
}
// if we got conflicts after unstashing, we don't want to call any git
// commands to continue rebasing/merging here
if gui.State.WorkingTreeState == "normal" {
if gui.workingTreeState() == "normal" {
return gui.handleEscapeMerge(gui.g, gui.getMainView())
}
// if there are no more files with merge conflicts, we should ask whether the user wants to continue
Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/patch_options_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (gui *Gui) getPatchCommitIndex() int {
}

func (gui *Gui) validateNormalWorkingTreeState() (bool, error) {
if gui.State.WorkingTreeState != "normal" {
if gui.workingTreeState() != "normal" {
return false, gui.createErrorPanel(gui.g, gui.Tr.SLocalize("CantPatchWhileRebasingError"))
}
return true, nil
Expand Down Expand Up @@ -128,7 +128,7 @@ func (gui *Gui) handleApplyPatch() error {
if err := gui.GitCommand.PatchManager.ApplyPatches(false); err != nil {
return gui.createErrorPanel(gui.g, err.Error())
}
return gui.refreshSidePanels(gui.g)
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
}

func (gui *Gui) handleResetPatch() error {
Expand Down
3 changes: 1 addition & 2 deletions pkg/gui/presentation/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ func getFullDescriptionDisplayStringsForCommit(c *commands.Commit, cherryPickedC
}

tagString := ""
truncatedDate := utils.TruncateWithEllipsis(c.Date, 15)
secondColumnString := blue.Sprint(truncatedDate)
secondColumnString := blue.Sprint(utils.UnixToDate(c.UnixTimestamp))
if c.Action != "" {
secondColumnString = cyan.Sprint(c.Action)
} else if c.ExtraInfo != "" {
Expand Down
Loading

0 comments on commit 198d237

Please sign in to comment.