1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-17 21:18:31 +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 {
return IsCurrentWorktree(w)
}
func IsCurrentWorktree(w *models.Worktree) bool {
pwd, err := os.Getwd()
if err != nil {
log.Fatalln(err.Error())
}
return pwd == w.Path
return EqualPath(pwd, w.Path)
}
func (self *WorktreeCommands) IsWorktreePathMissing(w *models.Worktree) bool {
@ -50,3 +54,9 @@ func (self *WorktreeCommands) IsWorktreePathMissing(w *models.Worktree) bool {
}
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
import (
"os"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
@ -34,22 +35,22 @@ func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) {
splitLines := strings.Split(worktreesOutput, "\x00")
var worktrees []*models.Worktree
var currentWorktree *models.Worktree
var current *models.Worktree
for _, splitLine := range splitLines {
if len(splitLine) == 0 && currentWorktree != nil {
worktrees = append(worktrees, currentWorktree)
currentWorktree = nil
if len(splitLine) == 0 && current != nil {
worktrees = append(worktrees, current)
current = nil
continue
}
if strings.HasPrefix(splitLine, "worktree ") {
path := strings.SplitN(splitLine, " ", 2)[1]
currentWorktree = &models.Worktree{
current = &models.Worktree{
IsMain: len(worktrees) == 0,
Path: path,
}
} else if strings.HasPrefix(splitLine, "branch ") {
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]
}
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
}