#!/bin/bash
# Git分支策略配置脚本

echo "=== 配置Git分支策略 ==="

cd /root/aiedu

# 1. 创建production分支（如果不存在）
if ! git branch | grep -q production; then
    echo "创建production分支..."
    git checkout -b production
    git push origin production
    echo "production分支已创建"
else
    echo "production分支已存在"
fi

# 2. 切换到production分支
git checkout production

# 3. 更新webhook脚本，监听production分支
echo "更新webhook配置..."
sed -i 's/refs\/heads\/main/refs\/heads\/production/g' /root/aiedu/scripts/webhook_handler.py

# 4. 重启webhook服务
systemctl restart kaopeilian-webhook.service

# 5. 创建.gitignore规则
echo "更新.gitignore..."
cat >> /root/aiedu/.gitignore << 'EOF'

# 生产环境配置文件（不提交）
kaopeilian-backend/.env.production
docker-compose.override.yml

# 服务器运行时文件
scripts/force_restart.sh
scripts/diagnose.sh
/var/log/kaopeilian_*.log
EOF

# 6. 提交配置变更
echo "提交配置变更到production分支..."
git add .gitignore scripts/webhook_handler.py
git commit -m "配置生产环境分支策略"
git push origin production

echo ""
echo "=== Git分支策略配置完成 ==="
echo ""
echo "使用说明："
echo "1. 开发者在main分支开发"
echo "2. 生产环境使用production分支"
echo "3. 发布流程："
echo "   git checkout production"
echo "   git merge main"
echo "   git push origin production"
echo ""
echo "4. 服务器自动更新监听production分支"
