Rewrite Google Code http URLs with https on git svn dcommit.

This fix is created to avoid having to rebuild all the WebRTC
Git mirrors since they're configured to sync with the http URL
instead of https (resulting in git-svn-id Git footers with the
http URL in the commit messages). Without rewriting this, it is
not possible to commit to SVN since Google code disallows committing
to a http URL.

BUG=412012
TEST=Created a CL using a Git repo like this (both before and after landing https://webrtc-codereview.appspot.com/32569004):
git clone https://chromium.googlesource.com/external/webrtc.git
cd webrtc
git auto-svn
git checkout master
git checkout -b test
(edit whitespace change)
git commit -am "Whitespace edit
git cl upload --bypass-hooks"
git cl dcommit

R=agable@chromium.org, machenbach@chromium.org, mmoss@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@293171 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/git_cl.py b/git_cl.py
index fbe52af..f50e702 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -284,6 +284,7 @@
     self.is_gerrit = None
     self.git_editor = None
     self.project = None
+    self.force_https_commit_url = None
     self.pending_ref_prefix = None
 
   def LazyUpdateIfNeeded(self):
@@ -455,6 +456,12 @@
       self.project = self._GetRietveldConfig('project', error_ok=True)
     return self.project
 
+  def GetForceHttpsCommitUrl(self):
+    if not self.force_https_commit_url:
+      self.force_https_commit_url = self._GetRietveldConfig(
+          'force-https-commit-url', error_ok=True)
+    return self.force_https_commit_url
+
   def GetPendingRefPrefix(self):
     if not self.pending_ref_prefix:
       self.pending_ref_prefix = self._GetRietveldConfig(
@@ -674,6 +681,19 @@
     return RunGit(['config', 'branch.%s.base-url' % self.GetBranch()],
                   error_ok=True).strip()
 
+  def GetGitSvnRemoteUrl(self):
+    """Return the configured git-svn remote URL parsed from git svn info.
+
+    Returns None if it is not set.
+    """
+    # URL is dependent on the current directory.
+    data = RunGit(['svn', 'info'], cwd=settings.GetRoot())
+    if data:
+      keys = dict(line.split(': ', 1) for line in data.splitlines()
+                  if ': ' in line)
+      return keys.get('URL', None)
+    return None
+
   def GetRemoteUrl(self):
     """Return the configured remote URL, e.g. 'git://example.org/foo.git/'.
 
@@ -1170,6 +1190,8 @@
   SetProperty('viewvc-url', 'VIEW_VC', unset_error_ok=True)
   SetProperty('bug-prefix', 'BUG_PREFIX', unset_error_ok=True)
   SetProperty('cpplint-regex', 'LINT_REGEX', unset_error_ok=True)
+  SetProperty('force-https-commit-url', 'FORCE_HTTPS_COMMIT_URL',
+              unset_error_ok=True)
   SetProperty('cpplint-ignore-regex', 'LINT_IGNORE_REGEX', unset_error_ok=True)
   SetProperty('project', 'PROJECT', unset_error_ok=True)
   SetProperty('pending-ref-prefix', 'PENDING_REF_PREFIX', unset_error_ok=True)
@@ -1718,12 +1740,7 @@
   remote_url = cl.GetGitBaseUrlFromConfig()
   if not remote_url:
     if settings.GetIsGitSvn():
-      # URL is dependent on the current directory.
-      data = RunGit(['svn', 'info'], cwd=settings.GetRoot())
-      if data:
-        keys = dict(line.split(': ', 1) for line in data.splitlines()
-                    if ': ' in line)
-        remote_url = keys.get('URL', None)
+      remote_url = cl.GetGitSvnRemoteUrl()
     else:
       if cl.GetRemoteUrl() and '/' in cl.GetUpstreamBranch():
         remote_url = (cl.GetRemoteUrl() + '@'
@@ -2098,9 +2115,19 @@
         revision = RunGit(['rev-parse', 'HEAD']).strip()
     else:
       # dcommit the merge branch.
-      _, output = RunGitWithCode(['svn', 'dcommit',
-                                  '-C%s' % options.similarity,
-                                  '--no-rebase', '--rmdir'])
+      cmd = [
+        'svn', 'dcommit',
+        '-C%s' % options.similarity,
+        '--no-rebase', '--rmdir',
+      ]
+      if settings.GetForceHttpsCommitUrl():
+        # Allow forcing https commit URLs for some projects that don't allow
+        # committing to http URLs (like Google Code).
+        remote_url = cl.GetGitSvnRemoteUrl()
+        if urlparse.urlparse(remote_url).scheme == 'http':
+          remote_url = remote_url.replace('http://', 'https://')
+        cmd.append('--commit-url=%s' % remote_url)
+      _, output = RunGitWithCode(cmd)
       if 'Committed r' in output:
         revision = re.match(
           '.*?\nCommitted r(\\d+)', output, re.DOTALL).group(1)
diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py
index 572d51f..36730bc 100755
--- a/tests/git_cl_test.py
+++ b/tests/git_cl_test.py
@@ -350,6 +350,7 @@
          'Issue: 12345\n\nR=john@chromium.org\n\n'
          'Review URL: https://codereview.example.com/12345'],),
        ''),
+      ((['git', 'config', 'rietveld.force-https-commit-url'],), ''),
       ((['git',
          'svn', 'dcommit', '-C50', '--no-rebase', '--rmdir'],),
        (('', None), 0)),
@@ -687,6 +688,8 @@
         ((['git', 'config', '--unset-all',
            'rietveld.cpplint-regex'],), ''),
         ((['git', 'config', '--unset-all',
+           'rietveld.force-https-commit-url'],), ''),
+        ((['git', 'config', '--unset-all',
            'rietveld.cpplint-ignore-regex'],), ''),
         ((['git', 'config', '--unset-all',
            'rietveld.project'],), ''),