From f06a0ce7371f41e9a8fd9ac05fbd6637e65ff0f3 Mon Sep 17 00:00:00 2001 From: michele Date: Wed, 8 Apr 2026 22:04:59 +0000 Subject: [PATCH] Fix node git status fallback --- app/Filament/Pages/Supporto/Modifiche.php | 140 +++++++++++++----- .../supporto/aggiornamento-launcher.blade.php | 3 +- 2 files changed, 109 insertions(+), 34 deletions(-) diff --git a/app/Filament/Pages/Supporto/Modifiche.php b/app/Filament/Pages/Supporto/Modifiche.php index 4f88bce..ab2cfb4 100644 --- a/app/Filament/Pages/Supporto/Modifiche.php +++ b/app/Filament/Pages/Supporto/Modifiche.php @@ -81,6 +81,8 @@ class Modifiche extends Page public bool $gitForce = false; + public ?string $gitWorkspacePath = null; + public ?string $gitCurrentBranch = null; public ?string $gitCurrentCommit = null; @@ -615,9 +617,14 @@ private function loadLatestCommits(): void { $this->latestCommits = []; + $repoPath = $this->resolveGitWorkspacePath(); + if ($repoPath === null) { + return; + } + $format = '%H|%h|%an|%ad|%s'; $command = 'git log -n 20 --date=format:"%d/%m/%Y %H:%M" --pretty=format:"' . $format . '"'; - $result = Process::path(base_path())->run($command); + $result = Process::path($repoPath)->run($command); if (! $result->successful()) { return; @@ -654,7 +661,13 @@ private function loadPendingGitPreview(): void return; } - $fetch = Process::path(base_path()) + $repoPath = $this->resolveGitWorkspacePath(); + if ($repoPath === null) { + $this->pendingGitPreviewIssue = 'Repository Git non disponibile su questo nodo. Il conteggio aggiornamenti verra mostrato dopo una sync valida o se il repository sorgente e configurato.'; + return; + } + + $fetch = Process::path($repoPath) ->timeout(120) ->run(['git', 'fetch', $remote, $branch, '--quiet']); @@ -676,7 +689,7 @@ private function loadPendingGitPreview(): void $format = '%H|%h|%an|%ad|%s'; $command = 'git log -n 12 --date=format:"%d/%m/%Y %H:%M" --pretty=format:"' . $format . '" ' . escapeshellarg($range); - $result = Process::path(base_path())->run(['bash', '-lc', $command]); + $result = Process::path($repoPath)->run(['bash', '-lc', $command]); if ($result->successful()) { $lines = preg_split('/\r\n|\r|\n/', trim($result->output())) ?: []; @@ -1004,8 +1017,13 @@ private function loadRecentFilamentPages(): void { $this->recentFilamentPages = []; + $repoPath = $this->resolveGitWorkspacePath(); + if ($repoPath === null) { + return; + } + $command = 'git log -n 30 --name-only --pretty=format:"@@@%h|%s" -- app/Filament/Pages resources/views/filament/pages'; - $result = Process::path(base_path())->run($command); + $result = Process::path($repoPath)->run($command); if (! $result->successful()) { return; } @@ -1057,28 +1075,37 @@ private function loadRecentFilamentPages(): void private function loadGitWorkspaceStatus(): void { - $this->gitCurrentBranch = $this->runGitProcess(['rev-parse', '--abbrev-ref', 'HEAD']); - $this->gitCurrentCommit = $this->runGitProcess(['rev-parse', '--short', 'HEAD']); - $this->gitCurrentCommitDate = $this->runGitProcess(['show', '-s', '--date=format:%d/%m/%Y %H:%M', '--format=%cd', 'HEAD']); - $this->gitRemoteCommit = $this->runGitProcess(['rev-parse', '--short', 'refs/remotes/' . $this->gitRemote . '/' . $this->gitBranch]); - $this->gitRemoteCommitDate = $this->runGitProcess(['show', '-s', '--date=format:%d/%m/%Y %H:%M', '--format=%cd', 'refs/remotes/' . $this->gitRemote . '/' . $this->gitBranch]); - $this->gitWorkingTreeDirty = trim((string) ($this->runGitProcess(['status', '--porcelain']) ?? '')) !== ''; - $this->gitAheadCount = 0; - $this->gitBehindCount = 0; + $repoPath = $this->resolveGitWorkspacePath(); - $aheadBehind = $this->runGitProcess(['rev-list', '--left-right', '--count', 'HEAD...refs/remotes/' . $this->gitRemote . '/' . $this->gitBranch]); + $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; + + $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 = '-'; + $this->gitAheadBehind = $repoPath === null ? 'non disponibile' : '-'; } $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); + $syncDate = isset($summary['synced_at']) ? (string) $summary['synced_at'] : null; + $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']) @@ -1087,6 +1114,10 @@ private function loadGitWorkspaceStatus(): void $this->lastGitSyncIssue = isset($summary['status']) && (string) $summary['status'] === 'failed' ? (string) ($summary['message'] ?? $this->lastGitSyncIssue) : null; + + if ($repoPath === null && $this->pendingGitPreviewIssue === null) { + $this->pendingGitPreviewIssue = 'Questo nodo sta leggendo i dati dall ultima sync registrata. Per vedere branch, commit e conteggi live serve configurare il repository Git sorgente sul server.'; + } } } } @@ -1187,28 +1218,36 @@ private function loadDevelopmentSnapshot(): void private function loadFunctionalHighlights(): void { - $this->functionalHighlights = []; - - $path = base_path('docs/NETGESCON-MODIFICHE.md'); - if (! is_file($path)) { - return; - } + $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']) ?? '')) !== ''; $lines = preg_split('/\r\n|\r|\n/', (string) @file_get_contents($path)) ?: []; foreach ($lines as $line) { - $line = trim((string) $line); + $aheadBehind = $this->runGitProcessAt($repoPath, ['rev-list', '--left-right', '--count', 'HEAD...refs/remotes/' . $this->gitRemote . '/' . $this->gitBranch]); if ($line === '') { continue; } if (! str_starts_with($line, '-')) { - continue; + $this->gitAheadBehind = $repoPath === null ? 'non disponibile' : '-'; } if (! str_contains($line, '[U]') && ! str_contains($line, '[P]')) { continue; } + $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); + $syncDate = isset($summary['synced_at']) ? (string) $summary['synced_at'] : null; + $this->gitCurrentCommitDate = $this->gitCurrentCommitDate ?: $syncDate; + $this->gitRemoteCommitDate = $this->gitRemoteCommitDate ?: $syncDate; $this->functionalHighlights[] = trim(ltrim($line, '- ')); if (count($this->functionalHighlights) >= 30) { break; @@ -1246,6 +1285,51 @@ private function resolveChangedAreaLabel(string $path): string return 'area applicativa'; } + private function resolveGitWorkspacePath(): ?string + { + $candidates = array_values(array_filter(array_unique([ + is_dir(base_path('.git')) ? base_path() : '', + trim((string) env('NETGESCON_GIT_SYNC_SOURCE_REPO', '')), + '/home/michele/netgescon/netgescon-day0', + '/home/michele/netgescon/netgescon-laravel', + storage_path('app/support/git-source'), + '/var/www/netgescon-git-source', + '/var/www/netgescon_backup_20250722_220433', + '/var/www/netgescon_backup_20250722_215454', + ]), static fn(string $path): bool => $path !== '')); + + foreach ($candidates as $candidate) { + if (! is_dir($candidate . '/.git')) { + continue; + } + + if ($this->runGitProcessAt($candidate, ['rev-parse', '--git-dir']) !== null) { + return $candidate; + } + } + + return null; + } + + private function runGitProcessAt(?string $repoPath, array $arguments): ?string + { + if ($repoPath === null) { + return null; + } + + $result = Process::path($repoPath) + ->timeout(120) + ->run(array_merge(['git', '-c', 'safe.directory=' . $repoPath], $arguments)); + + if (! $result->successful()) { + return null; + } + + $output = trim((string) $result->output()); + + return $output !== '' ? $output : null; + } + private function isSupportUpdateRegistryReady(): bool { try { @@ -1675,17 +1759,7 @@ public function renderSupportMarkdown(string $markdown): string private function runGitProcess(array $arguments): ?string { - $result = Process::path(base_path()) - ->timeout(120) - ->run(array_merge(['git'], $arguments)); - - if (! $result->successful()) { - return null; - } - - $output = trim((string) $result->output()); - - return $output !== '' ? $output : null; + return $this->runGitProcessAt($this->resolveGitWorkspacePath(), $arguments); } private function detectGitSyncIssue(string $output): string diff --git a/resources/views/filament/pages/supporto/aggiornamento-launcher.blade.php b/resources/views/filament/pages/supporto/aggiornamento-launcher.blade.php index 145b461..bcc1ae0 100644 --- a/resources/views/filament/pages/supporto/aggiornamento-launcher.blade.php +++ b/resources/views/filament/pages/supporto/aggiornamento-launcher.blade.php @@ -80,13 +80,14 @@
Stato nodo
+
Repository Git sorgente: {{ $this->gitWorkspacePath ?? '-' }}
Branch locale: {{ $this->gitCurrentBranch ?? '-' }}
Commit locale: {{ $this->gitCurrentCommit ?? '-' }}
Data commit locale: {{ $this->gitCurrentCommitDate ?? '-' }}
Commit remoto: {{ $this->gitRemoteCommit ?? '-' }}
Data commit remoto: {{ $this->gitRemoteCommitDate ?? '-' }}
Tracking: {{ $this->gitAheadBehind }}
-
Aggiornamenti da caricare sul nodo: {{ (int) $this->pendingGitCommitCount }}
+
Aggiornamenti da caricare sul nodo: {{ $this->gitWorkspacePath === null && filled($this->pendingGitPreviewIssue) ? 'n/d' : (int) $this->pendingGitCommitCount }}
Commit locali da inviare: {{ (int) $this->gitAheadCount }}
Working tree: {{ $this->gitWorkingTreeDirty ? 'sporco' : 'pulito' }}
Ultima sync Git: {{ $this->lastGitSyncAt ?? '-' }}