diff --git a/src/pkgmgr/actions/config/init.py b/src/pkgmgr/actions/config/init.py index 358991a..93950a8 100644 --- a/src/pkgmgr/actions/config/init.py +++ b/src/pkgmgr/actions/config/init.py @@ -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, } diff --git a/src/pkgmgr/core/git/__init__.py b/src/pkgmgr/core/git/__init__.py index d9fb03b..2f266c3 100644 --- a/src/pkgmgr/core/git/__init__.py +++ b/src/pkgmgr/core/git/__init__.py @@ -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 diff --git a/src/pkgmgr/core/git/queries/__init__.py b/src/pkgmgr/core/git/queries/__init__.py index 6874233..d0890ff 100644 --- a/src/pkgmgr/core/git/queries/__init__.py +++ b/src/pkgmgr/core/git/queries/__init__.py @@ -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", diff --git a/src/pkgmgr/core/git/queries/get_latest_commit.py b/src/pkgmgr/core/git/queries/get_latest_commit.py new file mode 100644 index 0000000..a748fb2 --- /dev/null +++ b/src/pkgmgr/core/git/queries/get_latest_commit.py @@ -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