mirror of
https://github.com/jesseduffield/lazygit.git
synced 2024-12-12 11:15:00 +02:00
4c5b1574f1
There are quite a few paths you might want to get e.g. the repo's path, the worktree's path, the repo's git dir path, the worktree's git dir path. I want these all obtained once and then used when needed rather than having to have IO whenever we need them. This is not so much about reducing time spent on IO as it is about not having to care about errors every time we want a path.
236 lines
6.6 KiB
Go
236 lines
6.6 KiB
Go
package git_commands
|
|
|
|
import (
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/go-errors/errors"
|
|
"github.com/jesseduffield/lazygit/pkg/env"
|
|
"github.com/samber/lo"
|
|
)
|
|
|
|
type RepoPaths interface {
|
|
// Current working directory of the program. Currently, this will always
|
|
// be the same as WorktreePath(), but in future we may support running
|
|
// lazygit from inside a subdirectory of the worktree.
|
|
CurrentPath() string
|
|
// Path to the current worktree. If we're in the main worktree, this will
|
|
// be the same as RepoPath()
|
|
WorktreePath() string
|
|
// Path of the worktree's git dir.
|
|
// If we're in the main worktree, this will be the .git dir under the RepoPath().
|
|
// If we're in a linked worktree, it will be the directory pointed at by the worktree's .git file
|
|
WorktreeGitDirPath() string
|
|
// Path of the repo. If we're in a the main worktree, this will be the same as WorktreePath()
|
|
// If we're in a bare repo, it will be the parent folder of the bare repo
|
|
RepoPath() string
|
|
// path of the git-dir for the repo.
|
|
// If this is a bare repo, it will be the location of the bare repo
|
|
// If this is a non-bare repo, it will be the location of the .git dir in
|
|
// the main worktree.
|
|
RepoGitDirPath() string
|
|
// Name of the repo. Basename of the folder containing the repo.
|
|
RepoName() string
|
|
}
|
|
|
|
type RepoDirsImpl struct {
|
|
currentPath string
|
|
worktreePath string
|
|
worktreeGitDirPath string
|
|
repoPath string
|
|
repoGitDirPath string
|
|
repoName string
|
|
}
|
|
|
|
var _ RepoPaths = &RepoDirsImpl{}
|
|
|
|
func (self *RepoDirsImpl) CurrentPath() string {
|
|
return self.currentPath
|
|
}
|
|
|
|
func (self *RepoDirsImpl) WorktreePath() string {
|
|
return self.worktreePath
|
|
}
|
|
|
|
func (self *RepoDirsImpl) WorktreeGitDirPath() string {
|
|
return self.worktreeGitDirPath
|
|
}
|
|
|
|
func (self *RepoDirsImpl) RepoPath() string {
|
|
return self.repoPath
|
|
}
|
|
|
|
func (self *RepoDirsImpl) RepoGitDirPath() string {
|
|
return self.repoGitDirPath
|
|
}
|
|
|
|
func (self *RepoDirsImpl) RepoName() string {
|
|
return self.repoName
|
|
}
|
|
|
|
func GetRepoPaths() (RepoPaths, error) {
|
|
currentPath, err := os.Getwd()
|
|
if err != nil {
|
|
return &RepoDirsImpl{}, errors.Errorf("failed to get current path: %v", err)
|
|
}
|
|
|
|
worktreePath := currentPath
|
|
repoGitDirPath, repoPath, err := GetCurrentRepoGitDirPath(currentPath)
|
|
if err != nil {
|
|
return &RepoDirsImpl{}, errors.Errorf("failed to get repo git dir path: %v", err)
|
|
}
|
|
worktreeGitDirPath, err := worktreeGitDirPath(currentPath)
|
|
if err != nil {
|
|
return &RepoDirsImpl{}, errors.Errorf("failed to get worktree git dir path: %v", err)
|
|
}
|
|
repoName := filepath.Base(repoPath)
|
|
|
|
return &RepoDirsImpl{
|
|
currentPath: currentPath,
|
|
worktreePath: worktreePath,
|
|
worktreeGitDirPath: worktreeGitDirPath,
|
|
repoPath: repoPath,
|
|
repoGitDirPath: repoGitDirPath,
|
|
repoName: repoName,
|
|
}, nil
|
|
}
|
|
|
|
// Returns the paths of linked worktrees
|
|
func linkedWortkreePaths(repoGitDirPath string) []string {
|
|
result := []string{}
|
|
// For each directory in this path we're going to cat the `gitdir` file and append its contents to our result
|
|
// That file points us to the `.git` file in the worktree.
|
|
worktreeGitDirsPath := filepath.Join(repoGitDirPath, "worktrees")
|
|
|
|
// ensure the directory exists
|
|
_, err := os.Stat(worktreeGitDirsPath)
|
|
if err != nil {
|
|
return result
|
|
}
|
|
|
|
_ = filepath.Walk(worktreeGitDirsPath, func(path string, info fs.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
return nil
|
|
}
|
|
|
|
gitDirPath := filepath.Join(path, "gitdir")
|
|
gitDirBytes, err := os.ReadFile(gitDirPath)
|
|
if err != nil {
|
|
// ignoring error
|
|
return nil
|
|
}
|
|
trimmedGitDir := strings.TrimSpace(string(gitDirBytes))
|
|
// removing the .git part
|
|
worktreeDir := filepath.Dir(trimmedGitDir)
|
|
result = append(result, worktreeDir)
|
|
return nil
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
// Returns the path of the git-dir for the worktree. For linked worktrees, the worktree has
|
|
// a .git file that points to the git-dir (which itself lives in the git-dir
|
|
// of the repo)
|
|
func worktreeGitDirPath(worktreePath string) (string, error) {
|
|
// if .git is a file, we're in a linked worktree, otherwise we're in
|
|
// the main worktree
|
|
dotGitPath := filepath.Join(worktreePath, ".git")
|
|
gitFileInfo, err := os.Stat(dotGitPath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if gitFileInfo.IsDir() {
|
|
return dotGitPath, nil
|
|
}
|
|
|
|
return linkedWorktreeGitDirPath(worktreePath)
|
|
}
|
|
|
|
func linkedWorktreeGitDirPath(worktreePath string) (string, error) {
|
|
dotGitPath := filepath.Join(worktreePath, ".git")
|
|
gitFileContents, err := os.ReadFile(dotGitPath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// The file will have `gitdir: /path/to/.git/worktrees/<worktree-name>`
|
|
gitDirLine := lo.Filter(strings.Split(string(gitFileContents), "\n"), func(line string, _ int) bool {
|
|
return strings.HasPrefix(line, "gitdir: ")
|
|
})
|
|
|
|
if len(gitDirLine) == 0 {
|
|
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", dotGitPath))
|
|
}
|
|
|
|
gitDir := strings.TrimPrefix(gitDirLine[0], "gitdir: ")
|
|
return gitDir, nil
|
|
}
|
|
|
|
func GetCurrentRepoGitDirPath(currentPath string) (string, string, error) {
|
|
var unresolvedGitPath string
|
|
if env.GetGitDirEnv() != "" {
|
|
unresolvedGitPath = env.GetGitDirEnv()
|
|
} else {
|
|
unresolvedGitPath = filepath.Join(currentPath, ".git")
|
|
}
|
|
|
|
gitPath, err := resolveSymlink(unresolvedGitPath)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
|
|
// check if .git is a file or a directory
|
|
gitFileInfo, err := os.Stat(gitPath)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
|
|
if gitFileInfo.IsDir() {
|
|
// must be in the main worktree
|
|
return gitPath, filepath.Dir(gitPath), nil
|
|
}
|
|
|
|
// either in a submodule, or worktree
|
|
worktreeGitPath, err := linkedWorktreeGitDirPath(currentPath)
|
|
if err != nil {
|
|
return "", "", errors.Errorf("could not find git dir for %s: %v", currentPath, err)
|
|
}
|
|
|
|
// confirm whether the next directory up is the worktrees/submodules directory
|
|
parent := filepath.Dir(worktreeGitPath)
|
|
if filepath.Base(parent) != "worktrees" && filepath.Base(parent) != "modules" {
|
|
return "", "", errors.Errorf("could not find git dir for %s", currentPath)
|
|
}
|
|
|
|
// if it's a submodule, we treat it as its own repo
|
|
if filepath.Base(parent) == "modules" {
|
|
return worktreeGitPath, currentPath, nil
|
|
}
|
|
|
|
gitDirPath := filepath.Dir(parent)
|
|
return gitDirPath, filepath.Dir(gitDirPath), nil
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
if l.Mode()&os.ModeSymlink == 0 {
|
|
return path, nil
|
|
}
|
|
|
|
return filepath.EvalSymlinks(path)
|
|
}
|