1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-24 08:52:21 +02:00

Validate recent repo before blindly opening it

This commit is contained in:
Luka Markušić 2022-06-09 15:57:54 +02:00 committed by Jesse Duffield
parent 658a6b239b
commit a955dbcfd7
2 changed files with 13 additions and 9 deletions

View File

@ -145,6 +145,11 @@ func isGitVersionValid(versionStr string) bool {
return true
}
func isDirectoryAGitRepository(dir string) (bool, error) {
info, err := os.Stat(filepath.Join(dir, ".git"))
return info != nil && info.IsDir(), err
}
func (app *App) setupRepo() (bool, error) {
if err := app.validateGitVersion(); err != nil {
return false, err
@ -161,9 +166,8 @@ func (app *App) setupRepo() (bool, error) {
if err != nil {
return false, err
}
info, _ := os.Stat(filepath.Join(cwd, ".git"))
if info != nil && info.IsDir() {
return false, err // Current directory appears to be a git repository.
if isRepo, err := isDirectoryAGitRepository(cwd); isRepo {
return false, err
}
shouldInitRepo := true
@ -181,12 +185,9 @@ func (app *App) setupRepo() (bool, error) {
if !shouldInitRepo {
// check if we have a recent repo we can open
recentRepos := app.Config.GetAppState().RecentRepos
if len(recentRepos) > 0 {
var err error
// try opening each repo in turn, in case any have been deleted
for _, repoDir := range recentRepos {
if err = os.Chdir(repoDir); err == nil {
for _, repoDir := range app.Config.GetAppState().RecentRepos {
if isRepo, _ := isDirectoryAGitRepository(repoDir); isRepo {
if err := os.Chdir(repoDir); err == nil {
return true, nil
}
}

View File

@ -110,6 +110,9 @@ func newRecentReposList(recentRepos []string, currentRepo string) (bool, []strin
newRepos := []string{currentRepo}
for _, repo := range recentRepos {
if repo != currentRepo {
if _, err := os.Stat(repo); err != nil {
continue
}
newRepos = append(newRepos, repo)
} else {
isNew = false