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
diff --git a/git_cl.py b/git_cl.py
index 56d86f2..72ad8f5 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -11,9 +11,11 @@
 import optparse
 import os
 import re
+import stat
 import sys
 import textwrap
 import urlparse
+import urllib
 import urllib2
 
 try:
@@ -154,6 +156,8 @@
       cr_settings_file = FindCodereviewSettingsFile()
       if cr_settings_file:
         LoadCodereviewSettingsFromFile(cr_settings_file)
+        self.updated = True
+        DownloadHooks(False)
       self.updated = True
 
   def GetDefaultServerUrl(self, error_ok=False):
@@ -709,11 +713,6 @@
   if 'GERRIT_HOST' in keyvals and 'GERRIT_PORT' in keyvals:
     RunGit(['config', 'gerrit.host', keyvals['GERRIT_HOST']])
     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:
     #should be of the form
@@ -723,6 +722,31 @@
             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]')
 def CMDconfig(parser, args):
   """edit configuration for this tree"""
@@ -730,6 +754,7 @@
   _, args = parser.parse_args(args)
   if len(args) == 0:
     GetCodereviewSettingsInteractively()
+    DownloadHooks(True)
     return 0
 
   url = args[0]
@@ -738,6 +763,7 @@
 
   # Load code review settings and download hooks (if available).
   LoadCodereviewSettingsFromFile(urllib2.urlopen(url))
+  DownloadHooks(True)
   return 0