MCPcopy
hub / github.com/coderamp-labs/gitingest / check_repo_exists

Function check_repo_exists

src/gitingest/utils/git_utils.py:114–157  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

112
113
114async 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
160def _parse_github_url(url: str) -> tuple[str, str, str]:

Callers 4

clone_repoFunction · 0.90
test_check_repo_existsFunction · 0.90

Calls 2

is_github_hostFunction · 0.85
_parse_github_urlFunction · 0.85

Tested by 2

test_check_repo_existsFunction · 0.72