From 8085a00d24ddc46b93c91be5788d801d52f4f1dd Mon Sep 17 00:00:00 2001 From: shaw Date: Fri, 8 Aug 2025 20:36:33 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=20.gitignore=20?= =?UTF-8?q?=E5=92=8C=E7=AE=A1=E7=90=86=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 .gitignore 中添加更多备份文件忽略规则 - 在 manage.sh 脚本中添加切换分支功能 - 优化脚本执行权限设置逻辑 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .gitignore | 3 + scripts/manage.sh | 270 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 267 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index c6d73630..6ce0c09a 100644 --- a/.gitignore +++ b/.gitignore @@ -143,6 +143,9 @@ desktop.ini # Backup files *.bak *.backup +*.backup.* +.env.backup.* +config.js.backup.* *~ # Archive files (unless specifically needed) diff --git a/scripts/manage.sh b/scripts/manage.sh index 6e8581ca..95834e94 100755 --- a/scripts/manage.sh +++ b/scripts/manage.sh @@ -978,6 +978,253 @@ update_model_pricing() { fi } +# 切换分支 +switch_branch() { + if ! check_installation; then + print_error "服务未安装,请先运行: $0 install" + return 1 + fi + + cd "$APP_DIR" + + # 获取当前分支 + local current_branch=$(git branch --show-current 2>/dev/null) + if [ -z "$current_branch" ]; then + print_error "无法获取当前分支信息" + return 1 + fi + + print_info "当前分支: ${GREEN}$current_branch${NC}" + + # 获取所有远程分支 + print_info "获取远程分支列表..." + git fetch origin --prune >/dev/null 2>&1 + + # 显示可用分支 + echo -e "\n${YELLOW}可用分支:${NC}" + local branches=$(git branch -r | grep -v HEAD | sed 's/origin\///' | sed 's/^ *//') + local branch_array=() + local i=1 + + while IFS= read -r branch; do + if [ "$branch" = "$current_branch" ]; then + echo -e " $i) $branch ${GREEN}(当前)${NC}" + else + echo " $i) $branch" + fi + branch_array+=("$branch") + ((i++)) + done <<< "$branches" + + echo "" + echo -n "请选择要切换的分支 (输入编号或分支名,0 取消): " + read branch_choice + + # 处理用户输入 + local target_branch="" + if [ "$branch_choice" = "0" ]; then + print_info "已取消切换" + return 0 + elif [[ "$branch_choice" =~ ^[0-9]+$ ]]; then + # 用户输入的是编号 + local index=$((branch_choice - 1)) + if [ $index -ge 0 ] && [ $index -lt ${#branch_array[@]} ]; then + target_branch="${branch_array[$index]}" + else + print_error "无效的编号" + return 1 + fi + else + # 用户输入的是分支名 + target_branch="$branch_choice" + # 验证分支是否存在 + if ! echo "$branches" | grep -q "^$target_branch$"; then + print_error "分支 '$target_branch' 不存在" + return 1 + fi + fi + + # 如果是同一个分支,无需切换 + if [ "$target_branch" = "$current_branch" ]; then + print_info "已经在分支 $target_branch 上" + return 0 + fi + + print_info "准备切换到分支: ${GREEN}$target_branch${NC}" + + # 保存当前运行状态 + local was_running=false + if pgrep -f "node.*src/app.js" > /dev/null; then + was_running=true + print_info "检测到服务正在运行,将在切换后自动重启..." + stop_service + fi + + # 处理本地修改(主要是权限变更导致的) + print_info "检查本地修改..." + + # 先重置所有权限相关的修改(特别是manage.sh的权限) + git status --porcelain | while read -r line; do + local file=$(echo "$line" | awk '{print $2}') + if [ -n "$file" ]; then + # 检查是否只是权限变更 + if git diff --summary "$file" 2>/dev/null | grep -q "mode change"; then + print_info "重置文件权限变更: $file" + git checkout HEAD -- "$file" 2>/dev/null || true + fi + fi + done + + # 检查是否还有其他实质性修改 + if git status --porcelain | grep -v "^??" | grep -q .; then + print_warning "检测到本地文件修改:" + git status --short | grep -v "^??" + echo "" + echo -n "是否要保存这些修改?(y/N): " + read -n 1 save_changes + echo + + if [[ "$save_changes" =~ ^[Yy]$ ]]; then + # 暂存修改 + print_info "暂存本地修改..." + git stash push -m "Branch switch from $current_branch to $target_branch $(date +%Y-%m-%d)" >/dev/null 2>&1 + else + # 丢弃修改 + print_info "丢弃本地修改..." + git reset --hard HEAD >/dev/null 2>&1 + fi + fi + + # 切换分支 + print_info "切换分支..." + + # 检查本地是否已有该分支 + if git show-ref --verify --quiet "refs/heads/$target_branch"; then + # 本地已有分支,切换并更新 + if ! git checkout "$target_branch" 2>/dev/null; then + print_error "切换分支失败" + return 1 + fi + + # 更新到最新 + print_info "更新到远程最新版本..." + git pull origin "$target_branch" --rebase 2>/dev/null || { + # 如果rebase失败,使用reset + print_warning "更新失败,强制同步到远程版本..." + git fetch origin "$target_branch" + git reset --hard "origin/$target_branch" + } + else + # 创建并切换到新分支 + if ! git checkout -b "$target_branch" "origin/$target_branch" 2>/dev/null; then + print_error "创建并切换分支失败" + return 1 + fi + fi + + print_success "已切换到分支: $target_branch" + + # 确保脚本有执行权限(切换分支后必须执行) + if [ -f "$APP_DIR/scripts/manage.sh" ]; then + chmod +x "$APP_DIR/scripts/manage.sh" + print_info "已设置脚本执行权限" + fi + + # 更新依赖(如果package.json有变化) + if git diff "$current_branch..$target_branch" --name-only | grep -q "package.json"; then + print_info "检测到 package.json 变化,更新依赖..." + npm install + fi + + # 更新前端文件(如果切换到不同版本) + if [ "$target_branch" != "$current_branch" ]; then + print_info "更新前端文件..." + + # 创建目标目录 + mkdir -p web/admin-spa/dist + + # 清理旧的前端文件 + if [ -d "web/admin-spa/dist" ]; then + rm -rf web/admin-spa/dist/* 2>/dev/null || true + fi + + # 尝试从对应的 web-dist 分支获取前端文件 + if git ls-remote --heads origin "web-dist-$target_branch" | grep -q "web-dist-$target_branch"; then + print_info "从 web-dist-$target_branch 分支下载前端文件..." + local web_branch="web-dist-$target_branch" + elif git ls-remote --heads origin web-dist | grep -q web-dist; then + print_info "从 web-dist 分支下载前端文件..." + local web_branch="web-dist" + else + print_warning "未找到预构建的前端文件" + web_branch="" + fi + + if [ -n "$web_branch" ]; then + # 创建临时目录用于 clone + TEMP_CLONE_DIR=$(mktemp -d) + + # 下载前端文件 + if git clone --depth 1 --branch "$web_branch" --single-branch \ + https://github.com/Wei-Shaw/claude-relay-service.git \ + "$TEMP_CLONE_DIR" 2>/dev/null; then + + # 复制文件到目标目录 + rsync -av --exclude='.git' --exclude='README.md' "$TEMP_CLONE_DIR/" web/admin-spa/dist/ 2>/dev/null || { + cp -r "$TEMP_CLONE_DIR"/* web/admin-spa/dist/ 2>/dev/null + rm -rf web/admin-spa/dist/.git 2>/dev/null + rm -f web/admin-spa/dist/README.md 2>/dev/null + } + + print_success "前端文件更新完成" + else + print_warning "下载前端文件失败" + fi + + # 清理临时目录 + rm -rf "$TEMP_CLONE_DIR" + fi + fi + + # 检查是否有暂存的修改可以恢复 + if [[ "$save_changes" =~ ^[Yy]$ ]] && git stash list | grep -q "Branch switch from $current_branch to $target_branch"; then + echo "" + echo -n "是否要恢复之前暂存的修改?(y/N): " + read -n 1 restore_stash + echo + + if [[ "$restore_stash" =~ ^[Yy]$ ]]; then + print_info "恢复暂存的修改..." + git stash pop >/dev/null 2>&1 || print_warning "恢复修改时出现冲突,请手动解决" + fi + fi + + # 如果之前在运行,则重新启动服务 + if [ "$was_running" = true ]; then + print_info "重新启动服务..." + start_service + fi + + # 显示切换后的信息 + echo "" + echo -e "${GREEN}=== 分支切换完成 ===${NC}" + echo -e "当前分支: ${GREEN}$target_branch${NC}" + + # 显示版本信息 + if [ -f "$APP_DIR/VERSION" ]; then + echo -e "当前版本: ${GREEN}$(cat "$APP_DIR/VERSION")${NC}" + fi + + # 显示最新提交 + local latest_commit=$(git log -1 --oneline 2>/dev/null) + if [ -n "$latest_commit" ]; then + echo -e "最新提交: ${GREEN}$latest_commit${NC}" + fi + + echo "" + print_info "提示:如遇到问题,可以运行 'crs update' 强制更新到最新版本" +} + # 显示状态 show_status() { echo -e "\n${BLUE}=== Claude Relay Service 状态 ===${NC}" @@ -1064,6 +1311,7 @@ show_help() { echo " stop - 停止服务" echo " restart - 重启服务" echo " status - 查看状态" + echo " switch-branch - 切换分支" echo " update-pricing - 更新模型价格数据" echo " symlink - 创建 crs 快捷命令" echo " help - 显示帮助" @@ -1141,11 +1389,12 @@ show_menu() { echo " 3) 停止服务" echo " 4) 重启服务" echo " 5) 更新服务" - echo " 6) 更新模型价格" - echo " 7) 卸载服务" - echo " 8) 退出" + echo " 6) 切换分支" + echo " 7) 更新模型价格" + echo " 8) 卸载服务" + echo " 9) 退出" echo "" - echo -n "请输入选项 [1-8]: " + echo -n "请输入选项 [1-9]: " fi } @@ -1233,18 +1482,24 @@ handle_menu_choice() { ;; 6) echo "" - update_model_pricing + switch_branch echo -n "按回车键继续..." read ;; 7) + echo "" + update_model_pricing + echo -n "按回车键继续..." + read + ;; + 8) echo "" uninstall_service if [ $? -eq 0 ]; then exit 0 fi ;; - 8) + 9) echo "退出管理工具" exit 0 ;; @@ -1417,6 +1672,9 @@ main() { status) show_status ;; + switch-branch) + switch_branch + ;; update-pricing) update_model_pricing ;;