Add `git cl checkout` to checkout by Rietveld issue

For example:

[5165c48...]d:\src\crashpad\crashpad>git cl checkout https://codereview.chromium.org/1355503005
Previous HEAD position was 5165c48... Use argument escaping function in util/win/process_info_test.cc
Switched to branch 'crashpad-info-cross-bitness'

[crashpad-info-cross-bitness]d:\src\crashpad\crashpad>git cl checkout 818433002
Multiple branches match issue 818433002:
0: file_io_open
1: file_io_open-2
2: file_io_open-3
Choose by index: 1
Switched to branch 'file_io_open-2'


R=dpranke@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@296823 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/git_cl.py b/git_cl.py
index aef7c4f..4964350 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -3541,6 +3541,50 @@
   return return_value
 
 
+@subcommand.usage('<codereview url or issue id>')
+def CMDcheckout(parser, args):
+  """Checks out a branch associated with a given Rietveld issue."""
+  _, args = parser.parse_args(args)
+
+  if len(args) != 1:
+    parser.print_help()
+    return 1
+
+  if re.match(r'\d+', args[0]):
+    target_issue = args[0]
+  elif args[0].startswith('http'):
+    target_issue = re.sub(r'.*/(\d+)/?', r'\1', args[0])
+  else:
+    parser.print_help()
+    return 1
+
+  key_and_issues = [x.split() for x in RunGit(
+      ['config', '--local', '--get-regexp', r'branch\..*\.rietveldissue'])
+      .splitlines()]
+  branches = []
+  for key, issue in key_and_issues:
+    if issue == target_issue:
+      branches.append(re.sub(r'branch\.(.*)\.rietveldissue', r'\1', key))
+
+  if len(branches) == 0:
+    print 'No branch found for issue %s.' % target_issue
+    return 1
+  if len(branches) == 1:
+    RunGit(['checkout', branches[0]])
+  else:
+    print 'Multiple branches match issue %s:' % target_issue
+    for i in range(len(branches)):
+      print '%d: %s' % (i, branches[i])
+    which = raw_input('Choose by index: ')
+    try:
+      RunGit(['checkout', branches[int(which)]])
+    except (IndexError, ValueError):
+      print 'Invalid selection, not checking out any branch.'
+      return 1
+
+  return 0
+
+
 def CMDlol(parser, args):
   # This command is intentionally undocumented.
   print zlib.decompress(base64.b64decode(