Return the commit SHA for `reference` from `git ls-remote` output. Annotated tags list the tag object at `refs/tags/ ` and the commit at `refs/tags/ ^{}`. Prefer the peeled commit.
(output: str, reference: str)
| 338 | |
| 339 | |
| 340 | def commit_from_ls_remote(output: str, reference: str) -> str: |
| 341 | """Return the commit SHA for `reference` from `git ls-remote` output. |
| 342 | |
| 343 | Annotated tags list the tag object at `refs/tags/<name>` and the commit at |
| 344 | `refs/tags/<name>^{}`. Prefer the peeled commit. |
| 345 | """ |
| 346 | peeled: dict[str, str] = {} |
| 347 | plain: dict[str, str] = {} |
| 348 | for line in output.splitlines(): |
| 349 | if not line.strip(): |
| 350 | continue |
| 351 | sha, _, ref = line.partition("\t") |
| 352 | sha = sha.lower() |
| 353 | if ref.endswith("^{}"): |
| 354 | peeled[ref[: -len("^{}")]] = sha |
| 355 | else: |
| 356 | plain[ref] = sha |
| 357 | tag_ref = f"refs/tags/{reference}" |
| 358 | head_ref = f"refs/heads/{reference}" |
| 359 | if tag_ref in peeled: |
| 360 | return peeled[tag_ref] |
| 361 | if tag_ref in plain: |
| 362 | return plain[tag_ref] |
| 363 | if head_ref in plain: |
| 364 | return plain[head_ref] |
| 365 | if reference in plain: |
| 366 | return plain[reference] |
| 367 | if len(peeled) == 1: |
| 368 | return next(iter(peeled.values())) |
| 369 | if len(plain) == 1: |
| 370 | return next(iter(plain.values())) |
| 371 | raise PinError(f"could not resolve {reference}") |
| 372 | |
| 373 | |
| 374 | def resolve_reference(repository: str, reference: str) -> str: |