refactor(git): add get_latest_commit query and remove subprocess usage
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

- Introduce core.git query get_latest_commit()
- Refactor config init to use git query instead of subprocess
- Fix __future__ import order in core.git package
- Export new query via core.git.queries API

https://chatgpt.com/share/69413c3e-3bcc-800f-b3b0-a3bf3b7bb875
This commit is contained in:
Kevin Veen-Birkenbach
2025-12-16 12:02:09 +01:00
parent 2a66c082eb
commit bb23bd94f2
4 changed files with 40 additions and 21 deletions

View File

@@ -14,7 +14,7 @@ with the expected structure:
For each discovered repository, the function:
• derives provider, account, repository from the folder structure
• (optionally) determines the latest commit hash via git log
• (optionally) determines the latest commit hash via git
• generates a unique CLI alias
• marks ignore=True for newly discovered repos
• skips repos already known in defaults or user config
@@ -23,11 +23,11 @@ For each discovered repository, the function:
from __future__ import annotations
import os
import subprocess
from typing import Any, Dict
from pkgmgr.core.command.alias import generate_alias
from pkgmgr.core.config.save import save_user_config
from pkgmgr.core.git.queries import get_latest_commit
def config_init(
@@ -116,27 +116,18 @@ def config_init(
print(f"[ADD] {provider}/{account}/{repo_name}")
# Determine commit hash
try:
result = subprocess.run(
["git", "log", "-1", "--format=%H"],
cwd=repo_path,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
)
verified = result.stdout.strip()
print(f"[INFO] Latest commit: {verified}")
except Exception as exc:
verified = ""
print(f"[WARN] Could not read commit: {exc}")
# Determine commit hash via git query
verified_commit = get_latest_commit(repo_path) or ""
if verified_commit:
print(f"[INFO] Latest commit: {verified_commit}")
else:
print("[WARN] Could not read commit (not a git repo or no commits).")
entry = {
entry: Dict[str, Any] = {
"provider": provider,
"account": account,
"repository": repo_name,
"verified": {"commit": verified},
"verified": {"commit": verified_commit},
"ignore": True,
}

View File

@@ -1,3 +1,5 @@
from __future__ import annotations
"""
Lightweight helper functions around Git commands.
@@ -6,8 +8,6 @@ logic (release, version, changelog) does not have to deal with the
details of subprocess handling.
"""
from __future__ import annotations
from .errors import GitError
from .run import run

View File

@@ -2,6 +2,7 @@ from __future__ import annotations
from .get_current_branch import get_current_branch
from .get_head_commit import get_head_commit
from .get_latest_commit import get_latest_commit
from .get_tags import get_tags
from .resolve_base_branch import GitBaseBranchNotFoundError, resolve_base_branch
from .list_remotes import list_remotes
@@ -14,6 +15,7 @@ from .get_config_value import get_config_value
__all__ = [
"get_current_branch",
"get_head_commit",
"get_latest_commit",
"get_tags",
"resolve_base_branch",
"GitBaseBranchNotFoundError",

View File

@@ -0,0 +1,26 @@
from __future__ import annotations
from typing import Optional
from ..errors import GitError
from ..run import run
def get_latest_commit(cwd: str = ".") -> Optional[str]:
"""
Return the latest commit hash for the repository in `cwd`.
Equivalent to:
git log -1 --format=%H
Returns:
The commit hash string, or None if it cannot be determined
(e.g. not a git repo, no commits, or other git failure).
"""
try:
output = run(["log", "-1", "--format=%H"], cwd=cwd)
except GitError:
return None
output = output.strip()
return output or None