From 48c6f5a7a7fea1dae101b6061e0ff569a6973967 Mon Sep 17 00:00:00 2001 From: Catrope Date: Sun, 22 Jan 2012 03:50:23 +0100 Subject: [PATCH] Break out the config reading into its own method This uses a dict to store the values and passes it around where needed (Amended to pass pep8) Change-Id: I498498e83c92a05742d2a7a8d44aff4452ee32b2 --- git-review | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/git-review b/git-review index 8a8ecd1..5d06367 100755 --- a/git-review +++ b/git-review @@ -277,7 +277,23 @@ def check_color_support(): return _has_color -def check_remote(branch, remote): +def get_config(): + """Get the configuration options set in the .gitremote file, if present.""" + """Returns a hashmap with hostname, port and project.""" + """If there is no .gitremote file, default values will be used.""" + config = dict(hostname=False, port='29418', project=False) + top_dir = run_command('git rev-parse --show-toplevel') + target_file = os.path.join(top_dir, ".gitreview") + if os.path.exists(target_file): + configParser = ConfigParser.ConfigParser(dict(port='29418')) + configParser.read(target_file) + config['hostname'] = configParser.get("gerrit", "host") + config['port'] = configParser.get("gerrit", "port") + config['project'] = configParser.get("gerrit", "project") + return config + + +def check_remote(branch, remote, hostname, port, project): """Check that a Gerrit Git remote repo exists, if not, set one.""" has_color = check_color_support() @@ -301,18 +317,8 @@ def check_remote(branch, remote): print output return - # Check for a .gitreview at the top of the repo with the gerrit location - top_dir = run_command('git rev-parse --show-toplevel') - target_file = os.path.join(top_dir, ".gitreview") - - if os.path.exists(target_file): - config = ConfigParser.ConfigParser(dict(port='29418')) - config.read(target_file) - hostname = config.get("gerrit", "host") - port = config.get("gerrit", "port") - project = config.get("gerrit", "project") - username = None - else: + if hostname == False or port == False or project == False: + # This means there was no .gitreview file print "No '.gitreview' file found in this repository." print "We don't know where your gerrit is. Please manually create " print "a remote named gerrit and try again." @@ -320,7 +326,7 @@ def check_remote(branch, remote): # Gerrit remote not present, try to add it try: - add_remote(username, hostname, port, project) + add_remote(None, hostname, port, project) except: print sys.exc_info()[2] print "We don't know where your gerrit is. Please manually create " @@ -540,6 +546,8 @@ to ensure proper behavior with gerrit. Thanks! def main(): + config = get_config() + usage = "git review [OPTIONS] ... [BRANCH]" parser = argparse.ArgumentParser(usage=usage, description=COPYRIGHT) @@ -593,7 +601,8 @@ def main(): status = 0 needs_update = latest_is_newer() - check_remote(branch, remote) + check_remote(branch, remote, + config['hostname'], config['port'], config['project']) if options.download is not None: print_exit_message(download_review(options.download, branch),