diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-17 14:38:08 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-17 14:38:08 +1000 |
commit | e57f6ff9c55ebeafbf1d77648c37ba6850be4158 (patch) | |
tree | 011b1100351eeb3f206c9856155ff39f2b55d1fe | |
parent | a748294bc14fed06473084c11753ea4087b314e3 (diff) |
Better logic for knowing which repo we're inbronze
-rw-r--r-- | pkg/commands/git_commands/worktree_loader.go | 4 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/refresh_helper.go | 4 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/worktree_helper.go | 37 | ||||
-rw-r--r-- | pkg/gui/presentation/status.go | 11 |
4 files changed, 49 insertions, 7 deletions
diff --git a/pkg/commands/git_commands/worktree_loader.go b/pkg/commands/git_commands/worktree_loader.go index 75b60d433..a6561a78a 100644 --- a/pkg/commands/git_commands/worktree_loader.go +++ b/pkg/commands/git_commands/worktree_loader.go @@ -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 diff --git a/pkg/gui/controllers/helpers/refresh_helper.go b/pkg/gui/controllers/helpers/refresh_helper.go index 2dc345881..e77e51a48 100644 --- a/pkg/gui/controllers/helpers/refresh_helper.go +++ b/pkg/gui/controllers/helpers/refresh_helper.go @@ -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) } diff --git a/pkg/gui/controllers/helpers/worktree_helper.go b/pkg/gui/controllers/helpers/worktree_helper.go index db59b1afe..bebf2cc66 100644 --- a/pkg/gui/controllers/helpers/worktree_helper.go +++ b/pkg/gui/controllers/helpers/worktree_helper.go @@ -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) +} diff --git a/pkg/gui/presentation/status.go b/pkg/gui/presentation/status.go index 650303d44..f70210c27 100644 --- a/pkg/gui/presentation/status.go +++ b/pkg/gui/presentation/status.go @@ -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) |