Add a setting to squash Gerrit uploads by default, and a --no-squash command line option to override it.

BUG=nope

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@298260 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/git_cl.py b/git_cl.py
index e99509f..b5289f7 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -434,6 +434,7 @@
     self.viewvc_url = None
     self.updated = False
     self.is_gerrit = None
+    self.squash_gerrit_uploads = None
     self.git_editor = None
     self.project = None
     self.force_https_commit_url = None
@@ -599,6 +600,14 @@
       self.is_gerrit = self._GetConfig('gerrit.host', error_ok=True)
     return self.is_gerrit
 
+  def GetSquashGerritUploads(self):
+    """Return true if uploads to Gerrit should be squashed by default."""
+    if self.squash_gerrit_uploads is None:
+      self.squash_gerrit_uploads = (
+          RunGit(['config', '--bool', 'gerrit.squash-uploads'],
+                 error_ok=True).strip() == 'true')
+    return self.squash_gerrit_uploads
+
   def GetGitEditor(self):
     """Return the editor specified in the git config, or None if none is."""
     if self.git_editor is None:
@@ -1387,6 +1396,10 @@
   if 'GERRIT_HOST' in keyvals:
     RunGit(['config', 'gerrit.host', keyvals['GERRIT_HOST']])
 
+  if 'GERRIT_SQUASH_UPLOADS' in keyvals:
+    RunGit(['config', 'gerrit.squash-uploads',
+            keyvals['GERRIT_SQUASH_UPLOADS']])
+
   if 'PUSH_URL_CONFIG' in keyvals and 'ORIGIN_URL_CONFIG' in keyvals:
     #should be of the form
     #PUSH_URL_CONFIG: url.ssh://gitrw.chromium.org.pushinsteadof
@@ -2354,6 +2367,9 @@
                          'Default: remote branch head, or master')
   parser.add_option('--squash', action='store_true',
                     help='Squash multiple commits into one (Gerrit only)')
+  parser.add_option('--no-squash', action='store_true',
+                    help='Don\'t squash multiple commits into one ' +
+                         '(Gerrit only)')
   parser.add_option('--email', default=None,
                     help='email address to use to connect to Rietveld')
   parser.add_option('--tbr-owners', dest='tbr_owners', action='store_true',
@@ -2437,6 +2453,12 @@
 
   print_stats(options.similarity, options.find_copies, args)
   if settings.GetIsGerrit():
+    if options.squash and options.no_squash:
+      DieWithError('Can only use one of --squash or --no-squash')
+
+    options.squash = ((settings.GetSquashGerritUploads() or options.squash) and
+                      not options.no_squash)
+
     return GerritUpload(options, args, cl, change)
   ret = RietveldUpload(options, args, cl, change)
   if not ret: