Fix staging git sync deployment

This commit is contained in:
michele 2026-04-08 22:25:54 +00:00
parent e0e2724db4
commit 4f7e851593
4 changed files with 101 additions and 26 deletions

View File

@ -105,6 +105,8 @@ public function handle(): int
$docsRepoPath = $deployPath; $docsRepoPath = $deployPath;
$newCommit = $currentCommit; $newCommit = $currentCommit;
$cleanupWorktree = null; $cleanupWorktree = null;
$sourceRepoAligned = ! $deploySync;
$sourceRepoMessage = null;
if ($deploySync) { if ($deploySync) {
$this->writeProgress(32, 'Creazione sorgente remota pulita', 'running'); $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; $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) { if ($cleanupWorktree instanceof \Closure) {
$cleanupWorktree(); $cleanupWorktree();
} }
@ -321,6 +333,8 @@ public function handle(): int
'repo_path' => $repoPath, 'repo_path' => $repoPath,
'deploy_path' => $deployPath, 'deploy_path' => $deployPath,
'mode' => $deploySync ? 'external-repo-rsync' : 'in-place-git', 'mode' => $deploySync ? 'external-repo-rsync' : 'in-place-git',
'source_repo_aligned'=> $sourceRepoAligned,
'source_repo_message'=> $sourceRepoMessage,
'rubrica_legami_qa' => $rubricaQaExit === 0 ? 'completed' : 'warning', 'rubrica_legami_qa' => $rubricaQaExit === 0 ? 'completed' : 'warning',
]); ]);
@ -377,6 +391,33 @@ private function buildWorktreePath(): string
return $baseDir . '/' . now()->format('YmdHis') . '-' . uniqid(); 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 private function resolveWorktreeBaseDir(): string
{ {
$candidates = [ $candidates = [

View File

@ -89,10 +89,16 @@ class Modifiche extends Page
public ?string $gitCurrentCommitDate = null; public ?string $gitCurrentCommitDate = null;
public ?string $gitSourceCommit = null;
public ?string $gitSourceCommitDate = null;
public ?string $gitRemoteCommit = null; public ?string $gitRemoteCommit = null;
public ?string $gitRemoteCommitDate = null; public ?string $gitRemoteCommitDate = null;
public bool $gitStatusUsesSyncSummary = false;
public bool $gitWorkingTreeDirty = false; public bool $gitWorkingTreeDirty = false;
public string $gitAheadBehind = '-'; 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.'; $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]); $count = $this->runGitProcess(['rev-list', '--count', $range]);
if (! is_string($count) || ! ctype_digit(trim($count))) { if (! is_string($count) || ! ctype_digit(trim($count))) {
@ -1077,35 +1087,47 @@ private function loadGitWorkspaceStatus(): void
{ {
$repoPath = $this->resolveGitWorkspacePath(); $repoPath = $this->resolveGitWorkspacePath();
$this->gitWorkspacePath = $repoPath; $this->gitWorkspacePath = $repoPath;
$this->gitCurrentBranch = $this->runGitProcessAt($repoPath, ['rev-parse', '--abbrev-ref', 'HEAD']); $this->gitCurrentBranch = $this->runGitProcessAt($repoPath, ['rev-parse', '--abbrev-ref', 'HEAD']);
$this->gitCurrentCommit = $this->runGitProcessAt($repoPath, ['rev-parse', '--short', 'HEAD']); $this->gitSourceCommit = $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->gitSourceCommitDate = $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->gitCurrentCommit = $this->gitSourceCommit;
$this->gitRemoteCommitDate = $this->runGitProcessAt($repoPath, ['show', '-s', '--date=format:%d/%m/%Y %H:%M', '--format=%cd', 'refs/remotes/' . $this->gitRemote . '/' . $this->gitBranch]); $this->gitCurrentCommitDate = $this->gitSourceCommitDate;
$this->gitWorkingTreeDirty = trim((string) ($this->runGitProcessAt($repoPath, ['status', '--porcelain']) ?? '')) !== ''; $this->gitRemoteCommit = $this->runGitProcessAt($repoPath, ['rev-parse', '--short', 'refs/remotes/' . $this->gitRemote . '/' . $this->gitBranch]);
$this->gitAheadCount = 0; $this->gitRemoteCommitDate = $this->runGitProcessAt($repoPath, ['show', '-s', '--date=format:%d/%m/%Y %H:%M', '--format=%cd', 'refs/remotes/' . $this->gitRemote . '/' . $this->gitBranch]);
$this->gitBehindCount = 0; $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]); $currentRef = 'HEAD';
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' : '-';
}
$summaryPath = storage_path('app/support/generated/git-sync-last.json'); $summaryPath = storage_path('app/support/generated/git-sync-last.json');
if (is_file($summaryPath)) { if (is_file($summaryPath)) {
$summary = json_decode((string) @file_get_contents($summaryPath), true); $summary = json_decode((string) @file_get_contents($summaryPath), true);
if (is_array($summary)) { if (is_array($summary)) {
$this->gitCurrentBranch = $this->gitCurrentBranch ?: (isset($summary['branch']) ? (string) $summary['branch'] : null); $summaryMode = (string) ($summary['mode'] ?? '');
$this->gitCurrentCommit = $this->gitCurrentCommit ?: (isset($summary['after_commit']) ? (string) $summary['after_commit'] : (isset($summary['before_commit']) ? (string) $summary['before_commit'] : null)); $summaryStatus = (string) ($summary['status'] ?? '');
$this->gitRemoteCommit = $this->gitRemoteCommit ?: (isset($summary['remote_commit']) ? (string) $summary['remote_commit'] : $this->gitCurrentCommit); $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; $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->lastGitDocsSyncAt = isset($summary['synced_at']) ? (string) $summary['synced_at'] : $this->lastGitDocsSyncAt;
$this->lastGitSyncAt = isset($summary['synced_at']) ? (string) $summary['synced_at'] : $this->lastGitSyncAt; $this->lastGitSyncAt = isset($summary['synced_at']) ? (string) $summary['synced_at'] : $this->lastGitSyncAt;
$this->lastGitSyncExitCode = isset($summary['exit_code']) && is_numeric($summary['exit_code']) $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 private function loadSupportUpdateEntries(): void

View File

@ -82,8 +82,12 @@
<div class="mt-3 space-y-1 text-[11px] text-slate-600"> <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>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>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>Commit deploy/ultimo sync: <span class="font-mono font-medium">{{ $this->gitCurrentCommit ?? '-' }}</span></div>
<div>Data commit locale: <span class="font-medium">{{ $this->gitCurrentCommitDate ?? '-' }}</span></div> <div>Data commit deploy: <span class="font-medium">{{ $this->gitCurrentCommitDate ?? '-' }}</span></div>
@if($this->gitStatusUsesSyncSummary && filled($this->gitSourceCommit) && $this->gitSourceCommit !== $this->gitCurrentCommit)
<div>Commit repository sorgente cache: <span class="font-mono font-medium">{{ $this->gitSourceCommit }}</span></div>
<div>Data commit repository sorgente: <span class="font-medium">{{ $this->gitSourceCommitDate ?? '-' }}</span></div>
@endif
<div>Commit remoto: <span class="font-mono font-medium">{{ $this->gitRemoteCommit ?? '-' }}</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>Data commit remoto: <span class="font-medium">{{ $this->gitRemoteCommitDate ?? '-' }}</span></div>
<div>Tracking: <span class="font-medium">{{ $this->gitAheadBehind }}</span></div> <div>Tracking: <span class="font-medium">{{ $this->gitAheadBehind }}</span></div>

View File

@ -64,7 +64,6 @@ fi
RSYNC_ARGS=( RSYNC_ARGS=(
-rlvh -rlvh
--update
--checksum --checksum
--no-perms --no-perms
--no-owner --no-owner