git_sync.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/bash
  2. # ============================================================
  3. # Git Auto Sync Script
  4. # ============================================================
  5. # 自动同步代码到 Gogs 仓库
  6. # ============================================================
  7. set -e
  8. GOGS_URL="https://git.zhonjin.com:40717/steven_roc"
  9. REPO_NAME="hikvision-isup-client"
  10. ACCESS_TOKEN="1942ce3d6478f7a572fee3dd4891e49f6c416aad"
  11. FULL_URL="${GOGS_URL}/${REPO_NAME}.git"
  12. echo "=== Git Repository Sync ==="
  13. echo ""
  14. # 检查 git
  15. if ! command -v git &> /dev/null; then
  16. echo "Error: git is not installed"
  17. exit 1
  18. fi
  19. # 初始化仓库(如果尚未初始化)
  20. if [ ! -d ".git" ]; then
  21. echo "[1/5] Initializing git repository..."
  22. git init
  23. git branch -M main
  24. fi
  25. # 配置用户
  26. git config user.name "steven_roc" 2>/dev/null || true
  27. git config user.email "steven_roc@zhonjin.com" 2>/dev/null || true
  28. # 添加远程仓库
  29. if ! git remote get-url origin &> /dev/null; then
  30. echo "[2/5] Adding remote origin..."
  31. git remote add origin "https://steven_roc:${ACCESS_TOKEN}@git.zhonjin.com:40717/steven_roc/${REPO_NAME}.git"
  32. fi
  33. # 添加所有文件
  34. echo "[3/5] Staging files..."
  35. git add -A
  36. # 检查是否有变更
  37. if git diff --staged --quiet; then
  38. echo "No changes to commit"
  39. else
  40. # 提交
  41. echo "[4/5] Committing changes..."
  42. git commit -m "$(date '+%Y-%m-%d %H:%M:%S') - Auto sync update"
  43. # 推送(注意: 不要使用 -o 参数,Gogs 可能不支持 push options)
  44. echo "[5/5] Pushing to Gogs..."
  45. git push -u origin main
  46. fi
  47. echo ""
  48. echo "=== Sync complete ==="
  49. echo "Repository: ${FULL_URL}"