Merge "Allow to specify default scheme in .gitreview file"

This commit is contained in:
Jenkins
2014-05-22 20:58:05 +00:00
committed by Gerrit Code Review
3 changed files with 44 additions and 20 deletions

View File

@@ -52,7 +52,7 @@ GLOBAL_CONFIG = "/etc/git-review/git-review.conf"
USER_CONFIG = os.path.join(CONFIGDIR, "git-review.conf") USER_CONFIG = os.path.join(CONFIGDIR, "git-review.conf")
PYPI_URL = "http://pypi.python.org/pypi/git-review/json" PYPI_URL = "http://pypi.python.org/pypi/git-review/json"
PYPI_CACHE_TIME = 60 * 60 * 24 # 24 hours PYPI_CACHE_TIME = 60 * 60 * 24 # 24 hours
DEFAULTS = dict(hostname=False, port='29418', project=False, DEFAULTS = dict(scheme='ssh', hostname=False, port=None, project=False,
defaultbranch='master', defaultremote="gerrit", defaultbranch='master', defaultremote="gerrit",
defaultrebase="1") defaultrebase="1")
@@ -298,15 +298,18 @@ def test_remote_url(remote_url):
return False return False
def make_remote_url(username, hostname, port, project): def make_remote_url(scheme, username, hostname, port, project):
"""Builds a gerrit remote URL.""" """Builds a gerrit remote URL."""
if port is None and scheme == 'ssh':
port = 29418
hostport = '%s:%s' % (hostname, port) if port else hostname
if username is None: if username is None:
return "ssh://%s:%s/%s" % (hostname, port, project) return "%s://%s/%s" % (scheme, hostport, project)
else: else:
return "ssh://%s@%s:%s/%s" % (username, hostname, port, project) return "%s://%s@%s/%s" % (scheme, username, hostport, project)
def add_remote(hostname, port, project, remote): def add_remote(scheme, hostname, port, project, remote):
"""Adds a gerrit remote.""" """Adds a gerrit remote."""
asked_for_username = False asked_for_username = False
@@ -315,16 +318,14 @@ def add_remote(hostname, port, project, remote):
username = os.getenv("USERNAME") username = os.getenv("USERNAME")
if not username: if not username:
username = os.getenv("USER") username = os.getenv("USER")
if port is None:
port = 29418
remote_url = make_remote_url(username, hostname, port, project) remote_url = make_remote_url(scheme, username, hostname, port, project)
if VERBOSE: if VERBOSE:
print("No remote set, testing %s" % remote_url) print("No remote set, testing %s" % remote_url)
if not test_remote_url(remote_url): if not test_remote_url(remote_url):
print("Could not connect to gerrit.") print("Could not connect to gerrit.")
username = do_input("Enter your gerrit username: ") username = do_input("Enter your gerrit username: ")
remote_url = make_remote_url(username, hostname, port, project) remote_url = make_remote_url(scheme, username, hostname, port, project)
print("Trying again with %s" % remote_url) print("Trying again with %s" % remote_url)
if not test_remote_url(remote_url): if not test_remote_url(remote_url):
raise Exception("Could not connect to gerrit at %s" % remote_url) raise Exception("Could not connect to gerrit at %s" % remote_url)
@@ -524,6 +525,7 @@ def load_config_file(config_file):
configParser = ConfigParser.ConfigParser() configParser = ConfigParser.ConfigParser()
configParser.read(config_file) configParser.read(config_file)
options = { options = {
'scheme': 'scheme',
'hostname': 'host', 'hostname': 'host',
'port': 'port', 'port': 'port',
'project': 'project', 'project': 'project',
@@ -551,7 +553,7 @@ def update_remote(remote):
return True return True
def check_remote(branch, remote, hostname, port, project): def check_remote(branch, remote, scheme, hostname, port, project):
"""Check that a Gerrit Git remote repo exists, if not, set one.""" """Check that a Gerrit Git remote repo exists, if not, set one."""
has_color = check_color_support() has_color = check_color_support()
@@ -573,7 +575,7 @@ def check_remote(branch, remote, hostname, port, project):
update_remote(remote) update_remote(remote)
return return
if hostname is False or port is False or project is False: if hostname is False or project is False:
# This means there was no .gitreview file # This means there was no .gitreview file
printwrap("No '.gitreview' file found in this repository. We don't " printwrap("No '.gitreview' file found in this repository. We don't "
"know where your gerrit is. Please manually create a remote " "know where your gerrit is. Please manually create a remote "
@@ -582,7 +584,7 @@ def check_remote(branch, remote, hostname, port, project):
# Gerrit remote not present, try to add it # Gerrit remote not present, try to add it
try: try:
add_remote(hostname, port, project, remote) add_remote(scheme, hostname, port, project, remote)
except Exception: except Exception:
print(sys.exc_info()[2]) print(sys.exc_info()[2])
printwrap("We don't know where your gerrit is. Please manually create " printwrap("We don't know where your gerrit is. Please manually create "
@@ -1141,7 +1143,7 @@ def main():
yes = options.yes yes = options.yes
status = 0 status = 0
check_remote(branch, remote, check_remote(branch, remote, config['scheme'],
config['hostname'], config['port'], config['project']) config['hostname'], config['port'], config['project'])
if options.changeidentifier: if options.changeidentifier:

View File

@@ -20,10 +20,14 @@ import sys
if sys.version < '3': if sys.version < '3':
import urllib import urllib
import urlparse
urlopen = urllib.urlopen urlopen = urllib.urlopen
urlparse = urlparse.urlparse
else: else:
import urllib.parse
import urllib.request import urllib.request
urlopen = urllib.request.urlopen urlopen = urllib.request.urlopen
urlparse = urllib.parse.urlparse
import fixtures import fixtures
import testtools import testtools
@@ -145,11 +149,7 @@ class BaseGitReviewTestCase(testtools.TestCase, GerritHelpers):
self._run_git('clone', self.project_uri) self._run_git('clone', self.project_uri)
utils.write_to_file(self._dir('test', 'test_file.txt'), utils.write_to_file(self._dir('test', 'test_file.txt'),
'test file created'.encode()) 'test file created'.encode())
cfg = ('[gerrit]\n' self._create_gitreview_file()
'host=%s\n'
'port=%s\n'
'project=test/test_project.git' % (ssh_addr, ssh_port))
utils.write_to_file(self._dir('test', '.gitreview'), cfg.encode())
# push changes to the Gerrit # push changes to the Gerrit
self._run_git('add', '--all') self._run_git('add', '--all')
@@ -238,6 +238,18 @@ class BaseGitReviewTestCase(testtools.TestCase, GerritHelpers):
host = '127.%s.%s.%s' % (self._test_counter, pid >> 8, pid & 255) host = '127.%s.%s.%s' % (self._test_counter, pid >> 8, pid & 255)
return host, 29418, host, 8080, self._dir('gerrit', 'site-' + host) return host, 29418, host, 8080, self._dir('gerrit', 'site-' + host)
def _create_gitreview_file(self):
cfg = ('[gerrit]\n'
'scheme=%s\n'
'host=%s\n'
'port=%s\n'
'project=test/test_project.git')
parsed = urlparse(self.project_uri)
host_port = parsed.netloc.rpartition('@')[-1]
host, __, port = host_port.partition(':')
cfg %= parsed.scheme, host, port
utils.write_to_file(self._dir('test', '.gitreview'), cfg.encode())
class HttpMixin(object): class HttpMixin(object):
"""HTTP remote_url mixin.""" """HTTP remote_url mixin."""

View File

@@ -31,8 +31,13 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase):
self.assertIn('remote: New Changes:', self._run_git_review()) self.assertIn('remote: New Changes:', self._run_git_review())
self.assertIn('Change-Id:', self._run_git('log', '-1')) self.assertIn('Change-Id:', self._run_git('log', '-1'))
def _configure_gitreview_username(self):
self._run_git('config', '--add', 'gitreview.username', 'test_user')
def test_git_review_s(self): def test_git_review_s(self):
"""Test git-review -s.""" """Test git-review -s."""
self._run_git('remote', 'rm', 'gerrit')
self._configure_gitreview_username()
self._run_git_review('-s') self._run_git_review('-s')
self._simple_change('test file modified', 'test commit message') self._simple_change('test file modified', 'test commit message')
self.assertIn('Change-Id:', self._run_git('log', '-1')) self.assertIn('Change-Id:', self._run_git('log', '-1'))
@@ -40,7 +45,7 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase):
def test_git_review_s_in_detached_head(self): def test_git_review_s_in_detached_head(self):
"""Test git-review -s in detached HEAD state.""" """Test git-review -s in detached HEAD state."""
self._run_git('remote', 'rm', 'gerrit') self._run_git('remote', 'rm', 'gerrit')
self._run_git('config', '--add', 'gitreview.username', 'test_user') self._configure_gitreview_username()
master_sha1 = self._run_git('rev-parse', 'master') master_sha1 = self._run_git('rev-parse', 'master')
self._run_git('checkout', master_sha1) self._run_git('checkout', master_sha1)
self._run_git_review('-s') self._run_git_review('-s')
@@ -55,7 +60,7 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase):
# Review setup with an outdated repo # Review setup with an outdated repo
self._run_git('remote', 'rm', 'gerrit') self._run_git('remote', 'rm', 'gerrit')
self._run_git('config', '--add', 'gitreview.username', 'test_user') self._configure_gitreview_username()
self._run_git_review('-s') self._run_git_review('-s')
self._simple_change('test file modified', 'test commit message 2') self._simple_change('test file modified', 'test commit message 2')
self.assertIn('Change-Id:', self._run_git('log', '-1')) self.assertIn('Change-Id:', self._run_git('log', '-1'))
@@ -192,3 +197,8 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase):
class HttpGitReviewTestCase(tests.HttpMixin, GitReviewTestCase): class HttpGitReviewTestCase(tests.HttpMixin, GitReviewTestCase):
"""Class for the git-review tests over HTTP(S).""" """Class for the git-review tests over HTTP(S)."""
def _configure_gitreview_username(self):
# trick to set http password
self._run_git('config', '--add', 'gitreview.username',
'test_user:test_pass')