1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""Repository discovery for the pgt TUI."""
from __future__ import annotations
from pathlib import Path
from pygit2 import GitError, Repository, discover_repository
from pygittools.tui.types import PageContext
def open_page_context(start: Path | None = None) -> PageContext:
"""Discover a git repository from ``start`` (or the current directory)."""
root = (start or Path.cwd()).resolve()
discovered = discover_repository(str(root))
if discovered is None:
raise GitError(f"No git repository found under {root}")
repo_path = Path(discovered)
return PageContext(repo=Repository(str(repo_path)), repo_path=repo_path)
def try_open_page_context(start: Path | None = None) -> PageContext | None:
try:
return open_page_context(start)
except GitError:
return None