"""~/.skills 仓库内的 git 操作。""" from __future__ import annotations import subprocess import sys from pathlib import Path from skiff.paths import SKILLS_HOME def _run_git(repo: Path, *args: str, check: bool = True) -> subprocess.CompletedProcess[str]: return subprocess.run( ["git", "-C", str(repo), *args], check=check, text=True, capture_output=not sys.stdout.isatty(), ) def ensure_git_repo(repo: Path) -> None: if not (repo / ".git").exists(): raise SystemExit(f"不是 git 仓库: {repo}") def has_staged_changes(repo: Path) -> bool: result = _run_git(repo, "diff", "--cached", "--quiet", check=False) return result.returncode == 1 def publish( *, paths: list[str], message: str | None, push: bool, no_commit: bool, ) -> None: repo = SKILLS_HOME.resolve() ensure_git_repo(repo) _run_git(repo, "add", "--", *paths) if not has_staged_changes(repo): print("没有可提交的变更", file=sys.stdout) return if no_commit: print(f"已暂存变更: {', '.join(paths)}", file=sys.stdout) return if not message: raise SystemExit("提交需要 -m/--message") _run_git(repo, "commit", "-m", message) print(f"已提交: {message}", file=sys.stdout) if push: _run_git(repo, "push") print("已推送到远程", file=sys.stdout)