Skip to content

Commit 05951f4

Browse files
fix
1 parent d3d98b9 commit 05951f4

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/gitingest/utils/git_utils.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,24 +215,38 @@ async def fetch_remote_branches_or_tags(url: str, *, ref_type: str, token: str |
215215
If the ``ref_type`` parameter is not "branches" or "tags".
216216
217217
"""
218+
if ref_type not in ("branches", "tags"):
219+
msg = f"Invalid fetch type: {ref_type}"
220+
raise ValueError(msg)
221+
218222
cmd = ["git"]
219223

220224
# Add authentication if needed
221225
if token and is_github_host(url):
222226
cmd += ["-c", create_git_auth_header(token, url=url)]
223227

224-
cmd += ["ls-remote", "--heads", url]
228+
cmd += ["ls-remote"]
229+
230+
fetch_tags = ref_type == "tags"
231+
to_fetch = "tags" if fetch_tags else "heads"
232+
233+
cmd += [f"--{to_fetch}"]
234+
235+
# `--refs` filters out the peeled tag objects (those ending with "^{}") (for tags)
236+
if fetch_tags:
237+
cmd += ["--refs"]
238+
239+
cmd += [url]
225240

226241
await ensure_git_installed()
227242
stdout, _ = await run_command(*cmd)
228-
229243
# For each line in the output:
230244
# - Skip empty lines and lines that don't contain "refs/{to_fetch}/"
231245
# - Extract the branch or tag name after "refs/{to_fetch}/"
232246
return [
233-
line.split("refs/heads/", 1)[1]
247+
line.split(f"refs/{to_fetch}/", 1)[1]
234248
for line in stdout.decode().splitlines()
235-
if line.strip() and "refs/heads/" in line
249+
if line.strip() and f"refs/{to_fetch}/" in line
236250
]
237251

238252

0 commit comments

Comments
 (0)