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
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 -> <repo root> = parents[5]
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_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",
]

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