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

201 lines
5.4 KiB
Go
Raw Normal View History

2018-09-19 11:15:29 +02:00
package gui
import (
"fmt"
2018-09-19 11:15:29 +02:00
"os"
"path/filepath"
"strings"
2022-07-29 22:53:05 +02:00
"sync"
2018-09-19 11:15:29 +02:00
2022-03-19 10:12:58 +02:00
"github.com/jesseduffield/generics/slices"
appTypes "github.com/jesseduffield/lazygit/pkg/app/types"
2018-09-19 11:15:29 +02:00
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/env"
"github.com/jesseduffield/lazygit/pkg/gui/presentation/icons"
"github.com/jesseduffield/lazygit/pkg/gui/style"
2022-01-29 10:09:20 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
2018-09-19 11:15:29 +02:00
)
func (gui *Gui) getCurrentBranch(path string) string {
2022-07-30 19:02:19 +02:00
readHeadFile := func(path string) (string, error) {
2022-09-13 12:11:03 +02:00
headFile, err := os.ReadFile(filepath.Join(path, "HEAD"))
2022-07-30 19:02:19 +02:00
if err == nil {
content := strings.TrimSpace(string(headFile))
refsPrefix := "ref: refs/heads/"
2023-03-19 02:20:29 +02:00
var branchDisplay string
if strings.HasPrefix(content, refsPrefix) {
// is a branch
branchDisplay = strings.TrimPrefix(content, refsPrefix)
} else {
// detached HEAD state, displaying short SHA
branchDisplay = utils.ShortSha(content)
}
return branchDisplay, nil
2022-07-30 19:02:19 +02:00
}
return "", err
}
2022-07-30 19:02:19 +02:00
gitDirPath := filepath.Join(path, ".git")
2022-07-30 19:02:19 +02:00
if gitDir, err := os.Stat(gitDirPath); err == nil {
if gitDir.IsDir() {
// ordinary repo
if branch, err := readHeadFile(gitDirPath); err == nil {
return branch
}
} else {
// worktree
2022-09-13 12:11:03 +02:00
if worktreeGitDir, err := os.ReadFile(gitDirPath); err == nil {
2022-07-30 19:02:19 +02:00
content := strings.TrimSpace(string(worktreeGitDir))
worktreePath := strings.TrimPrefix(content, "gitdir: ")
if branch, err := readHeadFile(worktreePath); err == nil {
return branch
}
}
}
}
2022-07-31 08:57:57 +02:00
return gui.c.Tr.LcBranchUnknown
}
2020-08-15 09:23:16 +02:00
func (gui *Gui) handleCreateRecentReposMenu() error {
// we'll show an empty panel if there are no recent repos
recentRepoPaths := []string{}
if len(gui.c.GetAppState().RecentRepos) > 0 {
// we skip the first one because we're currently in it
recentRepoPaths = gui.c.GetAppState().RecentRepos[1:]
}
2022-07-29 22:53:05 +02:00
currentBranches := sync.Map{}
wg := sync.WaitGroup{}
wg.Add(len(recentRepoPaths))
for _, path := range recentRepoPaths {
go func(path string) {
defer wg.Done()
currentBranches.Store(path, gui.getCurrentBranch(path))
}(path)
}
wg.Wait()
menuItems := slices.Map(recentRepoPaths, func(path string) *types.MenuItem {
branchName, _ := currentBranches.Load(path)
if icons.IsIconEnabled() {
2022-07-29 22:53:05 +02:00
branchName = icons.BRANCH_ICON + " " + fmt.Sprintf("%v", branchName)
}
2022-03-19 10:12:58 +02:00
return &types.MenuItem{
LabelColumns: []string{
filepath.Base(path),
style.FgCyan.Sprint(branchName),
style.FgMagenta.Sprint(path),
2020-02-14 14:26:09 +02:00
},
2022-01-28 11:44:36 +02:00
OnPress: func() error {
// 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
2022-01-28 11:44:36 +02:00
gui.RepoPathStack.Clear()
return gui.dispatchSwitchToRepo(path, false)
2020-02-14 14:26:09 +02:00
},
2018-09-19 11:15:29 +02:00
}
2022-03-19 10:12:58 +02:00
})
2018-09-19 11:15:29 +02:00
2022-01-29 10:09:20 +02:00
return gui.c.Menu(types.CreateMenuOptions{Title: gui.c.Tr.RecentRepos, Items: menuItems})
2018-09-19 11:15:29 +02:00
}
2020-11-27 09:07:14 +02:00
func (gui *Gui) handleShowAllBranchLogs() error {
cmdObj := gui.git.Branch.AllBranchesLogCmdObj()
task := types.NewRunPtyTask(cmdObj.GetCmd())
2020-11-27 09:07:14 +02:00
return gui.c.RenderToMainViews(types.RefreshMainOpts{
Pair: gui.c.MainViewPairs().Normal,
Main: &types.ViewUpdateOpts{
Title: gui.c.Tr.LogTitle,
Task: task,
2020-11-27 09:07:14 +02:00
},
})
}
func (gui *Gui) dispatchSwitchToRepo(path string, reuse bool) error {
env.UnsetGitDirEnvs()
2021-03-30 13:17:42 +02:00
originalPath, err := os.Getwd()
if err != nil {
return nil
}
if err := os.Chdir(path); err != nil {
2021-03-30 13:17:42 +02:00
if os.IsNotExist(err) {
return gui.c.ErrorMsg(gui.c.Tr.ErrRepositoryMovedOrDeleted)
2021-03-30 13:17:42 +02:00
}
return err
}
2022-02-06 04:42:17 +02:00
if err := commands.VerifyInGitRepo(gui.os); err != nil {
2021-03-30 13:17:42 +02:00
if err := os.Chdir(originalPath); err != nil {
return err
}
return err
}
2021-03-30 13:17:42 +02:00
2022-01-31 13:11:34 +02:00
if err := gui.recordCurrentDirectory(); err != nil {
return err
}
2022-01-15 03:04:00 +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
2022-01-29 02:22:35 +02:00
gui.Mutexes.SyncMutex.Lock()
defer gui.Mutexes.SyncMutex.Unlock()
2021-04-03 10:35:45 +02:00
2022-01-15 03:04:00 +02:00
gui.Mutexes.RefreshingFilesMutex.Lock()
defer gui.Mutexes.RefreshingFilesMutex.Unlock()
2021-04-03 10:35:45 +02:00
return gui.onNewRepo(appTypes.StartArgs{}, reuse)
}
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 {
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
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
}