mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-12 04:45:47 +02:00
Centralise logic for obtaining repo paths
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.
This commit is contained in:
parent
de57cfd6ff
commit
4c5b1574f1
22 changed files with 347 additions and 250 deletions
|
@ -1,11 +1,7 @@
|
|||
package git_commands
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||
)
|
||||
|
@ -76,89 +72,3 @@ func CheckedOutByOtherWorktree(branch *models.Branch, worktrees []*models.Worktr
|
|||
|
||||
return !worktree.IsCurrent
|
||||
}
|
||||
|
||||
// If in a non-bare repo, this returns the path of the main worktree
|
||||
// TODO: see if this works with a bare repo.
|
||||
func GetCurrentRepoPath() 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 {
|
||||
// fallback
|
||||
return currentPath()
|
||||
}
|
||||
|
||||
if gitFileInfo.IsDir() {
|
||||
// must be in the main worktree
|
||||
return currentPath()
|
||||
}
|
||||
|
||||
// either in a submodule, a worktree, or a bare repo
|
||||
worktreeGitPath, ok := LinkedWorktreeGitPath(pwd)
|
||||
if !ok {
|
||||
// fallback
|
||||
return currentPath()
|
||||
}
|
||||
|
||||
// confirm whether the next directory up is the 'worktrees' directory
|
||||
parent := filepath.Dir(worktreeGitPath)
|
||||
if filepath.Base(parent) != "worktrees" {
|
||||
// fallback
|
||||
return currentPath()
|
||||
}
|
||||
|
||||
// now we just jump up two more directories to get the repo name
|
||||
return filepath.Dir(filepath.Dir(parent))
|
||||
}
|
||||
|
||||
func GetCurrentRepoName() string {
|
||||
return filepath.Base(GetCurrentRepoPath())
|
||||
}
|
||||
|
||||
func currentPath() string {
|
||||
pwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Fatalln(err.Error())
|
||||
}
|
||||
return pwd
|
||||
}
|
||||
|
||||
func linkedWortkreePaths() []string {
|
||||
// first we need to get the repo dir
|
||||
repoPath := GetCurrentRepoPath()
|
||||
result := []string{}
|
||||
worktreePath := filepath.Join(repoPath, ".git", "worktrees")
|
||||
// for each directory in this path we're going to cat the `gitdir` file and append its contents to our result
|
||||
|
||||
// ensure the directory exists
|
||||
_, err := os.Stat(worktreePath)
|
||||
if err != nil {
|
||||
return result
|
||||
}
|
||||
|
||||
err = filepath.Walk(worktreePath, func(path string, info fs.FileInfo, err error) error {
|
||||
if info.IsDir() {
|
||||
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
|
||||
})
|
||||
if err != nil {
|
||||
return result
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue