优化脚本

This commit is contained in:
孙小云 2026-02-05 14:01:41 +08:00
parent 39ea52589f
commit 38946a67cd
1 changed files with 21 additions and 9 deletions

View File

@ -292,29 +292,41 @@ class GitMonitor:
else: else:
Logger.info("主仓库已存在,更新代码...") 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}" 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" cmd = "git pull"
result = subprocess.run(cmd, shell=True, cwd=repo_path, capture_output=True, text=True) result = subprocess.run(cmd, shell=True, cwd=repo_path, capture_output=True, text=True)
if result.returncode != 0: if result.returncode != 0:
Logger.error("拉取主仓库失败") Logger.error(f"拉取主仓库失败: {result.stderr}")
return False 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) result = subprocess.run(cmd, shell=True, cwd=repo_path, capture_output=True, text=True)
if result.returncode != 0: if result.returncode != 0:
Logger.error("初始化子模块失败") Logger.warning(f"清理子模块失败,继续执行: {result.stderr}")
return False
# 更新所有子模块到最新代码 # 初始化和更新所有子模块到主仓库记录的版本
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) result = subprocess.run(cmd, shell=True, cwd=repo_path, capture_output=True, text=True)
if result.returncode != 0: if result.returncode != 0:
Logger.error("更新子模块失败") Logger.error(f"更新子模块失败: {result.stderr}")
return False return False
Logger.info("主仓库和子模块更新成功") Logger.info("主仓库和子模块更新成功")