From 1641878af7312d59c93b72e37d42fa362d3a998d Mon Sep 17 00:00:00 2001
From: Seth Hillbrand <seth@kipro-pcb.com>
Date: Wed, 22 Jan 2025 14:16:44 -0800
Subject: [PATCH] Don't create a repository before the dialog

The user might cancel a dialog, leaving a broken repo.  We use a temp
repo to test the connection

Fixes https://gitlab.com/kicad/code/kicad/-/issues/16263
---
 kicad/project_tree_pane.cpp | 48 ++++++++++++++++++-------------------
 1 file changed, 23 insertions(+), 25 deletions(-)

diff --git a/kicad/project_tree_pane.cpp b/kicad/project_tree_pane.cpp
index df58f20930..02e4bb1c53 100644
--- a/kicad/project_tree_pane.cpp
+++ b/kicad/project_tree_pane.cpp
@@ -1596,38 +1596,36 @@ void PROJECT_TREE_PANE::onGitInitializeProject( wxCommandEvent& aEvent )
         git_repository_free( repo );
         return;
     }
-    else
-    {
-        // Directory is not a git repository
-        error = git_repository_init( &repo, dir.mb_str(), 0 );
 
-        if( error != 0 )
-        {
-            git_repository_free( repo );
-
-            if( m_gitLastError != git_error_last()->klass )
-            {
-                m_gitLastError = git_error_last()->klass;
-                DisplayErrorMessage( m_parent, _( "Failed to initialize git project." ),
-                                     git_error_last()->message );
-            }
-
-            return;
-        }
-        else
-        {
-            m_TreeProject->SetGitRepo( repo );
-            m_gitLastError = GIT_ERROR_NONE;
-        }
-    }
-
-    DIALOG_GIT_REPOSITORY dlg( wxGetTopLevelParent( this ), repo );
+    DIALOG_GIT_REPOSITORY dlg( wxGetTopLevelParent( this ), nullptr );
 
     dlg.SetTitle( _( "Set default remote" ) );
 
     if( dlg.ShowModal() != wxID_OK )
         return;
 
+   // Directory is not a git repository
+    error = git_repository_init( &repo, dir.mb_str(), 0 );
+
+    if( error != 0 )
+    {
+        git_repository_free( repo );
+
+        if( m_gitLastError != git_error_last()->klass )
+        {
+            m_gitLastError = git_error_last()->klass;
+            DisplayErrorMessage( m_parent, _( "Failed to initialize git project." ),
+                                    git_error_last()->message );
+        }
+
+        return;
+    }
+    else
+    {
+        m_TreeProject->SetGitRepo( repo );
+        m_gitLastError = GIT_ERROR_NONE;
+    }
+
     //Set up the git remote
 
     m_TreeProject->GitCommon()->SetConnType( dlg.GetRepoType() );