f3cd56b78e
Use ~/.pouch, the pouch CLI, and .pouch.yaml as the SSOT container. Keep the inner skills/ packages, and store ACK project state in .pouch/ack instead of docs/ack.
60 lines
1.4 KiB
Python
60 lines
1.4 KiB
Python
"""~/.pouch 仓库内的 git 操作。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from pouch.paths import POUCH_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 = POUCH_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)
|