From 63e1b3d145aab4f222c74a8d785775792fc6803f Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Tue, 16 Dec 2025 13:23:36 +0100 Subject: [PATCH] core.git: add get_repo_root query and use it in repository scaffold https://chatgpt.com/share/69414f70-fc60-800f-ba6a-cbea426ea913 --- src/pkgmgr/actions/repository/scaffold.py | 22 ++++++------------- src/pkgmgr/core/git/queries/__init__.py | 2 ++ src/pkgmgr/core/git/queries/get_repo_root.py | 23 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 src/pkgmgr/core/git/queries/get_repo_root.py diff --git a/src/pkgmgr/actions/repository/scaffold.py b/src/pkgmgr/actions/repository/scaffold.py index fa9ad6a..8bfd703 100644 --- a/src/pkgmgr/actions/repository/scaffold.py +++ b/src/pkgmgr/actions/repository/scaffold.py @@ -1,10 +1,12 @@ +# src/pkgmgr/actions/repository/scaffold.py from __future__ import annotations import os -import subprocess from pathlib import Path from typing import Any, Dict, Optional +from pkgmgr.core.git.queries import get_repo_root + try: from jinja2 import Environment, FileSystemLoader, StrictUndefined except Exception as exc: # pragma: no cover @@ -22,20 +24,10 @@ def _repo_root_from_here(anchor: Optional[Path] = None) -> str: Fallback to a conservative relative parent lookup. """ here = (anchor or Path(__file__)).resolve().parent - try: - r = subprocess.run( - ["git", "rev-parse", "--show-toplevel"], - cwd=str(here), - check=False, - capture_output=True, - text=True, - ) - if r.returncode == 0: - top = (r.stdout or "").strip() - if top: - return top - except Exception: - pass + + top = get_repo_root(cwd=str(here)) + if top: + return top # Fallback: src/pkgmgr/actions/repository/scaffold.py -> = parents[5] p = (anchor or Path(__file__)).resolve() diff --git a/src/pkgmgr/core/git/queries/__init__.py b/src/pkgmgr/core/git/queries/__init__.py index 6952657..fcf50e0 100644 --- a/src/pkgmgr/core/git/queries/__init__.py +++ b/src/pkgmgr/core/git/queries/__init__.py @@ -13,6 +13,7 @@ from .get_tags_at_ref import get_tags_at_ref, GitTagsAtRefQueryError from .get_config_value import get_config_value from .get_upstream_ref import get_upstream_ref from .list_tags import list_tags +from .get_repo_root import get_repo_root __all__ = [ "get_current_branch", @@ -31,4 +32,5 @@ __all__ = [ "get_config_value", "get_upstream_ref", "list_tags", + "get_repo_root", ] diff --git a/src/pkgmgr/core/git/queries/get_repo_root.py b/src/pkgmgr/core/git/queries/get_repo_root.py new file mode 100644 index 0000000..3de4cee --- /dev/null +++ b/src/pkgmgr/core/git/queries/get_repo_root.py @@ -0,0 +1,23 @@ +# src/pkgmgr/core/git/queries/get_repo_root.py +from __future__ import annotations + +from typing import Optional + +from ..errors import GitError +from ..run import run + + +def get_repo_root(*, cwd: str = ".") -> Optional[str]: + """ + Return the git repository root directory (top-level), or None if not available. + + Equivalent to: + git rev-parse --show-toplevel + """ + try: + out = run(["rev-parse", "--show-toplevel"], cwd=cwd) + except GitError: + return None + + out = out.strip() + return out or None