From ea802a77b7bdeb2d920b01f1223bcdb3936ced5f Mon Sep 17 00:00:00 2001 From: Gavin Mak Date: Fri, 28 Jun 2024 18:59:03 +0000 Subject: [PATCH] Don't show ".gitcookies have credentials" if no creds Running `git cl creds-check` without any credentials will show: ``` Your .gitcookies have credentials for these hosts: No Git/Gerrit credentials found ``` The first line of the output should not assume that there are hosts. Don't show this line if credentials are not found. Change-Id: Ie6eba3518f4b270eef5fbe0c51b4494762a3aef4 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5663779 Reviewed-by: Scott Lee Auto-Submit: Gavin Mak Commit-Queue: Scott Lee --- git_cl.py | 4 ++-- tests/git_cl_test.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/git_cl.py b/git_cl.py index 66616a1c1..64aa3adb0 100755 --- a/git_cl.py +++ b/git_cl.py @@ -3742,10 +3742,11 @@ class _GitCookiesChecker(object): def print_current_creds(self): hosts = sorted(self.get_hosts_with_creds()) if not hosts: - print('No Git/Gerrit credentials found') + print('No Git/Gerrit credentials found.') return lengths = [max(map(len, (row[i] for row in hosts))) for i in range(3)] header = [('Host', 'User', 'Which file'), ['=' * l for l in lengths]] + print('Your .gitcookies have credentials for these hosts:') for row in (header + hosts): print('\t'.join((('%%+%ds' % l) % s) for l, s in zip(lengths, row))) @@ -3905,7 +3906,6 @@ def CMDcreds_check(parser, args): checker = _GitCookiesChecker() checker.ensure_configured_gitcookies() - print('Your .gitcookies have credentials for these hosts:') checker.print_current_creds() if not checker.find_and_report_problems(): diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py index 676c9f672..210ca72dd 100755 --- a/tests/git_cl_test.py +++ b/tests/git_cl_test.py @@ -3135,6 +3135,7 @@ class TestGitCl(unittest.TestCase): CookiesAuthenticatorMock).start() git_cl._GitCookiesChecker().print_current_creds() self.assertEqual(list(sys.stdout.getvalue().splitlines()), [ + 'Your .gitcookies have credentials for these hosts:', ' Host\tUser\t Which file', '============================\t====\t===========', 'host-review.googlesource.com\tuser\t.gitcookies', @@ -3144,6 +3145,7 @@ class TestGitCl(unittest.TestCase): sys.stdout.truncate(0) git_cl._GitCookiesChecker().print_current_creds() self.assertEqual(list(sys.stdout.getvalue().splitlines()), [ + 'Your .gitcookies have credentials for these hosts:', ' Host\tUser\t Which file', '============================\t====\t===========', 'host-review.googlesource.com\tuser\t.gitcookies',