Skip to content

Commit

Permalink
Merge pull request #74 from akaihola/git-dubious-ownership
Browse files Browse the repository at this point in the history
Avoid Git's dubious ownership error
  • Loading branch information
akaihola authored Aug 9, 2024
2 parents bf51db2 + e95b765 commit 25b2013
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Added

Fixed
-----
- When running Git with a clean environment, keep all environment variables and just
override the language.


2.0.0_ - 2024-07-31
Expand Down
12 changes: 5 additions & 7 deletions src/darkgraylib/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

from darkgraylib.utils import GIT_DATEFORMAT, TextDocument


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -235,15 +234,14 @@ def _with_common_ancestor(cls, rev1: str, rev2: str, cwd: Path) -> "RevisionRang

@lru_cache(maxsize=1)
def make_git_env() -> Dict[str, str]:
"""Create custom minimal environment variables to use when invoking Git
"""Create custom environment variables to use when invoking Git.
This makes sure that
- Git always runs in English
- ``$PATH`` is preserved (essential on NixOS)
- the environment is otherwise cleared
This makes sure that:
- Git always runs in English (``LC_ALL`` is set to ``C.UTF-8``)
- The existing environment is preserved
"""
return {"LC_ALL": "C", "PATH": os.environ["PATH"]}
return {"LC_ALL": "C.UTF-8", **os.environ}


def git_check_output_lines(
Expand Down
22 changes: 21 additions & 1 deletion src/darkgraylib/tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def git_call(cmd, encoding=None):
cwd=str(Path("/path")),
encoding=encoding,
stderr=PIPE,
env={"LC_ALL": "C", "PATH": os.environ["PATH"]},
env=ANY,
)


Expand Down Expand Up @@ -453,3 +453,23 @@ def test_git_get_root_not_found(tmp_path, path):
root = git.git_get_root(tmp_path / path)

assert root is None


def test_git_output_in_english(git_repo, monkeypatch):
"""Git output is in English despite German language environment."""
# Set up German language environment
german_env = {
"LC_ALL": "de_DE.UTF-8",
"LC_CTYPE": "de_DE.UTF-8",
"LC_MESSAGES": "de_DE.UTF-8",
"LANG": "de_DE.UTF-8",
}
for key, value in german_env.items():
monkeypatch.setenv(key, value)

# Run a Git command and check its output
output = git.git_check_output_lines(["status"], git_repo.root)

# Verify that the output is in English
assert "No commits yet" in output
assert "Noch keine Commits" not in output

0 comments on commit 25b2013

Please sign in to comment.