From 517948b6d1dc891f7a5bf291687e859a35fb2be6 Mon Sep 17 00:00:00 2001 From: Andrii Shyshkalov Date: Wed, 15 Mar 2017 15:51:59 +0100 Subject: [PATCH] git cl creds-check: refactor gitcookies methods into a class. There will be many other methods that will need to share state. BUG=689543 R=machenbach@chromium.org Change-Id: I4ce6fe173ff7c92e57737417c65125156606a664 Reviewed-on: https://chromium-review.googlesource.com/455780 Commit-Queue: Andrii Shyshkalov Reviewed-by: Michael Achenbach --- git_cl.py | 106 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 59 insertions(+), 47 deletions(-) diff --git a/git_cl.py b/git_cl.py index 797cbc672..51577048d 100755 --- a/git_cl.py +++ b/git_cl.py @@ -3443,50 +3443,67 @@ def GetRietveldCodereviewSettingsInteractively(): 'run-post-upload-hook', False) -def _ensure_default_gitcookies_path(configured_path, default_path): - assert configured_path - if configured_path == default_path: - print('git is already configured to use your .gitcookies from %s' % - configured_path) - return +class _GitCookiesChecker(object): + """Provides facilties for validating and suggesting fixes to .gitcookies.""" + + def ensure_configured_gitcookies(self): + """Runs checks and suggests fixes to make git use .gitcookies from default + path.""" + default = gerrit_util.CookiesAuthenticator.get_gitcookies_path() + configured_path = RunGitSilent( + ['config', '--global', 'http.cookiefile']).strip() + if configured_path: + self._ensure_default_gitcookies_path(configured_path, default) + else: + self._configure_gitcookies_path(default) + + @staticmethod + def _ensure_default_gitcookies_path(configured_path, default_path): + assert configured_path + if configured_path == default_path: + print('git is already configured to use your .gitcookies from %s' % + configured_path) + return + + print('WARNING: you have configured custom path to .gitcookies: %s\n' + 'Gerrit and other depot_tools expect .gitcookies at %s\n' % + (configured_path, default_path)) - print('WARNING: you have configured custom path to .gitcookies: %s\n' - 'Gerrit and other depot_tools expect .gitcookies at %s\n' % - (configured_path, default_path)) + if not os.path.exists(configured_path): + print('However, your configured .gitcookies file is missing.') + confirm_or_exit('Reconfigure git to use default .gitcookies?', + action='reconfigure') + RunGit(['config', '--global', 'http.cookiefile', default_path]) + return + + if os.path.exists(default_path): + print('WARNING: default .gitcookies file already exists %s' % + default_path) + DieWithError('Please delete %s manually and re-run git cl creds-check' % + default_path) - if not os.path.exists(configured_path): - print('However, your configured .gitcookies file is missing.') - confirm_or_exit('Reconfigure git to use default .gitcookies?', - action='reconfigure') + confirm_or_exit('Move existing .gitcookies to default location?', + action='move') + shutil.move(configured_path, default_path) RunGit(['config', '--global', 'http.cookiefile', default_path]) - return + print('Moved and reconfigured git to use .gitcookies from %s' % + default_path) - if os.path.exists(default_path): - print('WARNING: default .gitcookies file already exists %s' % default_path) - DieWithError('Please delete %s manually and re-run git cl creds-check' % - default_path) - - confirm_or_exit('Move existing .gitcookies to default location?', - action='move') - shutil.move(configured_path, default_path) - RunGit(['config', '--global', 'http.cookiefile', default_path]) - print('Moved and reconfigured git to use .gitcookies from %s' % default_path) - - -def _configure_gitcookies_path(gitcookies_path): - netrc_path = gerrit_util.CookiesAuthenticator.get_netrc_path() - if os.path.exists(netrc_path): - print('You seem to be using outdated .netrc for git credentials: %s' % - netrc_path) - print('This tool will guide you through setting up recommended ' - '.gitcookies store for git credentials.\n' - '\n' - 'IMPORTANT: If something goes wrong and you decide to go back, do:\n' - ' git config --global --unset http.cookiefile\n' - ' mv %s %s.backup\n\n' % (gitcookies_path, gitcookies_path)) - confirm_or_exit(action='setup .gitcookies') - RunGit(['config', '--global', 'http.cookiefile', gitcookies_path]) - print('Configured git to use .gitcookies from %s' % gitcookies_path) + @staticmethod + def _configure_gitcookies_path(default_path): + netrc_path = gerrit_util.CookiesAuthenticator.get_netrc_path() + if os.path.exists(netrc_path): + print('You seem to be using outdated .netrc for git credentials: %s' % + netrc_path) + print('This tool will guide you through setting up recommended ' + '.gitcookies store for git credentials.\n' + '\n' + 'IMPORTANT: If something goes wrong and you decide to go back, do:\n' + ' git config --global --unset http.cookiefile\n' + ' mv %s %s.backup\n\n' % (default_path, default_path)) + confirm_or_exit(action='setup .gitcookies') + RunGit(['config', '--global', 'http.cookiefile', default_path]) + print('Configured git to use .gitcookies from %s' % default_path) def CMDcreds_check(parser, args): @@ -3496,13 +3513,8 @@ def CMDcreds_check(parser, args): if gerrit_util.GceAuthenticator.is_gce(): DieWithError('this command is not designed for GCE, are you on a bot?') - gitcookies_path = gerrit_util.CookiesAuthenticator.get_gitcookies_path() - configured_gitcookies_path = RunGitSilent( - ['config', '--global', 'http.cookiefile']).strip() - if configured_gitcookies_path: - _ensure_default_gitcookies_path(configured_gitcookies_path, gitcookies_path) - else: - _configure_gitcookies_path(gitcookies_path) + checker = _GitCookiesChecker() + checker.ensure_configured_gitcookies() # TODO(tandrii): finish this. return 0