fix(update): fix version comparison logic

This commit is contained in:
xboard 2025-02-09 14:05:25 +08:00
parent 39456923d3
commit 7ac2e2e2ed
2 changed files with 44 additions and 29 deletions

1
.gitignore vendored
View File

@ -24,6 +24,7 @@ docker-compose.yml
/docker /docker
storage/laravels.conf storage/laravels.conf
storage/laravels.pid storage/laravels.pid
storage/update_pending
storage/laravels-timer-process.pid storage/laravels-timer-process.pid
cli-php.ini cli-php.ini
frontend frontend

View File

@ -62,7 +62,6 @@ class UpdateService
// If unable to get current commit, try to get the first commit // If unable to get current commit, try to get the first commit
$currentCommit = $this->getFirstCommit(); $currentCommit = $this->getFirstCommit();
} }
// Get local git logs // Get local git logs
$localLogs = $this->getLocalGitLogs(); $localLogs = $this->getLocalGitLogs();
if (empty($localLogs)) { if (empty($localLogs)) {
@ -74,25 +73,41 @@ class UpdateService
$response = Http::withHeaders([ $response = Http::withHeaders([
'Accept' => 'application/vnd.github.v3+json', 'Accept' => 'application/vnd.github.v3+json',
'User-Agent' => 'XBoard-Update-Checker' '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()) { if ($response->successful()) {
$commits = $response->json(); $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; $currentIndex = -1;
$updateLogs = []; $updateLogs = [];
$isLocalNewer = false;
// Check if local is newer than remote // First, find the current version position in remote commit history
foreach ($localLogs as $localCommit) { foreach ($commits as $index => $commit) {
$localHash = $this->formatCommitHash($localCommit['hash']); $shortSha = $this->formatCommitHash($commit['sha']);
if ($localHash === $latestCommit) { if ($shortSha === $currentCommit) {
$currentIndex = $index;
break; break;
} }
// If local commit not in remote, local version is newer }
$isLocalNewer = true;
// 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;
}
// Record additional local commits
$updateLogs[] = [ $updateLogs[] = [
'version' => $localHash, 'version' => $localHash,
'message' => $localCommit['message'], 'message' => $localCommit['message'],
@ -100,19 +115,18 @@ class UpdateService
'date' => $localCommit['date'], 'date' => $localCommit['date'],
'is_local' => true 'is_local' => true
]; ];
$isLocalNewer = true;
}
} }
if (!$isLocalNewer) { // If local is not newer, collect commits that need to be updated
// If local is not newer, check remote updates if (!$isLocalNewer && $currentIndex > 0) {
foreach ($commits as $index => $commit) { $updateLogs = [];
$shortSha = $this->formatCommitHash($commit['sha']); // Collect all commits between current version and latest version
if ($shortSha === $currentCommit) { for ($i = 0; $i < $currentIndex; $i++) {
$currentIndex = $index; $commit = $commits[$i];
break;
}
// Collect update logs
$updateLogs[] = [ $updateLogs[] = [
'version' => $shortSha, 'version' => $this->formatCommitHash($commit['sha']),
'message' => $commit['commit']['message'], 'message' => $commit['commit']['message'],
'author' => $commit['commit']['author']['name'], 'author' => $commit['commit']['author']['name'],
'date' => $commit['commit']['author']['date'], 'date' => $commit['commit']['author']['date'],
@ -121,7 +135,7 @@ class UpdateService
} }
} }
$hasUpdate = !$isLocalNewer && $currentIndex !== 0 && $currentIndex !== -1; $hasUpdate = !$isLocalNewer && $currentIndex > 0;
$updateInfo = [ $updateInfo = [
'has_update' => $hasUpdate, 'has_update' => $hasUpdate,