From 7ac2e2e2ed1e24137a91c144d6fa44b929b580c3 Mon Sep 17 00:00:00 2001 From: xboard Date: Sun, 9 Feb 2025 14:05:25 +0800 Subject: [PATCH] fix(update): fix version comparison logic --- .gitignore | 1 + app/Services/UpdateService.php | 72 ++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 6ce6c19..2715539 100755 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ docker-compose.yml /docker storage/laravels.conf storage/laravels.pid +storage/update_pending storage/laravels-timer-process.pid cli-php.ini frontend diff --git a/app/Services/UpdateService.php b/app/Services/UpdateService.php index f602e7f..b7ecd5d 100644 --- a/app/Services/UpdateService.php +++ b/app/Services/UpdateService.php @@ -62,7 +62,6 @@ class UpdateService // If unable to get current commit, try to get the first commit $currentCommit = $this->getFirstCommit(); } - // Get local git logs $localLogs = $this->getLocalGitLogs(); if (empty($localLogs)) { @@ -74,45 +73,60 @@ class UpdateService $response = Http::withHeaders([ 'Accept' => 'application/vnd.github.v3+json', 'User-Agent' => 'XBoard-Update-Checker' - ])->get(self::GITHUB_API_URL . '?sha=master&per_page=50'); + ])->get(self::GITHUB_API_URL . '?per_page=50'); if ($response->successful()) { $commits = $response->json(); - $latestCommit = $this->formatCommitHash($commits[0]['sha']); - // Find current version position in commit history + if (empty($commits) || !is_array($commits)) { + Log::error('Invalid GitHub response format'); + return $this->getCachedUpdateInfo(); + } + + $latestCommit = $this->formatCommitHash($commits[0]['sha']); $currentIndex = -1; $updateLogs = []; - $isLocalNewer = false; - - // Check if local is newer than remote - foreach ($localLogs as $localCommit) { - $localHash = $this->formatCommitHash($localCommit['hash']); - if ($localHash === $latestCommit) { + + // First, find the current version position in remote commit history + foreach ($commits as $index => $commit) { + $shortSha = $this->formatCommitHash($commit['sha']); + if ($shortSha === $currentCommit) { + $currentIndex = $index; break; } - // If local commit not in remote, local version is newer - $isLocalNewer = true; - $updateLogs[] = [ - 'version' => $localHash, - 'message' => $localCommit['message'], - 'author' => $localCommit['author'], - 'date' => $localCommit['date'], - 'is_local' => true - ]; } - - if (!$isLocalNewer) { - // If local is not newer, check remote updates - foreach ($commits as $index => $commit) { - $shortSha = $this->formatCommitHash($commit['sha']); - if ($shortSha === $currentCommit) { - $currentIndex = $index; + + // Check local version status + $isLocalNewer = false; + if ($currentIndex === -1) { + // Current version not found in remote history, check local commits + foreach ($localLogs as $localCommit) { + $localHash = $this->formatCommitHash($localCommit['hash']); + // If latest remote commit found, local is not newer + if ($localHash === $latestCommit) { + $isLocalNewer = false; break; } - // Collect update logs + // Record additional local commits $updateLogs[] = [ - 'version' => $shortSha, + 'version' => $localHash, + 'message' => $localCommit['message'], + 'author' => $localCommit['author'], + 'date' => $localCommit['date'], + 'is_local' => true + ]; + $isLocalNewer = true; + } + } + + // If local is not newer, collect commits that need to be updated + if (!$isLocalNewer && $currentIndex > 0) { + $updateLogs = []; + // Collect all commits between current version and latest version + for ($i = 0; $i < $currentIndex; $i++) { + $commit = $commits[$i]; + $updateLogs[] = [ + 'version' => $this->formatCommitHash($commit['sha']), 'message' => $commit['commit']['message'], 'author' => $commit['commit']['author']['name'], 'date' => $commit['commit']['author']['date'], @@ -121,7 +135,7 @@ class UpdateService } } - $hasUpdate = !$isLocalNewer && $currentIndex !== 0 && $currentIndex !== -1; + $hasUpdate = !$isLocalNewer && $currentIndex > 0; $updateInfo = [ 'has_update' => $hasUpdate,