mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-08 23:56:15 +02:00
Merge pull request #2005 from mark2185/feature/recent-repos-path
Show active branch for recent repo
This commit is contained in:
commit
5f4c29d7b5
@ -148,7 +148,7 @@ func isGitVersionValid(versionStr string) bool {
|
|||||||
|
|
||||||
func isDirectoryAGitRepository(dir string) (bool, error) {
|
func isDirectoryAGitRepository(dir string) (bool, error) {
|
||||||
info, err := os.Stat(filepath.Join(dir, ".git"))
|
info, err := os.Stat(filepath.Join(dir, ".git"))
|
||||||
return info != nil && info.IsDir(), err
|
return info != nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) setupRepo() (bool, error) {
|
func (app *App) setupRepo() (bool, error) {
|
||||||
|
@ -1,24 +1,92 @@
|
|||||||
package gui
|
package gui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/jesseduffield/generics/slices"
|
"github.com/jesseduffield/generics/slices"
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands"
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
||||||
"github.com/jesseduffield/lazygit/pkg/env"
|
"github.com/jesseduffield/lazygit/pkg/env"
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/gui/presentation/icons"
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (gui *Gui) handleCreateRecentReposMenu() error {
|
func (gui *Gui) getCurrentBranch(path string) string {
|
||||||
recentRepoPaths := gui.c.GetAppState().RecentRepos
|
readHeadFile := func(path string) (string, error) {
|
||||||
|
headFile, err := ioutil.ReadFile(filepath.Join(path, "HEAD"))
|
||||||
|
if err == nil {
|
||||||
|
content := strings.TrimSpace(string(headFile))
|
||||||
|
refsPrefix := "ref: refs/heads/"
|
||||||
|
branchDisplay := ""
|
||||||
|
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
|
||||||
|
}
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
gitDirPath := filepath.Join(path, ".git")
|
||||||
|
|
||||||
|
if gitDir, err := os.Stat(gitDirPath); err == nil {
|
||||||
|
if gitDir.IsDir() {
|
||||||
|
// ordinary repo
|
||||||
|
if branch, err := readHeadFile(gitDirPath); err == nil {
|
||||||
|
return branch
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// worktree
|
||||||
|
if worktreeGitDir, err := ioutil.ReadFile(gitDirPath); err == nil {
|
||||||
|
content := strings.TrimSpace(string(worktreeGitDir))
|
||||||
|
worktreePath := strings.TrimPrefix(content, "gitdir: ")
|
||||||
|
if branch, err := readHeadFile(worktreePath); err == nil {
|
||||||
|
return branch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return gui.c.Tr.LcBranchUnknown
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gui *Gui) handleCreateRecentReposMenu() error {
|
||||||
|
// we skip the first one because we're currently in it
|
||||||
|
recentRepoPaths := gui.c.GetAppState().RecentRepos[1:]
|
||||||
|
|
||||||
|
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() {
|
||||||
|
branchName = icons.BRANCH_ICON + " " + fmt.Sprintf("%v", branchName)
|
||||||
|
}
|
||||||
|
|
||||||
// we won't show the current repo hence the -1
|
|
||||||
menuItems := slices.Map(recentRepoPaths[1:], func(path string) *types.MenuItem {
|
|
||||||
return &types.MenuItem{
|
return &types.MenuItem{
|
||||||
LabelColumns: []string{
|
LabelColumns: []string{
|
||||||
filepath.Base(path),
|
filepath.Base(path),
|
||||||
|
style.FgCyan.Sprint(branchName),
|
||||||
style.FgMagenta.Sprint(path),
|
style.FgMagenta.Sprint(path),
|
||||||
},
|
},
|
||||||
OnPress: func() error {
|
OnPress: func() error {
|
||||||
@ -110,7 +178,7 @@ func newRecentReposList(recentRepos []string, currentRepo string) (bool, []strin
|
|||||||
newRepos := []string{currentRepo}
|
newRepos := []string{currentRepo}
|
||||||
for _, repo := range recentRepos {
|
for _, repo := range recentRepos {
|
||||||
if repo != currentRepo {
|
if repo != currentRepo {
|
||||||
if _, err := os.Stat(repo); err != nil {
|
if _, err := os.Stat(filepath.Join(repo, ".git")); err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
newRepos = append(newRepos, repo)
|
newRepos = append(newRepos, repo)
|
||||||
|
@ -409,6 +409,7 @@ type TranslationSet struct {
|
|||||||
NoFilesStagedPrompt string
|
NoFilesStagedPrompt string
|
||||||
BranchNotFoundTitle string
|
BranchNotFoundTitle string
|
||||||
BranchNotFoundPrompt string
|
BranchNotFoundPrompt string
|
||||||
|
LcBranchUnknown string
|
||||||
UnstageLinesTitle string
|
UnstageLinesTitle string
|
||||||
UnstageLinesPrompt string
|
UnstageLinesPrompt string
|
||||||
LcCreateNewBranchFromCommit string
|
LcCreateNewBranchFromCommit string
|
||||||
@ -1046,6 +1047,7 @@ func EnglishTranslationSet() TranslationSet {
|
|||||||
NoFilesStagedPrompt: "You have not staged any files. Commit all files?",
|
NoFilesStagedPrompt: "You have not staged any files. Commit all files?",
|
||||||
BranchNotFoundTitle: "Branch not found",
|
BranchNotFoundTitle: "Branch not found",
|
||||||
BranchNotFoundPrompt: "Branch not found. Create a new branch named",
|
BranchNotFoundPrompt: "Branch not found. Create a new branch named",
|
||||||
|
LcBranchUnknown: "branch unknown",
|
||||||
UnstageLinesTitle: "Unstage lines",
|
UnstageLinesTitle: "Unstage lines",
|
||||||
UnstageLinesPrompt: "Are you sure you want to delete the selected lines (git reset)? It is irreversible.\nTo disable this dialogue set the config key of 'gui.skipUnstageLineWarning' to true",
|
UnstageLinesPrompt: "Are you sure you want to delete the selected lines (git reset)? It is irreversible.\nTo disable this dialogue set the config key of 'gui.skipUnstageLineWarning' to true",
|
||||||
LcCreateNewBranchFromCommit: "create new branch off of commit",
|
LcCreateNewBranchFromCommit: "create new branch off of commit",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user