test versione con output tabellare e log timestamp (ma non corretta)

This commit is contained in:
Samuele Locatelli
2025-07-08 16:26:05 +02:00
parent bf3500e3d4
commit 190f816973
+26 -20
View File
@@ -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
}