Skip to content

fix(git): force the default git locale on methods relying on parsing the output #1012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2024
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
3 changes: 3 additions & 0 deletions commitizen/cmd.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import subprocess
from typing import NamedTuple

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion commitizen/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down