mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-12 04:45:47 +02:00
Address PR comments
This commit is contained in:
parent
1ce9a87544
commit
db02c13bf6
7 changed files with 74 additions and 67 deletions
|
@ -1,5 +1,15 @@
|
||||||
package git_commands
|
package git_commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||||
|
)
|
||||||
|
|
||||||
type WorktreeCommands struct {
|
type WorktreeCommands struct {
|
||||||
*GitCommon
|
*GitCommon
|
||||||
}
|
}
|
||||||
|
@ -21,3 +31,22 @@ func (self *WorktreeCommands) Delete(worktreePath string, force bool) error {
|
||||||
|
|
||||||
return self.cmd.New(cmdArgs).Run()
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,6 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/go-errors/errors"
|
|
||||||
"io/fs"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -35,22 +30,3 @@ func (w *Worktree) Name() string {
|
||||||
func (w *Worktree) Main() bool {
|
func (w *Worktree) Main() bool {
|
||||||
return w.Id == 0
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
|
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||||
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WorktreesContext struct {
|
type WorktreesContext struct {
|
||||||
|
@ -22,7 +23,12 @@ func NewWorktreesContext(c *ContextCommon) *WorktreesContext {
|
||||||
)
|
)
|
||||||
|
|
||||||
getDisplayStrings := func(startIdx int, length int) [][]string {
|
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{
|
return &WorktreesContext{
|
||||||
|
|
|
@ -638,8 +638,7 @@ func (self *RefreshHelper) refreshStatus() {
|
||||||
}
|
}
|
||||||
|
|
||||||
name := presentation.GetBranchTextStyle(currentBranch.Name).Sprint(currentBranch.Name)
|
name := presentation.GetBranchTextStyle(currentBranch.Name).Sprint(currentBranch.Name)
|
||||||
var repoName string
|
repoName := utils.GetCurrentRepoName()
|
||||||
repoName = utils.GetCurrentRepoName()
|
|
||||||
mainWorktreeName := self.worktreeHelper.GetMainWorktreeName()
|
mainWorktreeName := self.worktreeHelper.GetMainWorktreeName()
|
||||||
if repoName != mainWorktreeName {
|
if repoName != mainWorktreeName {
|
||||||
repoName = fmt.Sprintf("%s(%s)", mainWorktreeName, style.FgBlue.Sprint(repoName))
|
repoName = fmt.Sprintf("%s(%s)", mainWorktreeName, style.FgBlue.Sprint(repoName))
|
||||||
|
|
|
@ -1,6 +1,15 @@
|
||||||
package helpers
|
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 {
|
type IWorktreeHelper interface {
|
||||||
GetMainWorktreeName() string
|
GetMainWorktreeName() string
|
||||||
|
@ -27,6 +36,25 @@ func (self *WorktreeHelper) GetMainWorktreeName() string {
|
||||||
return ""
|
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 {
|
func (self *WorktreeHelper) NewWorktree() error {
|
||||||
return self.c.Prompt(types.PromptOpts{
|
return self.c.Prompt(types.PromptOpts{
|
||||||
Title: self.c.Tr.NewWorktreePath,
|
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 {
|
if err := self.c.Git().Worktree.New(sanitizedBranchName(response)); err != nil {
|
||||||
return err
|
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})
|
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 ""
|
|
||||||
//}
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ func (self *WorktreesController) GetOnRenderToMain() func() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
missing := ""
|
missing := ""
|
||||||
if worktree.Missing() {
|
if self.c.Git().Worktree.IsWorktreePathMissing(worktree) {
|
||||||
missing = style.FgRed.Sprintf(" %s", self.c.Tr.MissingWorktree)
|
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)
|
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)
|
return self.c.ErrorMsg(self.c.Tr.CantDeleteCurrentWorktree)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,34 +1,26 @@
|
||||||
package presentation
|
package presentation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jesseduffield/generics/slices"
|
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui/presentation/icons"
|
"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/theme"
|
"github.com/jesseduffield/lazygit/pkg/theme"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetWorktreeListDisplayStrings(worktrees []*models.Worktree) [][]string {
|
func GetWorktreeDisplayString(isCurrent bool, isPathMissing bool, worktree *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 {
|
|
||||||
textStyle := theme.DefaultTextColor
|
textStyle := theme.DefaultTextColor
|
||||||
|
|
||||||
current := ""
|
current := ""
|
||||||
currentColor := style.FgCyan
|
currentColor := style.FgCyan
|
||||||
if w.Current() {
|
if isCurrent {
|
||||||
current = " *"
|
current = " *"
|
||||||
currentColor = style.FgGreen
|
currentColor = style.FgGreen
|
||||||
}
|
}
|
||||||
|
|
||||||
icon := icons.IconForWorktree(w, false)
|
icon := icons.IconForWorktree(worktree, false)
|
||||||
if w.Missing() {
|
if isPathMissing {
|
||||||
textStyle = style.FgRed
|
textStyle = style.FgRed
|
||||||
icon = icons.IconForWorktree(w, true)
|
icon = icons.IconForWorktree(worktree, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
res := make([]string, 0, 3)
|
res := make([]string, 0, 3)
|
||||||
|
@ -36,6 +28,6 @@ func getWorktreeDisplayStrings(w *models.Worktree) []string {
|
||||||
if icons.IsIconEnabled() {
|
if icons.IsIconEnabled() {
|
||||||
res = append(res, textStyle.Sprint(icon))
|
res = append(res, textStyle.Sprint(icon))
|
||||||
}
|
}
|
||||||
res = append(res, textStyle.Sprint(w.Name()))
|
res = append(res, textStyle.Sprint(worktree.Name()))
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue