From df74d7252753a5d35e64d30689b6d3fc7b7693e2 Mon Sep 17 00:00:00 2001 From: "Axel H." Date: Sat, 9 Mar 2024 19:05:08 +0100 Subject: [PATCH] fix(git): force the default git locale on methods relying on parsing the output --- commitizen/cmd.py | 3 +++ commitizen/git.py | 4 +++- tests/test_git.py | 12 ++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/commitizen/cmd.py b/commitizen/cmd.py index 51ef4523ff..ba48ac7881 100644 --- a/commitizen/cmd.py +++ b/commitizen/cmd.py @@ -1,3 +1,4 @@ +import os import subprocess from typing import NamedTuple @@ -28,6 +29,8 @@ def _try_decode(bytes_: bytes) -> str: def run(cmd: str, env=None) -> Command: + if env is not None: + env = {**os.environ, **env} process = subprocess.Popen( cmd, shell=True, diff --git a/commitizen/git.py b/commitizen/git.py index 6cdc7e2752..900ca9298e 100644 --- a/commitizen/git.py +++ b/commitizen/git.py @@ -175,7 +175,9 @@ def get_tags( f'%(object)"' ) extra = "--merged" if reachable_only else "" - c = cmd.run(f"git tag --format={formatter} --sort=-creatordate {extra}") + # Force the default language for parsing + env = {"LC_ALL": "C", "LANG": "C", "LANGUAGE": "C"} + c = cmd.run(f"git tag --format={formatter} --sort=-creatordate {extra}", env=env) if c.return_code != 0: if reachable_only and c.err == "fatal: malformed object name HEAD\n": # this can happen if there are no commits in the repo yet diff --git a/tests/test_git.py b/tests/test_git.py index 79eb49f10c..3b7a08f94a 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -71,6 +71,18 @@ def test_get_reachable_tags(tmp_commitizen_project): assert tag_names == {"1.0.0", "1.0.1"} +@pytest.mark.parametrize("locale", ["en_US", "fr_FR"]) +def test_get_reachable_tags_with_commits( + tmp_commitizen_project, locale: str, monkeypatch: pytest.MonkeyPatch +): + monkeypatch.setenv("LANG", f"{locale}.UTF-8") + monkeypatch.setenv("LANGUAGE", f"{locale}.UTF-8") + monkeypatch.setenv("LC_ALL", f"{locale}.UTF-8") + with tmp_commitizen_project.as_cwd(): + tags = git.get_tags(reachable_only=True) + assert tags == [] + + def test_get_tag_names(mocker: MockFixture): tag_str = "v1.0.0\n" "v0.5.0\n" "v0.0.1\n" mocker.patch("commitizen.cmd.run", return_value=FakeCommand(out=tag_str))