Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/64121.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent `_pygit2.GitError: error loading known_hosts` with certain pygit2/libgit2 versions.
19 changes: 12 additions & 7 deletions salt/utils/gitfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@

with warnings.catch_warnings():
warnings.simplefilter("ignore")
if "HOME" not in os.environ:
# Make sure $HOME env variable is set before importing pygit2 to prevent
# _pygit2.GitError: error loading known_hosts in some libgit2 versions.
# The internal "git_sysdir__dirs" from libgit2, is initializated
# when importing pygit2. The $HOME env must be present to allow libgit2
# guessing function to successfully set the homedir in the initializated
# libgit2 stack.
# https://github.com/saltstack/salt/issues/64121
os.environ["HOME"] = os.path.expanduser("~")
import pygit2
PYGIT2_VERSION = Version(pygit2.__version__)
LIBGIT2_VERSION = Version(pygit2.LIBGIT2_VERSION)
Expand Down Expand Up @@ -2012,13 +2021,9 @@ def init_remote(self):
"""
# https://github.com/libgit2/pygit2/issues/339
# https://github.com/libgit2/libgit2/issues/2122
# https://github.com/saltstack/salt/issues/64121
home = os.path.expanduser("~")
if "HOME" not in os.environ:
# Make sure $HOME env variable is set to prevent
# _pygit2.GitError: error loading known_hosts in some libgit2 versions.
os.environ["HOME"] = home
pygit2.settings.search_path[pygit2.GIT_CONFIG_LEVEL_GLOBAL] = home
pygit2.settings.search_path[pygit2.GIT_CONFIG_LEVEL_GLOBAL] = (
os.path.expanduser("~")
)
new = False
if not os.listdir(self._cachedir):
# Repo cachedir is empty, initialize a new repo there
Expand Down
3 changes: 2 additions & 1 deletion tests/pytests/unit/utils/test_gitfs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib
import os
import time

Expand Down Expand Up @@ -264,7 +265,7 @@ def test_checkout_pygit2_with_home_env_unset(_prepare_provider):
provider.credentials = None
with patched_environ(__cleanup__=["HOME"]):
assert "HOME" not in os.environ
provider.init_remote()
importlib.reload(salt.utils.gitfs)
assert "HOME" in os.environ


Expand Down