Disable git terminal prompt during fetch/clone

git fetch operation may prompt user to enter username and password.
This won't be visible to user when repo sync operation since stdout and
stderr are redirected. If that happens, user may think repo is doing
work and likely won't realize it's stuck on user's input.

This patch disables prompt for clone and fetch operations, and repo will
fail fast.

R=gavinmak@google.com

Bug: b/368644181
Change-Id: I2efa88ae66067587a00678eda155d861034b9127
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/438001
Reviewed-by: Nasser Grainawi <nasser.grainawi@linaro.org>
Tested-by: Josip Sokcevic <sokcevic@google.com>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
This commit is contained in:
Josip Sokcevic 2024-09-26 21:55:10 +00:00 committed by LUCI
parent d7ebdf56be
commit 621de7ed12
2 changed files with 22 additions and 6 deletions

View File

@ -313,10 +313,13 @@ class GitCommand:
cwd = None cwd = None
command_name = cmdv[0] command_name = cmdv[0]
command.append(command_name) command.append(command_name)
if command_name in ("fetch", "clone"):
env["GIT_TERMINAL_PROMPT"] = "0"
# Need to use the --progress flag for fetch/clone so output will be # Need to use the --progress flag for fetch/clone so output will be
# displayed as by default git only does progress output if stderr is a # displayed as by default git only does progress output if stderr is
# TTY. # a TTY.
if sys.stderr.isatty() and command_name in ("fetch", "clone"): if sys.stderr.isatty():
if "--progress" not in cmdv and "--quiet" not in cmdv: if "--progress" not in cmdv and "--quiet" not in cmdv:
command.append("--progress") command.append("--progress")
command.extend(cmdv[1:]) command.extend(cmdv[1:])

View File

@ -2697,6 +2697,19 @@ class Project:
) )
# Continue right away so we don't sleep as we shouldn't need to. # Continue right away so we don't sleep as we shouldn't need to.
continue continue
elif (
ret == 128
and gitcmd.stdout
and "fatal: could not read Username" in gitcmd.stdout
):
# User needs to be authenticated, and Git wants to prompt for
# username and password.
print(
"git requires authentication, but repo cannot perform "
"interactive authentication. Check git credentials.",
file=output_redir,
)
break
elif current_branch_only and is_sha1 and ret == 128: elif current_branch_only and is_sha1 and ret == 128:
# Exit code 128 means "couldn't find the ref you asked for"; if # Exit code 128 means "couldn't find the ref you asked for"; if
# we're in sha1 mode, we just tried sync'ing from the upstream # we're in sha1 mode, we just tried sync'ing from the upstream