Check whether a remote Git repository is reachable. Parameters ---------- url : str URL of the Git repository to check. token : str | None GitHub personal access token (PAT) for accessing private repositories. Returns ------- bool ``True`` if the
(url: str, token: str | None = None)
| 112 | |
| 113 | |
| 114 | async def check_repo_exists(url: str, token: str | None = None) -> bool: |
| 115 | """Check whether a remote Git repository is reachable. |
| 116 | |
| 117 | Parameters |
| 118 | ---------- |
| 119 | url : str |
| 120 | URL of the Git repository to check. |
| 121 | token : str | None |
| 122 | GitHub personal access token (PAT) for accessing private repositories. |
| 123 | |
| 124 | Returns |
| 125 | ------- |
| 126 | bool |
| 127 | ``True`` if the repository exists, ``False`` otherwise. |
| 128 | |
| 129 | Raises |
| 130 | ------ |
| 131 | RuntimeError |
| 132 | If the host returns an unrecognised status code. |
| 133 | |
| 134 | """ |
| 135 | headers = {} |
| 136 | |
| 137 | if token and is_github_host(url): |
| 138 | host, owner, repo = _parse_github_url(url) |
| 139 | # Public GitHub vs. GitHub Enterprise |
| 140 | base_api = "https://api.github.com" if host == "github.com" else f"https://{host}/api/v3" |
| 141 | url = f"{base_api}/repos/{owner}/{repo}" |
| 142 | headers["Authorization"] = f"Bearer {token}" |
| 143 | |
| 144 | async with httpx.AsyncClient(follow_redirects=True) as client: |
| 145 | try: |
| 146 | response = await client.head(url, headers=headers) |
| 147 | except httpx.RequestError: |
| 148 | return False |
| 149 | |
| 150 | status_code = response.status_code |
| 151 | |
| 152 | if status_code == HTTP_200_OK: |
| 153 | return True |
| 154 | if status_code in {HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN, HTTP_404_NOT_FOUND}: |
| 155 | return False |
| 156 | msg = f"Unexpected HTTP status {status_code} for {url}" |
| 157 | raise RuntimeError(msg) |
| 158 | |
| 159 | |
| 160 | def _parse_github_url(url: str) -> tuple[str, str, str]: |