aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKonstantin Ryabitsev <konstantin@linuxfoundation.org>2022-12-13 14:01:58 -0500
committerKonstantin Ryabitsev <konstantin@linuxfoundation.org>2022-12-13 14:01:58 -0500
commitaa837849d4141584f7cba58b5984cf506c68185e (patch)
treef311af284d9b08a9c924c338db059e8a288e9c8d
parent141d4a6db977aaaec02be59341e502f940086546 (diff)
downloadb4-aa837849d4141584f7cba58b5984cf506c68185e.tar.gz
Use git-credential if smtppass is not set
When we have smtpuser but smtpass is not set, invoke git-credential to request the password. Suggested-by: Luca Weiss <luca.weiss@fairphone.com> Link: https://msgid.link/COXAHN6YWLQH.33YW8LAHHXH21@otso Signed-off-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
-rw-r--r--b4/__init__.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/b4/__init__.py b/b4/__init__.py
index 9c00bbf..021e951 100644
--- a/b4/__init__.py
+++ b/b4/__init__.py
@@ -2211,6 +2211,18 @@ def git_run_command(gitdir: Optional[str], args: List[str], stdin: Optional[byte
return ecode, out
+def git_credential_fill(gitdir: Optional[str], protocol: str, host: str, username: str) -> Optional[str]:
+ stdin = f'protocol={protocol}\nhost={host}\nusername={username}\n'.encode()
+ ecode, out = git_run_command(gitdir, args=['credential', 'fill'], stdin=stdin)
+ if ecode == 0:
+ for line in out.splitlines():
+ if not line.startswith('password='):
+ continue
+ chunks = line.split('=', maxsplit=1)
+ return chunks[1]
+ return None
+
+
def git_get_command_lines(gitdir: Optional[str], args: list) -> List[str]:
ecode, out = git_run_command(gitdir, args)
lines = list()
@@ -3104,6 +3116,15 @@ def get_smtp(dryrun: bool = False) -> Tuple[Union[smtplib.SMTP, smtplib.SMTP_SSL
# If we got to this point, we should do authentication.
auser = sconfig.get('smtpuser')
apass = sconfig.get('smtppass')
+ if auser and not apass:
+ # Try with git-credential-helper
+ if port:
+ gchost = f'{server}:{port}'
+ else:
+ gchost = server
+ apass = git_credential_fill(None, protocol='smtp', host=gchost, username=auser)
+ if not apass:
+ raise smtplib.SMTPException('No password specified for connecting to %s', server)
if auser and apass:
# Let any exceptions bubble up
smtp.login(auser, apass)