diff --git a/src/pkgmgr/actions/release/files.py b/src/pkgmgr/actions/release/files.py index d1f2481..c56f13b 100644 --- a/src/pkgmgr/actions/release/files.py +++ b/src/pkgmgr/actions/release/files.py @@ -24,6 +24,8 @@ import tempfile from datetime import date, datetime from typing import Optional, Tuple +from pkgmgr.core.git.queries import get_config_value + # --------------------------------------------------------------------------- # Editor helper for interactive changelog messages @@ -74,10 +76,7 @@ def _open_editor_for_changelog(initial_message: Optional[str] = None) -> str: except OSError: pass - lines = [ - line for line in content.splitlines() - if not line.strip().startswith("#") - ] + lines = [line for line in content.splitlines() if not line.strip().startswith("#")] return "\n".join(lines).strip() @@ -85,6 +84,7 @@ def _open_editor_for_changelog(initial_message: Optional[str] = None) -> str: # File update helpers (pyproject + extra packaging + changelog) # --------------------------------------------------------------------------- + def update_pyproject_version( pyproject_path: str, new_version: str, @@ -365,24 +365,6 @@ def update_changelog( # --------------------------------------------------------------------------- -def _get_git_config_value(key: str) -> Optional[str]: - """ - Try to read a value from `git config --get `. - """ - try: - result = subprocess.run( - ["git", "config", "--get", key], - capture_output=True, - text=True, - check=False, - ) - except Exception: - return None - - value = result.stdout.strip() - return value or None - - def _get_debian_author() -> Tuple[str, str]: """ Determine the maintainer name/email for debian/changelog entries. @@ -396,9 +378,9 @@ def _get_debian_author() -> Tuple[str, str]: email = os.environ.get("GIT_AUTHOR_EMAIL") if not name: - name = _get_git_config_value("user.name") + name = get_config_value("user.name") if not email: - email = _get_git_config_value("user.email") + email = get_config_value("user.email") if not name: name = "Unknown Maintainer" diff --git a/src/pkgmgr/core/git/queries/__init__.py b/src/pkgmgr/core/git/queries/__init__.py index 1a3ff9e..6874233 100644 --- a/src/pkgmgr/core/git/queries/__init__.py +++ b/src/pkgmgr/core/git/queries/__init__.py @@ -9,6 +9,7 @@ from .get_remote_push_urls import get_remote_push_urls from .probe_remote_reachable import probe_remote_reachable from .get_changelog import get_changelog, GitChangelogQueryError from .get_tags_at_ref import get_tags_at_ref, GitTagsAtRefQueryError +from .get_config_value import get_config_value __all__ = [ "get_current_branch", @@ -23,4 +24,5 @@ __all__ = [ "GitChangelogQueryError", "get_tags_at_ref", "GitTagsAtRefQueryError", + "get_config_value", ] diff --git a/src/pkgmgr/core/git/queries/get_config_value.py b/src/pkgmgr/core/git/queries/get_config_value.py new file mode 100644 index 0000000..c859d5e --- /dev/null +++ b/src/pkgmgr/core/git/queries/get_config_value.py @@ -0,0 +1,34 @@ +from __future__ import annotations + +from typing import Optional + +from ..errors import GitError +from ..run import run + + +def _is_missing_key_error(exc: GitError) -> bool: + msg = str(exc).lower() + + # Ensure we only swallow the expected case for THIS command. + if "git config --get" not in msg: + return False + + # 'git config --get' returns exit code 1 when the key is not set. + return "exit code: 1" in msg + +def get_config_value(key: str, *, cwd: str = ".") -> Optional[str]: + """ + Return a value from `git config --get `, or None if not set. + + We keep core.git.run() strict (check=True) and interpret the known + 'not set' exit-code case here. + """ + try: + output = run(["config", "--get", key], cwd=cwd) + except GitError as exc: + if _is_missing_key_error(exc): + return None + raise + + output = output.strip() + return output or None