diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-27 21:52:24 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-30 18:35:23 +1000 |
commit | 2b24c15938c5ceb29a9b2d525aa1318b8bc8a87b (patch) | |
tree | 23727dd77a11b9ec456344dc2c482cb9879c8849 /pkg/commands | |
parent | e874f94cf8fb8928110a0165852a8967fa823a5c (diff) |
Add test for opening lazygit in the worktree of a bare repo
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/git.go | 60 | ||||
-rw-r--r-- | pkg/commands/git_commands/worktree_loader.go | 5 |
2 files changed, 38 insertions, 27 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index c2c53948a..3f7e823e7 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -1,6 +1,7 @@ package commands import ( + "fmt" "os" "path/filepath" "strings" @@ -66,13 +67,16 @@ func NewGitCommand( return nil, err } - repo, err := setupRepository(gogit.PlainOpenWithOptions, gogit.PlainOpenOptions{DetectDotGit: false, EnableDotGitCommonDir: true}, cmn.Tr.GitconfigParseErr) + dotGitDir, err := findDotGitDir(os.Stat, os.ReadFile) if err != nil { return nil, err } - dotGitDir, err := findDotGitDir(os.Stat, os.ReadFile) + repository, err := gogit.PlainOpenWithOptions(dotGitDir, &gogit.PlainOpenOptions{DetectDotGit: false, EnableDotGitCommonDir: true}) if err != nil { + if strings.Contains(err.Error(), `unquoted '\' must be followed by new line`) { + return nil, errors.New(cmn.Tr.GitconfigParseErr) + } return nil, err } @@ -82,7 +86,7 @@ func NewGitCommand( osCommand, gitConfig, dotGitDir, - repo, + repository, syncMutex, ), nil } @@ -218,8 +222,8 @@ func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir f } } -// resolvePath takes a path containing a symlink and returns the true path -func resolvePath(path string) (string, error) { +// takes a path containing a symlink and returns the true path +func resolveSymlink(path string) (string, error) { l, err := os.Lstat(path) if err != nil { return "", err @@ -232,27 +236,17 @@ func resolvePath(path string) (string, error) { return filepath.EvalSymlinks(path) } -func setupRepository(openGitRepository func(string, *gogit.PlainOpenOptions) (*gogit.Repository, error), options gogit.PlainOpenOptions, gitConfigParseErrorStr string) (*gogit.Repository, error) { - unresolvedPath := env.GetGitDirEnv() - if unresolvedPath == "" { - var err error - unresolvedPath, err = os.Getwd() - if err != nil { - return nil, err - } - } - - path, err := resolvePath(unresolvedPath) - if err != nil { - return nil, err - } - +func setupRepository( + openGitRepository func(string, *gogit.PlainOpenOptions) (*gogit.Repository, error), + options gogit.PlainOpenOptions, + gitConfigParseErrorStr string, + path string, +) (*gogit.Repository, error) { repository, err := openGitRepository(path, &options) if err != nil { if strings.Contains(err.Error(), `unquoted '\' must be followed by new line`) { return nil, errors.New(gitConfigParseErrorStr) } - return nil, err } @@ -260,26 +254,38 @@ func setupRepository(openGitRepository func(string, *gogit.PlainOpenOptions) (*g } func findDotGitDir(stat func(string) (os.FileInfo, error), readFile func(filename string) ([]byte, error)) (string, error) { - if env.GetGitDirEnv() != "" { - return env.GetGitDirEnv(), nil + unresolvedPath := env.GetGitDirEnv() + if unresolvedPath == "" { + var err error + unresolvedPath, err = os.Getwd() + if err != nil { + return "", err + } + unresolvedPath = filepath.Join(unresolvedPath, ".git") } - f, err := stat(".git") + path, err := resolveSymlink(unresolvedPath) + if err != nil { + return "", err + } + + f, err := stat(path) if err != nil { return "", err } if f.IsDir() { - return ".git", nil + return path, nil } - fileBytes, err := readFile(".git") + fileBytes, err := readFile(path) if err != nil { return "", err } + fileContent := string(fileBytes) if !strings.HasPrefix(fileContent, "gitdir: ") { - return "", errors.New(".git is a file which suggests we are in a submodule or a worktree but the file's contents do not contain a gitdir pointing to the actual .git directory") + return "", errors.New(fmt.Sprintf("%s is a file which suggests we are in a submodule or a worktree but the file's contents do not contain a gitdir pointing to the actual .git directory", path)) } return strings.TrimSpace(strings.TrimPrefix(fileContent, "gitdir: ")), nil } diff --git a/pkg/commands/git_commands/worktree_loader.go b/pkg/commands/git_commands/worktree_loader.go index 6c73eaa13..bbd8367f3 100644 --- a/pkg/commands/git_commands/worktree_loader.go +++ b/pkg/commands/git_commands/worktree_loader.go @@ -46,6 +46,11 @@ func (self *WorktreeLoader) GetWorktrees() ([]*models.Worktree, error) { current = nil continue } + if splitLine == "bare" { + current = nil + continue + } + if strings.HasPrefix(splitLine, "worktree ") { path := strings.SplitN(splitLine, " ", 2)[1] isMain := path == currentRepoPath |