Skip to content

Commit

Permalink
Cleanup background fetch (jesseduffield#4084)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhaller authored Dec 1, 2024
2 parents 62e31ef + 64cebfc commit a50712b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 28 deletions.
26 changes: 11 additions & 15 deletions pkg/gui/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gui
import (
"fmt"
"runtime"
"strings"
"time"

"github.com/jesseduffield/gocui"
Expand Down Expand Up @@ -76,21 +75,18 @@ func (self *BackgroundRoutineMgr) startBackgroundRoutines() {
func (self *BackgroundRoutineMgr) startBackgroundFetch() {
self.gui.waitForIntro.Wait()

isNew := self.gui.IsNewRepo
userConfig := self.gui.UserConfig()
if !isNew {
time.After(time.Duration(userConfig.Refresher.FetchInterval) * time.Second)
}
err := self.backgroundFetch()
if err != nil && strings.Contains(err.Error(), "exit status 128") && isNew {
self.gui.c.Alert(self.gui.c.Tr.NoAutomaticGitFetchTitle, self.gui.c.Tr.NoAutomaticGitFetchBody)
} else {
self.goEvery(time.Second*time.Duration(userConfig.Refresher.FetchInterval), self.gui.stopChan, func() error {
err := self.backgroundFetch()
self.gui.c.Render()
return err
})
fetch := func() error {
err := self.backgroundFetch()
self.gui.c.Render()
return err
}

// We want an immediate fetch at startup, and since goEvery starts by
// waiting for the interval, we need to trigger one manually first
_ = fetch()

userConfig := self.gui.UserConfig()
self.goEvery(time.Second*time.Duration(userConfig.Refresher.FetchInterval), self.gui.stopChan, fetch)
}

func (self *BackgroundRoutineMgr) startBackgroundFilesRefresh(refreshInterval int) {
Expand Down
2 changes: 0 additions & 2 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ type Gui struct {

PopupHandler types.IPopupHandler

IsNewRepo bool

IsRefreshingFiles bool

// we use this to decide whether we'll return to the original directory that
Expand Down
10 changes: 3 additions & 7 deletions pkg/gui/recent_repos_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,23 @@ func (gui *Gui) updateRecentRepoList() error {
if err != nil {
return err
}
known, recentRepos := newRecentReposList(recentRepos, currentRepo)
gui.IsNewRepo = known
recentRepos = newRecentReposList(recentRepos, currentRepo)
// TODO: migrate this file to use forward slashes on all OSes for consistency
// (windows uses backslashes at the moment)
gui.c.GetAppState().RecentRepos = recentRepos
return gui.c.SaveAppState()
}

// newRecentReposList returns a new repo list with a new entry but only when it doesn't exist yet
func newRecentReposList(recentRepos []string, currentRepo string) (bool, []string) {
isNew := true
func newRecentReposList(recentRepos []string, currentRepo string) []string {
newRepos := []string{currentRepo}
for _, repo := range recentRepos {
if repo != currentRepo {
if _, err := os.Stat(filepath.Join(repo, ".git")); err != nil {
continue
}
newRepos = append(newRepos, repo)
} else {
isNew = false
}
}
return isNew, newRepos
return newRepos
}
4 changes: 0 additions & 4 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ type TranslationSet struct {
NoBranchOnRemote string
Fetch string
FetchTooltip string
NoAutomaticGitFetchTitle string
NoAutomaticGitFetchBody string
FileEnter string
FileEnterTooltip string
FileStagingRequirements string
Expand Down Expand Up @@ -1235,8 +1233,6 @@ func EnglishTranslationSet() *TranslationSet {
NoBranchOnRemote: `This branch doesn't exist on remote. You need to push it to remote first.`,
Fetch: `Fetch`,
FetchTooltip: "Fetch changes from remote.",
NoAutomaticGitFetchTitle: `No automatic git fetch`,
NoAutomaticGitFetchBody: `Lazygit can't use "git fetch" in a private repo; use 'f' in the files panel to run "git fetch" manually`,
FileEnter: `Stage lines / Collapse directory`,
FileEnterTooltip: "If the selected item is a file, focus the staging view so you can stage individual hunks/lines. If the selected item is a directory, collapse/expand it.",
FileStagingRequirements: `Can only stage individual lines for tracked files`,
Expand Down

0 comments on commit a50712b

Please sign in to comment.