Skip to content

Commit

Permalink
Fix failed to stat '/root/.gitconfig' issue on gitfs (bsc#1230944) (b…
Browse files Browse the repository at this point in the history
…sc#1234881) (#699)

* Fix failed to stat '/root/.gitconfig' issue on gitfs (bsc#1230944) (bsc#1234881)

This commit ensures the right HOME value is set during Pygit2 remote
initialization, otherwise there are chances that it gets a wrong value
depending on the execution stack.

* Add changelog entry file

* Add test_checkout_pygit2_with_home_env_unset unit test
  • Loading branch information
meaksh authored Jan 29, 2025
1 parent 40a7163 commit 0ef67b3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/64121.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure the right HOME environment value is set during Pygit2 remote initialization.
9 changes: 5 additions & 4 deletions salt/utils/gitfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1889,7 +1889,12 @@ 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
new = False
if not os.listdir(self._cachedir):
Expand Down Expand Up @@ -1994,10 +1999,6 @@ def _fetch(self):
# 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()
Expand Down
1 change: 0 additions & 1 deletion tests/pytests/unit/utils/test_gitfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ def test_checkout_pygit2_with_home_env_unset(_prepare_provider):
with patched_environ(__cleanup__=["HOME"]):
assert "HOME" not in os.environ
provider.init_remote()
provider.fetch()
assert "HOME" in os.environ


Expand Down

0 comments on commit 0ef67b3

Please sign in to comment.