1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-09 13:47:11 +02:00

Address PR comments

This commit is contained in:
Joel Baranick 2022-09-10 22:36:47 -07:00 committed by Jesse Duffield
parent 1ce9a87544
commit db02c13bf6
7 changed files with 74 additions and 67 deletions

View File

@ -1,5 +1,15 @@
package git_commands
import (
"errors"
"fmt"
"io/fs"
"log"
"os"
"github.com/jesseduffield/lazygit/pkg/commands/models"
)
type WorktreeCommands struct {
*GitCommon
}
@ -21,3 +31,22 @@ func (self *WorktreeCommands) Delete(worktreePath string, force bool) error {
return self.cmd.New(cmdArgs).Run()
}
func (self *WorktreeCommands) IsCurrentWorktree(w *models.Worktree) bool {
pwd, err := os.Getwd()
if err != nil {
log.Fatalln(err.Error())
}
return pwd == w.Path
}
func (self *WorktreeCommands) IsWorktreePathMissing(w *models.Worktree) bool {
if _, err := os.Stat(w.Path); err != nil {
if errors.Is(err, fs.ErrNotExist) {
return true
}
log.Fatalln(fmt.Errorf("failed to check if worktree path `%s` exists\n%w", w.Path, err).Error())
}
return false
}

View File

@ -1,11 +1,6 @@
package models
import (
"fmt"
"github.com/go-errors/errors"
"io/fs"
"log"
"os"
"path/filepath"
)
@ -35,22 +30,3 @@ func (w *Worktree) Name() string {
func (w *Worktree) Main() bool {
return w.Id == 0
}
func (w *Worktree) Current() bool {
pwd, err := os.Getwd()
if err != nil {
log.Fatalln(err.Error())
}
return pwd == w.Path
}
func (w *Worktree) Missing() bool {
if _, err := os.Stat(w.Path); err != nil {
if errors.Is(err, fs.ErrNotExist) {
return true
}
log.Fatalln(fmt.Errorf("failed to check if worktree path `%s` exists\n%w", w.Path, err).Error())
}
return false
}

View File

@ -4,6 +4,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/samber/lo"
)
type WorktreesContext struct {
@ -22,7 +23,12 @@ func NewWorktreesContext(c *ContextCommon) *WorktreesContext {
)
getDisplayStrings := func(startIdx int, length int) [][]string {
return presentation.GetWorktreeListDisplayStrings(c.Model().Worktrees)
return lo.Map(c.Model().Worktrees, func(worktree *models.Worktree, _ int) []string {
return presentation.GetWorktreeDisplayString(
c.Git().Worktree.IsCurrentWorktree(worktree),
c.Git().Worktree.IsWorktreePathMissing(worktree),
worktree)
})
}
return &WorktreesContext{

View File

@ -638,8 +638,7 @@ func (self *RefreshHelper) refreshStatus() {
}
name := presentation.GetBranchTextStyle(currentBranch.Name).Sprint(currentBranch.Name)
var repoName string
repoName = utils.GetCurrentRepoName()
repoName := utils.GetCurrentRepoName()
mainWorktreeName := self.worktreeHelper.GetMainWorktreeName()
if repoName != mainWorktreeName {
repoName = fmt.Sprintf("%s(%s)", mainWorktreeName, style.FgBlue.Sprint(repoName))

View File

@ -1,6 +1,15 @@
package helpers
import "github.com/jesseduffield/lazygit/pkg/gui/types"
import (
"errors"
"fmt"
"io/fs"
"log"
"os"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type IWorktreeHelper interface {
GetMainWorktreeName() string
@ -27,6 +36,25 @@ func (self *WorktreeHelper) GetMainWorktreeName() string {
return ""
}
func (self *WorktreeHelper) IsCurrentWorktree(w *models.Worktree) bool {
pwd, err := os.Getwd()
if err != nil {
log.Fatalln(err.Error())
}
return pwd == w.Path
}
func (self *WorktreeHelper) IsWorktreePathMissing(w *models.Worktree) bool {
if _, err := os.Stat(w.Path); err != nil {
if errors.Is(err, fs.ErrNotExist) {
return true
}
log.Fatalln(fmt.Errorf("failed to check if worktree path `%s` exists\n%w", w.Path, err).Error())
}
return false
}
func (self *WorktreeHelper) NewWorktree() error {
return self.c.Prompt(types.PromptOpts{
Title: self.c.Tr.NewWorktreePath,
@ -35,30 +63,7 @@ func (self *WorktreeHelper) NewWorktree() error {
if err := self.c.Git().Worktree.New(sanitizedBranchName(response)); err != nil {
return err
}
//if self.c.CurrentContext() != self.contexts.Worktrees {
// if err := self.c.PushContext(self.contexts.Worktrees); err != nil {
// return err
// }
//}
// self.contexts.LocalCommits.SetSelectedLineIdx(0)
// self.contexts.Branches.SetSelectedLineIdx(0)
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
},
})
}
//func (self *WorktreeHelper) GetCurrentWorktreeName() string {
// for _, worktree := range self.c.Model().Worktrees {
// if worktree.Current() {
// if worktree.Main() {
// return ""
// }
// return worktree.Name()
// }
// }
//
// return ""
//}

View File

@ -63,7 +63,7 @@ func (self *WorktreesController) GetOnRenderToMain() func() error {
}
missing := ""
if worktree.Missing() {
if self.c.Git().Worktree.IsWorktreePathMissing(worktree) {
missing = style.FgRed.Sprintf(" %s", self.c.Tr.MissingWorktree)
}
@ -112,7 +112,7 @@ func (self *WorktreesController) delete(worktree *models.Worktree) error {
return self.c.ErrorMsg(self.c.Tr.CantDeleteMainWorktree)
}
if worktree.Current() {
if self.c.Git().Worktree.IsCurrentWorktree(worktree) {
return self.c.ErrorMsg(self.c.Tr.CantDeleteCurrentWorktree)
}

View File

@ -1,34 +1,26 @@
package presentation
import (
"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/presentation/icons"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/theme"
)
func GetWorktreeListDisplayStrings(worktrees []*models.Worktree) [][]string {
return slices.Map(worktrees, func(worktree *models.Worktree) []string {
return getWorktreeDisplayStrings(worktree)
})
}
// getWorktreeDisplayStrings returns the display string of branch
func getWorktreeDisplayStrings(w *models.Worktree) []string {
func GetWorktreeDisplayString(isCurrent bool, isPathMissing bool, worktree *models.Worktree) []string {
textStyle := theme.DefaultTextColor
current := ""
currentColor := style.FgCyan
if w.Current() {
if isCurrent {
current = " *"
currentColor = style.FgGreen
}
icon := icons.IconForWorktree(w, false)
if w.Missing() {
icon := icons.IconForWorktree(worktree, false)
if isPathMissing {
textStyle = style.FgRed
icon = icons.IconForWorktree(w, true)
icon = icons.IconForWorktree(worktree, true)
}
res := make([]string, 0, 3)
@ -36,6 +28,6 @@ func getWorktreeDisplayStrings(w *models.Worktree) []string {
if icons.IsIconEnabled() {
res = append(res, textStyle.Sprint(icon))
}
res = append(res, textStyle.Sprint(w.Name()))
res = append(res, textStyle.Sprint(worktree.Name()))
return res
}