Commit 3f23cdf5 authored by iannucci@chromium.org's avatar iannucci@chromium.org

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
parent 7b654f59
......@@ -25,6 +25,7 @@ import re
import signal
import sys
import tempfile
import textwrap
import threading
import subprocess2
......@@ -239,7 +240,27 @@ def branch_config_map(option):
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]
......
......@@ -392,6 +392,23 @@ class GitMutableStructuredTest(git_test_utils.GitRepoReadWriteTestBase,
self.repo.git('branch', '--set-upstream-to', 'root_A', 'branch_G')
self.repo.git('branch', '--set-upstream-to', 'root_X', 'root_A')
def testTooManyBranches(self):
for i in xrange(30):
self.repo.git('branch', 'a'*i)
with self.assertRaises(SystemExit):
self.repo.run(list, self.gc.branches())
self.repo.git('config', 'depot-tools.branch-limit', 'cat')
with self.assertRaises(SystemExit):
self.repo.run(list, self.gc.branches())
self.repo.git('config', 'depot-tools.branch-limit', '100')
# should not raise
self.assertEqual(36, len(self.repo.run(list, self.gc.branches())))
def testMergeBase(self):
self.repo.git('checkout', 'branch_K')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment