1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2026-06-20 01:19:23 +02:00

Better logic for knowing which repo we're in

This commit is contained in:
Jesse Duffield
2023-07-17 14:38:08 +10:00
parent a748294bc1
commit e57f6ff9c5
4 changed files with 49 additions and 7 deletions
+2 -2
View File
@@ -98,7 +98,7 @@ func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) {
func rebaseBranch(worktreePath string) (string, bool) {
// need to find the actual path of the worktree in the .git dir
gitPath, ok := worktreeGitPath(worktreePath)
gitPath, ok := WorktreeGitPath(worktreePath)
if !ok {
return "", false
}
@@ -116,7 +116,7 @@ func rebaseBranch(worktreePath string) (string, bool) {
return shortHeadName, true
}
func worktreeGitPath(worktreePath string) (string, bool) {
func WorktreeGitPath(worktreePath string) (string, bool) {
// first we get the path of the worktree, then we look at the contents of the `.git` file in that path
// then we look for the line that says `gitdir: /path/to/.git/worktrees/<worktree-name>`
// then we return that path
@@ -604,7 +604,9 @@ func (self *RefreshHelper) refreshStatus() {
linkedWorktreeName = self.worktreeHelper.GetLinkedWorktreeName()
}
status := presentation.FormatStatus(currentBranch, linkedWorktreeName, workingTreeState, self.c.Tr)
repoName := self.worktreeHelper.GetCurrentRepoName()
status := presentation.FormatStatus(repoName, currentBranch, linkedWorktreeName, workingTreeState, self.c.Tr)
self.c.SetViewContent(self.c.Views().Status, status)
}
@@ -3,7 +3,9 @@ package helpers
import (
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"github.com/jesseduffield/gocui"
@@ -263,3 +265,38 @@ func (self *WorktreeHelper) ViewBranchWorktreeOptions(branchName string, canChec
},
})
}
func (self *WorktreeHelper) GetCurrentRepoName() string {
pwd, err := os.Getwd()
if err != nil {
log.Fatalln(err.Error())
}
// check if .git is a file or a directory
gitPath := filepath.Join(pwd, ".git")
gitFileInfo, err := os.Stat(gitPath)
if err != nil {
log.Fatalln(err.Error())
}
// must be a worktree or bare repo
if !gitFileInfo.IsDir() {
worktreeGitPath, ok := git_commands.WorktreeGitPath(pwd)
if !ok {
return basePath()
}
// now we just jump up three directories to get the repo name
return filepath.Base(filepath.Dir(filepath.Dir(filepath.Dir(worktreeGitPath))))
}
return basePath()
}
func basePath() string {
pwd, err := os.Getwd()
if err != nil {
log.Fatalln(err.Error())
}
return filepath.Base(pwd)
}
+7 -4
View File
@@ -5,12 +5,12 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
"github.com/jesseduffield/lazygit/pkg/gui/presentation/icons"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/utils"
)
func FormatStatus(currentBranch *models.Branch, linkedWorktreeName string, workingTreeState enums.RebaseMode, tr *i18n.TranslationSet) string {
func FormatStatus(repoName string, currentBranch *models.Branch, linkedWorktreeName string, workingTreeState enums.RebaseMode, tr *i18n.TranslationSet) string {
status := ""
if currentBranch.IsRealBranch() {
@@ -22,10 +22,13 @@ func FormatStatus(currentBranch *models.Branch, linkedWorktreeName string, worki
}
name := GetBranchTextStyle(currentBranch.Name).Sprint(currentBranch.Name)
repoName := utils.GetCurrentRepoName()
// If the user is in a linked worktree (i.e. not the main worktree) we'll display that
if linkedWorktreeName != "" {
repoName = fmt.Sprintf("%s(%s)", repoName, style.FgCyan.Sprint(linkedWorktreeName))
icon := ""
if icons.IsIconEnabled() {
icon = icons.LINKED_WORKTREE_ICON + " "
}
repoName = fmt.Sprintf("%s(%s%s)", repoName, icon, style.FgCyan.Sprint(linkedWorktreeName))
}
status += fmt.Sprintf("%s → %s ", repoName, name)