Download commit-msg from gerrit-review.googlesource.com

Tell download tools/hooks/commit-msg mannually if it looks broken.

chrome-internal-reviews.googlesource.com requires
authentication to access tools/hooks/commit-msg, and
we'll get Google Sign-in page rather than expected shell script.

R=szager@chromium.org,maruel@chromium.org,ilevy@chromium.org
BUG=

Review URL: https://codereview.chromium.org/87173002

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@237454 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/git_cl.py b/git_cl.py
index 3cb6623..5e16632 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -1038,6 +1038,12 @@
     f.write(urllib2.urlopen(source).read())
 
 
+def hasSheBang(fname):
+  """Checks fname is a #! script."""
+  with open(fname) as f:
+    return f.read(2).startswith('#!')
+
+
 def DownloadHooks(force):
   """downloads hooks
 
@@ -1046,21 +1052,27 @@
   """
   if not settings.GetIsGerrit():
     return
-  server_url = settings.GetDefaultServerUrl()
-  src = '%s/tools/hooks/commit-msg' % server_url
+  src = 'https://gerrit-review.googlesource.com/tools/hooks/commit-msg'
   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:
       urlretrieve(src, dst)
+      if not hasSheBang(dst):
+        DieWithError('Not a script: %s\n'
+                     'You need to download from\n%s\n'
+                     'into .git/hooks/commit-msg and '
+                     'chmod +x .git/hooks/commit-msg' % (dst, src))
       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)
+      DieWithError('\nFailed to download hooks.\n'
+                   'You need to download from\n%s\n'
+                   'into .git/hooks/commit-msg and '
+                   'chmod +x .git/hooks/commit-msg' % src)
 
 
 @subcommand.usage('[repo root containing codereview.settings]')
diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py
index 23c0c64..dce632e 100755
--- a/tests/git_cl_test.py
+++ b/tests/git_cl_test.py
@@ -649,6 +649,7 @@
       return True
     self.mock(git_cl.os.path, 'exists', Exists)
     self.mock(git_cl, 'urlretrieve', self._mocked_call)
+    self.mock(git_cl, 'hasSheBang', self._mocked_call)
     self.calls = [
         ((['git', 'config', 'rietveld.server',
            'gerrit.chromium.org'],), ''),
@@ -665,12 +666,11 @@
         # DownloadHooks(False)
         ((['git', 'config', 'gerrit.host'],),
          'gerrit.chromium.org'),
-        ((['git', 'config', 'rietveld.server'],),
-         'gerrit.chromium.org'),
         ((['git', 'rev-parse', '--show-cdup'],), ''),
         ((commit_msg_path, os.X_OK,), False),
-        (('https://gerrit.chromium.org/tools/hooks/commit-msg',
+        (('https://gerrit-review.googlesource.com/tools/hooks/commit-msg',
           commit_msg_path,), ''),
+        ((commit_msg_path,), True),
         ((commit_msg_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR,), ''),
         # GetCodereviewSettingsInteractively
         ((['git', 'config', 'rietveld.server'],),