(target: Path)
| 414 | |
| 415 | |
| 416 | def git_target_metadata(target: Path) -> dict[str, Any]: |
| 417 | is_git = git_output(target, "rev-parse", "--git-dir") is not None |
| 418 | is_worktree = git_output(target, "rev-parse", "--is-inside-work-tree") == "true" |
| 419 | revision = git_output(target, "rev-parse", "--verify", "HEAD") |
| 420 | repository_root = git_output(target, "rev-parse", "--show-toplevel") if is_worktree else None |
| 421 | supported = ( |
| 422 | is_git |
| 423 | and is_worktree |
| 424 | and revision is not None |
| 425 | and repository_root is not None |
| 426 | and Path(repository_root).resolve() == target |
| 427 | ) |
| 428 | metadata: dict[str, Any] = { |
| 429 | "hasHead": revision is not None, |
| 430 | "isGit": is_git, |
| 431 | "isWorktree": is_worktree, |
| 432 | "reviewChangesSupported": supported, |
| 433 | } |
| 434 | if not is_git: |
| 435 | return metadata |
| 436 | branch = git_output(target, "symbolic-ref", "--quiet", "--short", "HEAD") |
| 437 | metadata.update({"branch": branch, "detachedHead": revision is not None and branch is None}) |
| 438 | if revision is not None: |
| 439 | metadata.update( |
| 440 | { |
| 441 | "commitSubject": git_output(target, "show", "-s", "--format=%s", "HEAD"), |
| 442 | "revision": revision, |
| 443 | "shortRevision": revision[:7], |
| 444 | } |
| 445 | ) |
| 446 | return metadata |
| 447 | |
| 448 | |
| 449 | def main() -> None: |
no test coverage detected