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 {
worktree.NameField = names[index]
worktree.Name = names[index]
}
// move current worktree to the top

View file

@ -21,11 +21,11 @@ type Worktree struct {
Branch string
// 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)
NameField string
Name string
}
func (w *Worktree) RefName() string {
return w.Name()
return w.Name
}
func (w *Worktree) ID() string {
@ -35,19 +35,3 @@ func (w *Worktree) ID() string {
func (w *Worktree) Description() string {
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(
func() []*models.Worktree { return c.Model().Worktrees },
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)
if ok && !worktreeForRef.Current() {
if ok && !worktreeForRef.IsCurrent {
return self.promptToCheckoutWorktree(worktreeForRef)
}
@ -218,7 +218,7 @@ func (self *BranchesController) worktreeForBranch(branch *models.Branch) (*model
func (self *BranchesController) promptToCheckoutWorktree(worktree *models.Worktree) error {
return self.c.Confirm(types.ConfirmOpts{
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 {
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{
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{
{
Label: "Switch to worktree",
@ -432,7 +432,7 @@ func (self *BranchesController) fastForward(branch *models.Branch) error {
worktreeGitDir := ""
// if it is the current worktree path, no need to specify the path
if !worktree.Current() {
if !worktree.IsCurrent {
worktreeGitDir = worktree.GitDir
}

View file

@ -34,8 +34,8 @@ func NewWorktreeHelper(c *HelperCommon, reposHelper *ReposHelper, refsHelper *Re
func (self *WorktreeHelper) GetMainWorktreeName() string {
for _, worktree := range self.c.Model().Worktrees {
if worktree.Main() {
return worktree.Name()
if worktree.IsMain {
return worktree.Name
}
}
@ -51,11 +51,11 @@ func (self *WorktreeHelper) GetLinkedWorktreeName() string {
// worktrees always have the current worktree on top
currentWorktree := worktrees[0]
if currentWorktree.Main() {
if currentWorktree.IsMain {
return ""
}
return currentWorktree.Name()
return currentWorktree.Name
}
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 {
if worktree.Current() {
if worktree.IsCurrent {
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(
templateStr,
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)
} else {
main := ""
if worktree.Main() {
if worktree.IsMain {
main = style.FgDefault.Sprintf(" %s", self.c.Tr.MainWorktree)
}
missing := ""
if worktree.PathMissing() {
if worktree.IsPathMissing {
missing = style.FgRed.Sprintf(" %s", self.c.Tr.MissingWorktree)
}
var builder strings.Builder
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%s\n", self.c.Tr.Path, style.FgCyan.Sprint(worktree.Path), missing)
_ = w.Flush()
@ -101,11 +101,11 @@ func (self *WorktreesController) add() error {
}
func (self *WorktreesController) remove(worktree *models.Worktree) error {
if worktree.Main() {
if worktree.IsMain {
return self.c.ErrorMsg(self.c.Tr.CantDeleteMainWorktree)
}
if worktree.Current() {
if worktree.IsCurrent {
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(),
}
gui.RepoStateMap[Repo(currentDir)] = gui.State
gui.RepoStateMap[Repo(worktreePath)] = gui.State
return initialContext(contextTree, startArgs)
}

View file

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

View file

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