From 1b2a02cb1a7671d02159a4c4c98466f2a199b90b Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Fri, 20 Jun 2025 15:22:07 +0800 Subject: [PATCH] feat: add git information display in update modals and enhance version check functionality --- py/routes/update_routes.py | 74 +++++++++++++++++++++++- static/css/components/download-modal.css | 4 +- static/css/components/update-modal.css | 13 ++++- static/js/managers/UpdateService.js | 24 +++++++- templates/components/modals.html | 1 + 5 files changed, 109 insertions(+), 7 deletions(-) diff --git a/py/routes/update_routes.py b/py/routes/update_routes.py index 29996604..14168684 100644 --- a/py/routes/update_routes.py +++ b/py/routes/update_routes.py @@ -2,6 +2,8 @@ import os import aiohttp import logging import toml +import subprocess +from datetime import datetime from aiohttp import web from typing import Dict, Any, List @@ -24,6 +26,9 @@ class UpdateRoutes: try: # Read local version from pyproject.toml local_version = UpdateRoutes._get_local_version() + + # Get git info (commit hash, branch) + git_info = UpdateRoutes._get_git_info() # Fetch remote version from GitHub remote_version, changelog = await UpdateRoutes._get_remote_version() @@ -39,7 +44,8 @@ class UpdateRoutes: 'current_version': local_version, 'latest_version': remote_version, 'update_available': update_available, - 'changelog': changelog + 'changelog': changelog, + 'git_info': git_info }) except Exception as e: @@ -72,6 +78,72 @@ class UpdateRoutes: logger.error(f"Failed to get local version: {e}", exc_info=True) return "v0.0.0" + @staticmethod + def _get_git_info() -> Dict[str, str]: + """Get Git repository information""" + current_dir = os.path.dirname(os.path.abspath(__file__)) + plugin_root = os.path.dirname(os.path.dirname(current_dir)) + + git_info = { + 'commit_hash': 'unknown', + 'short_hash': 'unknown', + 'branch': 'unknown', + 'commit_date': 'unknown' + } + + try: + # Check if we're in a git repository + if not os.path.exists(os.path.join(plugin_root, '.git')): + return git_info + + # Get current commit hash + result = subprocess.run( + ['git', 'rev-parse', 'HEAD'], + cwd=plugin_root, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + check=False + ) + if result.returncode == 0: + git_info['commit_hash'] = result.stdout.strip() + git_info['short_hash'] = git_info['commit_hash'][:7] + + # Get current branch name + result = subprocess.run( + ['git', 'rev-parse', '--abbrev-ref', 'HEAD'], + cwd=plugin_root, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + check=False + ) + if result.returncode == 0: + git_info['branch'] = result.stdout.strip() + + # Get commit date + result = subprocess.run( + ['git', 'show', '-s', '--format=%ci', 'HEAD'], + cwd=plugin_root, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + check=False + ) + if result.returncode == 0: + commit_date = result.stdout.strip() + # Format the date nicely if possible + try: + date_obj = datetime.strptime(commit_date, '%Y-%m-%d %H:%M:%S %z') + git_info['commit_date'] = date_obj.strftime('%Y-%m-%d') + except: + git_info['commit_date'] = commit_date + + except Exception as e: + logger.warning(f"Error getting git info: {e}") + + return git_info + @staticmethod async def _get_remote_version() -> tuple[str, List[str]]: """ diff --git a/static/css/components/download-modal.css b/static/css/components/download-modal.css index 80f5b1c3..b3d1f166 100644 --- a/static/css/components/download-modal.css +++ b/static/css/components/download-modal.css @@ -95,7 +95,7 @@ flex: 1; } -.version-info { +.version-content .version-info { display: flex; flex-wrap: wrap; flex-direction: row !important; @@ -104,7 +104,7 @@ font-size: 0.9em; } -.version-info .base-model { +.version-content .version-info .base-model { background: oklch(var(--lora-accent) / 0.1); color: var(--lora-accent); padding: 2px 8px; diff --git a/static/css/components/update-modal.css b/static/css/components/update-modal.css index f8e12d14..853ea3e8 100644 --- a/static/css/components/update-modal.css +++ b/static/css/components/update-modal.css @@ -37,13 +37,11 @@ display: flex; justify-content: space-between; align-items: center; - background: rgba(0, 0, 0, 0.02); /* 轻微的灰色背景 */ - border: 1px solid rgba(0, 0, 0, 0.08); /* 更明显的边框 */ border-radius: var(--border-radius-sm); padding: var(--space-3); } -.version-info { +.update-info .version-info { display: flex; flex-direction: column; gap: 8px; @@ -70,6 +68,15 @@ color: var(--lora-accent); } +/* Add styling for git info display */ +.git-info { + font-size: 0.85em; + opacity: 0.7; + margin-top: 4px; + font-family: monospace; + color: var(--text-color); +} + .update-link { display: flex; align-items: center; diff --git a/static/js/managers/UpdateService.js b/static/js/managers/UpdateService.js index c08e354c..694101a7 100644 --- a/static/js/managers/UpdateService.js +++ b/static/js/managers/UpdateService.js @@ -8,6 +8,11 @@ export class UpdateService { this.latestVersion = "v0.0.0"; // Initialize with default values this.updateInfo = null; this.updateAvailable = false; + this.gitInfo = { + short_hash: "unknown", + branch: "unknown", + commit_date: "unknown" + }; this.updateNotificationsEnabled = getStorageItem('show_update_notifications'); this.lastCheckTime = parseInt(getStorageItem('last_update_check') || '0'); } @@ -62,6 +67,7 @@ export class UpdateService { this.currentVersion = data.current_version || "v0.0.0"; this.latestVersion = data.latest_version || "v0.0.0"; this.updateInfo = data; + this.gitInfo = data.git_info || this.gitInfo; // Explicitly set update availability based on version comparison this.updateAvailable = this.isNewerVersion(this.latestVersion, this.currentVersion); @@ -77,7 +83,8 @@ export class UpdateService { console.log("Update check complete:", { currentVersion: this.currentVersion, latestVersion: this.latestVersion, - updateAvailable: this.updateAvailable + updateAvailable: this.updateAvailable, + gitInfo: this.gitInfo }); } } catch (error) { @@ -152,6 +159,21 @@ export class UpdateService { if (currentVersionEl) currentVersionEl.textContent = this.currentVersion; if (newVersionEl) newVersionEl.textContent = this.latestVersion; + // Update git info + const gitInfoEl = modal.querySelector('.git-info'); + if (gitInfoEl && this.gitInfo) { + if (this.gitInfo.short_hash !== 'unknown') { + let gitText = `Commit: ${this.gitInfo.short_hash}`; + if (this.gitInfo.commit_date !== 'unknown') { + gitText += ` - Date: ${this.gitInfo.commit_date}`; + } + gitInfoEl.textContent = gitText; + gitInfoEl.style.display = 'block'; + } else { + gitInfoEl.style.display = 'none'; + } + } + // Update changelog content if available if (this.updateInfo && this.updateInfo.changelog) { const changelogContent = modal.querySelector('.changelog-content'); diff --git a/templates/components/modals.html b/templates/components/modals.html index 43901727..2f3a5c0e 100644 --- a/templates/components/modals.html +++ b/templates/components/modals.html @@ -391,6 +391,7 @@ Current Version: v0.0.0 +
New Version: v0.0.0