1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-15 00:15:32 +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

@ -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
}