1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-27 00:51:18 +02:00
This commit is contained in:
Jesse Duffield
2021-04-03 19:35:45 +11:00
parent e6274c0757
commit 633b6f596d
4 changed files with 25 additions and 9 deletions

View File

@ -505,6 +505,8 @@ func (gui *Gui) handleRefreshFiles() error {
} }
func (gui *Gui) refreshStateFiles() error { func (gui *Gui) refreshStateFiles() error {
state := gui.State
// keep track of where the cursor is currently and the current file names // keep track of where the cursor is currently and the current file names
// when we refresh, go looking for a matching name // when we refresh, go looking for a matching name
// move the cursor to there. // move the cursor to there.
@ -517,22 +519,24 @@ func (gui *Gui) refreshStateFiles() error {
files := gui.GitCommand.GetStatusFiles(commands.GetStatusFileOptions{}) files := gui.GitCommand.GetStatusFiles(commands.GetStatusFileOptions{})
// for when you stage the old file of a rename and the new file is in a collapsed dir // for when you stage the old file of a rename and the new file is in a collapsed dir
state.FileManager.RWMutex.Lock()
for _, file := range files { for _, file := range files {
if selectedNode != nil && selectedNode.Path != "" && file.PreviousName == selectedNode.Path { if selectedNode != nil && selectedNode.Path != "" && file.PreviousName == selectedNode.Path {
gui.State.FileManager.ExpandToPath(file.Name) state.FileManager.ExpandToPath(file.Name)
} }
} }
gui.State.FileManager.SetFiles(files) state.FileManager.SetFiles(files)
state.FileManager.RWMutex.Unlock()
if err := gui.fileWatcher.addFilesToFileWatcher(files); err != nil { if err := gui.fileWatcher.addFilesToFileWatcher(files); err != nil {
return err return err
} }
if selectedNode != nil { if selectedNode != nil {
newIdx := gui.findNewSelectedIdx(prevNodes[prevSelectedLineIdx:], gui.State.FileManager.GetAllItems()) newIdx := gui.findNewSelectedIdx(prevNodes[prevSelectedLineIdx:], state.FileManager.GetAllItems())
if newIdx != -1 && newIdx != prevSelectedLineIdx { if newIdx != -1 && newIdx != prevSelectedLineIdx {
newNode := gui.State.FileManager.GetItemAtIndex(newIdx) newNode := state.FileManager.GetItemAtIndex(newIdx)
// when not in tree mode, we show merge conflict files at the top, so you // when not in tree mode, we show merge conflict files at the top, so you
// can work through them one by one without having to sift through a large // can work through them one by one without having to sift through a large
// set of files. If you have just fixed the merge conflicts of a file, we // set of files. If you have just fixed the merge conflicts of a file, we
@ -541,17 +545,17 @@ func (gui *Gui) refreshStateFiles() error {
// conflicts: the user in this case would rather work on the next file // conflicts: the user in this case would rather work on the next file
// with merge conflicts, which will have moved up to fill the gap left by // with merge conflicts, which will have moved up to fill the gap left by
// the last file, meaning the cursor doesn't need to move at all. // the last file, meaning the cursor doesn't need to move at all.
leaveCursor := !gui.State.FileManager.InTreeMode() && newNode != nil && leaveCursor := !state.FileManager.InTreeMode() && newNode != nil &&
selectedNode.File != nil && selectedNode.File.HasMergeConflicts && selectedNode.File != nil && selectedNode.File.HasMergeConflicts &&
newNode.File != nil && !newNode.File.HasMergeConflicts newNode.File != nil && !newNode.File.HasMergeConflicts
if !leaveCursor { if !leaveCursor {
gui.State.Panels.Files.SelectedLineIdx = newIdx state.Panels.Files.SelectedLineIdx = newIdx
} }
} }
} }
gui.refreshSelectedLine(gui.State.Panels.Files, gui.State.FileManager.GetItemsLength()) gui.refreshSelectedLine(state.Panels.Files, state.FileManager.GetItemsLength())
return nil return nil
} }

View File

@ -1,6 +1,8 @@
package filetree package filetree
import ( import (
"sync"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation" "github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -12,6 +14,7 @@ type FileManager struct {
showTree bool showTree bool
log *logrus.Entry log *logrus.Entry
collapsedPaths CollapsedPaths collapsedPaths CollapsedPaths
sync.RWMutex
} }
func NewFileManager(files []*models.File, log *logrus.Entry, showTree bool) *FileManager { func NewFileManager(files []*models.File, log *logrus.Entry, showTree bool) *FileManager {
@ -20,6 +23,7 @@ func NewFileManager(files []*models.File, log *logrus.Entry, showTree bool) *Fil
log: log, log: log,
showTree: showTree, showTree: showTree,
collapsedPaths: CollapsedPaths{}, collapsedPaths: CollapsedPaths{},
RWMutex: sync.RWMutex{},
} }
} }

View File

@ -482,7 +482,7 @@ func (gui *Gui) Run() error {
go utils.Safe(gui.startBackgroundFetch) go utils.Safe(gui.startBackgroundFetch)
} }
gui.goEvery(time.Second*time.Duration(userConfig.Refresher.RefreshInterval), gui.stopChan, gui.refreshFilesAndSubmodules) gui.goEvery(time.Millisecond*time.Duration(userConfig.Refresher.RefreshInterval), gui.stopChan, gui.refreshFilesAndSubmodules)
g.SetManager(gocui.ManagerFunc(gui.layout), gocui.ManagerFunc(gui.getFocusLayout())) g.SetManager(gocui.ManagerFunc(gui.layout), gocui.ManagerFunc(gui.getFocusLayout()))
@ -649,7 +649,7 @@ func (gui *Gui) startBackgroundFetch() {
prompt: gui.Tr.NoAutomaticGitFetchBody, prompt: gui.Tr.NoAutomaticGitFetchBody,
}) })
} else { } else {
gui.goEvery(time.Second*time.Duration(userConfig.Refresher.FetchInterval), gui.stopChan, func() error { gui.goEvery(time.Millisecond*time.Duration(userConfig.Refresher.FetchInterval), gui.stopChan, func() error {
err := gui.fetch(false) err := gui.fetch(false)
return err return err
}) })

View File

@ -79,6 +79,14 @@ func (gui *Gui) dispatchSwitchToRepo(path string) error {
gui.GitCommand = newGitCommand gui.GitCommand = newGitCommand
gui.g.Update(func(*gocui.Gui) error { gui.g.Update(func(*gocui.Gui) error {
// these two mutexes are used by our background goroutines (triggered via `gui.goEvery`. We don't want to
// switch to a repo while one of these goroutines is in the process of updating something
gui.Mutexes.FetchMutex.Lock()
defer gui.Mutexes.FetchMutex.Unlock()
gui.Mutexes.RefreshingFilesMutex.Lock()
defer gui.Mutexes.RefreshingFilesMutex.Unlock()
gui.resetState("") gui.resetState("")
return nil return nil