diff --git a/.devops/monitor.py b/.devops/monitor.py index d84bf87..eff66ca 100644 --- a/.devops/monitor.py +++ b/.devops/monitor.py @@ -292,29 +292,41 @@ class GitMonitor: else: Logger.info("主仓库已存在,更新代码...") + # 检查主仓库是否有未提交的修改 + cmd = "git status --porcelain" + result = subprocess.run(cmd, shell=True, cwd=repo_path, capture_output=True, text=True) + if result.stdout.strip(): + Logger.warning("主仓库有未提交的修改,先清理工作区...") + cmd = "git reset --hard HEAD" + subprocess.run(cmd, shell=True, cwd=repo_path, capture_output=True) + # 切换到主分支 cmd = f"git checkout {self.global_branch}" - subprocess.run(cmd, shell=True, cwd=repo_path, capture_output=True) + result = subprocess.run(cmd, shell=True, cwd=repo_path, capture_output=True, text=True) + if result.returncode != 0: + Logger.error(f"切换到分支 {self.global_branch} 失败: {result.stderr}") + return False # 拉取最新代码 cmd = "git pull" result = subprocess.run(cmd, shell=True, cwd=repo_path, capture_output=True, text=True) if result.returncode != 0: - Logger.error("拉取主仓库失败") + Logger.error(f"拉取主仓库失败: {result.stderr}") return False - # 初始化和更新所有子模块(包括新增的子模块) - cmd = "git submodule update --init --recursive" + # 清理所有子模块的本地修改(避免文件被意外清空的问题) + Logger.info("清理子模块本地修改...") + cmd = "git submodule foreach 'git reset --hard HEAD && git clean -fd'" result = subprocess.run(cmd, shell=True, cwd=repo_path, capture_output=True, text=True) if result.returncode != 0: - Logger.error("初始化子模块失败") - return False + Logger.warning(f"清理子模块失败,继续执行: {result.stderr}") - # 更新所有子模块到最新代码 - cmd = f"git submodule foreach 'git checkout {self.global_branch} && git pull'" + # 初始化和更新所有子模块到主仓库记录的版本 + Logger.info("更新子模块到主仓库记录的版本...") + cmd = "git submodule update --init --recursive --force" result = subprocess.run(cmd, shell=True, cwd=repo_path, capture_output=True, text=True) if result.returncode != 0: - Logger.error("更新子模块失败") + Logger.error(f"更新子模块失败: {result.stderr}") return False Logger.info("主仓库和子模块更新成功")