| 1701 | _ensure_notes_git_repo(repo_dir, remote_url, branch) |
| 1702 | _ensure_notes_gitignore(repo_dir) |
| 1703 | |
| 1704 | pulled, remote_conflict_discarded, remote_first_message = _remote_first_sync_notes_worktree(repo_dir, branch, language) |
| 1705 | pull_warning = remote_first_message if remote_conflict_discarded else "" |
| 1706 | |
| 1707 | _run_git(["add", "."], repo_dir) |
| 1708 | _guard_reading_notes_deletions(repo_dir, language) |
| 1709 | llm_review = _notes_llm_sync_review( |
| 1710 | repo_dir, |
| 1711 | branch, |
| 1712 | _env_bool("PAPERFLOW_READING_NOTES_GIT_LLM_REVIEW", default=True), |
| 1713 | response_language=language, |
| 1714 | ) |
| 1715 | staged_status = _git_output(["status", "--short"], repo_dir) |
| 1716 | committed = False |
| 1717 | commit_hash = "" |
| 1718 | if staged_status: |
| 1719 | message = f"Sync PaperFlow reading notes {datetime.now().strftime('%Y-%m-%d %H:%M')}" |
| 1720 | _run_git(["commit", "-m", message], repo_dir, timeout=120) |
| 1721 | committed = True |
| 1722 | commit_hash = _git_output(["rev-parse", "--short", "HEAD"], repo_dir) |
| 1723 | |
| 1724 | rebased = False |
| 1725 | rebase_output = "" |
| 1726 | |
| 1727 | local_ahead, _remote_ahead = _git_ahead_behind(repo_dir, branch) |
| 1728 | pushed = False |
| 1729 | push_output = "" |
| 1730 | if local_ahead: |
| 1731 | try: |
| 1732 | result = _run_git(["push", "-u", "origin", branch], repo_dir, timeout=120) |
| 1733 | push_output = (result.stdout or result.stderr or "").strip() |
| 1734 | pushed = True |
| 1735 | except subprocess.CalledProcessError as exc: |
| 1736 | push_output = (exc.stderr or exc.stdout or str(exc)).strip() |
| 1737 | if not _is_non_fast_forward_push(push_output): |
| 1738 | raise RuntimeError( |
| 1739 | f"GitHub push failed: {push_output}" if language == "en" else f"GitHub push 失败:{push_output}" |
| 1740 | ) from exc |
| 1741 | local_commit_patch = _run_git(["diff", "--binary", f"origin/{branch}..HEAD"], repo_dir).stdout or "" |
| 1742 | _run_git(["fetch", "origin", branch], repo_dir, timeout=120) |
| 1743 | _run_git(["reset", "--hard", f"origin/{branch}"], repo_dir, timeout=120) |
| 1744 | pulled = True |
| 1745 | if local_commit_patch: |
| 1746 | applied = _run_git(["apply", "--3way", "--whitespace=nowarn", "-"], repo_dir, check=False, timeout=120, input=local_commit_patch) |
| 1747 | if applied.returncode != 0: |
| 1748 | _run_git(["reset", "--hard", f"origin/{branch}"], repo_dir, timeout=120) |
| 1749 | _run_git(["clean", "-fd"], repo_dir) |
| 1750 | pull_warning = ( |
| 1751 | "Remote version was kept because local note changes conflicted with remote updates." |