def METHOD_NAME(git_url, checkout_dir, git_callbacks=None, branch=None):
  """Clone with retries."""
  logging.info('Cloning %s to %s', git_url, checkout_dir)
  for attempt in range(CLONE_TRIES):
    try:
      repo = clone(git_url, checkout_dir, git_callbacks)
      repo.cache = {}
      if branch:
        _checkout_branch(repo, branch)
      return repo
    except (pygit2.GitError, subprocess.CalledProcessError) as e:
      if attempt == CLONE_TRIES - 1:
        err_str = str(e)
        if isinstance(e, subprocess.CalledProcessError):
          # add the git output to the log
          err_str = f'{err_str}\n{e.stderr.decode()}'
        logging.error('Clone failed after %d attempts: %s', CLONE_TRIES,
                      err_str)
      shutil.rmtree(checkout_dir, ignore_errors=True)
      time.sleep(RETRY_SLEEP_SECONDS)
      continue
  return None