1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-17 01:42:45 +02:00

Move current worktree to top of list

This commit is contained in:
Jesse Duffield
2023-07-16 14:23:31 +10:00
parent 077ae99438
commit 53f4ccb809
2 changed files with 32 additions and 7 deletions

View File

@ -33,12 +33,16 @@ func (self *WorktreeCommands) Delete(worktreePath string, force bool) error {
} }
func (self *WorktreeCommands) IsCurrentWorktree(w *models.Worktree) bool { func (self *WorktreeCommands) IsCurrentWorktree(w *models.Worktree) bool {
return IsCurrentWorktree(w)
}
func IsCurrentWorktree(w *models.Worktree) bool {
pwd, err := os.Getwd() pwd, err := os.Getwd()
if err != nil { if err != nil {
log.Fatalln(err.Error()) log.Fatalln(err.Error())
} }
return pwd == w.Path return EqualPath(pwd, w.Path)
} }
func (self *WorktreeCommands) IsWorktreePathMissing(w *models.Worktree) bool { func (self *WorktreeCommands) IsWorktreePathMissing(w *models.Worktree) bool {
@ -50,3 +54,9 @@ func (self *WorktreeCommands) IsWorktreePathMissing(w *models.Worktree) bool {
} }
return false return false
} }
// checks if two paths are equal
// TODO: support relative paths
func EqualPath(a string, b string) bool {
return a == b
}

View File

@ -1,6 +1,7 @@
package git_commands package git_commands
import ( import (
"os"
"strings" "strings"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
@ -34,22 +35,22 @@ func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) {
splitLines := strings.Split(worktreesOutput, "\x00") splitLines := strings.Split(worktreesOutput, "\x00")
var worktrees []*models.Worktree var worktrees []*models.Worktree
var currentWorktree *models.Worktree var current *models.Worktree
for _, splitLine := range splitLines { for _, splitLine := range splitLines {
if len(splitLine) == 0 && currentWorktree != nil { if len(splitLine) == 0 && current != nil {
worktrees = append(worktrees, currentWorktree) worktrees = append(worktrees, current)
currentWorktree = nil current = nil
continue continue
} }
if strings.HasPrefix(splitLine, "worktree ") { if strings.HasPrefix(splitLine, "worktree ") {
path := strings.SplitN(splitLine, " ", 2)[1] path := strings.SplitN(splitLine, " ", 2)[1]
currentWorktree = &models.Worktree{ current = &models.Worktree{
IsMain: len(worktrees) == 0, IsMain: len(worktrees) == 0,
Path: path, Path: path,
} }
} else if strings.HasPrefix(splitLine, "branch ") { } else if strings.HasPrefix(splitLine, "branch ") {
branch := strings.SplitN(splitLine, " ", 2)[1] branch := strings.SplitN(splitLine, " ", 2)[1]
currentWorktree.Branch = strings.TrimPrefix(branch, "refs/heads/") current.Branch = strings.TrimPrefix(branch, "refs/heads/")
} }
} }
@ -61,6 +62,20 @@ func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) {
worktree.NameField = names[index] worktree.NameField = names[index]
} }
pwd, err := os.Getwd()
if err != nil {
return nil, err
}
// move current worktree to the top
for i, worktree := range worktrees {
if EqualPath(worktree.Path, pwd) {
worktrees = append(worktrees[:i], worktrees[i+1:]...)
worktrees = append([]*models.Worktree{worktree}, worktrees...)
break
}
}
return worktrees, nil return worktrees, nil
} }