From b16bf0fe3f3a06ad8935eaa7d205b83c04d69532 Mon Sep 17 00:00:00 2001 From: DLmaster361 Date: Fri, 12 Sep 2025 00:10:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BD=BF=E7=94=A8=E8=99=9A=E6=8B=9F?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E7=9A=84git?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/core/config.py | 59 +++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/app/core/config.py b/app/core/config.py index 4ab59b5..5e7293c 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -19,7 +19,7 @@ # Contact: DLmaster_361@163.com - +import os import re import shutil import asyncio @@ -28,7 +28,6 @@ import sqlite3 import calendar import requests import truststore -from git import Repo from pathlib import Path from fastapi import WebSocket from collections import defaultdict @@ -41,6 +40,16 @@ from app.utils import get_logger logger = get_logger("配置管理") +if (Path.cwd() / "environment/git/bin/git.exe").exists(): + os.environ["GIT_PYTHON_GIT_EXECUTABLE"] = str( + Path.cwd() / "environment/git/bin/git.exe" + ) + +try: + from git import Repo +except ImportError: + Repo = None + class GlobalConfig(ConfigBase): """全局配置""" @@ -602,7 +611,16 @@ class AppConfig(GlobalConfig): self.config_path.mkdir(parents=True, exist_ok=True) self.history_path.mkdir(parents=True, exist_ok=True) - self.repo = Repo(Path.cwd()) + # 初始化Git仓库(如果可用) + try: + if Repo is not None: + self.repo = Repo(Path.cwd()) + else: + self.repo = None + except Exception as e: + logger.warning(f"Git仓库初始化失败: {e}") + self.repo = None + self.server: Optional[uvicorn.Server] = None self.websocket: Optional[WebSocket] = None self.silence_dict: Dict[Path, datetime] = {} @@ -915,24 +933,33 @@ class AppConfig(GlobalConfig): await Config.websocket.send_json(data) async def get_git_version(self) -> tuple[bool, str, str]: + """获取Git版本信息,如果Git不可用则返回默认值""" - # 获取当前 commit - current_commit = self.repo.head.commit + if self.repo is None: + logger.warning("Git仓库不可用,返回默认版本信息") + return False, "unknown", "unknown" - # 获取 commit 哈希 - commit_hash = current_commit.hexsha + try: + # 获取当前 commit + current_commit = self.repo.head.commit - # 获取 commit 时间 - commit_time = datetime.fromtimestamp(current_commit.committed_date) + # 获取 commit 哈希 + commit_hash = current_commit.hexsha - # 检查是否为最新 commit - # 获取远程分支的最新 commit - origin = self.repo.remotes.origin - origin.fetch() # 拉取最新信息 - remote_commit = self.repo.commit(f"origin/{self.repo.active_branch.name}") - is_latest = bool(current_commit.hexsha == remote_commit.hexsha) + # 获取 commit 时间 + commit_time = datetime.fromtimestamp(current_commit.committed_date) - return is_latest, commit_hash, commit_time.strftime("%Y-%m-%d %H:%M:%S") + # 检查是否为最新 commit + # 获取远程分支的最新 commit + origin = self.repo.remotes.origin + origin.fetch() # 拉取最新信息 + remote_commit = self.repo.commit(f"origin/{self.repo.active_branch.name}") + is_latest = bool(current_commit.hexsha == remote_commit.hexsha) + + return is_latest, commit_hash, commit_time.strftime("%Y-%m-%d %H:%M:%S") + except Exception as e: + logger.warning(f"获取Git版本信息失败: {e}") + return False, "error", "error" async def add_script( self, script: Literal["MAA", "General"]