2018-09-19 11:15:29 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-04-03 04:43:43 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
2018-09-19 11:15:29 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
2020-09-27 08:17:26 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/env"
|
2021-07-27 15:00:37 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
2018-09-19 11:15:29 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2020-08-15 09:23:16 +02:00
|
|
|
func (gui *Gui) handleCreateRecentReposMenu() error {
|
2018-09-19 11:15:29 +02:00
|
|
|
recentRepoPaths := gui.Config.GetAppState().RecentRepos
|
|
|
|
reposCount := utils.Min(len(recentRepoPaths), 20)
|
2021-07-27 15:00:37 +02:00
|
|
|
|
2018-09-19 11:15:29 +02:00
|
|
|
// we won't show the current repo hence the -1
|
2020-02-14 14:26:09 +02:00
|
|
|
menuItems := make([]*menuItem, reposCount-1)
|
2018-09-19 11:15:29 +02:00
|
|
|
for i, path := range recentRepoPaths[1:reposCount] {
|
2020-09-29 01:02:44 +02:00
|
|
|
path := path // cos we're closing over the loop variable
|
2020-02-14 14:26:09 +02:00
|
|
|
menuItems[i] = &menuItem{
|
|
|
|
displayStrings: []string{
|
2020-09-29 01:02:44 +02:00
|
|
|
filepath.Base(path),
|
2021-07-27 15:00:37 +02:00
|
|
|
style.FgMagenta.Sprint(path),
|
2020-02-14 14:26:09 +02:00
|
|
|
},
|
|
|
|
onPress: func() error {
|
2021-04-03 04:43:43 +02:00
|
|
|
// if we were in a submodule, we want to forget about that stack of repos
|
|
|
|
// so that hitting escape in the new repo does nothing
|
|
|
|
gui.RepoPathStack = []string{}
|
2021-04-05 05:22:03 +02:00
|
|
|
return gui.dispatchSwitchToRepo(path, false)
|
2020-02-14 14:26:09 +02:00
|
|
|
},
|
2018-09-19 11:15:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 02:00:48 +02:00
|
|
|
return gui.createMenu(gui.Tr.RecentRepos, menuItems, createMenuOptions{showCancel: true})
|
2018-09-19 11:15:29 +02:00
|
|
|
}
|
|
|
|
|
2020-11-27 09:07:14 +02:00
|
|
|
func (gui *Gui) handleShowAllBranchLogs() error {
|
|
|
|
cmd := gui.OSCommand.ExecutableFromString(
|
|
|
|
gui.Config.GetUserConfig().Git.AllBranchesLogCmd,
|
|
|
|
)
|
2021-04-04 15:51:59 +02:00
|
|
|
task := NewRunPtyTask(cmd)
|
2020-11-27 09:07:14 +02:00
|
|
|
|
|
|
|
return gui.refreshMainViews(refreshMainOpts{
|
|
|
|
main: &viewUpdateOpts{
|
|
|
|
title: "Log",
|
|
|
|
task: task,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-05 05:22:03 +02:00
|
|
|
func (gui *Gui) dispatchSwitchToRepo(path string, reuse bool) error {
|
2020-09-29 01:02:44 +02:00
|
|
|
env.UnsetGitDirEnvs()
|
2021-03-30 13:17:42 +02:00
|
|
|
originalPath, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-29 01:02:44 +02:00
|
|
|
if err := os.Chdir(path); err != nil {
|
2021-03-30 13:17:42 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return gui.createErrorPanel(gui.Tr.ErrRepositoryMovedOrDeleted)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := commands.VerifyInGitRepo(gui.OSCommand); err != nil {
|
|
|
|
if err := os.Chdir(originalPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-29 01:02:44 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-03-30 13:17:42 +02:00
|
|
|
|
2020-09-29 01:02:44 +02:00
|
|
|
newGitCommand, err := commands.NewGitCommand(gui.Log, gui.OSCommand, gui.Tr, gui.Config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gui.GitCommand = newGitCommand
|
2021-04-03 04:43:43 +02:00
|
|
|
|
|
|
|
gui.g.Update(func(*gocui.Gui) error {
|
2021-04-03 10:35:45 +02:00
|
|
|
// 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()
|
|
|
|
|
2021-04-05 05:22:03 +02:00
|
|
|
gui.resetState("", reuse)
|
2021-04-03 04:43:43 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
2020-09-29 01:02:44 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 11:15:29 +02:00
|
|
|
// 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 {
|
2020-09-27 08:02:20 +02:00
|
|
|
if gui.GitCommand.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.Log.Info("Not appending bare repo to recent repo list")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-19 11:15:29 +02:00
|
|
|
recentRepos := gui.Config.GetAppState().RecentRepos
|
|
|
|
currentRepo, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-10 14:45:03 +02:00
|
|
|
known, recentRepos := newRecentReposList(recentRepos, currentRepo)
|
|
|
|
gui.Config.SetIsNewRepo(known)
|
|
|
|
gui.Config.GetAppState().RecentRepos = recentRepos
|
2021-07-27 22:03:37 +02:00
|
|
|
err = gui.Config.SaveAppState()
|
|
|
|
if err != nil && os.IsPermission(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2018-09-19 11:15:29 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 23:05:16 +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 {
|
|
|
|
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
|
|
|
}
|