File 3006.0-prevent-_pygit2.giterror-error-loading-known_.patch of Package salt

From 40a57afc65e71835127a437248ed655404cff0e8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pablo=20Su=C3=A1rez=20Hern=C3=A1ndez?=
 <psuarezhernandez@suse.com>
Date: Tue, 27 Jun 2023 11:24:39 +0100
Subject: [PATCH] 3006.0: Prevent _pygit2.GitError: error loading
 known_hosts when $HOME is not set (bsc#1210994) (#588)

* Prevent _pygit2.GitError: error loading known_hosts when $HOME is not set

* Add unit test to cover case of unset home
---
 salt/utils/gitfs.py            |  5 +++++
 tests/unit/utils/test_gitfs.py | 14 ++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/salt/utils/gitfs.py b/salt/utils/gitfs.py
index cc9895d8ab..38e84f38aa 100644
--- a/salt/utils/gitfs.py
+++ b/salt/utils/gitfs.py
@@ -34,6 +34,7 @@ import salt.utils.stringutils
 import salt.utils.url
 import salt.utils.user
 import salt.utils.versions
+import salt.syspaths
 from salt.config import DEFAULT_MASTER_OPTS as _DEFAULT_MASTER_OPTS
 from salt.exceptions import FileserverConfigError, GitLockError, get_error_message
 from salt.utils.event import tagify
@@ -1867,6 +1868,10 @@ class Pygit2(GitProvider):
             # pruning only available in pygit2 >= 0.26.2
             pass
         try:
+            # Make sure $HOME env variable is set to prevent
+            # _pygit2.GitError: error loading known_hosts in some libgit2 versions.
+            if "HOME" not in os.environ:
+                os.environ["HOME"] = salt.syspaths.HOME_DIR
             fetch_results = origin.fetch(**fetch_kwargs)
         except GitError as exc:  # pylint: disable=broad-except
             exc_str = get_error_message(exc).lower()
diff --git a/tests/unit/utils/test_gitfs.py b/tests/unit/utils/test_gitfs.py
index b99da3ef91..7c400b69af 100644
--- a/tests/unit/utils/test_gitfs.py
+++ b/tests/unit/utils/test_gitfs.py
@@ -14,6 +14,7 @@ import salt.utils.gitfs
 import salt.utils.platform
 import tests.support.paths
 from salt.exceptions import FileserverConfigError
+from tests.support.helpers import patched_environ
 from tests.support.mixins import AdaptedConfigurationTestCaseMixin
 from tests.support.mock import MagicMock, patch
 from tests.support.unit import TestCase
@@ -335,3 +336,16 @@ class TestPygit2(TestCase):
         self.assertIn(provider.cachedir, provider.checkout())
         provider.branch = "does_not_exist"
         self.assertIsNone(provider.checkout())
+
+    def test_checkout_with_home_env_unset(self):
+        remote = os.path.join(tests.support.paths.TMP, "pygit2-repo")
+        cache = os.path.join(tests.support.paths.TMP, "pygit2-repo-cache")
+        self._prepare_remote_repository(remote)
+        provider = self._prepare_cache_repository(remote, cache)
+        provider.remotecallbacks = None
+        provider.credentials = None
+        with patched_environ(__cleanup__=["HOME"]):
+            self.assertTrue("HOME" not in os.environ)
+            provider.init_remote()
+            provider.fetch()
+            self.assertTrue("HOME" in os.environ)
-- 
2.41.0


openSUSE Build Service is sponsored by