diff --git a/swh/loader/mercurial/identify.py b/swh/loader/mercurial/identify.py --- a/swh/loader/mercurial/identify.py +++ b/swh/loader/mercurial/identify.py @@ -3,11 +3,12 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +from codecs import escape_decode # type: ignore import json +import os +from pathlib import Path import re import subprocess -from codecs import escape_decode # type: ignore -from pathlib import Path from typing import Any, Dict, Iterator, List, NamedTuple, Optional, Union # WARNING: do not import unnecessary things here to keep cli startup time under @@ -196,7 +197,9 @@ def _output(self, *args) -> bytes: """Return the outpout of a `hg` call.""" - return subprocess.check_output(["hg", *args], cwd=self._root) + return subprocess.check_output( + ["hg", *args], cwd=self._root, env=self._get_env() + ) def _call(self, *args) -> None: """Perform a `hg` call.""" @@ -205,8 +208,18 @@ cwd=self._root, stderr=subprocess.PIPE, stdout=subprocess.PIPE, + env=self._get_env(), ) + def _get_env(self) -> Dict[str, str]: + """Return the smallest viable environment for `hg` suprocesses""" + env = { + "PATH": os.environ["PATH"], + "HGPLAIN": "", # Tells Mercurial to disable output customization + "HGRCPATH": "", # Tells Mercurial to ignore config files + } + return env + def root(self) -> Path: """Return the root of the Mercurial repository.""" return self._root diff --git a/swh/loader/mercurial/tests/conftest.py b/swh/loader/mercurial/tests/conftest.py --- a/swh/loader/mercurial/tests/conftest.py +++ b/swh/loader/mercurial/tests/conftest.py @@ -3,6 +3,7 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +import os from typing import Any, Dict import pytest @@ -37,3 +38,12 @@ "max_content_size": 104857600, "temp_directory": str(tmp_path), } + + +@pytest.fixture(autouse=True, scope="session") +def swh_mercurial_set_plain(): + """Mercurial is customizable by users, so we use built-in environment + variables to turn off all customization in tests. + """ + os.environ["HGPLAIN"] = "" + os.environ["HGRCPATH"] = ""