def checkout_project(api, proj, proj_config, root_dir, patch=None):
  """
  Args:
    proj: luci-config project name to checkout.
    proj_config: The recipes.cfg configuration for the project.
    root_dir: The temporary directory to check the project out in.
    patch: optional patch to apply to checkout.

  Returns:
    Path to repo on disk.
  """
  checkout_path = root_dir.join(proj)
  repo_path = checkout_path.join(proj)
  api.file.makedirs('%s directory' % proj, repo_path)

  # Not working yet, but maybe??
  #api.file.rmtree('clean old %s repo' % proj, checkout_path)

  config = api.gclient.make_config(
      GIT_MODE=True, CACHE_DIR=root_dir.join("__cache_dir"))
  soln = config.solutions.add()
  soln.name = proj
  soln.url = proj_config['repo_url']

  kwargs = {
      'suffix': proj,
      'gclient_config': config,
      'force': True,
      'cwd': checkout_path,
  }
  if patch:
    kwargs['rietveld'] = patch.server
    kwargs['issue'] = patch.issue
    kwargs['patchset'] = patch.patchset
  else:
    kwargs['patch'] = False

  api.bot_update.ensure_checkout(**kwargs)
  return repo_path