1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/recent_repos_panel.go

53 lines
1.5 KiB
Go
Raw Normal View History

2018-09-19 11:15:29 +02:00
package gui
import (
"os"
"path/filepath"
)
// updateRecentRepoList registers the fact that we opened lazygit in this repo,
// so that we can open the same repo via the 'recent repos' menu
func (gui *Gui) updateRecentRepoList() error {
2022-08-15 14:59:34 +02:00
isBareRepo, err := gui.git.Status.IsBareRepo()
if err != nil {
return err
}
if isBareRepo {
// we could totally do this but it would require storing both the git-dir and the
// worktree in our recent repos list, which is a change that would need to be
// backwards compatible
gui.c.Log.Info("Not appending bare repo to recent repo list")
return nil
}
recentRepos := gui.c.GetAppState().RecentRepos
2018-09-19 11:15:29 +02:00
currentRepo, err := os.Getwd()
if err != nil {
return err
}
2018-12-10 14:45:03 +02:00
known, recentRepos := newRecentReposList(recentRepos, currentRepo)
2021-12-29 02:50:20 +02:00
gui.IsNewRepo = known
// 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()
2018-09-19 11:15:29 +02:00
}
// newRecentReposList returns a new repo list with a new entry but only when it doesn't exist yet
2018-12-10 14:45:03 +02:00
func newRecentReposList(recentRepos []string, currentRepo string) (bool, []string) {
isNew := true
2018-09-19 11:15:29 +02:00
newRepos := []string{currentRepo}
for _, repo := range recentRepos {
if repo != currentRepo {
if _, err := os.Stat(filepath.Join(repo, ".git")); err != nil {
continue
}
2018-09-19 11:15:29 +02:00
newRepos = append(newRepos, repo)
2018-12-10 14:45:03 +02:00
} else {
isNew = false
2018-09-19 11:15:29 +02:00
}
}
2018-12-10 14:45:03 +02:00
return isNew, newRepos
2018-09-19 11:15:29 +02:00
}