1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-06 03:53:59 +02:00

Use forward-slashes on windows

We want to be using forward slashes everywhere internally, so if we get a path from windows
we should immediately convert it to use forward slashes.

I'm leaving out the recent repos list because that would require a migration
This commit is contained in:
Jesse Duffield 2023-07-29 13:15:58 +10:00
parent a77d24fdc6
commit b16bb409fc
2 changed files with 21 additions and 14 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
@ -77,6 +78,10 @@ func GetRepoPaths() (RepoPaths, error) {
return &RepoDirsImpl{}, errors.Errorf("failed to get current path: %v", err) return &RepoDirsImpl{}, errors.Errorf("failed to get current path: %v", err)
} }
// converting to forward slashes for the sake of windows (which uses backwards slashes). We want everything
// to have forward slashes internally
currentPath = filepath.ToSlash(currentPath)
worktreePath := currentPath worktreePath := currentPath
repoGitDirPath, repoPath, err := GetCurrentRepoGitDirPath(currentPath) repoGitDirPath, repoPath, err := GetCurrentRepoGitDirPath(currentPath)
if err != nil { if err != nil {
@ -86,7 +91,7 @@ func GetRepoPaths() (RepoPaths, error) {
if err != nil { if err != nil {
return &RepoDirsImpl{}, errors.Errorf("failed to get worktree git dir path: %v", err) return &RepoDirsImpl{}, errors.Errorf("failed to get worktree git dir path: %v", err)
} }
repoName := filepath.Base(repoPath) repoName := path.Base(repoPath)
return &RepoDirsImpl{ return &RepoDirsImpl{
currentPath: currentPath, currentPath: currentPath,
@ -103,7 +108,7 @@ func linkedWortkreePaths(repoGitDirPath string) []string {
result := []string{} result := []string{}
// For each directory in this path we're going to cat the `gitdir` file and append its contents to our result // 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. // That file points us to the `.git` file in the worktree.
worktreeGitDirsPath := filepath.Join(repoGitDirPath, "worktrees") worktreeGitDirsPath := path.Join(repoGitDirPath, "worktrees")
// ensure the directory exists // ensure the directory exists
_, err := os.Stat(worktreeGitDirsPath) _, err := os.Stat(worktreeGitDirsPath)
@ -111,7 +116,7 @@ func linkedWortkreePaths(repoGitDirPath string) []string {
return result return result
} }
_ = filepath.Walk(worktreeGitDirsPath, func(path string, info fs.FileInfo, err error) error { _ = filepath.Walk(worktreeGitDirsPath, func(currPath string, info fs.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
@ -120,7 +125,7 @@ func linkedWortkreePaths(repoGitDirPath string) []string {
return nil return nil
} }
gitDirPath := filepath.Join(path, "gitdir") gitDirPath := path.Join(currPath, "gitdir")
gitDirBytes, err := os.ReadFile(gitDirPath) gitDirBytes, err := os.ReadFile(gitDirPath)
if err != nil { if err != nil {
// ignoring error // ignoring error
@ -128,7 +133,7 @@ func linkedWortkreePaths(repoGitDirPath string) []string {
} }
trimmedGitDir := strings.TrimSpace(string(gitDirBytes)) trimmedGitDir := strings.TrimSpace(string(gitDirBytes))
// removing the .git part // removing the .git part
worktreeDir := filepath.Dir(trimmedGitDir) worktreeDir := path.Dir(trimmedGitDir)
result = append(result, worktreeDir) result = append(result, worktreeDir)
return nil return nil
}) })
@ -142,7 +147,7 @@ func linkedWortkreePaths(repoGitDirPath string) []string {
func worktreeGitDirPath(worktreePath string) (string, error) { func worktreeGitDirPath(worktreePath string) (string, error) {
// if .git is a file, we're in a linked worktree, otherwise we're in // if .git is a file, we're in a linked worktree, otherwise we're in
// the main worktree // the main worktree
dotGitPath := filepath.Join(worktreePath, ".git") dotGitPath := path.Join(worktreePath, ".git")
gitFileInfo, err := os.Stat(dotGitPath) gitFileInfo, err := os.Stat(dotGitPath)
if err != nil { if err != nil {
return "", err return "", err
@ -156,7 +161,7 @@ func worktreeGitDirPath(worktreePath string) (string, error) {
} }
func linkedWorktreeGitDirPath(worktreePath string) (string, error) { func linkedWorktreeGitDirPath(worktreePath string) (string, error) {
dotGitPath := filepath.Join(worktreePath, ".git") dotGitPath := path.Join(worktreePath, ".git")
gitFileContents, err := os.ReadFile(dotGitPath) gitFileContents, err := os.ReadFile(dotGitPath)
if err != nil { if err != nil {
return "", err return "", err
@ -180,7 +185,7 @@ func GetCurrentRepoGitDirPath(currentPath string) (string, string, error) {
if env.GetGitDirEnv() != "" { if env.GetGitDirEnv() != "" {
unresolvedGitPath = env.GetGitDirEnv() unresolvedGitPath = env.GetGitDirEnv()
} else { } else {
unresolvedGitPath = filepath.Join(currentPath, ".git") unresolvedGitPath = path.Join(currentPath, ".git")
} }
gitPath, err := resolveSymlink(unresolvedGitPath) gitPath, err := resolveSymlink(unresolvedGitPath)
@ -196,7 +201,7 @@ func GetCurrentRepoGitDirPath(currentPath string) (string, string, error) {
if gitFileInfo.IsDir() { if gitFileInfo.IsDir() {
// must be in the main worktree // must be in the main worktree
return gitPath, filepath.Dir(gitPath), nil return gitPath, path.Dir(gitPath), nil
} }
// either in a submodule, or worktree // either in a submodule, or worktree
@ -206,18 +211,18 @@ func GetCurrentRepoGitDirPath(currentPath string) (string, string, error) {
} }
// confirm whether the next directory up is the worktrees/submodules directory // confirm whether the next directory up is the worktrees/submodules directory
parent := filepath.Dir(worktreeGitPath) parent := path.Dir(worktreeGitPath)
if filepath.Base(parent) != "worktrees" && filepath.Base(parent) != "modules" { if path.Base(parent) != "worktrees" && path.Base(parent) != "modules" {
return "", "", errors.Errorf("could not find git dir for %s", currentPath) return "", "", errors.Errorf("could not find git dir for %s", currentPath)
} }
// if it's a submodule, we treat it as its own repo // if it's a submodule, we treat it as its own repo
if filepath.Base(parent) == "modules" { if path.Base(parent) == "modules" {
return worktreeGitPath, currentPath, nil return worktreeGitPath, currentPath, nil
} }
gitDirPath := filepath.Dir(parent) gitDirPath := path.Dir(parent)
return gitDirPath, filepath.Dir(gitDirPath), nil return gitDirPath, path.Dir(gitDirPath), nil
} }
// takes a path containing a symlink and returns the true path // takes a path containing a symlink and returns the true path

View File

@ -28,6 +28,8 @@ func (gui *Gui) updateRecentRepoList() error {
} }
known, recentRepos := newRecentReposList(recentRepos, currentRepo) known, recentRepos := newRecentReposList(recentRepos, currentRepo)
gui.IsNewRepo = known gui.IsNewRepo = known
// TODO: migrate this file to use forward slashes on all OSes for consistency
// (windows uses backslashes at the moment)
gui.c.GetAppState().RecentRepos = recentRepos gui.c.GetAppState().RecentRepos = recentRepos
return gui.c.SaveAppState() return gui.c.SaveAppState()
} }