1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-15 00:15:32 +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 a06a5cadee
commit b73efb2c22
8 changed files with 54 additions and 24 deletions

View File

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