Commit 78c4b98f authored by ukai@chromium.org's avatar ukai@chromium.org

Download hooks only in "git cl config"

also, use http download instead of scp,
because scp won't work well on Windows.

BUG=chromium:113153
Review URL: https://chromiumcodereview.appspot.com/9369023

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@121820 0039d316-1c4b-4281-b951-d872f2087c98
parent 0af9b70a
...@@ -11,9 +11,11 @@ import logging ...@@ -11,9 +11,11 @@ import logging
import optparse import optparse
import os import os
import re import re
import stat
import sys import sys
import textwrap import textwrap
import urlparse import urlparse
import urllib
import urllib2 import urllib2
try: try:
...@@ -154,6 +156,8 @@ class Settings(object): ...@@ -154,6 +156,8 @@ class Settings(object):
cr_settings_file = FindCodereviewSettingsFile() cr_settings_file = FindCodereviewSettingsFile()
if cr_settings_file: if cr_settings_file:
LoadCodereviewSettingsFromFile(cr_settings_file) LoadCodereviewSettingsFromFile(cr_settings_file)
self.updated = True
DownloadHooks(False)
self.updated = True self.updated = True
def GetDefaultServerUrl(self, error_ok=False): def GetDefaultServerUrl(self, error_ok=False):
...@@ -709,11 +713,6 @@ def LoadCodereviewSettingsFromFile(fileobj): ...@@ -709,11 +713,6 @@ def LoadCodereviewSettingsFromFile(fileobj):
if 'GERRIT_HOST' in keyvals and 'GERRIT_PORT' in keyvals: if 'GERRIT_HOST' in keyvals and 'GERRIT_PORT' in keyvals:
RunGit(['config', 'gerrit.host', keyvals['GERRIT_HOST']]) RunGit(['config', 'gerrit.host', keyvals['GERRIT_HOST']])
RunGit(['config', 'gerrit.port', keyvals['GERRIT_PORT']]) RunGit(['config', 'gerrit.port', keyvals['GERRIT_PORT']])
# Install the standard commit-msg hook.
RunCommand(['scp', '-p', '-P', keyvals['GERRIT_PORT'],
'%s:hooks/commit-msg' % keyvals['GERRIT_HOST'],
os.path.join(settings.GetRoot(),
'.git', 'hooks', 'commit-msg')])
if 'PUSH_URL_CONFIG' in keyvals and 'ORIGIN_URL_CONFIG' in keyvals: if 'PUSH_URL_CONFIG' in keyvals and 'ORIGIN_URL_CONFIG' in keyvals:
#should be of the form #should be of the form
...@@ -723,6 +722,31 @@ def LoadCodereviewSettingsFromFile(fileobj): ...@@ -723,6 +722,31 @@ def LoadCodereviewSettingsFromFile(fileobj):
keyvals['ORIGIN_URL_CONFIG']]) keyvals['ORIGIN_URL_CONFIG']])
def DownloadHooks(force):
"""downloads hooks
Args:
force: True to update hooks. False to install hooks if not present.
"""
if not settings.GetIsGerrit():
return
server_url = settings.GetDefaultServerUrl()
src = '%s/tools/hooks/commit-msg' % server_url
dst = os.path.join(settings.GetRoot(), '.git', 'hooks', 'commit-msg')
if not os.access(dst, os.X_OK):
if os.path.exists(dst):
if not force:
return
os.remove(dst)
try:
urllib.urlretrieve(src, dst)
os.chmod(dst, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
except Exception:
if os.path.exists(dst):
os.remove(dst)
DieWithError('\nFailed to download hooks from %s' % src)
@usage('[repo root containing codereview.settings]') @usage('[repo root containing codereview.settings]')
def CMDconfig(parser, args): def CMDconfig(parser, args):
"""edit configuration for this tree""" """edit configuration for this tree"""
...@@ -730,6 +754,7 @@ def CMDconfig(parser, args): ...@@ -730,6 +754,7 @@ def CMDconfig(parser, args):
_, args = parser.parse_args(args) _, args = parser.parse_args(args)
if len(args) == 0: if len(args) == 0:
GetCodereviewSettingsInteractively() GetCodereviewSettingsInteractively()
DownloadHooks(True)
return 0 return 0
url = args[0] url = args[0]
...@@ -738,6 +763,7 @@ def CMDconfig(parser, args): ...@@ -738,6 +763,7 @@ def CMDconfig(parser, args):
# Load code review settings and download hooks (if available). # Load code review settings and download hooks (if available).
LoadCodereviewSettingsFromFile(urllib2.urlopen(url)) LoadCodereviewSettingsFromFile(urllib2.urlopen(url))
DownloadHooks(True)
return 0 return 0
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
import os import os
import StringIO import StringIO
import stat
import sys import sys
import unittest import unittest
...@@ -42,6 +43,16 @@ class WatchlistsMock(object): ...@@ -42,6 +43,16 @@ class WatchlistsMock(object):
return ['joe@example.com'] return ['joe@example.com']
class CodereviewSettingsFileMock(object):
def __init__(self):
pass
# pylint: disable=R0201
def read(self):
return ("CODE_REVIEW_SERVER: gerrit.chromium.org\n" +
"GERRIT_HOST: gerrit.chromium.org\n" +
"GERRIT_PORT: 29418\n")
class TestGitCl(TestCase): class TestGitCl(TestCase):
def setUp(self): def setUp(self):
super(TestGitCl, self).setUp() super(TestGitCl, self).setUp()
...@@ -389,5 +400,61 @@ class TestGitCl(TestCase): ...@@ -389,5 +400,61 @@ class TestGitCl(TestCase):
['reviewer@example.com', 'another@example.com']) ['reviewer@example.com', 'another@example.com'])
def test_config_gerrit_download_hook(self):
self.mock(git_cl, 'FindCodereviewSettingsFile', CodereviewSettingsFileMock)
def ParseCodereviewSettingsContent(content):
keyvals = {}
keyvals['CODE_REVIEW_SERVER'] = 'gerrit.chromium.org'
keyvals['GERRIT_HOST'] = 'gerrit.chromium.org'
keyvals['GERRIT_PORT'] = '29418'
return keyvals
self.mock(git_cl.gclient_utils, 'ParseCodereviewSettingsContent',
ParseCodereviewSettingsContent)
self.mock(git_cl.os, 'access', self._mocked_call)
self.mock(git_cl.os, 'chmod', self._mocked_call)
def AbsPath(path):
if not path.startswith('/'):
return os.path.join('/usr/local/src', path)
return path
self.mock(git_cl.os.path, 'abspath', AbsPath)
def Exists(path):
if path == '/usr/local/src/.git/hooks/commit-msg':
return False
# others paths, such as /usr/share/locale/....
return True
self.mock(git_cl.os.path, 'exists', Exists)
self.mock(git_cl.urllib, 'urlretrieve', self._mocked_call)
self.calls = [
((['git', 'config', 'rietveld.server', 'gerrit.chromium.org'],), ''),
((['git', 'config', '--unset-all', 'rietveld.cc'],), ''),
((['git', 'config', '--unset-all', 'rietveld.tree-status-url'],), ''),
((['git', 'config', '--unset-all', 'rietveld.viewvc-url'],), ''),
((['git', 'config', 'gerrit.host', 'gerrit.chromium.org'],), ''),
((['git', 'config', 'gerrit.port', '29418'],), ''),
# DownloadHooks(False)
((['git', 'config', 'gerrit.host'],), 'gerrit.chromium.org'),
((['git', 'config', 'rietveld.server'],), 'gerrit.chromium.org'),
((['git', 'rev-parse', '--show-cdup'],), ''),
(('/usr/local/src/.git/hooks/commit-msg', os.X_OK,), False),
(('https://gerrit.chromium.org/tools/hooks/commit-msg',
'/usr/local/src/.git/hooks/commit-msg',), ''),
(('/usr/local/src/.git/hooks/commit-msg',
stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
# GetCodereviewSettingsInteractively
((['git', 'config', 'rietveld.server'],), 'gerrit.chromium.org'),
(('Rietveld server (host[:port]) [https://gerrit.chromium.org]:',),
''),
((['git', 'config', 'rietveld.cc'],), ''),
(('CC list:',), ''),
((['git', 'config', 'rietveld.tree-status-url'],), ''),
(('Tree status URL:',), ''),
((['git', 'config', 'rietveld.viewvc-url'],), ''),
(('ViewVC URL:',), ''),
# DownloadHooks(True)
(('/usr/local/src/.git/hooks/commit-msg', os.X_OK,), True),
]
git_cl.main(['config'])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment