Fix node git status fallback

This commit is contained in:
michele 2026-04-08 22:04:59 +00:00
parent 772a077e01
commit f06a0ce737
2 changed files with 109 additions and 34 deletions

View File

@ -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

View File

@ -80,13 +80,14 @@
<div class="rounded-xl border bg-white p-4">
<div class="text-sm font-semibold text-slate-900">Stato nodo</div>
<div class="mt-3 space-y-1 text-[11px] text-slate-600">
<div>Repository Git sorgente: <span class="font-mono font-medium">{{ $this->gitWorkspacePath ?? '-' }}</span></div>
<div>Branch locale: <span class="font-mono font-medium">{{ $this->gitCurrentBranch ?? '-' }}</span></div>
<div>Commit locale: <span class="font-mono font-medium">{{ $this->gitCurrentCommit ?? '-' }}</span></div>
<div>Data commit locale: <span class="font-medium">{{ $this->gitCurrentCommitDate ?? '-' }}</span></div>
<div>Commit remoto: <span class="font-mono font-medium">{{ $this->gitRemoteCommit ?? '-' }}</span></div>
<div>Data commit remoto: <span class="font-medium">{{ $this->gitRemoteCommitDate ?? '-' }}</span></div>
<div>Tracking: <span class="font-medium">{{ $this->gitAheadBehind }}</span></div>
<div>Aggiornamenti da caricare sul nodo: <span class="font-semibold {{ (int) $this->pendingGitCommitCount > 0 ? 'text-amber-700' : 'text-emerald-700' }}">{{ (int) $this->pendingGitCommitCount }}</span></div>
<div>Aggiornamenti da caricare sul nodo: <span class="font-semibold {{ (int) $this->pendingGitCommitCount > 0 ? 'text-amber-700' : 'text-emerald-700' }}">{{ $this->gitWorkspacePath === null && filled($this->pendingGitPreviewIssue) ? 'n/d' : (int) $this->pendingGitCommitCount }}</span></div>
<div>Commit locali da inviare: <span class="font-semibold {{ (int) $this->gitAheadCount > 0 ? 'text-sky-700' : 'text-slate-700' }}">{{ (int) $this->gitAheadCount }}</span></div>
<div>Working tree: <span class="font-medium {{ $this->gitWorkingTreeDirty ? 'text-rose-700' : 'text-emerald-700' }}">{{ $this->gitWorkingTreeDirty ? 'sporco' : 'pulito' }}</span></div>
<div>Ultima sync Git: <span class="font-medium">{{ $this->lastGitSyncAt ?? '-' }}</span></div>