Abort git tools if git repo has too many local branches.

This is a stopgap to prevent people with >20 local branches from running tools
like 'git rebase-update'. This usually indicates that the user isn't in the
habit of cleaning up old branches, which makes these tools unlikely to be useful
in the current state of their repo anyway.

R=agable@chromium.org
BUG=

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@263978 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/git_common.py b/git_common.py
index d730fed..6952c1e 100644
--- a/git_common.py
+++ b/git_common.py
@@ -25,6 +25,7 @@
 import signal
 import sys
 import tempfile
+import textwrap
 import threading
 
 import subprocess2
@@ -239,7 +240,27 @@
 
 def branches(*args):
   NO_BRANCH = ('* (no branch', '* (detached from ')
-  for line in run('branch', *args).splitlines():
+
+  key = 'depot-tools.branch-limit'
+  limit = 20
+  try:
+    limit = int(config(key, limit))
+  except ValueError:
+    pass
+
+  raw_branches = run('branch', *args).splitlines()
+
+  num = len(raw_branches)
+  if num > limit:
+    print >> sys.stderr, textwrap.dedent("""\
+    Your git repo has too many branches (%d/%d) for this tool to work well.
+
+    You may adjust this limit by running:
+      git config %s <new_limit>
+    """ % (num, limit, key))
+    sys.exit(1)
+
+  for line in raw_branches:
     if line.startswith(NO_BRANCH):
       continue
     yield line.split()[-1]