From 4f7e8515932faa63c199fd0d36c0311bda607ee5 Mon Sep 17 00:00:00 2001 From: michele Date: Wed, 8 Apr 2026 22:25:54 +0000 Subject: [PATCH] Fix staging git sync deployment --- .../Commands/NetgesconGitSyncCommand.php | 41 ++++++++++ app/Filament/Pages/Supporto/Modifiche.php | 77 +++++++++++++------ .../supporto/aggiornamento-launcher.blade.php | 8 +- scripts/ops/netgescon-sync-prod-update.sh | 1 - 4 files changed, 101 insertions(+), 26 deletions(-) diff --git a/app/Console/Commands/NetgesconGitSyncCommand.php b/app/Console/Commands/NetgesconGitSyncCommand.php index f102634..719422d 100644 --- a/app/Console/Commands/NetgesconGitSyncCommand.php +++ b/app/Console/Commands/NetgesconGitSyncCommand.php @@ -105,6 +105,8 @@ public function handle(): int $docsRepoPath = $deployPath; $newCommit = $currentCommit; $cleanupWorktree = null; + $sourceRepoAligned = ! $deploySync; + $sourceRepoMessage = null; if ($deploySync) { $this->writeProgress(32, 'Creazione sorgente remota pulita', 'running'); @@ -304,6 +306,16 @@ public function handle(): int $remoteCommit = $this->runGit($repoPath, ['rev-parse', '--short', $remoteRef]) ?? $newCommit; + if ($deploySync) { + $this->writeProgress(86, 'Allineamento repository sorgente cache', 'running'); + $sourceRepoAligned = $this->fastForwardSourceRepository($repoPath, $remote, $branch); + + if (! $sourceRepoAligned) { + $sourceRepoMessage = 'Deploy aggiornato, ma il repository Git sorgente cache non e stato allineato localmente.'; + $this->warn($sourceRepoMessage); + } + } + if ($cleanupWorktree instanceof \Closure) { $cleanupWorktree(); } @@ -321,6 +333,8 @@ public function handle(): int 'repo_path' => $repoPath, 'deploy_path' => $deployPath, 'mode' => $deploySync ? 'external-repo-rsync' : 'in-place-git', + 'source_repo_aligned'=> $sourceRepoAligned, + 'source_repo_message'=> $sourceRepoMessage, 'rubrica_legami_qa' => $rubricaQaExit === 0 ? 'completed' : 'warning', ]); @@ -377,6 +391,33 @@ private function buildWorktreePath(): string return $baseDir . '/' . now()->format('YmdHis') . '-' . uniqid(); } + private function fastForwardSourceRepository(string $repoPath, string $remote, string $branch): bool + { + $currentBranch = $this->runGit($repoPath, ['rev-parse', '--abbrev-ref', 'HEAD']); + + if ($currentBranch !== $branch) { + $checkout = Process::path($repoPath) + ->timeout(1800) + ->run($this->gitCommandArgs($repoPath, ['checkout', $branch])); + + if (! $checkout->successful()) { + $checkout = Process::path($repoPath) + ->timeout(1800) + ->run($this->gitCommandArgs($repoPath, ['checkout', '--track', '-b', $branch, 'refs/remotes/' . $remote . '/' . $branch])); + + if (! $checkout->successful()) { + return false; + } + } + } + + $pull = Process::path($repoPath) + ->timeout(1800) + ->run($this->gitCommandArgs($repoPath, ['pull', '--ff-only', $remote, $branch])); + + return $pull->successful(); + } + private function resolveWorktreeBaseDir(): string { $candidates = [ diff --git a/app/Filament/Pages/Supporto/Modifiche.php b/app/Filament/Pages/Supporto/Modifiche.php index 0472edf..bf54ea5 100644 --- a/app/Filament/Pages/Supporto/Modifiche.php +++ b/app/Filament/Pages/Supporto/Modifiche.php @@ -89,10 +89,16 @@ class Modifiche extends Page public ?string $gitCurrentCommitDate = null; + public ?string $gitSourceCommit = null; + + public ?string $gitSourceCommitDate = null; + public ?string $gitRemoteCommit = null; public ?string $gitRemoteCommitDate = null; + public bool $gitStatusUsesSyncSummary = false; + public bool $gitWorkingTreeDirty = false; public string $gitAheadBehind = '-'; @@ -675,7 +681,11 @@ private function loadPendingGitPreview(): void $this->pendingGitPreviewIssue = 'Impossibile aggiornare l anteprima remota da Gitea. Il conteggio mostrato potrebbe essere non aggiornato.'; } - $range = 'HEAD..refs/remotes/' . $remote . '/' . $branch; + $baseRef = $this->gitStatusUsesSyncSummary && filled($this->gitCurrentCommit) + ? (string) $this->gitCurrentCommit + : 'HEAD'; + + $range = $baseRef . '..refs/remotes/' . $remote . '/' . $branch; $count = $this->runGitProcess(['rev-list', '--count', $range]); if (! is_string($count) || ! ctype_digit(trim($count))) { @@ -1077,35 +1087,47 @@ private function loadGitWorkspaceStatus(): void { $repoPath = $this->resolveGitWorkspacePath(); - $this->gitWorkspacePath = $repoPath; - $this->gitCurrentBranch = $this->runGitProcessAt($repoPath, ['rev-parse', '--abbrev-ref', 'HEAD']); - $this->gitCurrentCommit = $this->runGitProcessAt($repoPath, ['rev-parse', '--short', 'HEAD']); - $this->gitCurrentCommitDate = $this->runGitProcessAt($repoPath, ['show', '-s', '--date=format:%d/%m/%Y %H:%M', '--format=%cd', 'HEAD']); - $this->gitRemoteCommit = $this->runGitProcessAt($repoPath, ['rev-parse', '--short', 'refs/remotes/' . $this->gitRemote . '/' . $this->gitBranch]); - $this->gitRemoteCommitDate = $this->runGitProcessAt($repoPath, ['show', '-s', '--date=format:%d/%m/%Y %H:%M', '--format=%cd', 'refs/remotes/' . $this->gitRemote . '/' . $this->gitBranch]); - $this->gitWorkingTreeDirty = trim((string) ($this->runGitProcessAt($repoPath, ['status', '--porcelain']) ?? '')) !== ''; - $this->gitAheadCount = 0; - $this->gitBehindCount = 0; + $this->gitWorkspacePath = $repoPath; + $this->gitCurrentBranch = $this->runGitProcessAt($repoPath, ['rev-parse', '--abbrev-ref', 'HEAD']); + $this->gitSourceCommit = $this->runGitProcessAt($repoPath, ['rev-parse', '--short', 'HEAD']); + $this->gitSourceCommitDate = $this->runGitProcessAt($repoPath, ['show', '-s', '--date=format:%d/%m/%Y %H:%M', '--format=%cd', 'HEAD']); + $this->gitCurrentCommit = $this->gitSourceCommit; + $this->gitCurrentCommitDate = $this->gitSourceCommitDate; + $this->gitRemoteCommit = $this->runGitProcessAt($repoPath, ['rev-parse', '--short', 'refs/remotes/' . $this->gitRemote . '/' . $this->gitBranch]); + $this->gitRemoteCommitDate = $this->runGitProcessAt($repoPath, ['show', '-s', '--date=format:%d/%m/%Y %H:%M', '--format=%cd', 'refs/remotes/' . $this->gitRemote . '/' . $this->gitBranch]); + $this->gitWorkingTreeDirty = trim((string) ($this->runGitProcessAt($repoPath, ['status', '--porcelain']) ?? '')) !== ''; + $this->gitAheadCount = 0; + $this->gitBehindCount = 0; + $this->gitStatusUsesSyncSummary = false; - $aheadBehind = $this->runGitProcessAt($repoPath, ['rev-list', '--left-right', '--count', 'HEAD...refs/remotes/' . $this->gitRemote . '/' . $this->gitBranch]); - if (is_string($aheadBehind) && preg_match('/^(\d+)\s+(\d+)$/', trim($aheadBehind), $matches) === 1) { - $this->gitAheadCount = (int) $matches[1]; - $this->gitBehindCount = (int) $matches[2]; - $this->gitAheadBehind = 'ahead ' . $this->gitAheadCount . ' / behind ' . $this->gitBehindCount; - } else { - $this->gitAheadBehind = $repoPath === null ? 'non disponibile' : '-'; - } + $currentRef = 'HEAD'; $summaryPath = storage_path('app/support/generated/git-sync-last.json'); if (is_file($summaryPath)) { $summary = json_decode((string) @file_get_contents($summaryPath), true); if (is_array($summary)) { - $this->gitCurrentBranch = $this->gitCurrentBranch ?: (isset($summary['branch']) ? (string) $summary['branch'] : null); - $this->gitCurrentCommit = $this->gitCurrentCommit ?: (isset($summary['after_commit']) ? (string) $summary['after_commit'] : (isset($summary['before_commit']) ? (string) $summary['before_commit'] : null)); - $this->gitRemoteCommit = $this->gitRemoteCommit ?: (isset($summary['remote_commit']) ? (string) $summary['remote_commit'] : $this->gitCurrentCommit); + $summaryMode = (string) ($summary['mode'] ?? ''); + $summaryStatus = (string) ($summary['status'] ?? ''); + $summaryCurrentCommit = isset($summary['after_commit']) ? (string) $summary['after_commit'] : (isset($summary['before_commit']) ? (string) $summary['before_commit'] : ''); + $summaryRemoteCommit = isset($summary['remote_commit']) ? (string) $summary['remote_commit'] : ''; $syncDate = isset($summary['synced_at']) ? (string) $summary['synced_at'] : null; - $this->gitCurrentCommitDate = $this->gitCurrentCommitDate ?: $syncDate; - $this->gitRemoteCommitDate = $this->gitRemoteCommitDate ?: $syncDate; + + $this->gitCurrentBranch = $this->gitCurrentBranch ?: (isset($summary['branch']) ? (string) $summary['branch'] : null); + + if ($summaryMode === 'external-repo-rsync' && $summaryStatus === 'completed' && $summaryCurrentCommit !== '') { + $this->gitStatusUsesSyncSummary = true; + $this->gitCurrentCommit = $summaryCurrentCommit; + $this->gitCurrentCommitDate = $syncDate ?: $this->gitCurrentCommitDate; + $this->gitRemoteCommit = $summaryRemoteCommit !== '' ? $summaryRemoteCommit : ($this->gitRemoteCommit ?: $summaryCurrentCommit); + $this->gitRemoteCommitDate = $syncDate ?: $this->gitRemoteCommitDate; + $currentRef = $summaryCurrentCommit; + } else { + $this->gitCurrentCommit = $this->gitCurrentCommit ?: ($summaryCurrentCommit !== '' ? $summaryCurrentCommit : null); + $this->gitRemoteCommit = $this->gitRemoteCommit ?: ($summaryRemoteCommit !== '' ? $summaryRemoteCommit : $this->gitCurrentCommit); + $this->gitCurrentCommitDate = $this->gitCurrentCommitDate ?: $syncDate; + $this->gitRemoteCommitDate = $this->gitRemoteCommitDate ?: $syncDate; + } + $this->lastGitDocsSyncAt = isset($summary['synced_at']) ? (string) $summary['synced_at'] : $this->lastGitDocsSyncAt; $this->lastGitSyncAt = isset($summary['synced_at']) ? (string) $summary['synced_at'] : $this->lastGitSyncAt; $this->lastGitSyncExitCode = isset($summary['exit_code']) && is_numeric($summary['exit_code']) @@ -1120,6 +1142,15 @@ private function loadGitWorkspaceStatus(): void } } } + + $aheadBehind = $this->runGitProcessAt($repoPath, ['rev-list', '--left-right', '--count', $currentRef . '...refs/remotes/' . $this->gitRemote . '/' . $this->gitBranch]); + if (is_string($aheadBehind) && preg_match('/^(\d+)\s+(\d+)$/', trim($aheadBehind), $matches) === 1) { + $this->gitAheadCount = (int) $matches[1]; + $this->gitBehindCount = (int) $matches[2]; + $this->gitAheadBehind = 'ahead ' . $this->gitAheadCount . ' / behind ' . $this->gitBehindCount; + } else { + $this->gitAheadBehind = $repoPath === null ? 'non disponibile' : '-'; + } } private function loadSupportUpdateEntries(): void diff --git a/resources/views/filament/pages/supporto/aggiornamento-launcher.blade.php b/resources/views/filament/pages/supporto/aggiornamento-launcher.blade.php index bcc1ae0..43ac542 100644 --- a/resources/views/filament/pages/supporto/aggiornamento-launcher.blade.php +++ b/resources/views/filament/pages/supporto/aggiornamento-launcher.blade.php @@ -82,8 +82,12 @@
Repository Git sorgente: {{ $this->gitWorkspacePath ?? '-' }}
Branch locale: {{ $this->gitCurrentBranch ?? '-' }}
-
Commit locale: {{ $this->gitCurrentCommit ?? '-' }}
-
Data commit locale: {{ $this->gitCurrentCommitDate ?? '-' }}
+
Commit deploy/ultimo sync: {{ $this->gitCurrentCommit ?? '-' }}
+
Data commit deploy: {{ $this->gitCurrentCommitDate ?? '-' }}
+ @if($this->gitStatusUsesSyncSummary && filled($this->gitSourceCommit) && $this->gitSourceCommit !== $this->gitCurrentCommit) +
Commit repository sorgente cache: {{ $this->gitSourceCommit }}
+
Data commit repository sorgente: {{ $this->gitSourceCommitDate ?? '-' }}
+ @endif
Commit remoto: {{ $this->gitRemoteCommit ?? '-' }}
Data commit remoto: {{ $this->gitRemoteCommitDate ?? '-' }}
Tracking: {{ $this->gitAheadBehind }}
diff --git a/scripts/ops/netgescon-sync-prod-update.sh b/scripts/ops/netgescon-sync-prod-update.sh index e589dfd..a33ea55 100755 --- a/scripts/ops/netgescon-sync-prod-update.sh +++ b/scripts/ops/netgescon-sync-prod-update.sh @@ -64,7 +64,6 @@ fi RSYNC_ARGS=( -rlvh - --update --checksum --no-perms --no-owner