Repository: jesseduffield/lazygit (language: go)

Issue:
Crash when I try to create a new git repository
**Describe the bug**
I was trying to create a new git repository in a empty folder, I've launched lazygit and at the question create new git repository I've answered yes, this is the error that the terminal has given me:

host> lazygit.exe
Not in a git repository. Create a new git repository? (y/n): y
2022/05/12 01:23:32 An error occurred! Please create an issue at: https://github.com/jesseduffield/lazygit/issues

*errors.errorString Must open lazygit in a git repository
/home/runner/work/lazygit/lazygit/pkg/commands/git.go:187 (0x132e08e)
/home/runner/work/lazygit/lazygit/pkg/commands/git.go:60 (0x132ca45)
/home/runner/work/lazygit/lazygit/pkg/gui/gui.go:480 (0x1393eba)
/home/runner/work/lazygit/lazygit/pkg/app/app.go:138 (0x13feb7d)
/home/runner/work/lazygit/lazygit/main.go:135 (0x140145d)
/opt/hostedtoolcache/go/1.16.15/x64/src/runtime/proc.go:225 (0xd9bfd6)
/opt/hostedtoolcache/go/1.16.15/x64/src/runtime/asm_amd64.s:1371 (0xdcd6a1)

**To Reproduce**
Steps to reproduce the behavior:
1. Go to an empty folder
2. Launch lazygit
3. Answer yes to question Create a new git repository

**Expected behavior**
It should have been created a new git repository

**Screenshots**
-

**Desktop (please complete the following information):**
 - OS: Windows 11
 - Lazygit Version commit=7be25a105d389a262ef040133a4270f2f745d255, build date=2022-03-17T07:07:02Z, build source=binaryRelease, version=0.34, os=windows, arch=amd64

**Additional context**
Add any other context about the problem here.



Discussion / hints:
I can reproduce it on `windows 10`, will look into it. It goes to a recent repo after saying `y` for some reason.

Fix patch:
diff --git a/pkg/app/app.go b/pkg/app/app.go
index f12c0f08ad5..c6dc12d7bde 100644
--- a/pkg/app/app.go
+++ b/pkg/app/app.go
@@ -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
@@ -172,7 +176,7 @@ func (app *App) setupRepo() (bool, error) {
 			// Offer to initialize a new repository in current directory.
 			fmt.Print(app.Tr.CreateRepo)
 			response, _ := bufio.NewReader(os.Stdin).ReadString('\n')
-			if strings.Trim(response, " \n") != "y" {
+			if strings.Trim(response, " \r\n") != "y" {
 				shouldInitRepo = false
 			}
 		} else if notARepository == "skip" {
@@ -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
 					}
 				}
diff --git a/pkg/gui/recent_repos_panel.go b/pkg/gui/recent_repos_panel.go
index dc2f9f17f61..22ec045054d 100644
--- a/pkg/gui/recent_repos_panel.go
+++ b/pkg/gui/recent_repos_panel.go
@@ -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


Test patch: