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
This commit is contained in:
Monty Taylor
2011-11-19 17:33:31 -02:00
parent da0b2d180a
commit 9b80937897

View File

@@ -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)