diff --git a/pkg/commands/git_commands/patch.go b/pkg/commands/git_commands/patch.go index dba700c3a80..871dadc0532 100644 --- a/pkg/commands/git_commands/patch.go +++ b/pkg/commands/git_commands/patch.go @@ -10,7 +10,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/commands/types/enums" - "github.com/jesseduffield/lazygit/pkg/utils" ) type PatchCommands struct { @@ -80,7 +79,7 @@ func (self *PatchCommands) applyPatchFile(filepath string, opts ApplyPatchOpts) } func (self *PatchCommands) SaveTemporaryPatch(patch string) (string, error) { - filepath := filepath.Join(self.os.GetTempDir(), utils.GetCurrentRepoName(), time.Now().Format("Jan _2 15.04.05.000000000")+".patch") + filepath := filepath.Join(self.os.GetTempDir(), GetCurrentRepoName(), time.Now().Format("Jan _2 15.04.05.000000000")+".patch") self.Log.Infof("saving temporary patch to %s", filepath) if err := self.os.CreateFileWithContent(filepath, patch); err != nil { return "", err diff --git a/pkg/commands/git_commands/worktree.go b/pkg/commands/git_commands/worktree.go index 5a56379573e..78fdd703f48 100644 --- a/pkg/commands/git_commands/worktree.go +++ b/pkg/commands/git_commands/worktree.go @@ -6,6 +6,7 @@ import ( "io/fs" "log" "os" + "path/filepath" "github.com/jesseduffield/lazygit/pkg/commands/models" ) @@ -105,3 +106,38 @@ func CheckedOutByOtherWorktree(branch *models.Branch, worktrees []*models.Worktr return !IsCurrentWorktree(worktree.Path) } + +func 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 := 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/commands/git_commands/worktree_loader.go b/pkg/commands/git_commands/worktree_loader.go index 75b60d43327..a6561a78a34 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/` // then we return that path diff --git a/pkg/gui/controllers/helpers/refresh_helper.go b/pkg/gui/controllers/helpers/refresh_helper.go index f1e24eecce5..ffbd85a596d 100644 --- a/pkg/gui/controllers/helpers/refresh_helper.go +++ b/pkg/gui/controllers/helpers/refresh_helper.go @@ -637,7 +637,9 @@ func (self *RefreshHelper) refreshStatus() { linkedWorktreeName = self.worktreeHelper.GetLinkedWorktreeName() } - status := presentation.FormatStatus(currentBranch, linkedWorktreeName, workingTreeState, self.c.Tr) + repoName := git_commands.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/working_tree_helper.go b/pkg/gui/controllers/helpers/working_tree_helper.go index 22d1089c998..a0a0447d73a 100644 --- a/pkg/gui/controllers/helpers/working_tree_helper.go +++ b/pkg/gui/controllers/helpers/working_tree_helper.go @@ -4,11 +4,11 @@ import ( "fmt" "regexp" + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/types" - "github.com/jesseduffield/lazygit/pkg/utils" ) type IWorkingTreeHelper interface { @@ -203,7 +203,7 @@ func (self *WorkingTreeHelper) prepareFilesForCommit() error { } func (self *WorkingTreeHelper) commitPrefixConfigForRepo() *config.CommitPrefixConfig { - cfg, ok := self.c.UserConfig.Git.CommitPrefixes[utils.GetCurrentRepoName()] + cfg, ok := self.c.UserConfig.Git.CommitPrefixes[git_commands.GetCurrentRepoName()] if !ok { return nil } diff --git a/pkg/gui/controllers/status_controller.go b/pkg/gui/controllers/status_controller.go index 8661b99b3a0..d8e906f3b0c 100644 --- a/pkg/gui/controllers/status_controller.go +++ b/pkg/gui/controllers/status_controller.go @@ -6,12 +6,12 @@ import ( "strings" "github.com/jesseduffield/generics/slices" + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/types/enums" "github.com/jesseduffield/lazygit/pkg/constants" "github.com/jesseduffield/lazygit/pkg/gui/presentation" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/types" - "github.com/jesseduffield/lazygit/pkg/utils" ) type StatusController struct { @@ -108,7 +108,8 @@ func (self *StatusController) onClick() error { cx, _ := self.c.Views().Status.Cursor() upstreamStatus := presentation.BranchStatus(currentBranch, self.c.Tr) - repoName := utils.GetCurrentRepoName() + // TODO: support worktrees here + repoName := git_commands.GetCurrentRepoName() workingTreeState := self.c.Git().Status.WorkingTreeState() switch workingTreeState { case enums.REBASE_MODE_REBASING, enums.REBASE_MODE_MERGING: diff --git a/pkg/gui/presentation/status.go b/pkg/gui/presentation/status.go index 650303d44df..f70210c2760 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) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 2f33862e8c6..bfebc13f096 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -3,9 +3,7 @@ package utils import ( "encoding/json" "fmt" - "log" "os" - "path/filepath" "regexp" "runtime" "strconv" @@ -15,15 +13,6 @@ import ( "github.com/jesseduffield/gocui" ) -// GetCurrentRepoName gets the repo's base name -func GetCurrentRepoName() string { - pwd, err := os.Getwd() - if err != nil { - log.Fatalln(err.Error()) - } - return filepath.Base(pwd) -} - // GetProjectRoot returns the path to the root of the project. Only to be used // in testing contexts, as with binaries it's unlikely this path will exist on // the machine