From 9b809378973936511a03d3b1b6571bb512ba6721 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Sat, 19 Nov 2011 17:33:31 -0200 Subject: [PATCH] Handle usernames better in remote setup. Fixes bug 891345. First we'll try to connect to gerrit for ls-projects as usual. If that doesn't work, it's a common case that the person's local username is different than their gerrit username, so let's ask them for their gerrit username at that point. Change-Id: Id8dfe9fb788609b7527f1ed2212433403c531bf4 --- git-review | 54 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/git-review b/git-review index 032d0c9..2c7411f 100755 --- a/git-review +++ b/git-review @@ -112,6 +112,35 @@ def set_hooks_commit_msg(): run_command("GIT_EDITOR=true git commit --amend") +def test_remote(username, hostname, port, project): + """ Tests that a possible gerrit remote works """ + + if username is None: + ssh_cmd = "ssh -p%s -o StrictHostKeyChecking=no %s gerrit ls-projects" + cmd = ssh_cmd % (port, hostname) + else: + ssh_cmd = "ssh -p%s -o StrictHostKeyChecking=no %s@%s " \ + "gerrit ls-projects" + cmd = ssh_cmd % (port, username, hostname) + (status, ssh_outout) = run_command_status(cmd) + if status == 0: + if VERBOSE: + print "%s@%s:%s worked." % (username, hostname, port) + return True + else: + if VERBOSE: + print "%s@%s:%s did not work." % (username, hostname, port) + return False + + +def make_remote_url(username, hostname, port, project): + """ Builds a gerrit remote URL """ + if username is None: + return "ssh://%s:%s/%s" % (hostname, port, project) + else: + return "ssh://%s@%s:%s/%s" % (username, hostname, port, project) + + def add_remote(username, hostname, port, project): """ Adds a gerrit remote. """ @@ -119,25 +148,24 @@ def add_remote(username, hostname, port, project): username = os.getenv("USERNAME") if username is None: username = os.getenv("USER") - if username is None: - username = raw_input("Enter your gerrit username: ") if port is None: port = 29418 - remote_url = "ssh://%s@%s:%s/%s" % (username, hostname, port, project) + remote_url = make_remote_url(username, hostname, port, project) if VERBOSE: print "No remote set, testing %s" % remote_url + if not test_remote(username, hostname, port, project): + print "Could not connect to gerrit." + username = raw_input("Enter your gerrit username: ") + remote_url = make_remote_url(username, hostname, port, project) + print "Trying again with %s" % remote_url + if not test_remote(username, hostname, port, project): + raise Exception("Could not connect to gerrit at %s" % remote_url) - ssh_cmd = "ssh -p%s -o StrictHostKeyChecking=no %s@%s gerrit ls-projects" - cmd = ssh_cmd % (port, username, hostname) - (status, ssh_outout) = run_command_status(cmd) - if status == 0: - if VERBOSE: - print "%s@%s:%s worked." % (username, hostname, port) - print "Creating a git remote called gerrit that maps to:" - print "\t%s" % remote_url - cmd = "git remote add -f gerrit %s" % remote_url - (status, remote_output) = run_command_status(cmd) + print "Creating a git remote called gerrit that maps to:" + print "\t%s" % remote_url + cmd = "git remote add -f gerrit %s" % remote_url + (status, remote_output) = run_command_status(cmd) if status != 0: raise Exception("Error running %s" % cmd)