from typing import Tuple
import pathlib


def _parse_github_path(path: str) -> Tuple[str, str, str]:
  """Parse the absolute github path.

  Args:
    path: The full github path.

  Returns:
    repo: The repository identifiant.
    branch: Repository branch.
    path: The inner path.

  Raises:
    ValueError: If the path is invalid
  """
  if path.endswith('/'):
    raise ValueError(
        f'Invalid github path: {path}. Trailing `/` not supported.'
    )
  parts = pathlib.PurePosixPath(path).parts
  if len(parts) < 5:
    raise ValueError(f'Invalid github path: {path}')

  # '/', 'tensorflow', 'datasets', 'tree', 'master', ...
  root, owner, repo, tree, branch, *sub_path = parts
  if root != '/' or tree != 'tree':
    raise ValueError(
        f'Invalid github path: {path}. Expected format: '
        '`/<owner>/<name>/tree/<branch>[/<path>]`. Note that `/blob/` isn\'t '
        'accepted. Only `/tree/`.'
    )

  return f'{owner}/{repo}', branch, '/'.join(sub_path)