git_sync.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/bin/bash
  2. # ============================================================
  3. # Git Auto Sync Script
  4. # ============================================================
  5. # This script configures and pushes code to Gogs repository
  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 Setup ==="
  13. echo ""
  14. # Check if git is installed
  15. if ! command -v git &> /dev/null; then
  16. echo "Git is not installed. Installing..."
  17. sudo apt-get install -y git
  18. fi
  19. # Initialize git repository if not already initialized
  20. if [ ! -d ".git" ]; then
  21. echo "[1/4] Initializing git repository..."
  22. git init
  23. git branch -M main
  24. echo "Git repository initialized"
  25. else
  26. echo "[1/4] Git repository already exists"
  27. fi
  28. # Add remote origin with access token
  29. echo "[2/4] Configuring remote origin..."
  30. # Remove existing remote if exists
  31. git remote remove origin 2>/dev/null || true
  32. # Add remote with credentials in URL
  33. git remote add origin "https://steven_roc:${ACCESS_TOKEN}@git.zhonjin.com:40717/steven_roc/${REPO_NAME}.git"
  34. echo "Remote origin added: ${FULL_URL}"
  35. # Configure git user (if not already configured)
  36. if ! git config user.name &> /dev/null || [ "$(git config user.name)" = "" ]; then
  37. git config user.name "steven_roc"
  38. fi
  39. if ! git config user.email &> /dev/null || [ "$(git config user.email)" = "" ]; then
  40. git config user.email "steven_roc@zhonjin.com"
  41. fi
  42. # Add all files
  43. echo "[3/4] Staging files..."
  44. git add -A
  45. # Check if there are changes to commit
  46. if git diff --staged --quiet; then
  47. echo "No changes to commit"
  48. else
  49. # Commit
  50. echo "[4/4] Committing changes..."
  51. git commit -m "Initial commit: Hikvision ISUP Client project"
  52. echo "Changes committed"
  53. # Push to remote
  54. echo "Pushing to remote..."
  55. git push -u origin main
  56. echo ""
  57. echo "=== Git setup complete ==="
  58. echo "Repository: ${FULL_URL}"
  59. echo ""
  60. echo "Future sync commands:"
  61. echo " git add -A"
  62. echo " git commit -m 'Your commit message'"
  63. echo " git push"
  64. else
  65. echo "Nothing to push"
  66. fi
  67. echo ""
  68. echo "=== Git setup complete ==="