#!/bin/bash ############################################################################### # 将 clip-as-service 从 Git 子模块转为普通目录 # # 用途: # 移除 Git 子模块配置,将 clip-as-service 直接纳入主项目管理 # # 使用方法: # ./scripts/remove_clip_submodule.sh # # 注意: # - 此操作会修改 Git 配置,请确保已提交当前更改 # - 建议先创建备份分支 # - 执行前请先阅读 docs/GIT_SUBMODULE_GUIDE.md # ############################################################################### set -e # 遇到错误立即退出 # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # 项目路径 PROJECT_ROOT="/data/tw/SearchEngine" echo -e "${BLUE}========================================${NC}" echo -e "${BLUE}将 clip-as-service 从子模块转为普通目录${NC}" echo -e "${BLUE}========================================${NC}" echo "" # 检查是否在项目根目录 if [ ! -f "${PROJECT_ROOT}/.git" ] && [ ! -d "${PROJECT_ROOT}/.git" ]; then echo -e "${RED}错误: 当前目录不是 Git 仓库根目录${NC}" echo -e "${YELLOW}请在项目根目录运行此脚本${NC}" exit 1 fi cd "${PROJECT_ROOT}" # 检查是否有未提交的更改 if [ -n "$(git status --porcelain)" ]; then echo -e "${RED}错误: Git 工作区有未提交的更改${NC}" echo -e "${YELLOW}请先提交或暂存所有更改${NC}" echo "" echo "未提交的文件:" git status --short echo "" echo -e "${YELLOW}建议操作:${NC}" echo " 1. 提交更改: git add . && git commit -m '保存点'" echo " 2. 或创建备份分支: git branch backup-before-submodule-removal" exit 1 fi # 确认操作 echo -e "${YELLOW}此操作将会:${NC}" echo " 1. 移除 clip-as-service 的子模块配置" echo " 2. 将其作为普通目录纳入 Git 管理" echo " 3. 更新 .gitmodules 文件" echo "" echo -e "${YELLOW}请确保已阅读: docs/GIT_SUBMODULE_GUIDE.md${NC}" echo "" read -p "$(echo -e ${YELLOW}是否继续? [y/N]: ${NC})" -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo -e "${YELLOW}已取消操作${NC}" exit 0 fi # 创建备份分支 BACKUP_BRANCH="backup-before-submodule-removal-$(date +%Y%m%d-%H%M%S)" echo -e "${BLUE}创建备份分支: ${BACKUP_BRANCH}${NC}" git branch "${BACKUP_BRANCH}" echo -e "${GREEN}✓ 备份分支已创建${NC}" echo "" # 步骤 1: 备份 .gitmodules echo -e "${BLUE}步骤 1: 备份配置文件...${NC}" if [ -f ".gitmodules" ]; then cp .gitmodules .gitmodules.backup echo -e "${GREEN}✓ 已备份 .gitmodules → .gitmodules.backup${NC}" fi # 步骤 2: 移除子模块追踪 echo -e "${BLUE}步骤 2: 移除子模块追踪...${NC}" if git ls-files --stage | grep -q "160000.*third-party/clip-as-service"; then git rm --cached third-party/clip-as-service echo -e "${GREEN}✓ 已移除子模块缓存${NC}" else echo -e "${YELLOW}⚠ 子模块未在 Git 索引中,跳过此步骤${NC}" fi # 步骤 3: 清理 .gitmodules echo -e "${BLUE}步骤 3: 清理 .gitmodules...${NC}" if [ -f ".gitmodules" ]; then # 删除 clip-as-service 相关的配置块 # 从 [submodule "third-party/clip-as-service"] 到下一个空行或文件末尾 sed -i '/\[submodule "third-party\/clip-as-service"\]/,/\s*$/d' .gitmodules # 如果文件为空或只包含空行,删除它 if [ ! -s ".gitmodules" ] || [ $(wc -l < .gitmodules) -eq 0 ]; then rm .gitmodules echo -e "${GREEN}✓ 已删除空的 .gitmodules 文件${NC}" else echo -e "${GREEN}✓ 已更新 .gitmodules${NC}" fi else echo -e "${YELLOW}⚠ .gitmodules 文件不存在,跳过此步骤${NC}" fi # 步骤 4: 删除子模块的 Git 仓库 echo -e "${BLUE}步骤 4: 删除子模块 Git 仓库...${NC}" if [ -d "third-party/clip-as-service/.git" ]; then rm -rf third-party/clip-as-service/.git echo -e "${GREEN}✓ 已删除子模块 Git 仓库${NC}" else echo -e "${YELLOW}⚠ 子模块 Git 仓库不存在,跳过此步骤${NC}" fi # 步骤 5: 清理 Git 模块缓存 echo -e "${BLUE}步骤 5: 清理 Git 模块缓存...${NC}" if [ -d ".git/modules/third-party/clip-as-service" ]; then rm -rf .git/modules/third-party/clip-as-service echo -e "${GREEN}✓ 已清理 Git 模块缓存${NC}" else echo -e "${YELLOW}⚠ Git 模块缓存不存在,跳过此步骤${NC}" fi # 步骤 6: 将目录作为普通文件添加 echo -e "${BLUE}步骤 6: 将目录添加到 Git...${NC}" git add third-party/clip-as-service/ if [ -f ".gitmodules" ]; then git add .gitmodules fi echo -e "${GREEN}✓ 已添加到 Git${NC}" # 显示状态 echo "" echo -e "${BLUE}========================================${NC}" echo -e "${BLUE}操作完成!当前状态:${NC}" echo -e "${BLUE}========================================${NC}" echo "" git status echo "" # 统计信息 echo -e "${BLUE}统计信息:${NC}" FILE_COUNT=$(find third-party/clip-as-service -type f | wc -l) DIR_SIZE=$(du -sh third-party/clip-as-service | cut -f1) echo " 文件数量: ${FILE_COUNT}" echo " 目录大小: ${DIR_SIZE}" echo "" # 下一步提示 echo -e "${GREEN}========================================${NC}" echo -e "${GREEN}✓ 子模块转换完成!${NC}" echo -e "${GREEN}========================================${NC}" echo "" echo -e "${BLUE}下一步操作:${NC}" echo "" echo "1. 查看将要提交的更改:" echo " git diff --cached --stat" echo "" echo "2. 提交更改:" echo " git commit -m 'feat: 将 clip-as-service 从子模块转为普通目录'" echo "" echo "3. 推送到远程:" echo " git push origin main" echo "" echo -e "${BLUE}注意事项:${NC}" echo " - 备份分支: ${BACKUP_BRANCH}" echo " - 如需回退: git reset --hard ${BACKUP_BRANCH}" echo " - 配置备份: .gitmodules.backup" echo "" echo -e "${BLUE}验证转换:${NC}" echo " cd third-party/clip-as-service" echo " git status # 应该显示 'not a git repository'" echo "" # 提示查看详细文档 echo -e "${YELLOW}📖 详细文档: docs/GIT_SUBMODULE_GUIDE.md${NC}" echo "" # 询问是否立即提交 read -p "$(echo -e ${YELLOW}是否立即提交更改? [Y/n]: ${NC})" -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]] || [ -z "$REPLY" ]; then echo -e "${BLUE}正在提交更改...${NC}" git commit -m "feat: 将 clip-as-service 从子模块转为普通目录 - 移除子模块配置,改为普通目录 - 便于自定义配置和管理 - 备份分支: ${BACKUP_BRANCH} " echo -e "${GREEN}✓ 已提交${NC}" echo "" echo -e "${YELLOW}现在可以推送到远程仓库:${NC}" echo " git push origin main" else echo -e "${YELLOW}跳过提交${NC}" echo -e "${YELLOW}你可以稍后手动提交:${NC}" echo " git commit -m 'feat: 将 clip-as-service 从子模块转为普通目录'" fi echo "" echo -e "${GREEN}所有操作完成!${NC}"