Skip to content

Commit 459249e

Browse files
author
iTrooz
authored
Disable Git/SSH prompts when --no-input is given (#13228)
1 parent 29cd0c4 commit 459249e

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

news/12718.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disable Git and SSH prompts when ``--no-input`` is passed.

src/pip/_internal/vcs/git.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import urllib.parse
66
import urllib.request
77
from dataclasses import replace
8-
from typing import List, Optional, Tuple
8+
from typing import Any, List, Optional, Tuple
99

1010
from pip._internal.exceptions import BadCommand, InstallationError
1111
from pip._internal.utils.misc import HiddenText, display_path, hide_url
@@ -77,6 +77,15 @@ class Git(VersionControl):
7777
def get_base_rev_args(rev: str) -> List[str]:
7878
return [rev]
7979

80+
@classmethod
81+
def run_command(cls, *args: Any, **kwargs: Any) -> str:
82+
if os.environ.get("PIP_NO_INPUT"):
83+
extra_environ = kwargs.get("extra_environ", {})
84+
extra_environ["GIT_TERMINAL_PROMPT"] = "0"
85+
extra_environ["GIT_SSH_COMMAND"] = "ssh -oBatchMode=yes"
86+
kwargs["extra_environ"] = extra_environ
87+
return super().run_command(*args, **kwargs)
88+
8089
def is_immutable_rev_checkout(self, url: str, dest: str) -> bool:
8190
_, rev_options = self.get_url_rev_options(hide_url(url))
8291
if not rev_options.rev:

tests/functional/test_install_config.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,34 @@ def test_do_not_prompt_for_authentication(
341341
assert "ERROR: HTTP error 401" in result.stderr
342342

343343

344+
def test_do_not_prompt_for_authentication_git(
345+
script: PipTestEnvironment, data: TestData, cert_factory: CertFactory
346+
) -> None:
347+
"""Test behaviour if --no-input option is given while installing
348+
from a git http url requiring authentication
349+
"""
350+
server = make_mock_server()
351+
# Disable vscode user/password prompt, will make tests fail inside vscode
352+
script.environ["GIT_ASKPASS"] = ""
353+
354+
# Return 401 on all URLs
355+
server.mock.side_effect = lambda _, __: authorization_response(
356+
data.packages / "simple-3.0.tar.gz"
357+
)
358+
359+
url = f"git+http://{server.host}:{server.port}/simple"
360+
361+
with server_running(server):
362+
result = script.pip(
363+
"install",
364+
url,
365+
"--no-input",
366+
expect_error=True,
367+
)
368+
369+
assert "terminal prompts disabled" in result.stderr
370+
371+
344372
@pytest.fixture(params=(True, False), ids=("interactive", "noninteractive"))
345373
def interactive(request: pytest.FixtureRequest) -> bool:
346374
return request.param

0 commit comments

Comments
 (0)