diff --git a/git_review/cmd.py b/git_review/cmd.py index 97e4127..228dc3a 100755 --- a/git_review/cmd.py +++ b/git_review/cmd.py @@ -52,7 +52,7 @@ GLOBAL_CONFIG = "/etc/git-review/git-review.conf" USER_CONFIG = os.path.join(CONFIGDIR, "git-review.conf") PYPI_URL = "http://pypi.python.org/pypi/git-review/json" 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", defaultrebase="1") @@ -298,15 +298,18 @@ def test_remote_url(remote_url): return False -def make_remote_url(username, hostname, port, project): +def make_remote_url(scheme, username, hostname, port, project): """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: - return "ssh://%s:%s/%s" % (hostname, port, project) + return "%s://%s/%s" % (scheme, hostport, project) 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.""" asked_for_username = False @@ -315,16 +318,14 @@ def add_remote(hostname, port, project, remote): username = os.getenv("USERNAME") if not username: 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: print("No remote set, testing %s" % remote_url) if not test_remote_url(remote_url): print("Could not connect to gerrit.") 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) if not test_remote_url(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.read(config_file) options = { + 'scheme': 'scheme', 'hostname': 'host', 'port': 'port', 'project': 'project', @@ -551,7 +553,7 @@ def update_remote(remote): 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.""" has_color = check_color_support() @@ -573,7 +575,7 @@ def check_remote(branch, remote, hostname, port, project): update_remote(remote) 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 printwrap("No '.gitreview' file found in this repository. We don't " "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 try: - add_remote(hostname, port, project, remote) + add_remote(scheme, hostname, port, project, remote) except Exception: print(sys.exc_info()[2]) printwrap("We don't know where your gerrit is. Please manually create " @@ -1141,7 +1143,7 @@ def main(): yes = options.yes status = 0 - check_remote(branch, remote, + check_remote(branch, remote, config['scheme'], config['hostname'], config['port'], config['project']) if options.changeidentifier: diff --git a/git_review/tests/__init__.py b/git_review/tests/__init__.py index 192175e..a7b08c6 100644 --- a/git_review/tests/__init__.py +++ b/git_review/tests/__init__.py @@ -20,10 +20,14 @@ import sys if sys.version < '3': import urllib + import urlparse urlopen = urllib.urlopen + urlparse = urlparse.urlparse else: + import urllib.parse import urllib.request urlopen = urllib.request.urlopen + urlparse = urllib.parse.urlparse import fixtures import testtools @@ -145,11 +149,7 @@ class BaseGitReviewTestCase(testtools.TestCase, GerritHelpers): self._run_git('clone', self.project_uri) utils.write_to_file(self._dir('test', 'test_file.txt'), 'test file created'.encode()) - cfg = ('[gerrit]\n' - '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()) + self._create_gitreview_file() # push changes to the Gerrit 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) 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): """HTTP remote_url mixin.""" diff --git a/git_review/tests/test_git_review.py b/git_review/tests/test_git_review.py index f49c81c..a796dfa 100644 --- a/git_review/tests/test_git_review.py +++ b/git_review/tests/test_git_review.py @@ -31,8 +31,13 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase): self.assertIn('remote: New Changes:', self._run_git_review()) 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): """Test git-review -s.""" + self._run_git('remote', 'rm', 'gerrit') + self._configure_gitreview_username() self._run_git_review('-s') self._simple_change('test file modified', 'test commit message') 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): """Test git-review -s in detached HEAD state.""" 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') self._run_git('checkout', master_sha1) self._run_git_review('-s') @@ -55,7 +60,7 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase): # Review setup with an outdated repo 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._simple_change('test file modified', 'test commit message 2') self.assertIn('Change-Id:', self._run_git('log', '-1')) @@ -192,3 +197,8 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase): class HttpGitReviewTestCase(tests.HttpMixin, GitReviewTestCase): """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')