1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-05-27 23:08:02 +02:00

Use fields rather than methods on worktrees

I would prefer to use methods to keep things immutable but I'd rather be consistent with the other
models and update them all at once
This commit is contained in:
Jesse Duffield 2023-07-28 18:53:00 +10:00
parent 4c5b1574f1
commit 06be88aef7
9 changed files with 27 additions and 45 deletions

View File

@ -87,7 +87,7 @@ func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) {
})) }))
for index, worktree := range worktrees { for index, worktree := range worktrees {
worktree.NameField = names[index] worktree.Name = names[index]
} }
// move current worktree to the top // move current worktree to the top

View File

@ -21,11 +21,11 @@ type Worktree struct {
Branch string Branch string
// based on the path, but uniquified. Not the same name that git uses in the worktrees/ folder (no good reason for this, // based on the path, but uniquified. Not the same name that git uses in the worktrees/ folder (no good reason for this,
// I just prefer my naming convention better) // I just prefer my naming convention better)
NameField string Name string
} }
func (w *Worktree) RefName() string { func (w *Worktree) RefName() string {
return w.Name() return w.Name
} }
func (w *Worktree) ID() string { func (w *Worktree) ID() string {
@ -35,19 +35,3 @@ func (w *Worktree) ID() string {
func (w *Worktree) Description() string { func (w *Worktree) Description() string {
return w.RefName() return w.RefName()
} }
func (w *Worktree) Name() string {
return w.NameField
}
func (w *Worktree) Main() bool {
return w.IsMain
}
func (w *Worktree) Current() bool {
return w.IsCurrent
}
func (w *Worktree) PathMissing() bool {
return w.IsPathMissing
}

View File

@ -17,7 +17,7 @@ func NewWorktreesContext(c *ContextCommon) *WorktreesContext {
viewModel := NewFilteredListViewModel( viewModel := NewFilteredListViewModel(
func() []*models.Worktree { return c.Model().Worktrees }, func() []*models.Worktree { return c.Model().Worktrees },
func(Worktree *models.Worktree) []string { func(Worktree *models.Worktree) []string {
return []string{Worktree.Name()} return []string{Worktree.Name}
}, },
) )

View File

@ -203,7 +203,7 @@ func (self *BranchesController) press(selectedBranch *models.Branch) error {
} }
worktreeForRef, ok := self.worktreeForBranch(selectedBranch) worktreeForRef, ok := self.worktreeForBranch(selectedBranch)
if ok && !worktreeForRef.Current() { if ok && !worktreeForRef.IsCurrent {
return self.promptToCheckoutWorktree(worktreeForRef) return self.promptToCheckoutWorktree(worktreeForRef)
} }
@ -218,7 +218,7 @@ func (self *BranchesController) worktreeForBranch(branch *models.Branch) (*model
func (self *BranchesController) promptToCheckoutWorktree(worktree *models.Worktree) error { func (self *BranchesController) promptToCheckoutWorktree(worktree *models.Worktree) error {
return self.c.Confirm(types.ConfirmOpts{ return self.c.Confirm(types.ConfirmOpts{
Title: "Switch to worktree", Title: "Switch to worktree",
Prompt: fmt.Sprintf("This branch is checked out by worktree %s. Do you want to switch to that worktree?", worktree.Name()), Prompt: fmt.Sprintf("This branch is checked out by worktree %s. Do you want to switch to that worktree?", worktree.Name),
HandleConfirm: func() error { HandleConfirm: func() error {
return self.c.Helpers().Worktree.Switch(worktree, context.LOCAL_BRANCHES_CONTEXT_KEY) return self.c.Helpers().Worktree.Switch(worktree, context.LOCAL_BRANCHES_CONTEXT_KEY)
}, },
@ -337,7 +337,7 @@ func (self *BranchesController) promptWorktreeBranchDelete(selectedBranch *model
} }
return self.c.Menu(types.CreateMenuOptions{ return self.c.Menu(types.CreateMenuOptions{
Title: fmt.Sprintf("Branch %s is checked out by worktree %s", selectedBranch.Name, worktree.Name()), Title: fmt.Sprintf("Branch %s is checked out by worktree %s", selectedBranch.Name, worktree.Name),
Items: []*types.MenuItem{ Items: []*types.MenuItem{
{ {
Label: "Switch to worktree", Label: "Switch to worktree",
@ -432,7 +432,7 @@ func (self *BranchesController) fastForward(branch *models.Branch) error {
worktreeGitDir := "" worktreeGitDir := ""
// if it is the current worktree path, no need to specify the path // if it is the current worktree path, no need to specify the path
if !worktree.Current() { if !worktree.IsCurrent {
worktreeGitDir = worktree.GitDir worktreeGitDir = worktree.GitDir
} }

View File

@ -34,8 +34,8 @@ func NewWorktreeHelper(c *HelperCommon, reposHelper *ReposHelper, refsHelper *Re
func (self *WorktreeHelper) GetMainWorktreeName() string { func (self *WorktreeHelper) GetMainWorktreeName() string {
for _, worktree := range self.c.Model().Worktrees { for _, worktree := range self.c.Model().Worktrees {
if worktree.Main() { if worktree.IsMain {
return worktree.Name() return worktree.Name
} }
} }
@ -51,11 +51,11 @@ func (self *WorktreeHelper) GetLinkedWorktreeName() string {
// worktrees always have the current worktree on top // worktrees always have the current worktree on top
currentWorktree := worktrees[0] currentWorktree := worktrees[0]
if currentWorktree.Main() { if currentWorktree.IsMain {
return "" return ""
} }
return currentWorktree.Name() return currentWorktree.Name
} }
func (self *WorktreeHelper) NewWorktree() error { func (self *WorktreeHelper) NewWorktree() error {
@ -153,7 +153,7 @@ func (self *WorktreeHelper) NewWorktreeCheckout(base string, canCheckoutBase boo
} }
func (self *WorktreeHelper) Switch(worktree *models.Worktree, contextKey types.ContextKey) error { func (self *WorktreeHelper) Switch(worktree *models.Worktree, contextKey types.ContextKey) error {
if worktree.Current() { if worktree.IsCurrent {
return self.c.ErrorMsg(self.c.Tr.AlreadyInWorktree) return self.c.ErrorMsg(self.c.Tr.AlreadyInWorktree)
} }
@ -173,7 +173,7 @@ func (self *WorktreeHelper) Remove(worktree *models.Worktree, force bool) error
message := utils.ResolvePlaceholderString( message := utils.ResolvePlaceholderString(
templateStr, templateStr,
map[string]string{ map[string]string{
"worktreeName": worktree.Name(), "worktreeName": worktree.Name,
}, },
) )

View File

@ -67,18 +67,18 @@ func (self *WorktreesController) GetOnRenderToMain() func() error {
task = types.NewRenderStringTask(self.c.Tr.NoWorktreesThisRepo) task = types.NewRenderStringTask(self.c.Tr.NoWorktreesThisRepo)
} else { } else {
main := "" main := ""
if worktree.Main() { if worktree.IsMain {
main = style.FgDefault.Sprintf(" %s", self.c.Tr.MainWorktree) main = style.FgDefault.Sprintf(" %s", self.c.Tr.MainWorktree)
} }
missing := "" missing := ""
if worktree.PathMissing() { if worktree.IsPathMissing {
missing = style.FgRed.Sprintf(" %s", self.c.Tr.MissingWorktree) missing = style.FgRed.Sprintf(" %s", self.c.Tr.MissingWorktree)
} }
var builder strings.Builder var builder strings.Builder
w := tabwriter.NewWriter(&builder, 0, 0, 2, ' ', 0) w := tabwriter.NewWriter(&builder, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintf(w, "%s:\t%s%s\n", self.c.Tr.Name, style.FgGreen.Sprint(worktree.Name()), main) _, _ = fmt.Fprintf(w, "%s:\t%s%s\n", self.c.Tr.Name, style.FgGreen.Sprint(worktree.Name), main)
_, _ = fmt.Fprintf(w, "%s:\t%s\n", self.c.Tr.Branch, style.FgYellow.Sprint(worktree.Branch)) _, _ = fmt.Fprintf(w, "%s:\t%s\n", self.c.Tr.Branch, style.FgYellow.Sprint(worktree.Branch))
_, _ = fmt.Fprintf(w, "%s:\t%s%s\n", self.c.Tr.Path, style.FgCyan.Sprint(worktree.Path), missing) _, _ = fmt.Fprintf(w, "%s:\t%s%s\n", self.c.Tr.Path, style.FgCyan.Sprint(worktree.Path), missing)
_ = w.Flush() _ = w.Flush()
@ -101,11 +101,11 @@ func (self *WorktreesController) add() error {
} }
func (self *WorktreesController) remove(worktree *models.Worktree) error { func (self *WorktreesController) remove(worktree *models.Worktree) error {
if worktree.Main() { if worktree.IsMain {
return self.c.ErrorMsg(self.c.Tr.CantDeleteMainWorktree) return self.c.ErrorMsg(self.c.Tr.CantDeleteMainWorktree)
} }
if worktree.Current() { if worktree.IsCurrent {
return self.c.ErrorMsg(self.c.Tr.CantDeleteCurrentWorktree) return self.c.ErrorMsg(self.c.Tr.CantDeleteCurrentWorktree)
} }

View File

@ -374,7 +374,7 @@ func (gui *Gui) resetState(startArgs appTypes.StartArgs) types.Context {
SearchState: types.NewSearchState(), SearchState: types.NewSearchState(),
} }
gui.RepoStateMap[Repo(currentDir)] = gui.State gui.RepoStateMap[Repo(worktreePath)] = gui.State
return initialContext(contextTree, startArgs) return initialContext(contextTree, startArgs)
} }

View File

@ -22,13 +22,13 @@ func GetWorktreeDisplayString(tr *i18n.TranslationSet, worktree *models.Worktree
current := "" current := ""
currentColor := style.FgCyan currentColor := style.FgCyan
if worktree.Current() { if worktree.IsCurrent {
current = " *" current = " *"
currentColor = style.FgGreen currentColor = style.FgGreen
} }
icon := icons.IconForWorktree(false) icon := icons.IconForWorktree(false)
if worktree.PathMissing() { if worktree.IsPathMissing {
textStyle = style.FgRed textStyle = style.FgRed
icon = icons.IconForWorktree(true) icon = icons.IconForWorktree(true)
} }
@ -39,11 +39,11 @@ func GetWorktreeDisplayString(tr *i18n.TranslationSet, worktree *models.Worktree
res = append(res, textStyle.Sprint(icon)) res = append(res, textStyle.Sprint(icon))
} }
name := worktree.Name() name := worktree.Name
if worktree.Main() { if worktree.IsMain {
name += " " + tr.MainWorktree name += " " + tr.MainWorktree
} }
if worktree.PathMissing() && !icons.IsIconEnabled() { if worktree.IsPathMissing && !icons.IsIconEnabled() {
name += " " + tr.MissingWorktree name += " " + tr.MissingWorktree
} }
res = append(res, textStyle.Sprint(name)) res = append(res, textStyle.Sprint(name))

View File

@ -17,11 +17,9 @@ var SwitchTabFromMenu = NewIntegrationTest(NewIntegrationTestArgs{
Press(keys.Universal.OptionMenuAlt1) Press(keys.Universal.OptionMenuAlt1)
t.ExpectPopup().Menu().Title(Equals("Keybindings")). t.ExpectPopup().Menu().Title(Equals("Keybindings")).
// Looping back around to the end to side-step the worktrees view which is Select(Contains("Next tab")).
// only present on recent git versions
Select(Contains("Previous tab")).
Confirm() Confirm()
t.Views().Submodules().IsFocused() t.Views().Worktrees().IsFocused()
}, },
}) })