From bb62444fe9af776d18cfe7d385da0ca3af4fc842 Mon Sep 17 00:00:00 2001
From: aris-kimi <aris_kimi@hotmail.com>
Date: Mon, 3 Mar 2025 01:13:44 +0200
Subject: [PATCH] Fix a git2 related crash when closing KiCad

Fixes: https://gitlab.com/kicad/code/kicad/-/issues/20202
---
 common/git/kicad_git_common.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/common/git/kicad_git_common.cpp b/common/git/kicad_git_common.cpp
index 4900bcc2b6..2c354361cb 100644
--- a/common/git/kicad_git_common.cpp
+++ b/common/git/kicad_git_common.cpp
@@ -235,6 +235,9 @@ std::pair<std::set<wxString>,std::set<wxString>> KIGIT_COMMON::GetDifferentFiles
         std::set<wxString> modified_set;
         git_revwalk* walker = nullptr;
 
+        if( !m_repo )
+            return modified_set;
+
         if( git_revwalk_new( &walker, m_repo ) != GIT_OK )
         {
             wxLogTrace( traceGit, "Failed to create revwalker" );
@@ -304,6 +307,9 @@ std::pair<std::set<wxString>,std::set<wxString>> KIGIT_COMMON::GetDifferentFiles
             git_diff_options diff_opts;
             git_diff_init_options( &diff_opts, GIT_DIFF_OPTIONS_VERSION );
 
+            if( !diff_opts.flags || !m_repo || !parent_tree || !tree )
+                continue;
+
             if( git_diff_tree_to_tree( &diff, m_repo, parent_tree, tree, &diff_opts ) == GIT_OK )
             {
                 size_t num_deltas = git_diff_num_deltas( diff );