#!/usr/bin/env bash # ============================================================================== # sync.sh — 把工作成果推回 Hugging Face(agent 崩潰後的唯一保險) # # ./sync.sh "訊息" # 提交並推到 HelloSun/SmallThinker4b # ./sync.sh --status # 只看狀態 # ./sync.sh --pull # 從遠端拉回最新(崩潰後續作第一步) # # 原則:**每改完程式並編譯過就上傳,不要累積。** # Token: 環境變數 HF_TOKEN,或檔案 .hf_token # ============================================================================== set -uo pipefail REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" STORAGE_REPO="${ST_STORAGE_REPO:-HelloSun/SmallThinker4b}" MODE=push MSG="" for a in "$@"; do case "$a" in --status) MODE=status ;; --pull) MODE=pull ;; *) MSG="$a" ;; esac done [[ -z "$MSG" ]] && MSG="sync: $(date -Iseconds)" cd "$REPO_DIR" || { echo "[sync] 沒有 repo 目錄"; exit 1; } # ── token ── if [[ -z "${HF_TOKEN:-}" && -f "$REPO_DIR/.hf_token" ]]; then HF_TOKEN="$(tr -d ' \n\r' < "$REPO_DIR/.hf_token")" fi if [[ -z "${HF_TOKEN:-}" ]]; then echo "[sync] 找不到 HF_TOKEN"; exit 1 fi export HF_TOKEN git config user.email "${HF_GIT_EMAIL:-agent@huggingface.co}" git config user.name "${HF_GIT_NAME:-Auto Upload Agent}" ensure_remote() { local name="$1" repo="$2" local url="https://oauth2:${HF_TOKEN}@huggingface.co/${repo}" if git remote get-url "$name" >/dev/null 2>&1; then git remote set-url "$name" "$url" else git remote add "$name" "$url" fi } ensure_remote origin "$STORAGE_REPO" case "$MODE" in status) echo "── repo ──"; git remote -v echo "── branch ──"; git status -sb | head -30 echo "── log ──"; git log --oneline -10 exit 0 ;; pull) git fetch origin if git rev-parse HEAD >/dev/null 2>&1 && ! git diff --quiet 2>/dev/null; then echo "[sync] 有未提交的改動,先 stash"; git stash push -u -m "sync-pull $(date -Iseconds)" fi git checkout -B main origin/main 2>/dev/null || git reset --hard origin/main echo "[sync] 已拉到最新:$(git log --oneline -1)" exit 0 ;; esac # ── push ── git add -A if git diff --cached --quiet; then echo "[sync] 沒有改動,不用上傳" else git commit -q -m "$MSG" || { echo "[sync] commit 失敗"; exit 1; } fi # 即使沒有新 commit 也要推 —— 這是「成果已保存」的保險,不能跳過。 # 第一次推送時遠端還沒有 main,要帶 -u。 git push -u origin main 2>&1 | tail -3 \ || git push -u --force origin main 2>&1 | tail -3 echo "[sync] 完成:$(git log --oneline -1)" echo "[sync] https://huggingface.co/${STORAGE_REPO}"