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
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:
@@ -14,7 +14,7 @@ with the expected structure:
|
|||||||
|
|
||||||
For each discovered repository, the function:
|
For each discovered repository, the function:
|
||||||
• derives provider, account, repository from the folder structure
|
• 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
|
• generates a unique CLI alias
|
||||||
• marks ignore=True for newly discovered repos
|
• marks ignore=True for newly discovered repos
|
||||||
• skips repos already known in defaults or user config
|
• skips repos already known in defaults or user config
|
||||||
@@ -23,11 +23,11 @@ For each discovered repository, the function:
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
|
||||||
from pkgmgr.core.command.alias import generate_alias
|
from pkgmgr.core.command.alias import generate_alias
|
||||||
from pkgmgr.core.config.save import save_user_config
|
from pkgmgr.core.config.save import save_user_config
|
||||||
|
from pkgmgr.core.git.queries import get_latest_commit
|
||||||
|
|
||||||
|
|
||||||
def config_init(
|
def config_init(
|
||||||
@@ -116,27 +116,18 @@ def config_init(
|
|||||||
|
|
||||||
print(f"[ADD] {provider}/{account}/{repo_name}")
|
print(f"[ADD] {provider}/{account}/{repo_name}")
|
||||||
|
|
||||||
# Determine commit hash
|
# Determine commit hash via git query
|
||||||
try:
|
verified_commit = get_latest_commit(repo_path) or ""
|
||||||
result = subprocess.run(
|
if verified_commit:
|
||||||
["git", "log", "-1", "--format=%H"],
|
print(f"[INFO] Latest commit: {verified_commit}")
|
||||||
cwd=repo_path,
|
else:
|
||||||
stdout=subprocess.PIPE,
|
print("[WARN] Could not read commit (not a git repo or no commits).")
|
||||||
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}")
|
|
||||||
|
|
||||||
entry = {
|
entry: Dict[str, Any] = {
|
||||||
"provider": provider,
|
"provider": provider,
|
||||||
"account": account,
|
"account": account,
|
||||||
"repository": repo_name,
|
"repository": repo_name,
|
||||||
"verified": {"commit": verified},
|
"verified": {"commit": verified_commit},
|
||||||
"ignore": True,
|
"ignore": True,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Lightweight helper functions around Git commands.
|
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.
|
details of subprocess handling.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from .errors import GitError
|
from .errors import GitError
|
||||||
from .run import run
|
from .run import run
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from .get_current_branch import get_current_branch
|
from .get_current_branch import get_current_branch
|
||||||
from .get_head_commit import get_head_commit
|
from .get_head_commit import get_head_commit
|
||||||
|
from .get_latest_commit import get_latest_commit
|
||||||
from .get_tags import get_tags
|
from .get_tags import get_tags
|
||||||
from .resolve_base_branch import GitBaseBranchNotFoundError, resolve_base_branch
|
from .resolve_base_branch import GitBaseBranchNotFoundError, resolve_base_branch
|
||||||
from .list_remotes import list_remotes
|
from .list_remotes import list_remotes
|
||||||
@@ -14,6 +15,7 @@ from .get_config_value import get_config_value
|
|||||||
__all__ = [
|
__all__ = [
|
||||||
"get_current_branch",
|
"get_current_branch",
|
||||||
"get_head_commit",
|
"get_head_commit",
|
||||||
|
"get_latest_commit",
|
||||||
"get_tags",
|
"get_tags",
|
||||||
"resolve_base_branch",
|
"resolve_base_branch",
|
||||||
"GitBaseBranchNotFoundError",
|
"GitBaseBranchNotFoundError",
|
||||||
|
|||||||
26
src/pkgmgr/core/git/queries/get_latest_commit.py
Normal file
26
src/pkgmgr/core/git/queries/get_latest_commit.py
Normal 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
|
||||||
Reference in New Issue
Block a user