From 190f816973a34d6df97b55d9b2e9207b90940758 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 8 Jul 2025 16:26:05 +0200 Subject: [PATCH] test versione con output tabellare e log timestamp (ma non corretta) --- GitLocalUtils/CheckLocalGitStatus.ps1 | 46 +++++++++++++++------------ 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/GitLocalUtils/CheckLocalGitStatus.ps1 b/GitLocalUtils/CheckLocalGitStatus.ps1 index aaafe0d..8455144 100644 --- a/GitLocalUtils/CheckLocalGitStatus.ps1 +++ b/GitLocalUtils/CheckLocalGitStatus.ps1 @@ -10,9 +10,12 @@ param ( [string]$RootPath = "C:\Users\samuele.steamw\source", # Default alla cartella dei sorgenti, da passare invocando il comando [switch]$SwitchToMain, [string]$OutputFile = "$PSScriptRoot\outdated_repos.txt", + [switch]$FormatAsTable, [switch]$Help ) +$branchesToCheck = @("main", "master") +$outdatedRepos = @() # Mostra help message se --help or -h passato come argomento if ($Help -or $args -contains "--help" -or $args -contains "-h") { @@ -36,13 +39,13 @@ Examples: exit } -$branchesToCheck = @("main", "master") -$outdatedRepos = @() # Function to check Git status function Check-GitStatus { param ( - [string]$repoPath + [string]$repoPath, + [string]$OutputFile, + [switch]$FormatAsTable ) Write-Host "`nChecking repository at: $repoPath" -ForegroundColor Cyan @@ -50,23 +53,24 @@ function Check-GitStatus { if (-not (Test-Path ".git")) { Write-Host "Not a Git repository." -ForegroundColor Yellow + "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - Not a Git repository." | Out-File -FilePath $OutputFile -Append -Encoding UTF8 Pop-Location return } git fetch origin *> $null 2>&1 - $originalBranch = git rev-parse --abbrev-ref HEAD 2>$null - # Check which of 'main' or 'master' exists on the remote $remoteBranches = git ls-remote --heads origin | ForEach-Object { - ($_ -split "\s+")[1] -replace "refs/heads/", "" + ($_ -split "\\s+")[1] -replace "refs/heads/", "" } $targetBranch = $branchesToCheck | Where-Object { $remoteBranches -contains $_ } | Select-Object -First 1 if (-not $targetBranch) { - Write-Host "No 'main' or 'master' branch found on remote." -ForegroundColor Red + $message = "No 'main' or 'master' branch found on remote." + Write-Host $message -ForegroundColor Red + "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $message" | Out-File -FilePath $OutputFile -Append -Encoding UTF8 Pop-Location return } @@ -76,7 +80,6 @@ function Check-GitStatus { git checkout $targetBranch *> $null 2>&1 } - $status = git status -sb 2>$null $aheadBehind = git rev-list --left-right --count origin/$targetBranch...HEAD 2>$null if ($aheadBehind) { @@ -85,8 +88,10 @@ function Check-GitStatus { $ahead = [int]$parts[1] if ($ahead -gt 0 -or $behind -gt 0) { - Write-Host "šŸ”ŗ Repo is not up to date with origin/$targetBranch (ahead: $ahead, behind: $behind)" -ForegroundColor Yellow - $outdatedRepos += $repoPath + $message = "šŸ”ŗ $repoPath is not up to date with origin/$targetBranch (ahead: $ahead, behind: $behind)" + Write-Host $message -ForegroundColor Yellow + "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $message" | Out-File -FilePath $OutputFile -Append -Encoding UTF8 + $script:hasOutdated = $true } else { Write-Host "āœ… Repo is up to date with origin/$targetBranch" -ForegroundColor Green } @@ -105,19 +110,20 @@ $repos = Get-ChildItem -Path $RootPath -Recurse -Directory | Where-Object { Test-Path "$($_.FullName)\.git" } +# Clear or create output file +"" | Out-File -FilePath $OutputFile -Encoding UTF8 +$script:hasOutdated = $false + # Check each repository foreach ($repo in $repos) { - Check-GitStatus -repoPath $repo.FullName + Check-GitStatus -repoPath $repo.FullName -OutputFile $OutputFile -FormatAsTable:$FormatAsTable } -# Output outdated repos to file -if ($outdatedRepos.Count -gt 0) { - "`nšŸ“„ Outdated repositories:" | Out-File -FilePath $OutputFile -Encoding UTF8 - foreach ($repo in $outdatedRepos) { - $repo | Tee-Object -FilePath $OutputFile -Append - } +# Final summary +if ($script:hasOutdated) { Write-Host "`nšŸ“„ Outdated repositories saved to: $OutputFile" -ForegroundColor Cyan } else { - "All repositories are up to date!" | Out-File -FilePath $OutputFile -Encoding UTF8 - Write-Host "`nšŸŽ‰ All repositories are up to date!" -ForegroundColor Green -} + $msg = "šŸŽ‰ All repositories are up to date!" + Write-Host "`n$msg" -ForegroundColor Green + "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $msg" | Out-File -FilePath $OutputFile -Append -Encoding UTF8 +} \ No newline at end of file