Skip to content

Commit 6654ef1

Browse files
authored
fix(git): force the default git locale on methods relying on parsing the output (#1012)
1 parent 0e93206 commit 6654ef1

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

commitizen/cmd.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import subprocess
23
from typing import NamedTuple
34

@@ -28,6 +29,8 @@ def _try_decode(bytes_: bytes) -> str:
2829

2930

3031
def run(cmd: str, env=None) -> Command:
32+
if env is not None:
33+
env = {**os.environ, **env}
3134
process = subprocess.Popen(
3235
cmd,
3336
shell=True,

commitizen/git.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ def get_tags(
175175
f'%(object)"'
176176
)
177177
extra = "--merged" if reachable_only else ""
178-
c = cmd.run(f"git tag --format={formatter} --sort=-creatordate {extra}")
178+
# Force the default language for parsing
179+
env = {"LC_ALL": "C", "LANG": "C", "LANGUAGE": "C"}
180+
c = cmd.run(f"git tag --format={formatter} --sort=-creatordate {extra}", env=env)
179181
if c.return_code != 0:
180182
if reachable_only and c.err == "fatal: malformed object name HEAD\n":
181183
# this can happen if there are no commits in the repo yet

tests/test_git.py

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ def test_get_reachable_tags(tmp_commitizen_project):
7171
assert tag_names == {"1.0.0", "1.0.1"}
7272

7373

74+
@pytest.mark.parametrize("locale", ["en_US", "fr_FR"])
75+
def test_get_reachable_tags_with_commits(
76+
tmp_commitizen_project, locale: str, monkeypatch: pytest.MonkeyPatch
77+
):
78+
monkeypatch.setenv("LANG", f"{locale}.UTF-8")
79+
monkeypatch.setenv("LANGUAGE", f"{locale}.UTF-8")
80+
monkeypatch.setenv("LC_ALL", f"{locale}.UTF-8")
81+
with tmp_commitizen_project.as_cwd():
82+
tags = git.get_tags(reachable_only=True)
83+
assert tags == []
84+
85+
7486
def test_get_tag_names(mocker: MockFixture):
7587
tag_str = "v1.0.0\n" "v0.5.0\n" "v0.0.1\n"
7688
mocker.patch("commitizen.cmd.run", return_value=FakeCommand(out=tag_str))

0 commit comments

Comments
 (0)