core.git: add get_repo_root query and use it in repository scaffold
Some checks failed
Mark stable commit / test-unit (push) Has been cancelled
Mark stable commit / test-integration (push) Has been cancelled
Mark stable commit / test-env-virtual (push) Has been cancelled
Mark stable commit / test-env-nix (push) Has been cancelled
Mark stable commit / test-e2e (push) Has been cancelled
Mark stable commit / test-virgin-user (push) Has been cancelled
Mark stable commit / test-virgin-root (push) Has been cancelled
Mark stable commit / lint-shell (push) Has been cancelled
Mark stable commit / lint-python (push) Has been cancelled
Mark stable commit / mark-stable (push) Has been cancelled

https://chatgpt.com/share/69414f70-fc60-800f-ba6a-cbea426ea913
This commit is contained in:
Kevin Veen-Birkenbach
2025-12-16 13:23:36 +01:00
parent 2f89de1ff5
commit 63e1b3d145
3 changed files with 32 additions and 15 deletions

View File

@@ -1,10 +1,12 @@
# src/pkgmgr/actions/repository/scaffold.py
from __future__ import annotations from __future__ import annotations
import os import os
import subprocess
from pathlib import Path from pathlib import Path
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
from pkgmgr.core.git.queries import get_repo_root
try: try:
from jinja2 import Environment, FileSystemLoader, StrictUndefined from jinja2 import Environment, FileSystemLoader, StrictUndefined
except Exception as exc: # pragma: no cover 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. Fallback to a conservative relative parent lookup.
""" """
here = (anchor or Path(__file__)).resolve().parent here = (anchor or Path(__file__)).resolve().parent
try:
r = subprocess.run( top = get_repo_root(cwd=str(here))
["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: if top:
return top return top
except Exception:
pass
# Fallback: src/pkgmgr/actions/repository/scaffold.py -> <repo root> = parents[5] # Fallback: src/pkgmgr/actions/repository/scaffold.py -> <repo root> = parents[5]
p = (anchor or Path(__file__)).resolve() p = (anchor or Path(__file__)).resolve()

View File

@@ -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_config_value import get_config_value
from .get_upstream_ref import get_upstream_ref from .get_upstream_ref import get_upstream_ref
from .list_tags import list_tags from .list_tags import list_tags
from .get_repo_root import get_repo_root
__all__ = [ __all__ = [
"get_current_branch", "get_current_branch",
@@ -31,4 +32,5 @@ __all__ = [
"get_config_value", "get_config_value",
"get_upstream_ref", "get_upstream_ref",
"list_tags", "list_tags",
"get_repo_root",
] ]

View File

@@ -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