(
repo_dir: Path,
branch: str,
enabled: bool,
response_language: str = "zh",
)
| 1504 | if remote_url: |
| 1505 | if "origin" in remotes: |
| 1506 | current = _git_output(["remote", "get-url", "origin"], repo_dir) |
| 1507 | if current != remote_url: |
| 1508 | _run_git(["remote", "set-url", "origin", remote_url], repo_dir) |
| 1509 | else: |
| 1510 | _run_git(["remote", "add", "origin", remote_url], repo_dir) |
| 1511 | if remote_url: |
| 1512 | _run_git(["fetch", "origin", branch], repo_dir, timeout=120) |
| 1513 | try: |
| 1514 | _git_output(["rev-parse", "--verify", "HEAD"], repo_dir) |
| 1515 | except subprocess.CalledProcessError: |
| 1516 | _run_git(["symbolic-ref", "HEAD", f"refs/heads/{branch}"], repo_dir) |
| 1517 | _run_git(["update-ref", f"refs/heads/{branch}", f"refs/remotes/origin/{branch}"], repo_dir) |
| 1518 | _run_git(["reset", "--mixed"], repo_dir) |
| 1519 | |
| 1520 | |
| 1521 | def _rebase_notes_branch(repo_dir: Path, branch: str, language: str) -> str: |
| 1522 | try: |
| 1523 | result = _run_git(["rebase", f"origin/{branch}"], repo_dir, timeout=120) |
| 1524 | return (result.stdout or result.stderr or "").strip() |
| 1525 | except subprocess.CalledProcessError as exc: |
| 1526 | rebase_output = (exc.stderr or exc.stdout or str(exc)).strip() |
| 1527 | _run_git(["rebase", "--abort"], repo_dir, check=False, timeout=60) |
| 1528 | raise RuntimeError( |
| 1529 | ( |
| 1530 | "GitHub sync needs remote changes, but automatic rebase conflicted. " |
| 1531 | f"Resolve the reading-notes repo manually, then sync again: {rebase_output}" |
| 1532 | ) |
| 1533 | if language == "en" |
| 1534 | else ( |
| 1535 | "GitHub 同步需要合并远端新提交,但自动 rebase 发生冲突。" |
| 1536 | f"请手动处理阅读笔记仓库冲突后再同步:{rebase_output}" |
| 1537 | ) |
| 1538 | ) from exc |
| 1539 | |
| 1540 | |
| 1541 | def _is_non_fast_forward_push(output: str) -> bool: |
| 1542 | normalized = output.lower() |
| 1543 | return "non-fast-forward" in normalized or "fetch first" in normalized |
| 1544 | |
| 1545 | |
| 1546 | def _guard_reading_notes_deletions(repo_dir: Path, language: str) -> None: |
| 1547 | deleted = [ |
| 1548 | line.strip() |
| 1549 | for line in _git_output(["diff", "--cached", "--name-only", "--diff-filter=D"], repo_dir).splitlines() |
| 1550 | if line.strip() |
| 1551 | ] |
| 1552 | if not deleted: |
| 1553 | return |
| 1554 | _run_git(["checkout", "HEAD", "--", *deleted], repo_dir, check=False) |
| 1555 | preview = "\n".join(f"- {path}" for path in deleted[:12]) |
| 1556 | if len(deleted) > 12: |
| 1557 | preview += f"\n- ... and {len(deleted) - 12} more" |
| 1558 | raise RuntimeError( |
| 1559 | ( |
| 1560 | "GitHub sync blocked because it would delete reading-note files. " |
| 1561 | "PaperFlow GUI sync is additive by default; delete files manually with Git if that is intended.\n" |
| 1562 | f"{preview}" |
| 1563 | ) |
no test coverage detected